mirror of
https://github.com/Respect/Validation.git
synced 2024-06-22 07:16:32 +02:00
Turned Fluent Interface into a composite validator by nature, added support for chained composite validations, small fix on String\NotEmpty validator
This commit is contained in:
parent
d2e78a25e8
commit
0491cd885f
|
@ -7,6 +7,11 @@ abstract class AbstractCompositeRule extends AbstractRule implements Validatable
|
|||
|
||||
protected $rules = array();
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->addRules(func_get_args());
|
||||
}
|
||||
|
||||
protected function appendRule(Validatable $validator)
|
||||
{
|
||||
$this->rules[spl_object_hash($validator)] = $validator;
|
||||
|
|
|
@ -4,8 +4,9 @@ namespace Respect\Validation\Composite;
|
|||
|
||||
use Respect\Validation\AbstractCompositeRule;
|
||||
use Respect\Validation\InvalidException;
|
||||
use Respect\Validation\Validatable;
|
||||
|
||||
class All extends AbstractCompositeRule
|
||||
class All extends AbstractCompositeRule implements Validatable
|
||||
{
|
||||
|
||||
public function validate($input)
|
||||
|
|
|
@ -4,8 +4,9 @@ namespace Respect\Validation\Composite;
|
|||
|
||||
use Respect\Validation\AbstractCompositeRule;
|
||||
use Respect\Validation\InvalidException;
|
||||
use Respect\Validation\Validatable;
|
||||
|
||||
class One extends AbstractCompositeRule
|
||||
class One extends AbstractCompositeRule implements Validatable
|
||||
{
|
||||
|
||||
public function assert($input)
|
||||
|
|
|
@ -16,7 +16,7 @@ class NotEmpty extends AbstractRule implements Validatable
|
|||
|
||||
public function assert($input)
|
||||
{
|
||||
if (!$this->is($input))
|
||||
if (!$this->validate($input))
|
||||
throw new Exception();
|
||||
}
|
||||
|
||||
|
|
|
@ -2,24 +2,24 @@
|
|||
|
||||
namespace Respect\Validation;
|
||||
|
||||
use Respect\Validation\Composite\All;
|
||||
use ReflectionClass;
|
||||
|
||||
class Validator
|
||||
class Validator extends All
|
||||
{
|
||||
|
||||
protected $subject;
|
||||
protected $rule;
|
||||
protected $ruleName;
|
||||
protected $arguments = array();
|
||||
protected $validators = array();
|
||||
|
||||
public function getSubject()
|
||||
{
|
||||
return $this->subject;
|
||||
}
|
||||
|
||||
public function getRule()
|
||||
public function getRuleName()
|
||||
{
|
||||
return $this->rule;
|
||||
return $this->ruleName;
|
||||
}
|
||||
|
||||
public function getArguments()
|
||||
|
@ -32,9 +32,9 @@ class Validator
|
|||
$this->subject = $subject;
|
||||
}
|
||||
|
||||
public function setRule($rule)
|
||||
public function setRuleName($ruleName)
|
||||
{
|
||||
$this->rule = $rule;
|
||||
$this->ruleName = $ruleName;
|
||||
}
|
||||
|
||||
public function setArguments(array $arguments)
|
||||
|
@ -47,61 +47,55 @@ class Validator
|
|||
$this->arguments[] = $argument;
|
||||
}
|
||||
|
||||
public function addValidator(Validatable $validator)
|
||||
public function __get($property)
|
||||
{
|
||||
$this->validators[spl_object_hash($validator)] = $validator;
|
||||
$this->applyParts(func_get_args());
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getValidators()
|
||||
protected function applyParts($parts)
|
||||
{
|
||||
return $this->validators;
|
||||
}
|
||||
|
||||
public function __call($method, $arguments)
|
||||
{
|
||||
array_unshift($arguments, $method);
|
||||
foreach ($arguments as $a) {
|
||||
foreach ($parts as $a) {
|
||||
if (!isset($this->subject)) {
|
||||
$this->setSubject($a);
|
||||
continue;
|
||||
}
|
||||
if (!isset($this->rule)) {
|
||||
$this->setRule($a);
|
||||
if (!isset($this->ruleName)) {
|
||||
$this->setRuleName($a);
|
||||
continue;
|
||||
}
|
||||
$this->addArgument($a);
|
||||
}
|
||||
$this->checkForCompleteRule();
|
||||
}
|
||||
|
||||
public function __call($method, $arguments)
|
||||
{
|
||||
array_unshift($arguments, $method);
|
||||
$this->applyParts($arguments);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function validates($input)
|
||||
protected function checkForCompleteRule()
|
||||
{
|
||||
$v = new Composite\All();
|
||||
$v->addRules($this->validators);
|
||||
return $v->validate($input);
|
||||
}
|
||||
|
||||
public function checkForCompleteRule()
|
||||
{
|
||||
if (!isset($this->subject, $this->rule))
|
||||
if (!isset($this->subject, $this->ruleName))
|
||||
return;
|
||||
$this->addValidator(
|
||||
$this->addRule(
|
||||
static::buildRule(
|
||||
array($this->subject, $this->rule), $this->arguments
|
||||
array($this->subject, $this->ruleName), $this->arguments
|
||||
)
|
||||
);
|
||||
$this->subject = null;
|
||||
$this->rule = null;
|
||||
$this->ruleName = null;
|
||||
$this->arguments = array();
|
||||
}
|
||||
|
||||
public static function __callStatic($subject, $arguments)
|
||||
{
|
||||
$rule = array_shift($arguments);
|
||||
$ruleName = array_shift($arguments);
|
||||
$validator = new static;
|
||||
$validator->setSubject($subject);
|
||||
$validator->setRule($rule);
|
||||
$validator->setRuleName($ruleName);
|
||||
$validator->setArguments($arguments);
|
||||
$validator->checkForCompleteRule();
|
||||
return $validator;
|
||||
|
|
|
@ -16,46 +16,57 @@ class ValidatorTest extends ValidatorTestCase
|
|||
{
|
||||
$v = Validator::date('between', 'yesterday', 'tomorrow');
|
||||
$this->assertType('Respect\Validation\Validator', $v);
|
||||
$this->assertEquals(1, count($v->getValidators()));
|
||||
$this->assertEquals(1, count($v->getRules()));
|
||||
$this->assertNull($v->getSubject());
|
||||
$this->assertNull($v->getRule());
|
||||
$this->assertNull($v->getRuleName());
|
||||
$this->assertEquals(array(), $v->getArguments());
|
||||
}
|
||||
|
||||
public function testPartialInputChain()
|
||||
{
|
||||
$v = Validator::date()->between('yesterday', 'tomorrow')->string('notEmpty');
|
||||
$v = Validator::date()->between('yesterday', 'tomorrow')
|
||||
->string('notEmpty');
|
||||
$this->assertType('Respect\Validation\Validator', $v);
|
||||
$this->assertEquals(2, count($v->getValidators()));
|
||||
$this->assertEquals(2, count($v->getRules()));
|
||||
$this->assertNull($v->getSubject());
|
||||
$this->assertNull($v->getRule());
|
||||
$this->assertNull($v->getRuleName());
|
||||
$this->assertEquals(array(), $v->getArguments());
|
||||
}
|
||||
|
||||
public function testValidateSimple()
|
||||
{
|
||||
$v = Validator::string('notEmpty')->validates('foo');
|
||||
$v = Validator::string('notEmpty')->validate('foo');
|
||||
$this->assertTrue($v);
|
||||
}
|
||||
|
||||
public function testValidateArguments()
|
||||
{
|
||||
$v = Validator::date('between', 'yesterday', 'tomorrow')->validates('now');
|
||||
$v = Validator::date('between', 'yesterday', 'tomorrow')
|
||||
->validate('now');
|
||||
$this->assertTrue($v);
|
||||
}
|
||||
|
||||
public function testValidateFluent()
|
||||
{
|
||||
$v = Validator::date()->between('yesterday', 'tomorrow')->validates('now');
|
||||
$v = Validator::date()->between('yesterday', 'tomorrow')
|
||||
->validate('now');
|
||||
$this->assertTrue($v);
|
||||
}
|
||||
|
||||
public function testValidateFluentChain()
|
||||
{
|
||||
$v = Validator::date()
|
||||
->between('yesterday', 'tomorrow')
|
||||
$v = Validator::date()->between('yesterday', 'tomorrow')
|
||||
->string('notEmpty')
|
||||
->validates('now');
|
||||
->assert('now');
|
||||
$this->assertTrue($v);
|
||||
}
|
||||
|
||||
public function testValidatorComposite()
|
||||
{
|
||||
$v = Validator::composite()->one(
|
||||
Validator::string()->notEmpty,
|
||||
Validator::date()->between('+2 years', '+3 years')
|
||||
)->validate('now');
|
||||
$this->assertTrue($v);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue