This repository has been archived by the owner on Jan 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
[stdlib] Add guard utils and traits #5365
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,22 @@ | |||
<?php | |||
/** | |||
* Zend Framework (http://framework.zend.com/) | |||
* | |||
* @link http://github.com/zendframework/zf2 for the canonical source repository | |||
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) | |||
* @license http://framework.zend.com/license/new-bsd New BSD License | |||
*/ | |||
|
|||
namespace Zend\Stdlib\Guard; | |||
|
|||
/** | |||
* An aggregate for all guard traits | |||
*/ | |||
trait AllGuardsTrait | |||
{ | |||
|
|||
use ArrayOrTraversableGuardTrait; | |||
use EmptyGuardTrait; | |||
use NullGuardTrait; | |||
|
|||
} |
41 changes: 41 additions & 0 deletions
41
library/Zend/Stdlib/Guard/ArrayOrTraversableGuardTrait.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,41 @@ | |||
<?php | |||
/** | |||
* Zend Framework (http://framework.zend.com/) | |||
* | |||
* @link http://github.com/zendframework/zf2 for the canonical source repository | |||
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) | |||
* @license http://framework.zend.com/license/new-bsd New BSD License | |||
*/ | |||
|
|||
namespace Zend\Stdlib\Guard; | |||
|
|||
/** | |||
* Provide a guard method for array or Traversable data | |||
*/ | |||
trait ArrayOrTraversableGuardTrait | |||
{ | |||
|
|||
/** | |||
* Verifies that the data is an array or Traversable | |||
* | |||
* @param mixed $data the data to verify | |||
* @param string $dataName the data name | |||
* @param string $exceptionClass FQCN for the exception | |||
* @throws \Exception | |||
*/ | |||
protected function guardForArrayOrTraversable( | |||
$data, | |||
$dataName = 'Argument', | |||
$exceptionClass = 'Zend\Stdlib\Exception\InvalidArgumentException' | |||
) { | |||
if (!is_array($data) && !($data instanceof \Traversable)) { | |||
$message = sprintf( | |||
"%s must be an array or Traversable, [%s] given", | |||
$dataName, | |||
is_object($data) ? get_class($data) : gettype($data) | |||
); | |||
throw new $exceptionClass($message); | |||
} | |||
} | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,37 @@ | |||
<?php | |||
/** | |||
* Zend Framework (http://framework.zend.com/) | |||
* | |||
* @link http://github.com/zendframework/zf2 for the canonical source repository | |||
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) | |||
* @license http://framework.zend.com/license/new-bsd New BSD License | |||
*/ | |||
|
|||
namespace Zend\Stdlib\Guard; | |||
|
|||
/** | |||
* Provide a guard method against empty data | |||
*/ | |||
trait EmptyGuardTrait | |||
{ | |||
|
|||
/** | |||
* Verify that the data is not empty | |||
* | |||
* @param mixed $data the data to verify | |||
* @param string $dataName the data name | |||
* @param string $exceptionClass FQCN for the exception | |||
* @throws \Exception | |||
*/ | |||
protected function guardAgainstEmpty( | |||
$data, | |||
$dataName = 'Argument', | |||
$exceptionClass = 'Zend\Stdlib\Exception\InvalidArgumentException' | |||
) { | |||
if (empty($data)) { | |||
$message = sprintf('%s cannot be empty', $dataName); | |||
throw new $exceptionClass($message); | |||
} | |||
} | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,86 @@ | |||
<?php | |||
/** | |||
* Zend Framework (http://framework.zend.com/) | |||
* | |||
* @link http://github.com/zendframework/zf2 for the canonical source repository | |||
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) | |||
* @license http://framework.zend.com/license/new-bsd New BSD License | |||
*/ | |||
|
|||
namespace Zend\Stdlib\Guard; | |||
|
|||
/** | |||
* Static guard helper class | |||
* | |||
* Bridges the gap for allowing refactoring until traits can be used by default. | |||
* | |||
* @deprecated | |||
*/ | |||
abstract class GuardUtils | |||
{ | |||
|
|||
const DEFAULT_EXCEPTION_CLASS = 'Zend\Stdlib\Exception\InvalidArgumentException'; | |||
|
|||
|
|||
/** | |||
* Verifies that the data is an array or Traversable | |||
* | |||
* @param mixed $data the data to verify | |||
* @param string $dataName the data name | |||
* @param string $exceptionClass FQCN for the exception | |||
* @throws \Exception | |||
*/ | |||
public static function guardForArrayOrTraversable( | |||
$data, | |||
$dataName = 'Argument', | |||
$exceptionClass = self::DEFAULT_EXCEPTION_CLASS | |||
) { | |||
if (!is_array($data) && !($data instanceof \Traversable)) { | |||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Import |
|||
$message = sprintf( | |||
'%s must be an array or Traversable, [%s] given', | |||
$dataName, | |||
is_object($data) ? get_class($data) : gettype($data) | |||
); | |||
throw new $exceptionClass($message); | |||
} | |||
} | |||
|
|||
/** | |||
* Verify that the data is not empty | |||
* | |||
* @param mixed $data the data to verify | |||
* @param string $dataName the data name | |||
* @param string $exceptionClass FQCN for the exception | |||
* @throws \Exception | |||
*/ | |||
public static function guardAgainstEmpty( | |||
$data, | |||
$dataName = 'Argument', | |||
$exceptionClass = self::DEFAULT_EXCEPTION_CLASS | |||
) { | |||
if (empty($data)) { | |||
$message = sprintf('%s cannot be empty', $dataName); | |||
throw new $exceptionClass($message); | |||
} | |||
} | |||
|
|||
/** | |||
* Verify that the data is not null | |||
* | |||
* @param mixed $data the data to verify | |||
* @param string $dataName the data name | |||
* @param string $exceptionClass FQCN for the exception | |||
* @throws \Exception | |||
*/ | |||
public static function guardAgainstNull( | |||
$data, | |||
$dataName = 'Argument', | |||
$exceptionClass = self::DEFAULT_EXCEPTION_CLASS | |||
) { | |||
if (is_null($data)) { | |||
$message = sprintf('%s cannot be null', $dataName); | |||
throw new $exceptionClass($message); | |||
} | |||
} | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,37 @@ | |||
<?php | |||
/** | |||
* Zend Framework (http://framework.zend.com/) | |||
* | |||
* @link http://github.com/zendframework/zf2 for the canonical source repository | |||
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) | |||
* @license http://framework.zend.com/license/new-bsd New BSD License | |||
*/ | |||
|
|||
namespace Zend\Stdlib\Guard; | |||
|
|||
/** | |||
* Provide a guard method against null data | |||
*/ | |||
trait NullGuardTrait | |||
{ | |||
|
|||
/** | |||
* Verify that the data is not null | |||
* | |||
* @param mixed $data the data to verify | |||
* @param string $dataName the data name | |||
* @param string $exceptionClass FQCN for the exception | |||
* @throws \Exception | |||
*/ | |||
protected function guardAgainstNull( | |||
$data, | |||
$dataName = 'Argument', | |||
$exceptionClass = 'Zend\Stdlib\Exception\InvalidArgumentException' | |||
) { | |||
if (is_null($data)) { | |||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use |
|||
$message = sprintf('%s cannot be null', $dataName); | |||
throw new $exceptionClass($message); | |||
} | |||
} | |||
|
|||
} |
54 changes: 54 additions & 0 deletions
54
tests/ZendTest/Stdlib/Guard/ArrayOrTraversableGuardTraitTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,54 @@ | |||
<?php | |||
/** | |||
* Zend Framework (http://framework.zend.com/) | |||
* | |||
* @link http://github.com/zendframework/zf2 for the canonical source repository | |||
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) | |||
* @license http://framework.zend.com/license/new-bsd New BSD License | |||
*/ | |||
|
|||
namespace ZendTest\Stdlib\Guard; | |||
|
|||
use PHPUnit_Framework_TestCase as TestCase; | |||
use ZendTest\Stdlib\TestAsset\GuardedObject; | |||
use Zend\Stdlib\ArrayObject; | |||
|
|||
/** | |||
* @requires PHP 5.4 | |||
* @group Zend_StdLib_Guard | |||
* @covers Zend\Stdlib\Guard\ArrayOrTraversableGuardTrait | |||
*/ | |||
class ArrayOrTraversableGuardTraitTest extends TestCase | |||
{ | |||
|
|||
public function setUp() | |||
{ | |||
if (version_compare(PHP_VERSION, '5.4.0', '<')) { | |||
$this->markTestSkipped('Only valid for php >= 5.4'); | |||
} | |||
} | |||
|
|||
public function testGuardForArrayOrTraversableThrowsException() | |||
{ | |||
$object = new GuardedObject; | |||
$this->setExpectedException( | |||
'Zend\Stdlib\Exception\InvalidArgumentException', | |||
'Argument must be an array or Traversable, [string] given' | |||
); | |||
$object->setArrayOrTraversable(''); | |||
} | |||
|
|||
public function testGuardForArrayOrTraversableAllowsArray() | |||
{ | |||
$object = new GuardedObject; | |||
$this->assertNull($object->setArrayOrTraversable(array())); | |||
} | |||
|
|||
public function testGuardForArrayOrTraversableAllowsTraversable() | |||
{ | |||
$object = new GuardedObject; | |||
$traversable = new ArrayObject; | |||
$this->assertNull($object->setArrayOrTraversable($traversable)); | |||
} | |||
|
|||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,46 @@ | |||
<?php | |||
/** | |||
* Zend Framework (http://framework.zend.com/) | |||
* | |||
* @link http://github.com/zendframework/zf2 for the canonical source repository | |||
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com) | |||
* @license http://framework.zend.com/license/new-bsd New BSD License | |||
*/ | |||
|
|||
namespace ZendTest\Stdlib\Guard; | |||
|
|||
use PHPUnit_Framework_TestCase as TestCase; | |||
use ZendTest\Stdlib\TestAsset\GuardedObject; | |||
|
|||
/** | |||
* @requires PHP 5.4 | |||
* @group Zend_StdLib_Guard | |||
* @covers Zend\Stdlib\Guard\EmptyGuardTrait | |||
*/ | |||
class EmptyGuardTraitTest extends TestCase | |||
{ | |||
|
|||
public function setUp() | |||
{ | |||
if (version_compare(PHP_VERSION, '5.4.0', '<')) { | |||
$this->markTestSkipped('Only valid for php >= 5.4'); | |||
} | |||
} | |||
|
|||
public function testGuardAgainstEmptyThrowsException() | |||
{ | |||
$object = new GuardedObject; | |||
$this->setExpectedException( | |||
'Zend\Stdlib\Exception\InvalidArgumentException', | |||
'Argument cannot be empty' | |||
); | |||
$object->setNotEmpty(''); | |||
} | |||
|
|||
public function testGuardAgainstEmptyAllowsNonEmptyString() | |||
{ | |||
$object = new GuardedObject; | |||
$this->assertNull($object->setNotEmpty('foo')); | |||
} | |||
|
|||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Import
Traversable
so it does not need to be globally qualified here.