mirror of
https://github.com/Respect/Validation.git
synced 2024-06-08 16:52:16 +02:00
Optional Abstract Validator which implements messages methods, better message handling
This commit is contained in:
parent
666c0d5330
commit
348cb019fe
31
library/Respect/Validation/AbstractValidator.php
Normal file
31
library/Respect/Validation/AbstractValidator.php
Normal file
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation;
|
||||
|
||||
abstract class AbstractValidator
|
||||
{
|
||||
|
||||
protected $messages = array();
|
||||
|
||||
public function setMessage($code, $message)
|
||||
{
|
||||
$this->messages[$code] = $message;
|
||||
}
|
||||
|
||||
public function getMessage($code)
|
||||
{
|
||||
return $this->messages[$code];
|
||||
}
|
||||
|
||||
public function setMessages(array $messages)
|
||||
{
|
||||
foreach ($messages as $code => $message)
|
||||
$this->setMessage($code, $message);
|
||||
}
|
||||
|
||||
public function getMessages()
|
||||
{
|
||||
return $this->messages;
|
||||
}
|
||||
|
||||
}
|
|
@ -2,12 +2,13 @@
|
|||
|
||||
namespace Respect\Validation\Date;
|
||||
|
||||
use Respect\Validation\AbstractValidator;
|
||||
use DateTime;
|
||||
|
||||
abstract class AbstractDateValidator
|
||||
abstract class AbstractDateValidator extends AbstractValidator
|
||||
{
|
||||
|
||||
protected $format=DateTime::RFC1036;
|
||||
protected $format = DateTime::RFC1036;
|
||||
|
||||
protected function setFormat($format=DateTime::RFC1036)
|
||||
{
|
||||
|
|
|
@ -7,9 +7,13 @@ use Respect\Validation\ComponentException;
|
|||
|
||||
class Between extends AbstractDateValidator implements Validatable
|
||||
{
|
||||
const MSG_OUT_OF_BOUNDS = 'Date_Between_1';
|
||||
|
||||
protected $min;
|
||||
protected $max;
|
||||
protected $messages = array(
|
||||
self::MSG_OUT_OF_BOUNDS => '%s is not between %s and %s.'
|
||||
);
|
||||
|
||||
public function __construct($min, $max, $format=null)
|
||||
{
|
||||
|
@ -18,7 +22,8 @@ class Between extends AbstractDateValidator implements Validatable
|
|||
$this->max = $this->getDateObject($max);
|
||||
if ($this->min > $this->max)
|
||||
throw new ComponentException(
|
||||
sprintf('%s cannot be less than %s for validation', $this->formatDate($this->min), $this->formatDate($this->max))
|
||||
sprintf('%s cannot be less than %s for validation',
|
||||
$this->formatDate($this->min), $this->formatDate($this->max))
|
||||
);
|
||||
if (!is_null($format))
|
||||
$this->setFormat($format);
|
||||
|
@ -30,7 +35,9 @@ class Between extends AbstractDateValidator implements Validatable
|
|||
if (!$this->isValid($target))
|
||||
throw new OutOfBoundsException(
|
||||
sprintf(
|
||||
'%s is not between %s and %s.', $this->formatDate($target), $this->formatDate($this->min), $this->formatDate($this->max)
|
||||
$this->getMessage(self::MSG_OUT_OF_BOUNDS),
|
||||
$this->formatDate($target), $this->formatDate($this->min),
|
||||
$this->formatDate($this->max)
|
||||
)
|
||||
);
|
||||
return true;
|
||||
|
@ -42,14 +49,4 @@ class Between extends AbstractDateValidator implements Validatable
|
|||
return $target >= $this->min and $target <= $this->max;
|
||||
}
|
||||
|
||||
public function getMessages()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function setMessages(array $messages)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -4,7 +4,7 @@ namespace Respect\Validation;
|
|||
|
||||
use ReflectionClass;
|
||||
|
||||
class Validator implements Validatable
|
||||
class Validator extends AbstractValidator implements Validatable
|
||||
{
|
||||
|
||||
protected $validators = array();
|
||||
|
@ -26,7 +26,8 @@ class Validator implements Validatable
|
|||
}
|
||||
if (is_object($validator))
|
||||
throw new ComponentException(
|
||||
sprintf('%s does not implement the Respect\Validator\Validatable interface required for validators', get_class($validator))
|
||||
sprintf('%s does not implement the Respect\Validator\Validatable interface required for validators',
|
||||
get_class($validator))
|
||||
);
|
||||
$validatorFqn = explode('\\', get_called_class());
|
||||
array_pop($validatorFqn);
|
||||
|
@ -38,7 +39,8 @@ class Validator implements Validatable
|
|||
);
|
||||
if (!$implementedInterface)
|
||||
throw new ComponentException(
|
||||
sprintf('%s does not implement the Respect\Validator\Validatable interface required for validators', $validatorFqn)
|
||||
sprintf('%s does not implement the Respect\Validator\Validatable interface required for validators',
|
||||
$validatorFqn)
|
||||
);
|
||||
if ($validatorClass->hasMethod('__construct')) {
|
||||
$validatorInstance = $validatorClass->newInstanceArgs(
|
||||
|
@ -58,7 +60,8 @@ class Validator implements Validatable
|
|||
return isset($this->validators[spl_object_hash($validator)]);
|
||||
else
|
||||
return (boolean) array_filter(
|
||||
$this->validators, function($v) use ($validator) {
|
||||
$this->validators,
|
||||
function($v) use ($validator) {
|
||||
return (integer) ($v instanceof $validator);
|
||||
});
|
||||
}
|
||||
|
@ -77,7 +80,8 @@ class Validator implements Validatable
|
|||
if (!empty($v) && !is_array($v))
|
||||
throw new ComponentException(
|
||||
sprintf(
|
||||
'Arguments for array-specified validators must be an array, you provided %s', $v
|
||||
'Arguments for array-specified validators must be an array, you provided %s',
|
||||
$v
|
||||
)
|
||||
);
|
||||
$validatorArgs = empty($v) ? array() : $v;
|
||||
|
@ -127,7 +131,8 @@ class Validator implements Validatable
|
|||
{
|
||||
$validators = $this->getValidators();
|
||||
return count($validators) === count(array_filter(
|
||||
$validators, function($v) use($input) {
|
||||
$validators,
|
||||
function($v) use($input) {
|
||||
return $v->isValid($input);
|
||||
}
|
||||
));
|
||||
|
@ -136,13 +141,14 @@ class Validator implements Validatable
|
|||
public function isOneValid($input)
|
||||
{
|
||||
return (boolean) array_filter(
|
||||
$this->getValidators(), function($v) use($input) {
|
||||
$this->getValidators(),
|
||||
function($v) use($input) {
|
||||
return $v->isValid($input);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public function getMessages()
|
||||
/*public function getMessages()
|
||||
{
|
||||
return $this->messages;
|
||||
}
|
||||
|
@ -154,6 +160,6 @@ class Validator implements Validatable
|
|||
'You must set exactly the same amount of messages currently present in the validator'
|
||||
);
|
||||
$this->messages = $messages;
|
||||
}
|
||||
}*/
|
||||
|
||||
}
|
|
@ -19,6 +19,7 @@
|
|||
*/
|
||||
class SplClassLoader
|
||||
{
|
||||
|
||||
private $_fileExtension = '.php';
|
||||
private $_namespace;
|
||||
private $_includePath;
|
||||
|
@ -120,17 +121,21 @@ class SplClassLoader
|
|||
*/
|
||||
public function loadClass($className)
|
||||
{
|
||||
if (null === $this->_namespace || $this->_namespace.$this->_namespaceSeparator === substr($className, 0, strlen($this->_namespace.$this->_namespaceSeparator))) {
|
||||
if (null === $this->_namespace || $this->_namespace . $this->_namespaceSeparator === substr($className,
|
||||
0, strlen($this->_namespace . $this->_namespaceSeparator))) {
|
||||
$fileName = '';
|
||||
$namespace = '';
|
||||
if (false !== ($lastNsPos = strripos($className, $this->_namespaceSeparator))) {
|
||||
if (false !== ($lastNsPos = strripos($className,
|
||||
$this->_namespaceSeparator))) {
|
||||
$namespace = substr($className, 0, $lastNsPos);
|
||||
$className = substr($className, $lastNsPos + 1);
|
||||
$fileName = str_replace($this->_namespaceSeparator, DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
|
||||
$fileName = str_replace($this->_namespaceSeparator,
|
||||
DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
|
||||
}
|
||||
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . $this->_fileExtension;
|
||||
|
||||
require ($this->_includePath !== null ? $this->_includePath . DIRECTORY_SEPARATOR : '') . $fileName;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -119,7 +119,7 @@ class ValidatorTest extends ValidatorTestCase
|
|||
{
|
||||
$this->object->addValidators(func_get_args());
|
||||
$this->object->addValidator(
|
||||
$this->buildMockValidator('Aids', array('ergera'))
|
||||
$this->buildMockValidator('Aids', array('Aids_1' => 'aesfg'))
|
||||
);
|
||||
$this->assertFalse($this->object->isValid('any'));
|
||||
}
|
||||
|
@ -142,7 +142,7 @@ class ValidatorTest extends ValidatorTestCase
|
|||
*/
|
||||
public function testValidateOneValid($invalidA, $invalidB, $invalidC)
|
||||
{
|
||||
$valid = $this->buildMockValidator('Darth', array('o54n'));
|
||||
$valid = $this->buildMockValidator('Darth', array('Darth_1' => 'o54n'));
|
||||
$this->object->addValidators(func_get_args());
|
||||
$this->object->addValidator($valid);
|
||||
$this->assertTrue($this->object->validateOne('any'));
|
||||
|
@ -166,7 +166,7 @@ class ValidatorTest extends ValidatorTestCase
|
|||
*/
|
||||
public function testIsValidOneValid($invalidA, $invalidB, $invalidC)
|
||||
{
|
||||
$valid = $this->buildMockValidator('Darth', array('o54n'));
|
||||
$valid = $this->buildMockValidator('Darth', array('Darth_1' => 'o54n'));
|
||||
$this->object->addValidators(func_get_args());
|
||||
$this->object->addValidator($valid);
|
||||
$this->assertTrue($this->object->isOneValid('any'));
|
||||
|
@ -201,18 +201,4 @@ class ValidatorTest extends ValidatorTestCase
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForMockImpossibleValidators
|
||||
* @expectedException Respect\Validation\ComponentException
|
||||
*/
|
||||
public function testSetInvalidMessages($invalidA, $invalidB, $invalidC)
|
||||
{
|
||||
$this->object->addValidators(func_get_args());
|
||||
$messages = $this->object->getMessages();
|
||||
$messages = array_map('strrev', $messages);
|
||||
array_pop($messages);
|
||||
$this->object->setMessages($messages);
|
||||
$this->assertEquals($messages, $this->object->getMessages());
|
||||
}
|
||||
|
||||
}
|
|
@ -42,7 +42,8 @@ abstract class ValidatorTestCase extends \PHPUnit_Framework_TestCase
|
|||
public function isValid(\$input) {}
|
||||
public function setMessages(array \$messages) {}
|
||||
public function getMessages() {
|
||||
return " . var_export($messages, true) . ";
|
||||
return " . var_export($messages,
|
||||
true) . ";
|
||||
}
|
||||
}
|
||||
");
|
||||
|
@ -54,13 +55,13 @@ abstract class ValidatorTestCase extends \PHPUnit_Framework_TestCase
|
|||
public function providerForMockImpossibleValidators()
|
||||
{
|
||||
$firstValidator = $this->buildMockValidator(
|
||||
'Bar', array('sfga', 'dfgb'), true
|
||||
'Bar', array('Bar_1' => 'fga', 'Bar_2' => 'dfgb'), true
|
||||
);
|
||||
$secondValidator = $this->buildMockValidator(
|
||||
'Baz', array('dgd', 'dfgE', 'dfgf'), true
|
||||
'Baz', array('Baz_1' => 'gedg', 'Baz_2' => 'rihg49'), true
|
||||
);
|
||||
$thirdValidator = $this->buildMockValidator(
|
||||
'Bat', array('a34t'), true
|
||||
'Bat', array('Bat_1' => 'dfdsgdgfgb'), true
|
||||
);
|
||||
return array(
|
||||
array($firstValidator, $secondValidator, $thirdValidator),
|
||||
|
@ -72,13 +73,13 @@ abstract class ValidatorTestCase extends \PHPUnit_Framework_TestCase
|
|||
public function providerForMockValidators()
|
||||
{
|
||||
$firstValidator = $this->buildMockValidator(
|
||||
'Bar', array('a435', 'b345'), false
|
||||
'Bar', array('Bar_1' => 'fga', 'Bar_2' => 'dfgb'), false
|
||||
);
|
||||
$secondValidator = $this->buildMockValidator(
|
||||
'Baz', array('345d', '435E', 'f345'), false
|
||||
'Baz', array('Baz_1' => 'gedg', 'Baz_2' => 'rihg49'), false
|
||||
);
|
||||
$thirdValidator = $this->buildMockValidator(
|
||||
'Bat', array('345324a'), false
|
||||
'Bat', array('Bat_1' => 'dfdsgdgfgb'), false
|
||||
);
|
||||
return array(
|
||||
array($firstValidator, $secondValidator, $thirdValidator),
|
||||
|
|
Loading…
Reference in a new issue