mirror of
https://github.com/Respect/Validation.git
synced 2024-06-08 08:42:15 +02:00
CompositeValidator split in two (All and One), removed Chain for further rewrite
This commit is contained in:
parent
8a7f2770c9
commit
5850afdad5
|
@ -2,7 +2,8 @@
|
|||
|
||||
namespace Respect\Validation;
|
||||
|
||||
class CompositeValidator extends AbstractNode implements Validatable
|
||||
abstract class AbstractCompositeValidator extends AbstractNode
|
||||
implements Validatable
|
||||
{
|
||||
|
||||
protected $validators = array();
|
||||
|
@ -78,42 +79,4 @@ class CompositeValidator extends AbstractNode implements Validatable
|
|||
return $exceptions;
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
$exceptions = $this->iterateValidation($input);
|
||||
if (!empty($exceptions))
|
||||
throw new InvalidException($exceptions);
|
||||
return true;
|
||||
}
|
||||
|
||||
public function validateOne($input)
|
||||
{
|
||||
$validators = $this->getValidators();
|
||||
$exceptions = $this->iterateValidation($input);
|
||||
if (count($exceptions) === count($validators))
|
||||
throw new InvalidException($exceptions);
|
||||
return true;
|
||||
}
|
||||
|
||||
public function isValid($input)
|
||||
{
|
||||
$validators = $this->getValidators();
|
||||
return count($validators) === count(array_filter(
|
||||
$validators,
|
||||
function($v) use($input) {
|
||||
return $v->isValid($input);
|
||||
}
|
||||
));
|
||||
}
|
||||
|
||||
public function isOneValid($input)
|
||||
{
|
||||
return (boolean) array_filter(
|
||||
$this->getValidators(),
|
||||
function($v) use($input) {
|
||||
return $v->isValid($input);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,42 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation;
|
||||
|
||||
use LogicException;
|
||||
|
||||
class Chain extends CompositeValidator
|
||||
{
|
||||
|
||||
protected $input;
|
||||
protected $validators;
|
||||
|
||||
public static function all($validator, $anotherValidator=null, $etc=null)
|
||||
{
|
||||
$validator = new static;
|
||||
foreach (func_get_args() as $v)
|
||||
if (!is_array($v))
|
||||
$validator->addValidator($v);
|
||||
else
|
||||
$validator->addValidators($v);
|
||||
return $validator;
|
||||
}
|
||||
|
||||
public static function __callStatic($method, $arguments)
|
||||
{
|
||||
$validator = new static;
|
||||
$validator->__call($method, $arguments);
|
||||
return $validator;
|
||||
}
|
||||
|
||||
public function __call($method, $arguments)
|
||||
{
|
||||
if (0 === strpos($method, 'valid'))
|
||||
foreach ($arguments as $a)
|
||||
if (!is_array($a))
|
||||
$this->addValidator(substr($method, 5) . '\\' . $a);
|
||||
else
|
||||
$this->addValidators($a, substr($method, 5));
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
31
library/Respect/Validation/Composite/All.php
Normal file
31
library/Respect/Validation/Composite/All.php
Normal file
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Composite;
|
||||
|
||||
use Respect\Validation\AbstractCompositeValidator;
|
||||
use Respect\Validation\InvalidException;
|
||||
|
||||
class All extends AbstractCompositeValidator
|
||||
{
|
||||
|
||||
public function isValid($input)
|
||||
{
|
||||
$validators = $this->getValidators();
|
||||
return count($validators) === count(array_filter(
|
||||
$validators,
|
||||
function($v) use($input) {
|
||||
return $v->isValid($input);
|
||||
}
|
||||
));
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
$exceptions = $this->iterateValidation($input);
|
||||
if (!empty($exceptions))
|
||||
throw new InvalidException($exceptions);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
}
|
30
library/Respect/Validation/Composite/One.php
Normal file
30
library/Respect/Validation/Composite/One.php
Normal file
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Composite;
|
||||
|
||||
use Respect\Validation\AbstractCompositeValidator;
|
||||
use Respect\Validation\InvalidException;
|
||||
|
||||
class One extends AbstractCompositeValidator
|
||||
{
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
$validators = $this->getValidators();
|
||||
$exceptions = $this->iterateValidation($input);
|
||||
if (count($exceptions) === count($validators))
|
||||
throw new InvalidException($exceptions);
|
||||
return true;
|
||||
}
|
||||
|
||||
public function isValid($input)
|
||||
{
|
||||
return (boolean) array_filter(
|
||||
$this->getValidators(),
|
||||
function($v) use($input) {
|
||||
return $v->isValid($input);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Date;
|
||||
|
||||
class AbstractDateException extends \OutOfBoundsException
|
||||
{
|
||||
|
||||
}
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Respect\Validation\Date;
|
||||
|
||||
class OutOfBoundsException extends \OutOfBoundsException
|
||||
class OutOfBoundsException extends AbstractDateException
|
||||
{
|
||||
|
||||
}
|
|
@ -0,0 +1,110 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation;
|
||||
|
||||
class AbstractCompositeValidatorTest extends ValidatorTestCase
|
||||
{
|
||||
|
||||
protected $object;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->object = $this->getMockForAbstractClass(
|
||||
'Respect\Validation\AbstractCompositeValidator'
|
||||
);
|
||||
}
|
||||
|
||||
protected function tearDown()
|
||||
{
|
||||
unset($this->object);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForMockImpossibleValidators
|
||||
*/
|
||||
public function testAddExistentValidator($validator)
|
||||
{
|
||||
$this->object->addValidator($validator);
|
||||
$this->assertContains($validator, $this->object->getValidators());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Respect\Validation\ComponentException
|
||||
*/
|
||||
public function testAddNonValidator()
|
||||
{
|
||||
$this->object->addValidator(new \stdClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Respect\Validation\ComponentException
|
||||
*/
|
||||
public function testAddNonValidator2()
|
||||
{
|
||||
if (!class_exists('Respect\Validation\Foo\Freak', false)) {
|
||||
eval("
|
||||
namespace Respect\Validation\Foo;
|
||||
class Freak{}
|
||||
");
|
||||
}
|
||||
$this->object->addValidator('Foo\Freak');
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForMockImpossibleValidators
|
||||
*/
|
||||
public function testAddValidatorsMessages($a, $b)
|
||||
{
|
||||
$messagesA = $a->getMessages();
|
||||
$messagesB = $b->getMessages();
|
||||
$this->object->addValidators(func_get_args());
|
||||
$messagesObject = $this->object->getMessages();
|
||||
foreach ($messagesA as $m) {
|
||||
$this->assertContains($m, $messagesObject);
|
||||
}
|
||||
foreach ($messagesB as $m) {
|
||||
$this->assertContains($m, $messagesObject);
|
||||
}
|
||||
}
|
||||
|
||||
public function testBuildValidators()
|
||||
{
|
||||
$this->providerForMockImpossibleValidators();
|
||||
$this->object->addValidators(array(
|
||||
'Foo\Bar', 'Foo\Baz', 'Foo\Bat' => array(1, 2, 3)
|
||||
));
|
||||
$this->assertValidatorPresence($this->object, 'Bar', 'Baz', 'Bat');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Respect\Validation\ComponentException
|
||||
*/
|
||||
public function testBuildValidatorsInvalid()
|
||||
{
|
||||
$this->providerForMockImpossibleValidators();
|
||||
$this->object->addValidators(array(
|
||||
'Foo\Bar', 'Foo\Baz', 'Foo\Bat' => 'balkbal'
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForMockImpossibleValidators
|
||||
*/
|
||||
public function testSetMessages($a, $b)
|
||||
{
|
||||
$messagesA = $a->getMessages();
|
||||
$messagesB = $b->getMessages();
|
||||
$this->object->addValidators(func_get_args());
|
||||
$this->object->setMessages(
|
||||
array_map('strrev', $this->object->getMessages())
|
||||
);
|
||||
$messagesObject = $this->object->getMessages();
|
||||
foreach ($messagesA as $m) {
|
||||
$this->assertContains(strrev($m), $messagesObject);
|
||||
}
|
||||
foreach ($messagesB as $m) {
|
||||
$this->assertContains(strrev($m), $messagesObject);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,61 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation;
|
||||
|
||||
class ChainTest extends ValidatorTestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @dataProvider providerForMockImpossibleValidators
|
||||
*/
|
||||
public function testSimpleChainValidators()
|
||||
{
|
||||
$validator = Chain::validFoo('Bar', 'Baz', 'Bat');
|
||||
$this->assertValidatorPresence($validator, 'Bar', 'Baz', 'Bat');
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForMockImpossibleValidators
|
||||
*/
|
||||
public function testNotSoSimpleChainValidators()
|
||||
{
|
||||
$validator = Chain::validFoo('Bar', 'Baz')->validFoo('Bat');
|
||||
$this->assertValidatorPresence($validator, 'Bar', 'Baz', 'Bat');
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForMockImpossibleValidators
|
||||
*/
|
||||
public function testComplexChainValidators()
|
||||
{
|
||||
$validator = Chain::validFoo(array(
|
||||
'Bar',
|
||||
'Baz' => array(234, 2342)
|
||||
))->validFoo('Bat');
|
||||
$this->assertValidatorPresence($validator, 'Bar', 'Baz', 'Bat');
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForMockImpossibleValidators
|
||||
*/
|
||||
public function testAll()
|
||||
{
|
||||
$validator = Chain::all(
|
||||
'Foo\Bar', 'Foo\Baz', 'Foo\Bat'
|
||||
);
|
||||
$this->assertValidatorPresence($validator, 'Bar', 'Baz', 'Bat');
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForMockImpossibleValidators
|
||||
*/
|
||||
public function testAllArray()
|
||||
{
|
||||
$validator = Chain::all(
|
||||
array('Foo\Bar', 'Foo\Baz', 'Foo\Bat')
|
||||
);
|
||||
$this->assertValidatorPresence($validator, 'Bar', 'Baz', 'Bat');
|
||||
}
|
||||
|
||||
|
||||
}
|
69
tests/library/Respect/Validation/Composite/AllTest.php
Normal file
69
tests/library/Respect/Validation/Composite/AllTest.php
Normal file
|
@ -0,0 +1,69 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Composite;
|
||||
|
||||
use Respect\Validation\ValidatorTestCase;
|
||||
use Respect\Validation\InvalidException;
|
||||
|
||||
class AllTest extends ValidatorTestCase
|
||||
{
|
||||
protected $object;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->object = new All;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForMockValidators
|
||||
*/
|
||||
public function testValidateManyValid($a, $b, $c)
|
||||
{
|
||||
$this->object->addValidators(func_get_args());
|
||||
$this->assertTrue($this->object->validate('any'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForMockValidators
|
||||
*/
|
||||
public function testManyIsValid($a, $b, $c)
|
||||
{
|
||||
$this->object->addValidators(func_get_args());
|
||||
$this->assertTrue($this->object->isValid('any'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForMockImpossibleValidators
|
||||
*/
|
||||
public function testManyIsInvalid($a, $b, $c)
|
||||
{
|
||||
$this->object->addValidators(func_get_args());
|
||||
$this->assertFalse($this->object->isValid('any'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForMockImpossibleValidators
|
||||
*/
|
||||
public function testManyIsInvalid2($a, $b, $c)
|
||||
{
|
||||
$this->object->addValidators(func_get_args());
|
||||
$this->object->addValidator(
|
||||
$this->buildMockValidator('Aids', array('Aids_1' => 'aesfg'))
|
||||
);
|
||||
$this->assertFalse($this->object->isValid('any'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForMockImpossibleValidators
|
||||
*/
|
||||
public function testValidateAllInvalid($a, $b, $c)
|
||||
{
|
||||
$this->object->addValidators(func_get_args());
|
||||
try {
|
||||
$this->object->validate('any');
|
||||
} catch (InvalidException $e) {
|
||||
$this->assertEquals(3, count($e->getExceptions()));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
62
tests/library/Respect/Validation/Composite/OneTest.php
Normal file
62
tests/library/Respect/Validation/Composite/OneTest.php
Normal file
|
@ -0,0 +1,62 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Composite;
|
||||
|
||||
use Respect\Validation\ValidatorTestCase;
|
||||
use Respect\Validation\InvalidException;
|
||||
|
||||
class OneTest extends ValidatorTestCase
|
||||
{
|
||||
|
||||
protected $object;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->object = new One;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForMockImpossibleValidators
|
||||
*/
|
||||
public function testValidateOneValid($invalidA, $invalidB, $invalidC)
|
||||
{
|
||||
$valid = $this->buildMockValidator('Darth', array('Darth_1' => 'o54n'));
|
||||
$this->object->addValidators(func_get_args());
|
||||
$this->object->addValidator($valid);
|
||||
$this->assertTrue($this->object->validate('any'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForMockImpossibleValidators
|
||||
*/
|
||||
public function testValidateOneAllRotten($invalidA, $invalidB, $invalidC)
|
||||
{
|
||||
$this->object->addValidators(func_get_args());
|
||||
try {
|
||||
$this->object->validate('any');
|
||||
} catch (InvalidException $e) {
|
||||
$this->assertEquals(3, count($e->getExceptions()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForMockImpossibleValidators
|
||||
*/
|
||||
public function testIsValidOneValid($invalidA, $invalidB, $invalidC)
|
||||
{
|
||||
$valid = $this->buildMockValidator('Darth', array('Darth_1' => 'o54n'));
|
||||
$this->object->addValidators(func_get_args());
|
||||
$this->object->addValidator($valid);
|
||||
$this->assertTrue($this->object->isValid('any'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForMockImpossibleValidators
|
||||
*/
|
||||
public function testIsValidOneAllRotten($invalidA, $invalidB, $invalidC)
|
||||
{
|
||||
$this->object->addValidators(func_get_args());
|
||||
$this->assertFalse($this->object->isValid('any'));
|
||||
}
|
||||
|
||||
}
|
|
@ -1,204 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation;
|
||||
|
||||
class CompositeValidatorTest extends ValidatorTestCase
|
||||
{
|
||||
|
||||
protected $object;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->object = new CompositeValidator;
|
||||
}
|
||||
|
||||
protected function tearDown()
|
||||
{
|
||||
unset($this->object);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForMockImpossibleValidators
|
||||
*/
|
||||
public function testAddExistentValidator($validator)
|
||||
{
|
||||
$this->object->addValidator($validator);
|
||||
$this->assertContains($validator, $this->object->getValidators());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Respect\Validation\ComponentException
|
||||
*/
|
||||
public function testAddNonValidator()
|
||||
{
|
||||
$this->object->addValidator(new \stdClass);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Respect\Validation\ComponentException
|
||||
*/
|
||||
public function testAddNonValidator2()
|
||||
{
|
||||
if (!class_exists('Respect\Validation\Foo\Freak', false)) {
|
||||
eval("
|
||||
namespace Respect\Validation\Foo;
|
||||
class Freak{}
|
||||
");
|
||||
}
|
||||
$this->object->addValidator('Foo\Freak');
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForMockImpossibleValidators
|
||||
*/
|
||||
public function testAddValidatorsMessages($a, $b)
|
||||
{
|
||||
$messagesA = $a->getMessages();
|
||||
$messagesB = $b->getMessages();
|
||||
$this->object->addValidators(func_get_args());
|
||||
$messagesObject = $this->object->getMessages();
|
||||
foreach ($messagesA as $m) {
|
||||
$this->assertContains($m, $messagesObject);
|
||||
}
|
||||
foreach ($messagesB as $m) {
|
||||
$this->assertContains($m, $messagesObject);
|
||||
}
|
||||
}
|
||||
|
||||
public function testBuildValidators()
|
||||
{
|
||||
$this->providerForMockImpossibleValidators();
|
||||
$this->object->addValidators(array(
|
||||
'Foo\Bar', 'Foo\Baz', 'Foo\Bat' => array(1, 2, 3)
|
||||
));
|
||||
$this->assertValidatorPresence($this->object, 'Bar', 'Baz', 'Bat');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Respect\Validation\ComponentException
|
||||
*/
|
||||
public function testBuildValidatorsInvalid()
|
||||
{
|
||||
$this->providerForMockImpossibleValidators();
|
||||
$this->object->addValidators(array(
|
||||
'Foo\Bar', 'Foo\Baz', 'Foo\Bat' => 'balkbal'
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForMockValidators
|
||||
*/
|
||||
public function testValidateManyValid($a, $b, $c)
|
||||
{
|
||||
$this->object->addValidators(func_get_args());
|
||||
$this->assertTrue($this->object->validate('any'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForMockValidators
|
||||
*/
|
||||
public function testManyIsValid($a, $b, $c)
|
||||
{
|
||||
$this->object->addValidators(func_get_args());
|
||||
$this->assertTrue($this->object->isValid('any'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForMockImpossibleValidators
|
||||
*/
|
||||
public function testManyIsInvalid($a, $b, $c)
|
||||
{
|
||||
$this->object->addValidators(func_get_args());
|
||||
$this->assertFalse($this->object->isValid('any'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForMockImpossibleValidators
|
||||
*/
|
||||
public function testManyIsInvalid2($a, $b, $c)
|
||||
{
|
||||
$this->object->addValidators(func_get_args());
|
||||
$this->object->addValidator(
|
||||
$this->buildMockValidator('Aids', array('Aids_1' => 'aesfg'))
|
||||
);
|
||||
$this->assertFalse($this->object->isValid('any'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForMockImpossibleValidators
|
||||
*/
|
||||
public function testValidateAllInvalid($a, $b, $c)
|
||||
{
|
||||
$this->object->addValidators(func_get_args());
|
||||
try {
|
||||
$this->object->validate('any');
|
||||
} catch (InvalidException $e) {
|
||||
$this->assertEquals(3, count($e->getExceptions()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForMockImpossibleValidators
|
||||
*/
|
||||
public function testValidateOneValid($invalidA, $invalidB, $invalidC)
|
||||
{
|
||||
$valid = $this->buildMockValidator('Darth', array('Darth_1' => 'o54n'));
|
||||
$this->object->addValidators(func_get_args());
|
||||
$this->object->addValidator($valid);
|
||||
$this->assertTrue($this->object->validateOne('any'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForMockImpossibleValidators
|
||||
*/
|
||||
public function testValidateOneAllRotten($invalidA, $invalidB, $invalidC)
|
||||
{
|
||||
$this->object->addValidators(func_get_args());
|
||||
try {
|
||||
$this->object->validateOne('any');
|
||||
} catch (InvalidException $e) {
|
||||
$this->assertEquals(3, count($e->getExceptions()));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForMockImpossibleValidators
|
||||
*/
|
||||
public function testIsValidOneValid($invalidA, $invalidB, $invalidC)
|
||||
{
|
||||
$valid = $this->buildMockValidator('Darth', array('Darth_1' => 'o54n'));
|
||||
$this->object->addValidators(func_get_args());
|
||||
$this->object->addValidator($valid);
|
||||
$this->assertTrue($this->object->isOneValid('any'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForMockImpossibleValidators
|
||||
*/
|
||||
public function testIsValidOneAllRotten($invalidA, $invalidB, $invalidC)
|
||||
{
|
||||
$this->object->addValidators(func_get_args());
|
||||
$this->assertFalse($this->object->isOneValid('any'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForMockImpossibleValidators
|
||||
*/
|
||||
public function testSetMessages($a, $b)
|
||||
{
|
||||
$messagesA = $a->getMessages();
|
||||
$messagesB = $b->getMessages();
|
||||
$this->object->addValidators(func_get_args());
|
||||
$this->object->setMessages(
|
||||
array_map('strrev', $this->object->getMessages())
|
||||
);
|
||||
$messagesObject = $this->object->getMessages();
|
||||
foreach ($messagesA as $m) {
|
||||
$this->assertContains(strrev($m), $messagesObject);
|
||||
}
|
||||
foreach ($messagesB as $m) {
|
||||
$this->assertContains(strrev($m), $messagesObject);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue