Permalink
Browse files

Fixed Rules\Attribute and Rules\Key on non-mandatory rule checking.

  • Loading branch information...
alganet committed Apr 27, 2011
1 parent 892d39a commit d254d466cc02be04a7a3387133d6008a4d965aff
@@ -16,7 +16,8 @@
abstract public function getReferenceValue($input);
public function __construct($reference, Validatable $validator=null, $mandatory=true)
public function __construct($reference, Validatable $validator=null,
$mandatory=true)
{
$this->setName($reference);
$this->reference = $reference;
@@ -26,39 +27,44 @@ public function __construct($reference, Validatable $validator=null, $mandatory=
public function assert($input)
{
if (!$this->verifyMandatory($input))
$hasReference = $this->hasReference($input);
if ($this->mandatory && !$hasReference)
throw $this->reportError($input, array('hasReference' => false));
elseif (!$this->validator)
elseif ((!$this->mandatory && !$hasReference) || !$this->validator)
return true;
else
try {
return $this->validator->assert($this->getReferenceValue($input));
} catch (ValidationException $e) {
throw $this
->reportError($this->reference, array('hasReference' => true))
->addRelated($e);
}
try {
return $this->validator->assert($this->getReferenceValue($input));
} catch (ValidationException $e) {
throw $this
->reportError($this->reference, array('hasReference' => true))
->addRelated($e);
}
}
public function check($input)
{
if (!$this->verifyMandatory($input))
$hasReference = $this->hasReference($input);
if ($this->mandatory && !$hasReference)
throw $this->reportError($input, array('hasReference' => false));
else
return $this->validator->check($this->getReferenceValue($input));
elseif (!$this->mandatory && !$hasReference)
return true;
return $this->validator->check($this->getReferenceValue($input));
}
public function validate($input)
{
if (!$this->verifyMandatory($input))
$hasReference = $this->hasReference($input);
if ($this->mandatory && !$hasReference)
return false;
else
return $this->validator->validate($this->getReferenceValue($input));
}
elseif (!$this->mandatory && !$hasReference)
return true;
protected function verifyMandatory($input)
{
return!$this->mandatory || $this->hasReference($input);
return $this->validator->validate($this->getReferenceValue($input));
}
}
@@ -58,6 +58,14 @@ public function testValidatorAttribute()
$this->assertTrue($validator->assert($obj));
}
public function testNotMandatory()
{
$subValidator = new Length(1, 3);
$validator = new Attribute('bar', $subValidator, false);
$obj = new \stdClass;
$this->assertTrue($validator->validate($obj));
}
public function testValidatorPrivateAttribute()
{
$subValidator = new Length(1, 3);
@@ -40,4 +40,13 @@ public function testValidatorAttribute()
$obj['bar'] = 'foo';
$this->assertTrue($validator->assert($obj));
}
public function testNotMandatory()
{
$subValidator = new Length(1, 3);
$validator = new Key('bar', $subValidator, false);
$obj = array();
$this->assertTrue($validator->validate($obj));
}
}

0 comments on commit d254d46

Please sign in to comment.