Refactoring on AbstractRelated, Call validator for validating inputs after passing them for a callback

This commit is contained in:
Alexandre Gomes Gaigalas 2010-12-06 19:00:28 -02:00
parent e38135ba78
commit 32c5fe4ce0
9 changed files with 78 additions and 13 deletions

View file

@ -0,0 +1,12 @@
<?php
namespace Respect\Validation\Exceptions;
class CallException extends ValidationException
{
const INVALID_CALL= 'Call_1';
public static $defaultTemplates = array(
self::INVALID_CALL => '"%s" is invalid',
);
}

View file

@ -5,7 +5,6 @@ namespace Respect\Validation\Rules;
use ReflectionProperty;
use ReflectionException;
use Respect\Validation\Validatable;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Exceptions\ValidationException;
abstract class AbstractRelated extends AbstractRule implements Validatable
@ -18,10 +17,6 @@ abstract class AbstractRelated extends AbstractRule implements Validatable
public function __construct($reference,
Validatable $referenceValidator=null, $mandatory=true)
{
if (!is_string($reference) || empty($reference))
throw new ComponentException(
'Invalid reference name'
);
$this->reference = $reference;
$this->referenceValidator = $referenceValidator;
$this->mandatory = $mandatory;

View file

@ -3,10 +3,22 @@
namespace Respect\Validation\Rules;
use ReflectionProperty;
use Respect\Validation\Validatable;
use Respect\Validation\Exceptions\ComponentException;
class Attribute extends AbstractRelated
{
public function __construct($reference,
Validatable $referenceValidator=null, $mandatory=true)
{
if (!is_string($reference) || empty($reference))
throw new ComponentException(
'Invalid attribute/property name'
);
parent::__construct($reference, $referenceValidator, $mandatory);
}
protected function hasReference($input)
{
return property_exists($input, $this->reference);

View file

@ -0,0 +1,18 @@
<?php
namespace Respect\Validation\Rules;
class Call extends AbstractRelated
{
protected function hasReference($input)
{
return is_callable($this->reference);
}
protected function getReferenceValue($input)
{
return call_user_func($this->reference, $input);
}
}

View file

@ -23,12 +23,4 @@ class Callback extends AbstractRule
return call_user_func($this->callback, $input);
}
public function assert($input)
{
if (!$this->validate($input))
throw $this->getException() ? : $this->createException()
->configure($input, $this->callback);
return true;
}
}

View file

@ -2,9 +2,22 @@
namespace Respect\Validation\Rules;
use Respect\Validation\Validatable;
use Respect\Validation\Exceptions\ComponentException;
class Key extends AbstractRelated
{
public function __construct($reference,
Validatable $referenceValidator=null, $mandatory=true)
{
if (!is_string($reference) || empty($reference))
throw new ComponentException(
'Invalid array key name'
);
parent::__construct($reference, $referenceValidator, $mandatory);
}
protected function hasReference($input)
{
return array_key_exists($this->reference, $input);

View file

@ -0,0 +1,23 @@
<?php
namespace Respect\Validation\Rules;
class CallTest extends \PHPUnit_Framework_TestCase
{
public function testCallbackOk()
{
$v = new Call('str_split', new Arr);
$this->assertTrue($v->assert('test'));
}
/**
* @expectedException Respect\Validation\Exceptions\ValidationException
*/
public function testCallbackNot()
{
$v = new Call('strrev', new Arr);
$this->assertTrue($v->assert('test'));
}
}