mirror of
https://github.com/Respect/Validation.git
synced 2024-06-04 23:02:16 +02:00
New Each validator. Removed Traversable and refactored Arr. Moved check() to AbstractRule, just the rules that need to overload it now implement it.
This commit is contained in:
parent
35aee77111
commit
92d7c1e919
12
library/Respect/Validation/Exceptions/ArrException.php
Normal file
12
library/Respect/Validation/Exceptions/ArrException.php
Normal file
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class ArrException extends ValidationException
|
||||
{
|
||||
const INVALID_ARR = 'Arr_1';
|
||||
public static $defaultTemplates = array(
|
||||
self::INVALID_ARR => '"%s" is not an array',
|
||||
);
|
||||
|
||||
}
|
12
library/Respect/Validation/Exceptions/EachException.php
Normal file
12
library/Respect/Validation/Exceptions/EachException.php
Normal file
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class EachException extends ValidationException
|
||||
{
|
||||
const INVALID_EACH= 'Each_1';
|
||||
public static $defaultTemplates = array(
|
||||
self::INVALID_EACH => '%3$d invalid itens found',
|
||||
);
|
||||
|
||||
}
|
|
@ -8,7 +8,7 @@ use Respect\Validation\Validatable;
|
|||
use Respect\Validation\Exceptions\ComponentException;
|
||||
use Respect\Validation\Exceptions\ValidationException;
|
||||
|
||||
abstract class AbstractComposite extends AbstractRule
|
||||
abstract class AbstractComposite extends AbstractRule implements Validatable
|
||||
{
|
||||
|
||||
protected $rules = array();
|
||||
|
|
|
@ -9,7 +9,7 @@ use Respect\Validation\Validatable;
|
|||
use \ReflectionProperty;
|
||||
use \ReflectionException;
|
||||
|
||||
abstract class AbstractRelated extends AbstractRule
|
||||
abstract class AbstractRelated extends AbstractRule implements Validatable
|
||||
{
|
||||
|
||||
protected $mandatory = true;
|
||||
|
@ -32,6 +32,8 @@ abstract class AbstractRelated extends AbstractRule
|
|||
|
||||
abstract protected function getReferenceValue($input);
|
||||
|
||||
abstract protected function createException();
|
||||
|
||||
protected function reportError($input, ValidationException $related=null)
|
||||
{
|
||||
$e = $this->getException();
|
||||
|
@ -49,7 +51,8 @@ abstract class AbstractRelated extends AbstractRule
|
|||
if ($this->mandatory && !$this->hasReference($input))
|
||||
return false;
|
||||
if (!is_null($this->referenceValidator))
|
||||
return $this->referenceValidator->validate($this->getReferenceValue($input));
|
||||
return $this->referenceValidator
|
||||
->validate($this->getReferenceValue($input));
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -59,7 +62,9 @@ abstract class AbstractRelated extends AbstractRule
|
|||
throw $this->reportError($input);
|
||||
try {
|
||||
if (!is_null($this->referenceValidator))
|
||||
$this->referenceValidator->assert($this->getReferenceValue($input));
|
||||
$this->referenceValidator->assert(
|
||||
$this->getReferenceValue($input)
|
||||
);
|
||||
} catch (ValidationException $e) {
|
||||
throw $this->reportError($input, $e);
|
||||
} catch (ReflectionException $e) {
|
||||
|
@ -70,7 +75,13 @@ abstract class AbstractRelated extends AbstractRule
|
|||
|
||||
public function check($input)
|
||||
{
|
||||
return $this->assert($input);
|
||||
if ($this->mandatory && !$this->hasReference($input))
|
||||
throw $this->reportError($input);
|
||||
if (!is_null($this->referenceValidator))
|
||||
$this->referenceValidator->check(
|
||||
$this->getReferenceValue($input)
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
|
@ -25,4 +25,9 @@ abstract class AbstractRule implements Validatable
|
|||
$this->exception = $e;
|
||||
}
|
||||
|
||||
public function check($input)
|
||||
{
|
||||
return $this->assert($input);
|
||||
}
|
||||
|
||||
}
|
66
library/Respect/Validation/Rules/AbstractVector.php
Normal file
66
library/Respect/Validation/Rules/AbstractVector.php
Normal file
|
@ -0,0 +1,66 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Validatable;
|
||||
use Respect\Validation\Exceptions\ValidationException;
|
||||
use Traversable;
|
||||
|
||||
abstract class AbstractVector extends AbstractRule
|
||||
{
|
||||
|
||||
protected $itemValidator;
|
||||
|
||||
public function __construct(Validatable $itemValidator)
|
||||
{
|
||||
$this->itemValidator = $itemValidator;
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
if (!is_array($input) || $input instanceof Traversable)
|
||||
return false;
|
||||
foreach ($input as $item) {
|
||||
if (!$this->itemValidator->validate($item))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
protected function reportError($input, $item=null, array $related = array())
|
||||
{
|
||||
$e = $this->getException();
|
||||
if ($e)
|
||||
return $e;
|
||||
$e = $this->createException();
|
||||
$e->setRelated($related);
|
||||
$e->configure($input, $item, count($related));
|
||||
return $e;
|
||||
}
|
||||
|
||||
abstract protected function createException();
|
||||
|
||||
public function assert($input)
|
||||
{
|
||||
if (!is_array($input) || $input instanceof Traversable)
|
||||
throw $this->reportError($input);
|
||||
$exceptions = array();
|
||||
foreach ($input as $item)
|
||||
try {
|
||||
$this->itemValidator->assert($item);
|
||||
} catch (ValidationException $e) {
|
||||
$exceptions[] = $e;
|
||||
}
|
||||
if (!empty($exceptions))
|
||||
throw $this->reportError($input, $item, $exceptions);
|
||||
return true;
|
||||
}
|
||||
|
||||
public function check($input)
|
||||
{
|
||||
foreach ($input as $item)
|
||||
$this->itemValidator->check($item);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
|
@ -37,9 +37,4 @@ class Alpha extends AbstractRule
|
|||
return true;
|
||||
}
|
||||
|
||||
public function check($input)
|
||||
{
|
||||
return $this->assert($input);
|
||||
}
|
||||
|
||||
}
|
25
library/Respect/Validation/Rules/Arr.php
Normal file
25
library/Respect/Validation/Rules/Arr.php
Normal file
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Rules\AbstractRule;
|
||||
use Respect\Validation\Exceptions\ArrException;
|
||||
use \ArrayObject;
|
||||
|
||||
class Arr extends AbstractRule
|
||||
{
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
return is_array($input) || $input instanceof ArrayObject;
|
||||
}
|
||||
|
||||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw $this->getException() ? : ArrException::create()
|
||||
->configure($input);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
|
@ -37,9 +37,4 @@ class Between extends AllOf
|
|||
return true;
|
||||
}
|
||||
|
||||
public function check($input)
|
||||
{
|
||||
return $this->assert($input);
|
||||
}
|
||||
|
||||
}
|
|
@ -33,9 +33,4 @@ class Callback extends AbstractRule
|
|||
return true;
|
||||
}
|
||||
|
||||
public function check($input)
|
||||
{
|
||||
return $this->assert($input);
|
||||
}
|
||||
|
||||
}
|
|
@ -41,9 +41,4 @@ class Date extends AbstractRule
|
|||
return true;
|
||||
}
|
||||
|
||||
public function check($input)
|
||||
{
|
||||
return $this->assert($input);
|
||||
}
|
||||
|
||||
}
|
|
@ -20,9 +20,4 @@ class Digits extends AbstractRule
|
|||
return true;
|
||||
}
|
||||
|
||||
public function check($input)
|
||||
{
|
||||
return $this->assert($input);
|
||||
}
|
||||
|
||||
}
|
15
library/Respect/Validation/Rules/Each.php
Normal file
15
library/Respect/Validation/Rules/Each.php
Normal file
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Exceptions\EachException;
|
||||
|
||||
class Each extends AbstractVector
|
||||
{
|
||||
|
||||
protected function createException()
|
||||
{
|
||||
return EachException::create();
|
||||
}
|
||||
|
||||
}
|
|
@ -21,9 +21,4 @@ class Float extends AbstractRule
|
|||
return true;
|
||||
}
|
||||
|
||||
public function check($input)
|
||||
{
|
||||
return $this->assert($input);
|
||||
}
|
||||
|
||||
}
|
|
@ -8,11 +8,6 @@ use Respect\Validation\Exceptions\HexaException;
|
|||
class Hexa extends AbstractRule
|
||||
{
|
||||
|
||||
public function createException()
|
||||
{
|
||||
return new HexaException;
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
return ctype_xdigit($input);
|
||||
|
@ -21,14 +16,9 @@ class Hexa extends AbstractRule
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw $this->getException() ? : HexaException::create()
|
||||
->configure($input);
|
||||
throw $this->getException() ? : HexaException::create()
|
||||
->configure($input);
|
||||
return true;
|
||||
}
|
||||
|
||||
public function check($input)
|
||||
{
|
||||
return $this->assert($input);
|
||||
}
|
||||
|
||||
}
|
|
@ -10,11 +10,6 @@ class Instance extends AbstractRule
|
|||
|
||||
public $instance;
|
||||
|
||||
public function createException()
|
||||
{
|
||||
return new InstanceException;
|
||||
}
|
||||
|
||||
public function __construct($instance)
|
||||
{
|
||||
$this->instance = $instance;
|
||||
|
@ -28,14 +23,9 @@ class Instance extends AbstractRule
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw $this->getException() ? : InstanceException::create()
|
||||
->configure($input, $this->instance);
|
||||
throw $this->getException() ? : InstanceException::create()
|
||||
->configure($input, $this->instance);
|
||||
return true;
|
||||
}
|
||||
|
||||
public function check($input)
|
||||
{
|
||||
return $this->assert($input);
|
||||
}
|
||||
|
||||
}
|
|
@ -13,11 +13,6 @@ class Ip extends AbstractRule
|
|||
$this->options = $options;
|
||||
}
|
||||
|
||||
public function createException()
|
||||
{
|
||||
return new IpException;
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
return filter_var(
|
||||
|
@ -28,14 +23,9 @@ class Ip extends AbstractRule
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw $this->getException() ? : IpException::create()
|
||||
->configure($input);
|
||||
throw $this->getException() ? : IpException::create()
|
||||
->configure($input);
|
||||
return true;
|
||||
}
|
||||
|
||||
public function check($input)
|
||||
{
|
||||
return $this->assert($input);
|
||||
}
|
||||
|
||||
}
|
|
@ -28,9 +28,4 @@ class Max extends AbstractRule
|
|||
return true;
|
||||
}
|
||||
|
||||
public function check($input)
|
||||
{
|
||||
return $this->assert($input);
|
||||
}
|
||||
|
||||
}
|
|
@ -28,9 +28,4 @@ class Min extends AbstractRule
|
|||
return true;
|
||||
}
|
||||
|
||||
public function check($input)
|
||||
{
|
||||
return $this->assert($input);
|
||||
}
|
||||
|
||||
}
|
|
@ -8,11 +8,6 @@ use Respect\Validation\Exceptions\NoWhitespaceException;
|
|||
class NoWhitespace extends AbstractRule
|
||||
{
|
||||
|
||||
public function createException()
|
||||
{
|
||||
return new NoWhitespaceException;
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
return preg_match('#^\S+$#', $input);
|
||||
|
@ -21,14 +16,9 @@ class NoWhitespace extends AbstractRule
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw $this->getException() ? : NoWhitespaceException::create()
|
||||
->configure($input);
|
||||
throw $this->getException() ? : NoWhitespaceException::create()
|
||||
->configure($input);
|
||||
return true;
|
||||
}
|
||||
|
||||
public function check($input)
|
||||
{
|
||||
return $this->assert($input);
|
||||
}
|
||||
|
||||
}
|
|
@ -7,11 +7,6 @@ use Respect\Validation\Exceptions\NoneOfException;
|
|||
class NoneOf extends AbstractComposite
|
||||
{
|
||||
|
||||
public function createException()
|
||||
{
|
||||
return new NoneOfException;
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
$validators = $this->getRules();
|
||||
|
@ -35,9 +30,4 @@ class NoneOf extends AbstractComposite
|
|||
return true;
|
||||
}
|
||||
|
||||
public function check($input)
|
||||
{
|
||||
return $this->assert($input);
|
||||
}
|
||||
|
||||
}
|
|
@ -8,11 +8,6 @@ use Respect\Validation\Rules\AbstractRule;
|
|||
class NotEmpty extends AbstractRule
|
||||
{
|
||||
|
||||
public function createException()
|
||||
{
|
||||
return new NotEmptyException;
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
if (is_string($input))
|
||||
|
@ -23,14 +18,9 @@ class NotEmpty extends AbstractRule
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw $this->getException() ? : NotEmptyException::create()
|
||||
->configure($input);
|
||||
throw $this->getException() ? : NotEmptyException::create()
|
||||
->configure($input);
|
||||
return true;
|
||||
}
|
||||
|
||||
public function check($input)
|
||||
{
|
||||
return $this->assert($input);
|
||||
}
|
||||
|
||||
}
|
|
@ -8,11 +8,6 @@ use Respect\Validation\Exceptions\NullValueException;
|
|||
class NullValue extends AbstractRule
|
||||
{
|
||||
|
||||
public function createException()
|
||||
{
|
||||
return new NullValueException;
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
return is_null($input);
|
||||
|
@ -21,14 +16,9 @@ class NullValue extends AbstractRule
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw $this->getException() ? : NullValueException::create()
|
||||
->configure($input);
|
||||
throw $this->getException() ? : NullValueException::create()
|
||||
->configure($input);
|
||||
return true;
|
||||
}
|
||||
|
||||
public function check($input)
|
||||
{
|
||||
return $this->assert($input);
|
||||
}
|
||||
|
||||
}
|
|
@ -8,11 +8,6 @@ use Respect\Validation\Exceptions\NumericException;
|
|||
class Numeric extends AbstractRule
|
||||
{
|
||||
|
||||
public function createException()
|
||||
{
|
||||
return new NumericException;
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
return is_numeric($input);
|
||||
|
@ -21,14 +16,9 @@ class Numeric extends AbstractRule
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw $this->getException() ? : NumericException::create()
|
||||
->configure($input);
|
||||
throw $this->getException() ? : NumericException::create()
|
||||
->configure($input);
|
||||
return true;
|
||||
}
|
||||
|
||||
public function check($input)
|
||||
{
|
||||
return $this->assert($input);
|
||||
}
|
||||
|
||||
}
|
|
@ -8,11 +8,6 @@ use Respect\Validation\Exceptions\ObjectException;
|
|||
class Object extends AbstractRule
|
||||
{
|
||||
|
||||
public function createException()
|
||||
{
|
||||
return new ObjectException;
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
return is_object($input);
|
||||
|
@ -21,14 +16,9 @@ class Object extends AbstractRule
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw $this->getException() ? : ObjectException::create()
|
||||
->configure($input);
|
||||
throw $this->getException() ? : ObjectException::create()
|
||||
->configure($input);
|
||||
return true;
|
||||
}
|
||||
|
||||
public function check($input)
|
||||
{
|
||||
return $this->assert($input);
|
||||
}
|
||||
|
||||
}
|
|
@ -7,11 +7,6 @@ use Respect\Validation\Exceptions\OneOfException;
|
|||
class OneOf extends AbstractComposite
|
||||
{
|
||||
|
||||
public function createException()
|
||||
{
|
||||
return new OneOfException(OneOfException::INVALID_ONE_OF);
|
||||
}
|
||||
|
||||
public function assert($input)
|
||||
{
|
||||
$validators = $this->getRules();
|
||||
|
@ -33,9 +28,4 @@ class OneOf extends AbstractComposite
|
|||
return false;
|
||||
}
|
||||
|
||||
public function check($input)
|
||||
{
|
||||
return $this->assert($input);
|
||||
}
|
||||
|
||||
}
|
|
@ -15,11 +15,6 @@ class Regex extends AbstractRule
|
|||
$this->regex = $regex;
|
||||
}
|
||||
|
||||
public function createException()
|
||||
{
|
||||
return new RegexException;
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
return preg_match("/{$this->regex}/", $input);
|
||||
|
@ -28,14 +23,10 @@ class Regex extends AbstractRule
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw $this->getException() ? : RegexException::create()
|
||||
->configure($input, $this->regex);
|
||||
throw $this->getException() ? : RegexException::create()
|
||||
->configure($input, $this->regex);
|
||||
return true;
|
||||
}
|
||||
|
||||
public function check($input)
|
||||
{
|
||||
return $this->assert($input);
|
||||
}
|
||||
|
||||
}
|
|
@ -46,15 +46,10 @@ class Sf extends AbstractRule
|
|||
'',
|
||||
$input
|
||||
);
|
||||
throw $this->getException() ? : SfException::create()
|
||||
->configure($violation->getMessage());
|
||||
throw $this->getException() ? : SfException::create()
|
||||
->configure($violation->getMessage());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public function check($input)
|
||||
{
|
||||
return $this->assert($input);
|
||||
}
|
||||
|
||||
}
|
|
@ -69,9 +69,4 @@ class StringLength extends AbstractRule
|
|||
return true;
|
||||
}
|
||||
|
||||
public function check($input)
|
||||
{
|
||||
return $this->assert($input);
|
||||
}
|
||||
|
||||
}
|
|
@ -25,11 +25,6 @@ class Zend extends AbstractRule
|
|||
$this->zendValidator = $zendMirror->newInstance();
|
||||
}
|
||||
|
||||
public function createException()
|
||||
{
|
||||
return new ZendException;
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
return $this->zendValidator->isValid($input);
|
||||
|
@ -48,9 +43,4 @@ class Zend extends AbstractRule
|
|||
return true;
|
||||
}
|
||||
|
||||
public function check($input)
|
||||
{
|
||||
return $this->assert($input);
|
||||
}
|
||||
|
||||
}
|
52
tests/library/Respect/Validation/Rules/ArrTest.php
Normal file
52
tests/library/Respect/Validation/Rules/ArrTest.php
Normal file
|
@ -0,0 +1,52 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class ArrTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
protected $object;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->object = new Arr;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForArray
|
||||
*
|
||||
*/
|
||||
public function testArray($input)
|
||||
{
|
||||
$this->assertTrue($this->object->assert($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForNotArray
|
||||
* @expectedException Respect\Validation\Exceptions\ValidationException
|
||||
*/
|
||||
public function testNotArray($input)
|
||||
{
|
||||
$this->assertTrue($this->object->assert($input));
|
||||
}
|
||||
|
||||
public function providerForArray()
|
||||
{
|
||||
return array(
|
||||
array(array()),
|
||||
array(array(1, 2, 3)),
|
||||
array(new \ArrayObject),
|
||||
);
|
||||
}
|
||||
|
||||
public function providerForNotArray()
|
||||
{
|
||||
return array(
|
||||
array(null),
|
||||
array(121),
|
||||
array(new \stdClass),
|
||||
array(false),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
41
tests/library/Respect/Validation/Rules/EachTest.php
Normal file
41
tests/library/Respect/Validation/Rules/EachTest.php
Normal file
|
@ -0,0 +1,41 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class EachTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
public function testEach()
|
||||
{
|
||||
$v = new Each(new NotEmpty());
|
||||
$result = $v->validate(array(1, 2, 3, 4, 5));
|
||||
$this->assertTrue($result);
|
||||
}
|
||||
|
||||
public function testEachOneInvalid()
|
||||
{
|
||||
$v = new Each(new NotEmpty());
|
||||
$result = $v->validate(array('', 2, 3, 4, 5));
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\EachException
|
||||
*/
|
||||
public function testEachOneInvalidAssertion()
|
||||
{
|
||||
$v = new Each(new NotEmpty());
|
||||
$result = $v->assert(array('', 2, 3, 4, 5));
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\EachException
|
||||
*/
|
||||
public function testEachOneInvalidInput()
|
||||
{
|
||||
$v = new Each(new NotEmpty());
|
||||
$result = $v->assert(123);
|
||||
$this->assertFalse($result);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue