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:
Alexandre Gomes Gaigalas 2010-09-25 01:48:49 -03:00
parent d2e78a25e8
commit 0491cd885f
6 changed files with 59 additions and 47 deletions

View file

@ -7,6 +7,11 @@ abstract class AbstractCompositeRule extends AbstractRule implements Validatable
protected $rules = array(); protected $rules = array();
public function __construct()
{
$this->addRules(func_get_args());
}
protected function appendRule(Validatable $validator) protected function appendRule(Validatable $validator)
{ {
$this->rules[spl_object_hash($validator)] = $validator; $this->rules[spl_object_hash($validator)] = $validator;

View file

@ -4,8 +4,9 @@ namespace Respect\Validation\Composite;
use Respect\Validation\AbstractCompositeRule; use Respect\Validation\AbstractCompositeRule;
use Respect\Validation\InvalidException; use Respect\Validation\InvalidException;
use Respect\Validation\Validatable;
class All extends AbstractCompositeRule class All extends AbstractCompositeRule implements Validatable
{ {
public function validate($input) public function validate($input)

View file

@ -4,8 +4,9 @@ namespace Respect\Validation\Composite;
use Respect\Validation\AbstractCompositeRule; use Respect\Validation\AbstractCompositeRule;
use Respect\Validation\InvalidException; use Respect\Validation\InvalidException;
use Respect\Validation\Validatable;
class One extends AbstractCompositeRule class One extends AbstractCompositeRule implements Validatable
{ {
public function assert($input) public function assert($input)

View file

@ -16,7 +16,7 @@ class NotEmpty extends AbstractRule implements Validatable
public function assert($input) public function assert($input)
{ {
if (!$this->is($input)) if (!$this->validate($input))
throw new Exception(); throw new Exception();
} }

View file

@ -2,24 +2,24 @@
namespace Respect\Validation; namespace Respect\Validation;
use Respect\Validation\Composite\All;
use ReflectionClass; use ReflectionClass;
class Validator class Validator extends All
{ {
protected $subject; protected $subject;
protected $rule; protected $ruleName;
protected $arguments = array(); protected $arguments = array();
protected $validators = array();
public function getSubject() public function getSubject()
{ {
return $this->subject; return $this->subject;
} }
public function getRule() public function getRuleName()
{ {
return $this->rule; return $this->ruleName;
} }
public function getArguments() public function getArguments()
@ -32,9 +32,9 @@ class Validator
$this->subject = $subject; $this->subject = $subject;
} }
public function setRule($rule) public function setRuleName($ruleName)
{ {
$this->rule = $rule; $this->ruleName = $ruleName;
} }
public function setArguments(array $arguments) public function setArguments(array $arguments)
@ -47,61 +47,55 @@ class Validator
$this->arguments[] = $argument; $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; foreach ($parts as $a) {
}
public function __call($method, $arguments)
{
array_unshift($arguments, $method);
foreach ($arguments as $a) {
if (!isset($this->subject)) { if (!isset($this->subject)) {
$this->setSubject($a); $this->setSubject($a);
continue; continue;
} }
if (!isset($this->rule)) { if (!isset($this->ruleName)) {
$this->setRule($a); $this->setRuleName($a);
continue; continue;
} }
$this->addArgument($a); $this->addArgument($a);
} }
$this->checkForCompleteRule(); $this->checkForCompleteRule();
}
public function __call($method, $arguments)
{
array_unshift($arguments, $method);
$this->applyParts($arguments);
return $this; return $this;
} }
public function validates($input) protected function checkForCompleteRule()
{ {
$v = new Composite\All(); if (!isset($this->subject, $this->ruleName))
$v->addRules($this->validators);
return $v->validate($input);
}
public function checkForCompleteRule()
{
if (!isset($this->subject, $this->rule))
return; return;
$this->addValidator( $this->addRule(
static::buildRule( static::buildRule(
array($this->subject, $this->rule), $this->arguments array($this->subject, $this->ruleName), $this->arguments
) )
); );
$this->subject = null; $this->subject = null;
$this->rule = null; $this->ruleName = null;
$this->arguments = array(); $this->arguments = array();
} }
public static function __callStatic($subject, $arguments) public static function __callStatic($subject, $arguments)
{ {
$rule = array_shift($arguments); $ruleName = array_shift($arguments);
$validator = new static; $validator = new static;
$validator->setSubject($subject); $validator->setSubject($subject);
$validator->setRule($rule); $validator->setRuleName($ruleName);
$validator->setArguments($arguments); $validator->setArguments($arguments);
$validator->checkForCompleteRule(); $validator->checkForCompleteRule();
return $validator; return $validator;

View file

@ -16,46 +16,57 @@ class ValidatorTest extends ValidatorTestCase
{ {
$v = Validator::date('between', 'yesterday', 'tomorrow'); $v = Validator::date('between', 'yesterday', 'tomorrow');
$this->assertType('Respect\Validation\Validator', $v); $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->getSubject());
$this->assertNull($v->getRule()); $this->assertNull($v->getRuleName());
$this->assertEquals(array(), $v->getArguments()); $this->assertEquals(array(), $v->getArguments());
} }
public function testPartialInputChain() 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->assertType('Respect\Validation\Validator', $v);
$this->assertEquals(2, count($v->getValidators())); $this->assertEquals(2, count($v->getRules()));
$this->assertNull($v->getSubject()); $this->assertNull($v->getSubject());
$this->assertNull($v->getRule()); $this->assertNull($v->getRuleName());
$this->assertEquals(array(), $v->getArguments()); $this->assertEquals(array(), $v->getArguments());
} }
public function testValidateSimple() public function testValidateSimple()
{ {
$v = Validator::string('notEmpty')->validates('foo'); $v = Validator::string('notEmpty')->validate('foo');
$this->assertTrue($v); $this->assertTrue($v);
} }
public function testValidateArguments() public function testValidateArguments()
{ {
$v = Validator::date('between', 'yesterday', 'tomorrow')->validates('now'); $v = Validator::date('between', 'yesterday', 'tomorrow')
->validate('now');
$this->assertTrue($v); $this->assertTrue($v);
} }
public function testValidateFluent() public function testValidateFluent()
{ {
$v = Validator::date()->between('yesterday', 'tomorrow')->validates('now'); $v = Validator::date()->between('yesterday', 'tomorrow')
->validate('now');
$this->assertTrue($v); $this->assertTrue($v);
} }
public function testValidateFluentChain() public function testValidateFluentChain()
{ {
$v = Validator::date() $v = Validator::date()->between('yesterday', 'tomorrow')
->between('yesterday', 'tomorrow')
->string('notEmpty') ->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); $this->assertTrue($v);
} }