Rules for Traversable items

This commit is contained in:
Alexandre Gomes Gaigalas 2010-12-01 11:08:18 -02:00
parent de3c73ab91
commit 1c15766fcd
5 changed files with 88 additions and 34 deletions

View file

@ -5,24 +5,17 @@ namespace Respect\Validation\Rules;
use Respect\Validation\Rules\AbstractRule;
use Respect\Validation\Exceptions\ArrException;
class Arr extends AbstractRule
class Arr extends Traversable
{
public function validate($input)
protected function isTraversable($input)
{
return is_array($input);
}
public function assert($input)
protected function buildException($input)
{
if (!$this->validate($input))
throw new ArrException($input);
return true;
}
public function check($input)
{
return $this->assert($input);
return new ArrException($input);
}
}

View file

@ -2,10 +2,10 @@
namespace Respect\Validation\Rules;
use Respect\Validation\Rules\AbstractRule;
use Respect\Validation\Exceptions\HasAttributeException;
use Respect\Validation\Rules\All;
use Respect\Validation\Rules\AllOf;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Validatable;
use \ReflectionProperty;
use \ReflectionException;
@ -14,7 +14,7 @@ class HasAttribute extends AllOf
protected $attribute = '';
public function __construct($attribute, $attributeValidator=null)
public function __construct($attribute, Validatable $attributeValidator=null)
{
if (!is_string($attribute))
throw new ComponentException(

View file

@ -4,26 +4,68 @@ namespace Respect\Validation\Rules;
use Respect\Validation\Rules\AbstractRule;
use Respect\Validation\Exceptions\TraversableException;
use Respect\Validation\Validatable;
use \Traversable as Tvsable;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Exceptions\InvalidException;
class Traversable extends AbstractRule
class Traversable extends AllOf
{
public function validate($input)
protected $itemValidator;
public function __construct(Validatable $itemValidator=null)
{
$this->itemValidator = $itemValidator;
}
protected function isTraversable($input)
{
return is_array($input) || $input instanceof Tvsable;
}
protected function buildException($input)
{
return new TraversableException($input);
}
public function validate($input)
{
if (!$this->isTraversable($input))
return false;
if (!is_null($this->itemValidator))
foreach ($input as $item)
if (!$this->itemValidator->validate($item))
return false;
return true;
}
public function assert($input)
{
if (!$this->validate($input))
throw new TraversableException($input);
$exceptions = array();
if (!$this->isTraversable($input))
$exceptions[] = $this->buildException($input);
if (!is_null($this->itemValidator))
foreach ($input as $item)
try {
$this->itemValidator->assert($item);
} catch (InvalidException $e) {
$exceptions[] = $e;
}
if (!empty($exceptions))
throw $this->buildException($exceptions);
return true;
}
public function check($input)
{
return $this->assert($input);
if (!$this->isTraversable($input))
throw $this->buildException($exceptions);
if (!is_null($this->itemValidator))
foreach ($input as $item)
if (!$this->itemValidator->check($item))
return false;
return true;
}
}

View file

@ -10,8 +10,8 @@
<email>alexandre@gaigalas.net</email>
<active>yes</active>
</lead>
<date>2010-11-30</date>
<time>17:53:14</time>
<date>2010-12-01</date>
<time>11:08:03</time>
<version>
<release>0.1.0</release>
<api>0.1.0</api>
@ -55,14 +55,14 @@ First Version
<file baseinstalldir="Respect/Validation" md5sum="166932c321924d56c62df8072d219384" name="Rules/AllOf.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="12038e77ba8c7d4e0d26008537c5d6ba" name="Rules/Alnum.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="06ec4840e4a25174086488f899839833" name="Rules/Alpha.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="a2c659f9e3316296fadfea34bf63fe1f" name="Rules/Arr.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="e6860f2465befbbc1651429bd65b7f50" name="Rules/Arr.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="c5b6ef783e0a75f5b4998da8d3eccfc9" name="Rules/AtLeast.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="e0480101515ce1f4652eb6e44ac20b64" name="Rules/Between.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="299c17ed1c9255dec843fcf357fe83c2" name="Rules/Callback.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="72fc8b1df1b769d70451f3048a2cec39" name="Rules/Date.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="f6fb904763f6b00d7899dd994e226159" name="Rules/Digits.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="a7408cba21b34fe579b86f31b2945da9" name="Rules/Float.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="0e5effaf9205ee869d8a7b2c106d8189" name="Rules/HasAttribute.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="8c553c836a524d8080d88451e90fb031" name="Rules/HasAttribute.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="13989dea5d499f5ff011d8282dc71816" name="Rules/HasKey.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="b31e08b8169315283b5f5a8cc29e060c" name="Rules/HasOptionalAttribute.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="7866c852b26b09b962d3fb5d1aa5e5ca" name="Rules/HasOptionalKey.php" role="php" />
@ -80,7 +80,7 @@ First Version
<file baseinstalldir="Respect/Validation" md5sum="acba8599cb024751e51337a1211dd082" name="Rules/Regex.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="3fcd2d83507b2b03cbd45dd63db068c6" name="Rules/Sf.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="c25c184df18e7d20843c0b7d2eb9f68e" name="Rules/StringLength.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="d7cae01caf795f71785d0643884fdad9" name="Rules/Traversable.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="030e557259d1157b2164cd01a5f0a601" name="Rules/Traversable.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="2b0f271b8ea0e870c937663be56e5bd4" name="Rules/Zend.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="ff9ba258245ed557bf3487b3270780cb" name="Validatable.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="f58eaf419950d54b71f14222ca284530" name="Validator.php" role="php" />
@ -107,7 +107,7 @@ First Version
<release>alpha</release>
<api>alpha</api>
</stability>
<date>2010-11-30</date>
<date>2010-12-01</date>
<license uri="http://www.opensource.org/licenses/bsd-license.php">BSD Style</license>
<notes>
First Version

View file

@ -5,20 +5,14 @@ namespace Respect\Validation\Rules;
class TraversableTest extends \PHPUnit_Framework_TestCase
{
protected $object;
protected function setUp()
{
$this->object = new Traversable;
}
/**
* @dataProvider providerForTraversable
*
*/
public function testTraversable($input)
{
$this->assertTrue($this->object->assert($input));
$v = new Traversable;
$this->assertTrue($v->assert($input));
}
/**
@ -27,7 +21,32 @@ class TraversableTest extends \PHPUnit_Framework_TestCase
*/
public function testNotTraversable($input)
{
$this->assertTrue($this->object->assert($input));
$v = new Traversable;
$this->assertTrue($v->assert($input));
}
public function testTraversableItemValidator()
{
$v = new Traversable(new StringLength(5, 10));
$this->assertTrue(
$v->assert(
array('alganet'), array('kingolabs'), array('respect')
)
);
}
/**
* @dataProvider providerForNotTraversable
* @expectedException Respect\Validation\Exceptions\TraversableException
*/
public function testTraversableItemValidatorFalse()
{
$v = new Traversable(new StringLength(15, 30));
$this->assertFalse(
$v->assert(
array('alganet'), array('kingolabs'), array('respect')
)
);
}
public function providerForTraversable()