mirror of
https://github.com/Respect/Validation.git
synced 2024-06-04 14:52:17 +02:00
Refactoring on AbstractRelated, Call validator for validating inputs after passing them for a callback
This commit is contained in:
parent
e38135ba78
commit
32c5fe4ce0
12
library/Respect/Validation/Exceptions/CallException.php
Normal file
12
library/Respect/Validation/Exceptions/CallException.php
Normal 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',
|
||||
);
|
||||
|
||||
}
|
|
@ -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;
|
||||
|
|
|
@ -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);
|
||||
|
|
18
library/Respect/Validation/Rules/Call.php
Normal file
18
library/Respect/Validation/Rules/Call.php
Normal 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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
|
|
23
tests/library/Respect/Validation/Rules/CallTest.php
Normal file
23
tests/library/Respect/Validation/Rules/CallTest.php
Normal 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'));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue