This repository has been archived by the owner on Jan 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/5364' into develop
- Loading branch information
Showing
10 changed files
with
453 additions
and
0 deletions.
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2014 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; | ||
} |
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 | 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-2014 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace Zend\Stdlib\Guard; | ||
|
||
use Traversable; | ||
|
||
/** | ||
* 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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2014 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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace Zend\Stdlib\Guard; | ||
|
||
use Traversable; | ||
|
||
/** | ||
* 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)) { | ||
$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 (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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2014 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 (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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2014 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 | ||
* @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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2014 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 | ||
* @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')); | ||
} | ||
} |
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 | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2014 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 Zend\Stdlib\Guard\GuardUtils; | ||
use Zend\Stdlib\ArrayObject; | ||
|
||
/** | ||
* @covers Zend\Stdlib\Guard\GuardUtils | ||
*/ | ||
class GuardUtilsTest extends TestCase | ||
{ | ||
public function testGuardForArrayOrTraversableThrowsException() | ||
{ | ||
$this->setExpectedException( | ||
'Zend\Stdlib\Exception\InvalidArgumentException', | ||
'Argument must be an array or Traversable, [string] given' | ||
); | ||
GuardUtils::guardForArrayOrTraversable(''); | ||
} | ||
|
||
public function testGuardForArrayOrTraversableAllowsArray() | ||
{ | ||
$this->assertNull(GuardUtils::guardForArrayOrTraversable(array())); | ||
} | ||
|
||
public function testGuardForArrayOrTraversableAllowsTraversable() | ||
{ | ||
$traversable = new ArrayObject; | ||
$this->assertNull(GuardUtils::guardForArrayOrTraversable($traversable)); | ||
} | ||
|
||
public function testGuardAgainstEmptyThrowsException() | ||
{ | ||
$this->setExpectedException( | ||
'Zend\Stdlib\Exception\InvalidArgumentException', | ||
'Argument cannot be empty' | ||
); | ||
GuardUtils::guardAgainstEmpty(''); | ||
} | ||
|
||
public function testGuardAgainstEmptyAllowsNonEmptyString() | ||
{ | ||
$this->assertNull(GuardUtils::guardAgainstEmpty('foo')); | ||
} | ||
|
||
public function testGuardAgainstNullThrowsException() | ||
{ | ||
$this->setExpectedException( | ||
'Zend\Stdlib\Exception\InvalidArgumentException', | ||
'Argument cannot be null' | ||
); | ||
GuardUtils::guardAgainstNull(null); | ||
} | ||
|
||
public function testGuardAgainstNullAllowsNonNull() | ||
{ | ||
$this->assertNull(GuardUtils::guardAgainstNull('foo')); | ||
} | ||
} |
Oops, something went wrong.