mirror of
https://github.com/Respect/Validation.git
synced 2024-06-04 23:02:16 +02:00
Major refactoring on the validation exceptions and messages.
This commit is contained in:
parent
87f88ae641
commit
bc63da4e0e
|
@ -6,5 +6,19 @@ use InvalidArgumentException;
|
|||
|
||||
class AttributeNotPresentException extends InvalidException
|
||||
{
|
||||
|
||||
const MSG_ATTRIBUTE_NOT_PRESENT = 'HasAttribute_1';
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_ATTRIBUTE_NOT_PRESENT => 'Object %s does not have the attribute %s'
|
||||
);
|
||||
|
||||
public function __construct($input, $attributeName)
|
||||
{
|
||||
parent::__construct(
|
||||
sprintf(
|
||||
$this->getMessageTemplate(self::MSG_ATTRIBUTE_NOT_PRESENT),
|
||||
$this->getStringRepresentation($input), $attributeName
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -6,5 +6,22 @@ use Exception;
|
|||
|
||||
class CallbackException extends InvalidException
|
||||
{
|
||||
|
||||
const MSG_CALLBACK = 'Callback_1';
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_CALLBACK => '%s does not validate against the provided callback %s.'
|
||||
);
|
||||
|
||||
public function __construct($input, $callback)
|
||||
{
|
||||
parent::__construct(
|
||||
$this->getMessageTemplate(
|
||||
sprintf(
|
||||
self::MSG_CALLBACK,
|
||||
$this->getStringRepresentation($input),
|
||||
$this->getStringRepresentation($callback)
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -4,5 +4,20 @@ namespace Respect\Validation\Exceptions;
|
|||
|
||||
class DateOutOfBoundsException extends InvalidException
|
||||
{
|
||||
|
||||
const MSG_OUT_OF_BOUNDS = 'DateBetween_1';
|
||||
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_OUT_OF_BOUNDS => '%s is not between %s and %s.'
|
||||
);
|
||||
|
||||
public function __construct($input, $min, $max)
|
||||
{
|
||||
parent::__construct(
|
||||
sprintf(
|
||||
$this->getMessageTemplate(self::MSG_OUT_OF_BOUNDS), $input,
|
||||
$min, $max
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -4,5 +4,15 @@ namespace Respect\Validation\Exceptions;
|
|||
|
||||
class EmptyStringException extends InvalidException
|
||||
{
|
||||
|
||||
const MSG_EMPTY_STRING = 'StringNotEmpty_1';
|
||||
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_EMPTY_STRING => 'You provided an empty string'
|
||||
);
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct($this->getMessageTemplate(self::MSG_EMPTY_STRING));
|
||||
}
|
||||
|
||||
}
|
|
@ -4,5 +4,21 @@ namespace Respect\Validation\Exceptions;
|
|||
|
||||
class InvalidDate extends InvalidException
|
||||
{
|
||||
|
||||
const MSG_INVALID_DATE = 'Date_1';
|
||||
const MSG_INVALID_FORMAT = 'Date_2';
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_INVALID_DATE => '%s is not a valid date reference',
|
||||
self::MSG_INVALID_FORMAT => '%s is not a valid date in the %s format',
|
||||
);
|
||||
|
||||
public function __construct($input, $format)
|
||||
{
|
||||
$code = is_null($format) ? static::MSG_INVALID_DATE : static::MSG_INVALID_FORMAT;
|
||||
parent::__construct(
|
||||
sprintf(
|
||||
$this->getMessageTemplate($code), $input, $format
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -37,4 +37,35 @@ class InvalidException extends InvalidArgumentException
|
|||
return $this->exceptions;
|
||||
}
|
||||
|
||||
protected $messageTemplates = array();
|
||||
|
||||
public function setMessageTemplate($code, $template)
|
||||
{
|
||||
$this->messageTemplates[$code] = $template;
|
||||
}
|
||||
|
||||
public function getMessageTemplate($code)
|
||||
{
|
||||
return $this->messageTemplates[$code];
|
||||
}
|
||||
|
||||
public function setMessageTemplates(array $templates)
|
||||
{
|
||||
foreach ($templates as $code => $message)
|
||||
$this->setMessageTemplate($code, $message);
|
||||
}
|
||||
|
||||
public function getMessageTemplates()
|
||||
{
|
||||
return $this->messageTemplates;
|
||||
}
|
||||
|
||||
protected function getStringRepresentation($mixed)
|
||||
{
|
||||
if (is_object($mixed))
|
||||
return get_class($mixed);
|
||||
else
|
||||
return strval($mixed);
|
||||
}
|
||||
|
||||
}
|
|
@ -4,5 +4,19 @@ namespace Respect\Validation\Exceptions;
|
|||
|
||||
class InvalidIpException extends InvalidException
|
||||
{
|
||||
|
||||
const MSG_NOT_IP = 'Ip_1';
|
||||
public $options;
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_NOT_IP => '%s is not a valid IP address'
|
||||
);
|
||||
|
||||
public function __construct($input)
|
||||
{
|
||||
parent::__construct(
|
||||
sprintf(
|
||||
$this->getMessageTemplate(self::MSG_NOT_IP), $input
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -6,5 +6,19 @@ use InvalidArgumentException;
|
|||
|
||||
class KeyNotPresentException extends InvalidException
|
||||
{
|
||||
|
||||
const MSG_KEY_NOT_PRESENT = 'HasKey_1';
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_KEY_NOT_PRESENT => 'Array %s does not have the key %s'
|
||||
);
|
||||
|
||||
public function __construct($input, $keyName)
|
||||
{
|
||||
parent::__construct(
|
||||
sprintf(
|
||||
$this->getMessageTemplate(self::MSG_KEY_NOT_PRESENT),
|
||||
$this->getStringRepresentation($input), $keyName
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -4,5 +4,22 @@ namespace Respect\Validation\Exceptions;
|
|||
|
||||
class NotAlphaException extends InvalidException
|
||||
{
|
||||
|
||||
const MSG_NOT_ALPHA = 'Alpha_1';
|
||||
const MSG_NOT_ALPHA_ADDITIONAL = 'Alpha_2';
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_NOT_ALPHA => '"%s" does not contains only letters',
|
||||
self::MSG_NOT_ALPHA_ADDITIONAL => '"%s" does not contains only letters (including "%s")'
|
||||
);
|
||||
|
||||
public function __construct($input, $additionalChars='')
|
||||
{
|
||||
$code = empty($additionalChars) ? self::MSG_NOT_ALPHA : self::MSG_NOT_ALPHA_ADDITIONAL;
|
||||
parent::__construct(
|
||||
sprintf(
|
||||
$this->getMessageTemplate($code),
|
||||
$this->getStringRepresentation($input), $additionalChars
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -4,5 +4,22 @@ namespace Respect\Validation\Exceptions;
|
|||
|
||||
class NotAlphanumericException extends InvalidException
|
||||
{
|
||||
|
||||
const MSG_NOT_ALPHANUMERIC = 'Alnum_1';
|
||||
const MSG_NOT_ALPHANUMERIC_ADDITIONAL = 'Alnum_2';
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_NOT_ALPHANUMERIC => '"%s" does not contains only letters and digits',
|
||||
self::MSG_NOT_ALPHANUMERIC_ADDITIONAL => '"%s" does not contains only letters and digits (including "%s")'
|
||||
);
|
||||
|
||||
public function __construct($input, $additionalChars='')
|
||||
{
|
||||
$code = empty($additionalChars) ? self::MSG_NOT_ALPHANUMERIC : self::MSG_NOT_ALPHANUMERIC_ADDITIONAL;
|
||||
parent::__construct(
|
||||
sprintf(
|
||||
$this->getMessageTemplate($code),
|
||||
$this->getStringRepresentation($input), $additionalChars
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -4,5 +4,40 @@ namespace Respect\Validation\Exceptions;
|
|||
|
||||
class NotBetweenException extends InvalidException
|
||||
{
|
||||
|
||||
|
||||
protected $min;
|
||||
protected $max;
|
||||
protected $type;
|
||||
const MSG_LESS = 'Between_1';
|
||||
const MSG_MORE = 'Between_2';
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_LESS => '%s is less than the specified minimum of %s',
|
||||
self::MSG_MORE => '%s is more than the specified maximum of %s',
|
||||
);
|
||||
|
||||
public function __construct($input, $isMinValid, $isMaxValid, $min, $max)
|
||||
{
|
||||
$messages = array();
|
||||
if (!$isMinValid)
|
||||
$messages[] = sprintf(
|
||||
$this->getMessageTemplate(self::MSG_LESS),
|
||||
$this->getStringRepresentation($input),
|
||||
$this->getStringRepresentation($min)
|
||||
);
|
||||
if (!$isMaxValid)
|
||||
$messages[] = sprintf(
|
||||
$this->getMessageTemplate(self::MSG_MORE),
|
||||
$this->getStringRepresentation($input),
|
||||
$this->getStringRepresentation($max)
|
||||
);
|
||||
if (count($messages) > 1) {
|
||||
$exceptions = array();
|
||||
foreach ($messages as $m)
|
||||
$exceptions = new static($m);
|
||||
parent::__construct($exceptions);
|
||||
} else {
|
||||
parent::__construct($messages[0]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -4,5 +4,16 @@ namespace Respect\Validation\Exceptions;
|
|||
|
||||
class NotDigitsException extends InvalidException
|
||||
{
|
||||
|
||||
const MSG_NOT_DIGITS = 'Digits_1';
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_NOT_DIGITS => '%s does not contain only digits'
|
||||
);
|
||||
|
||||
public function __construct($input)
|
||||
{
|
||||
parent::__construct(
|
||||
sprintf($this->getMessageTemplate(self::MSG_NOT_DIGITS), $input)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -4,5 +4,16 @@ namespace Respect\Validation\Exceptions;
|
|||
|
||||
class NotFloatException extends InvalidException
|
||||
{
|
||||
|
||||
const MSG_NOT_FLOAT = 'Float_1';
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_NOT_FLOAT => '%s is not a valid float number'
|
||||
);
|
||||
|
||||
public function __construct($input)
|
||||
{
|
||||
parent::__construct(
|
||||
sprintf($this->getMessageTemplate(self::MSG_NOT_FLOAT), $input)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -4,5 +4,18 @@ namespace Respect\Validation\Exceptions;
|
|||
|
||||
class NotHexadecimalException extends InvalidException
|
||||
{
|
||||
|
||||
const MSG_NOT_HEXADECIMAL = 'Hexa_1';
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_NOT_HEXADECIMAL => '%s is not a valid hexadecimal number'
|
||||
);
|
||||
|
||||
public function __construct($input)
|
||||
{
|
||||
parent::__construct(
|
||||
sprintf(
|
||||
$this->getMessageTemplate(self::MSG_NOT_HEXADECIMAL), $input
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -4,5 +4,16 @@ namespace Respect\Validation\Exceptions;
|
|||
|
||||
class NotNullException extends InvalidException
|
||||
{
|
||||
|
||||
const MSG_NOT_NULL = 'NullValue_1';
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_NOT_NULL => '%s is not null'
|
||||
);
|
||||
|
||||
public function __construct($input)
|
||||
{
|
||||
parent::__construct(
|
||||
sprintf($this->getMessageTemplate(self::MSG_NOT_NULL), $input)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -5,4 +5,16 @@ namespace Respect\Validation\Exceptions;
|
|||
class NotNumericException extends InvalidException
|
||||
{
|
||||
|
||||
const MSG_NOT_NUMERIC = 'Numeric_1';
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_NOT_NUMERIC => '%s is not a numeric value'
|
||||
);
|
||||
|
||||
public function __construct($input)
|
||||
{
|
||||
parent::__construct(
|
||||
sprintf($this->getMessageTemplate(self::MSG_NOT_NUMERIC),
|
||||
$input)
|
||||
);
|
||||
}
|
||||
}
|
|
@ -4,5 +4,39 @@ namespace Respect\Validation\Exceptions;
|
|||
|
||||
class NumberOutOfBoundsException extends InvalidException
|
||||
{
|
||||
|
||||
|
||||
protected $min;
|
||||
protected $max;
|
||||
const MSG_NUMBER_LESS = 'NumberBetween_1';
|
||||
const MSG_NUMBER_MORE = 'NumberBetween_2';
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_NUMBER_LESS => '%s is less than the specified minimum (%s)',
|
||||
self::MSG_NUMBER_MORE => '%s is more than the specified maximum (%s)',
|
||||
);
|
||||
|
||||
public function __construct($input, $isMinValid, $isMaxValid, $min, $max)
|
||||
{
|
||||
$messages = array();
|
||||
if (!$isMinValid)
|
||||
$messages[] = sprintf(
|
||||
$this->getMessageTemplate(self::MSG_NUMBER_LESS),
|
||||
$this->getStringRepresentation($input),
|
||||
$this->getStringRepresentation($min)
|
||||
);
|
||||
if (!$isMaxValid)
|
||||
$messages[] = sprintf(
|
||||
$this->getMessageTemplate(self::MSG_NUMBER_MORE),
|
||||
$this->getStringRepresentation($input),
|
||||
$this->getStringRepresentation($max)
|
||||
);
|
||||
if (count($messages) > 1) {
|
||||
$exceptions = array();
|
||||
foreach ($messages as $m)
|
||||
$exceptions = new static($m);
|
||||
parent::__construct($exceptions);
|
||||
} else {
|
||||
parent::__construct($messages[0]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -6,5 +6,19 @@ use Exception;
|
|||
|
||||
class RegexException extends InvalidException
|
||||
{
|
||||
|
||||
const MSG_REGEX = 'Regex_1';
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_REGEX => '%s does not validate against the provided regular expression: %s.'
|
||||
);
|
||||
|
||||
public function __construct($input, $regex)
|
||||
{
|
||||
parent::__construct(
|
||||
sprintf(
|
||||
$this->getMessageTemplate(self::MSG_REGEX),
|
||||
$this->getStringRepresentation($input), $regex
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -4,5 +4,37 @@ namespace Respect\Validation\Exceptions;
|
|||
|
||||
class StringLengthException extends InvalidException
|
||||
{
|
||||
|
||||
const MSG_LENGTH_MIN = 'StringLength_1';
|
||||
const MSG_LENGTH_MAX = 'StringLength_2';
|
||||
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_LENGTH_MIN => '%s does not have at least %s characters',
|
||||
self::MSG_LENGTH_MAX => '%s exceeds the maximum of %s characters'
|
||||
);
|
||||
|
||||
public function __construct($input, $isMinValid, $isMaxValid, $min, $max)
|
||||
{
|
||||
$messages = array();
|
||||
if (!$isMinValid)
|
||||
$messages[] = sprintf(
|
||||
$this->getMessageTemplate(self::MSG_LENGTH_MIN),
|
||||
$this->getStringRepresentation($input),
|
||||
$this->getStringRepresentation($min)
|
||||
);
|
||||
if (!$isMaxValid)
|
||||
$messages[] = sprintf(
|
||||
$this->getMessageTemplate(self::MSG_LENGTH_MAX),
|
||||
$this->getStringRepresentation($input),
|
||||
$this->getStringRepresentation($max)
|
||||
);
|
||||
if (count($messages) > 1) {
|
||||
$exceptions = array();
|
||||
foreach ($messages as $m)
|
||||
$exceptions = new static($m);
|
||||
parent::__construct($exceptions);
|
||||
} else {
|
||||
parent::__construct($messages[0]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -4,5 +4,17 @@ namespace Respect\Validation\Exceptions;
|
|||
|
||||
class WhitespaceFoundException extends InvalidException
|
||||
{
|
||||
|
||||
const MSG_WHITESPACE_FOUND = 'NoWhitespace_1';
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_WHITESPACE_FOUND => '%s contains spaces, tabs, line breaks or other not allowed charaters.'
|
||||
);
|
||||
|
||||
public function __construct($input)
|
||||
{
|
||||
parent::__construct(
|
||||
sprintf($this->getMessageTemplate(self::MSG_WHITESPACE_FOUND),
|
||||
$input)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -9,7 +9,7 @@ use Respect\Validation\Exceptions\ComponentException;
|
|||
use Respect\Validation\Exceptions\InvalidException;
|
||||
use Exception;
|
||||
|
||||
abstract class AbstractComposite extends AbstractRule
|
||||
abstract class AbstractComposite extends AbstractRule
|
||||
{
|
||||
|
||||
protected $rules = array();
|
||||
|
@ -22,9 +22,6 @@ abstract class AbstractComposite extends AbstractRule
|
|||
protected function appendRule(Validatable $validator)
|
||||
{
|
||||
$this->rules[spl_object_hash($validator)] = $validator;
|
||||
$this->setMessageTemplates(array_merge(
|
||||
$this->getMessageTemplates(), $validator->getMessageTemplates()
|
||||
));
|
||||
}
|
||||
|
||||
public function addRule($validator, $arguments=array())
|
||||
|
@ -86,7 +83,7 @@ abstract class AbstractComposite extends AbstractRule
|
|||
foreach ($validators as $v)
|
||||
try {
|
||||
$v->assert($input);
|
||||
} catch (Exception $e) {
|
||||
} catch (InvalidException $e) {
|
||||
$exceptions[] = $e;
|
||||
}
|
||||
return $exceptions;
|
||||
|
|
|
@ -7,41 +7,6 @@ use Respect\Validation\Validatable;
|
|||
abstract class AbstractRule implements Validatable
|
||||
{
|
||||
|
||||
protected $messageTemplates = array();
|
||||
|
||||
public function setMessageTemplate($code, $template)
|
||||
{
|
||||
$this->messageTemplates[$code] = $template;
|
||||
}
|
||||
|
||||
public function getMessageTemplate($code)
|
||||
{
|
||||
return $this->messageTemplates[$code];
|
||||
}
|
||||
|
||||
public static function getMessage($code) {
|
||||
return $this->$messageTemplates[$code];
|
||||
}
|
||||
|
||||
public function setMessageTemplates(array $templates)
|
||||
{
|
||||
foreach ($templates as $code => $message)
|
||||
$this->setMessageTemplate($code, $message);
|
||||
}
|
||||
|
||||
public function getMessageTemplates()
|
||||
{
|
||||
return $this->messageTemplates;
|
||||
}
|
||||
|
||||
protected function getStringRepresentation($mixed)
|
||||
{
|
||||
if (is_object($mixed))
|
||||
return get_class($mixed);
|
||||
else
|
||||
return strval($mixed);
|
||||
}
|
||||
|
||||
public function __invoke($input)
|
||||
{
|
||||
return $this->validate($input);
|
||||
|
|
|
@ -8,22 +8,14 @@ use Respect\Validation\Exceptions\ComponentException;
|
|||
|
||||
class Alnum extends AbstractRule
|
||||
{
|
||||
const MSG_NOT_ALPHANUMERIC = 'Alnum_1';
|
||||
const MSG_NOT_ALPHANUMERIC_ADDITIONAL = 'Alnum_2';
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_NOT_ALPHANUMERIC => '"%s" does not contains only letters and digits',
|
||||
self::MSG_NOT_ALPHANUMERIC_ADDITIONAL => '"%s" does not contains only letters and digits (including "%s")'
|
||||
);
|
||||
|
||||
protected $additionalChars = '';
|
||||
|
||||
public function __construct($additionalChars='')
|
||||
{
|
||||
if (!is_string($additionalChars))
|
||||
throw new ComponentException(
|
||||
sprintf(
|
||||
'"%s" is not a valid list of additional characters to be loaded',
|
||||
$this->getStringRepresentation($additionalChars)
|
||||
)
|
||||
'Invalid list of additional characters to be loaded'
|
||||
);
|
||||
$this->additionalChars = $additionalChars;
|
||||
}
|
||||
|
@ -39,21 +31,7 @@ class Alnum extends AbstractRule
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
if (empty($this->additionalChars))
|
||||
throw new NotAlphanumericException(
|
||||
sprintf(
|
||||
$this->getMessageTemplate(self::MSG_NOT_ALPHANUMERIC),
|
||||
$this->getStringRepresentation($input)
|
||||
)
|
||||
);
|
||||
else
|
||||
throw new NotAlphanumericException(
|
||||
sprintf(
|
||||
$this->getMessageTemplate(self::MSG_NOT_ALPHANUMERIC_ADDITIONAL),
|
||||
$this->getStringRepresentation($input),
|
||||
$this->additionalChars
|
||||
)
|
||||
);
|
||||
throw new NotAlphanumericException($input, $this->additionalChars);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -8,22 +8,14 @@ use Respect\Validation\Exceptions\ComponentException;
|
|||
|
||||
class Alpha extends AbstractRule
|
||||
{
|
||||
const MSG_NOT_ALPHA = 'Alpha_1';
|
||||
const MSG_NOT_ALPHA_ADDITIONAL = 'Alpha_2';
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_NOT_ALPHA => '"%s" does not contains only letters',
|
||||
self::MSG_NOT_ALPHA_ADDITIONAL => '"%s" does not contains only letters (including "%s")'
|
||||
);
|
||||
|
||||
protected $additionalChars = '';
|
||||
|
||||
public function __construct($additionalChars='')
|
||||
{
|
||||
if (!is_string($additionalChars))
|
||||
throw new ComponentException(
|
||||
sprintf(
|
||||
'"%s" is not a valid list of additional characters to be loaded',
|
||||
$this->getStringRepresentation($additionalChars)
|
||||
)
|
||||
'Invalid list of additional characters to be loaded'
|
||||
);
|
||||
$this->additionalChars = $additionalChars;
|
||||
}
|
||||
|
@ -39,21 +31,7 @@ class Alpha extends AbstractRule
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
if (empty($this->additionalChars))
|
||||
throw new NotAlphaException(
|
||||
sprintf(
|
||||
$this->getMessageTemplate(self::MSG_NOT_ALPHA),
|
||||
$this->getStringRepresentation($input)
|
||||
)
|
||||
);
|
||||
else
|
||||
throw new NotAlphaException(
|
||||
sprintf(
|
||||
$this->getMessageTemplate(self::MSG_NOT_ALPHA_ADDITIONAL),
|
||||
$this->getStringRepresentation($input),
|
||||
$this->additionalChars
|
||||
)
|
||||
);
|
||||
throw new NotAlphaException($input, $this->additionalChars);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -12,16 +12,6 @@ use Respect\Validation\Validatable;
|
|||
class Between extends AbstractRule
|
||||
{
|
||||
|
||||
protected $min;
|
||||
protected $max;
|
||||
protected $type;
|
||||
const MSG_LESS = 'Between_1';
|
||||
const MSG_MORE = 'Between_2';
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_LESS => '%s is less than the specified minimum of %s',
|
||||
self::MSG_MORE => '%s is more than the specified maximum of %s',
|
||||
);
|
||||
|
||||
public function __construct($min=null, $max=null, Validatable $type=null)
|
||||
{
|
||||
$this->min = $min;
|
||||
|
@ -67,21 +57,15 @@ class Between extends AbstractRule
|
|||
{
|
||||
if (!is_null($this->type))
|
||||
$this->type->assert($input);
|
||||
if (!$this->validateMin($input))
|
||||
$validMin = $this->validateMin($input);
|
||||
$validMax = $this->validateMax($input);
|
||||
if (!$validMin || !$validMax)
|
||||
throw new NotBetweenException(
|
||||
sprintf(
|
||||
$this->getMessageTemplate(self::MSG_LESS),
|
||||
$this->getStringRepresentation($input),
|
||||
$this->getStringRepresentation($this->min)
|
||||
)
|
||||
);
|
||||
if (!$this->validateMax($input))
|
||||
throw new NotBetweenException(
|
||||
sprintf(
|
||||
$this->getMessageTemplate(self::MSG_MORE),
|
||||
$this->getStringRepresentation($input),
|
||||
$this->getStringRepresentation($this->max)
|
||||
)
|
||||
$input,
|
||||
$validMin,
|
||||
$validMax,
|
||||
$this->min,
|
||||
$this->max
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -8,20 +8,14 @@ use Respect\Validation\Exceptions\ComponentException;
|
|||
|
||||
class Callback extends AbstractRule
|
||||
{
|
||||
const MSG_CALLBACK = 'Callback_1';
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_CALLBACK => '%s does not validate against the provided callback.'
|
||||
);
|
||||
|
||||
protected $callback;
|
||||
|
||||
public function __construct($callback)
|
||||
{
|
||||
if (!is_callable($callback))
|
||||
throw new ComponentException(
|
||||
sprintf(
|
||||
'"%s is not a valid callback',
|
||||
$this->getStringRepresentation($callback)
|
||||
)
|
||||
'Invalid callback'
|
||||
);
|
||||
$this->callback = $callback;
|
||||
}
|
||||
|
@ -34,11 +28,7 @@ class Callback extends AbstractRule
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw new CallbackException(
|
||||
sprintf($this->getMessageTemplate(self::MSG_CALLBACK),
|
||||
$this->getStringRepresentation($input)
|
||||
)
|
||||
);
|
||||
throw new CallbackException($input, $this->callback);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -8,12 +8,6 @@ use Respect\Validation\Exceptions\InvalidDate;
|
|||
|
||||
class Date extends AbstractDate
|
||||
{
|
||||
const MSG_INVALID_DATE = 'Date_1';
|
||||
const MSG_INVALID_FORMAT = 'Date_2';
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_INVALID_DATE => '%s is not a valid date reference',
|
||||
self::MSG_INVALID_FORMAT => '%s is not a valid date in the %s format',
|
||||
);
|
||||
|
||||
public function __construct($format=null)
|
||||
{
|
||||
|
@ -35,20 +29,7 @@ class Date extends AbstractDate
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
if (is_null($this->format))
|
||||
throw new InvalidDate(
|
||||
sprintf($this->getMessageTemplate(static::MSG_INVALID_DATE),
|
||||
$input)
|
||||
);
|
||||
else
|
||||
throw new InvalidDate(
|
||||
sprintf(
|
||||
$this->getMessageTemplate(static::MSG_INVALID_FORMAT),
|
||||
$input, $this->format
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
throw new InvalidDate($input, $this->format);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -10,27 +10,19 @@ use Respect\Validation\Validator;
|
|||
|
||||
class DateBetween extends AbstractDate
|
||||
{
|
||||
const MSG_OUT_OF_BOUNDS = 'DateBetween_1';
|
||||
|
||||
protected $min;
|
||||
protected $max;
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_OUT_OF_BOUNDS => '%s is not between %s and %s.'
|
||||
);
|
||||
|
||||
public function __construct($min, $max, $format=null)
|
||||
{
|
||||
if (!Validator::date()->validate($min))
|
||||
throw new ComponentException(
|
||||
sprintf(
|
||||
'Invalid Date: %s', $this->getStringRepresentation($min)
|
||||
)
|
||||
'Invalid min date'
|
||||
);
|
||||
if (!Validator::date()->validate($max))
|
||||
throw new ComponentException(
|
||||
sprintf(
|
||||
'Invalid Date: %s', $this->getStringRepresentation($max)
|
||||
)
|
||||
'Invalid max date'
|
||||
);
|
||||
$this->min = $this->getDateObject($min);
|
||||
$this->max = $this->getDateObject($max);
|
||||
|
@ -48,11 +40,9 @@ class DateBetween extends AbstractDate
|
|||
$target = $this->getDateObject($input);
|
||||
if (!$this->validate($target))
|
||||
throw new DateOutOfBoundsException(
|
||||
sprintf(
|
||||
$this->getMessageTemplate(self::MSG_OUT_OF_BOUNDS),
|
||||
$this->formatDate($target), $this->formatDate($this->min),
|
||||
$this->formatDate($this->max)
|
||||
)
|
||||
$this->formatDate($target),
|
||||
$this->formatDate($this->min),
|
||||
$this->formatDate($this->max)
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -7,10 +7,6 @@ use Respect\Validation\Exceptions\NotDigitsException;
|
|||
|
||||
class Digits extends AbstractRule
|
||||
{
|
||||
const MSG_NOT_DIGITS = 'Digits_1';
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_NOT_DIGITS => '%s does not contain only digits'
|
||||
);
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
|
@ -20,9 +16,7 @@ class Digits extends AbstractRule
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw new NotDigitsException(
|
||||
sprintf($this->getMessageTemplate(self::MSG_NOT_DIGITS), $input)
|
||||
);
|
||||
throw new NotDigitsException($input);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -7,11 +7,6 @@ use Respect\Validation\Exceptions\NotFloatException;
|
|||
|
||||
class Float extends AbstractRule
|
||||
{
|
||||
const MSG_NOT_FLOAT = 'Float_1';
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_NOT_FLOAT => '%s is not a valid float number'
|
||||
);
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
return filter_var($input, FILTER_VALIDATE_FLOAT);
|
||||
|
@ -20,9 +15,7 @@ class Float extends AbstractRule
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw new NotFloatException(
|
||||
sprintf($this->getMessageTemplate(self::MSG_NOT_FLOAT), $input)
|
||||
);
|
||||
throw new NotFloatException($input);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -9,20 +9,14 @@ use Respect\Validation\Exceptions\ComponentException;
|
|||
|
||||
class HasAttribute extends All
|
||||
{
|
||||
const MSG_ATTRIBUTE_NOT_PRESENT = 'HasAttribute_1';
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_ATTRIBUTE_NOT_PRESENT => 'Object does not have the attribute %s'
|
||||
);
|
||||
|
||||
protected $attribute = '';
|
||||
|
||||
public function __construct($attribute, $attributeValidator=null)
|
||||
{
|
||||
if (!is_string($attribute))
|
||||
throw new ComponentException(
|
||||
sprintf(
|
||||
'"%s" is not a valid attribute name',
|
||||
$this->getStringRepresentation($attribute)
|
||||
)
|
||||
'Invalid attribute name'
|
||||
);
|
||||
$this->attribute = $attribute;
|
||||
if (!is_null($attributeValidator))
|
||||
|
@ -38,12 +32,7 @@ class HasAttribute extends All
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw new AttributeNotPresentException(
|
||||
sprintf(
|
||||
$this->getMessageTemplate(self::MSG_ATTRIBUTE_NOT_PRESENT),
|
||||
$this->attribute
|
||||
)
|
||||
);
|
||||
throw new AttributeNotPresentException($input, $this->attribute);
|
||||
return parent::validate($input->{$this->attribute});
|
||||
}
|
||||
|
||||
|
|
|
@ -10,20 +10,14 @@ use Respect\Validation\Validator;
|
|||
|
||||
class HasKey extends All
|
||||
{
|
||||
const MSG_KEY_NOT_PRESENT = 'HasKey_1';
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_KEY_NOT_PRESENT => 'Array does not have the key %s'
|
||||
);
|
||||
|
||||
protected $key = '';
|
||||
|
||||
public function __construct($key, $valueValidator=null)
|
||||
{
|
||||
if (!Validator::alnum()->validate($key))
|
||||
throw new ComponentException(
|
||||
sprintf(
|
||||
'"%s" is not a valid key name',
|
||||
$this->getStringRepresentation($key)
|
||||
)
|
||||
'Invalid key name'
|
||||
);
|
||||
$this->key = $key;
|
||||
if (!is_null($valueValidator))
|
||||
|
@ -39,12 +33,7 @@ class HasKey extends All
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw new KeyNotPresentException(
|
||||
sprintf(
|
||||
$this->getMessageTemplate(self::MSG_KEY_NOT_PRESENT),
|
||||
$this->key
|
||||
)
|
||||
);
|
||||
throw new KeyNotPresentException($input, $this->key);
|
||||
return parent::validate($input[$this->key]);
|
||||
}
|
||||
|
||||
|
|
|
@ -7,10 +7,6 @@ use Respect\Validation\Exceptions\NotHexadecimalException;
|
|||
|
||||
class Hexa extends AbstractRule
|
||||
{
|
||||
const MSG_NOT_HEXADECIMAL = 'Hexa_1';
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_NOT_HEXADECIMAL => '%s is not a valid hexadecimal number'
|
||||
);
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
|
@ -20,10 +16,7 @@ class Hexa extends AbstractRule
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw new NotHexadecimalException(
|
||||
sprintf($this->getMessageTemplate(self::MSG_NOT_HEXADECIMAL),
|
||||
$input)
|
||||
);
|
||||
throw new NotHexadecimalException($input);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -7,11 +7,6 @@ use Respect\Validation\Exceptions\InvalidIpException;
|
|||
|
||||
class Ip extends AbstractRule
|
||||
{
|
||||
const MSG_NOT_IP = 'Ip_1';
|
||||
public $options;
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_NOT_IP => '%s is not a valid IP address'
|
||||
);
|
||||
|
||||
public function __construct($options=null)
|
||||
{
|
||||
|
@ -28,9 +23,7 @@ class Ip extends AbstractRule
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw new InvalidIpException(
|
||||
sprintf($this->getMessageTemplate(self::MSG_NOT_IP), $input)
|
||||
);
|
||||
throw new InvalidIpException($input);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -5,13 +5,8 @@ namespace Respect\Validation\Rules;
|
|||
use Respect\Validation\Rules\AbstractRule;
|
||||
use Respect\Validation\Exceptions\WhitespaceFoundException;
|
||||
|
||||
class NoWhitespace extends AbstractRule
|
||||
class NoWhitespace extends AbstractRule
|
||||
{
|
||||
const MSG_WHITESPACE_FOUND = 'NoWhitespace_1';
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_WHITESPACE_FOUND => '%s contains spaces, tabs, line breaks or other not allowed charaters.'
|
||||
);
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
return preg_match('#^\S+$#', $input);
|
||||
|
@ -20,9 +15,7 @@ class NoWhitespace extends AbstractRule
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw new WhitespaceFoundException(
|
||||
sprintf($this->getMessageTemplate(self::MSG_WHITESPACE_FOUND), $input)
|
||||
);
|
||||
throw new WhitespaceFoundException($input);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -7,10 +7,6 @@ use Respect\Validation\Exceptions\NotNullException;
|
|||
|
||||
class NullValue extends AbstractRule
|
||||
{
|
||||
const MSG_NOT_NULL = 'NullValue_1';
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_NOT_NULL => '%s is not null'
|
||||
);
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
|
@ -20,9 +16,7 @@ class NullValue extends AbstractRule
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw new NotNullException(
|
||||
sprintf($this->getMessageTemplate(self::MSG_NOT_NULL), $input)
|
||||
);
|
||||
throw new NotNullException($input);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -7,10 +7,6 @@ use Respect\Validation\Exceptions\NotNumericException;
|
|||
|
||||
class Numeric extends AbstractRule
|
||||
{
|
||||
const MSG_NOT_NUMERIC = 'Numeric_1';
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_NOT_NUMERIC => '%s is not a numeric value'
|
||||
);
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
|
@ -20,9 +16,7 @@ class Numeric extends AbstractRule
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw new NotNumericException(
|
||||
sprintf($this->getMessageTemplate(self::MSG_NOT_NUMERIC), $input)
|
||||
);
|
||||
throw new NotNumericException($input);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -13,15 +13,6 @@ use Respect\Validation\Exceptions\ComponentException;
|
|||
class NumericBetween extends AbstractRule
|
||||
{
|
||||
|
||||
protected $min;
|
||||
protected $max;
|
||||
const MSG_NUMBER_LESS = 'NumberBetween_1';
|
||||
const MSG_NUMBER_MORE = 'NumberBetween_2';
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_NUMBER_LESS => '%s is less than the specified minimum (%s)',
|
||||
self::MSG_NUMBER_MORE => '%s is more than the specified maximum (%s)',
|
||||
);
|
||||
|
||||
public function __construct($min=null, $max=null)
|
||||
{
|
||||
$this->min = $min;
|
||||
|
@ -68,15 +59,15 @@ class NumericBetween extends AbstractRule
|
|||
{
|
||||
$validNumeric = new Numeric();
|
||||
$validNumeric->assert($input);
|
||||
if (!$this->validateMin($input))
|
||||
$validMin = $this->validateMin($input);
|
||||
$validMax = $this->validateMax($input);
|
||||
if (!$validMin || !$validMax)
|
||||
throw new NumberOutOfBoundsException(
|
||||
sprintf($this->getMessageTemplate(self::MSG_NUMBER_LESS),
|
||||
$input, $this->min)
|
||||
);
|
||||
if (!$this->validateMax($input))
|
||||
throw new NumberOutOfBoundsException(
|
||||
sprintf($this->getMessageTemplate(self::MSG_NUMBER_MORE),
|
||||
$input, $this->max)
|
||||
$input,
|
||||
$validMin,
|
||||
$validMax,
|
||||
$this->min,
|
||||
$this->max
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -7,10 +7,7 @@ use Respect\Validation\Exceptions\RegexException;
|
|||
|
||||
class Regex extends AbstractRule
|
||||
{
|
||||
const MSG_REGEX = 'Regex_1';
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_REGEX => '%s does not validate against the provided regular expression: %s.'
|
||||
);
|
||||
|
||||
protected $regex;
|
||||
|
||||
public function __construct($regex)
|
||||
|
@ -26,12 +23,7 @@ class Regex extends AbstractRule
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw new RegexException(
|
||||
sprintf(
|
||||
$this->getMessageTemplate(self::MSG_REGEX),
|
||||
$this->getStringRepresentation($input), $this->regex
|
||||
)
|
||||
);
|
||||
throw new RegexException($input, $this->regex);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -11,13 +11,6 @@ use Respect\Validation\Exceptions\InvalidException;
|
|||
|
||||
class StringLength extends AbstractRule
|
||||
{
|
||||
const MSG_LENGTH_MIN = 'StringLength_1';
|
||||
const MSG_LENGTH_MAX = 'StringLength_2';
|
||||
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_LENGTH_MIN => '%s does not have at least %s characters',
|
||||
self::MSG_LENGTH_MAX => '%s exceeds the maximum of %s characters'
|
||||
);
|
||||
protected $min;
|
||||
protected $max;
|
||||
|
||||
|
@ -65,15 +58,15 @@ class StringLength extends AbstractRule
|
|||
|
||||
public function assert($input)
|
||||
{
|
||||
if (!$this->validateMin($input))
|
||||
$validMin = $this->validateMin($input);
|
||||
$validMax = $this->validateMax($input);
|
||||
if (!$validMin || !$validMax)
|
||||
throw new StringLengthException(
|
||||
sprintf($this->getMessageTemplate(self::MSG_LENGTH_MIN), $input,
|
||||
$this->min)
|
||||
);
|
||||
if (!$this->validateMax($input))
|
||||
throw new StringLengthException(
|
||||
sprintf($this->getMessageTemplate(self::MSG_LENGTH_MAX), $input,
|
||||
$this->max)
|
||||
$input,
|
||||
$validMin,
|
||||
$validMax,
|
||||
$this->min,
|
||||
$this->max
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
|
|
@ -7,11 +7,6 @@ use Respect\Validation\Rules\AbstractRule;
|
|||
|
||||
class StringNotEmpty extends AbstractRule
|
||||
{
|
||||
const MSG_EMPTY_STRING = 'StringNotEmpty_1';
|
||||
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_EMPTY_STRING => 'You provided an empty string'
|
||||
);
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
|
@ -22,9 +17,7 @@ class StringNotEmpty extends AbstractRule
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw new EmptyStringException(
|
||||
$this->getMessageTemplate(self::MSG_EMPTY_STRING)
|
||||
);
|
||||
throw new EmptyStringException();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -8,12 +8,4 @@ interface Validatable
|
|||
public function assert($input);
|
||||
|
||||
public function validate($input);
|
||||
|
||||
public function getMessageTemplates();
|
||||
|
||||
public function setMessageTemplates(array $templates);
|
||||
|
||||
public function setMessageTemplate($code, $templage);
|
||||
|
||||
public function getMessageTemplate($code);
|
||||
}
|
|
@ -50,23 +50,6 @@ class AbstractCompositeTest extends ValidatorTestCase
|
|||
$this->object->addRule('FooFreak');
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForMockImpossibleValidators
|
||||
*/
|
||||
public function testAddValidatorsMessages($a, $b)
|
||||
{
|
||||
$messagesA = $a->getMessages();
|
||||
$messagesB = $b->getMessages();
|
||||
$this->object->addRules(func_get_args());
|
||||
$messagesObject = $this->object->getMessages();
|
||||
foreach ($messagesA as $m) {
|
||||
$this->assertContains($m, $messagesObject);
|
||||
}
|
||||
foreach ($messagesB as $m) {
|
||||
$this->assertContains($m, $messagesObject);
|
||||
}
|
||||
}
|
||||
|
||||
public function testBuildValidators()
|
||||
{
|
||||
$this->providerForMockImpossibleValidators();
|
||||
|
@ -88,24 +71,4 @@ class AbstractCompositeTest extends ValidatorTestCase
|
|||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForMockImpossibleValidators
|
||||
*/
|
||||
public function testSetMessages($a, $b)
|
||||
{
|
||||
$messagesA = $a->getMessages();
|
||||
$messagesB = $b->getMessages();
|
||||
$this->object->addRules(func_get_args());
|
||||
$this->object->setMessages(
|
||||
array_map('strrev', $this->object->getMessages())
|
||||
);
|
||||
$messagesObject = $this->object->getMessages();
|
||||
foreach ($messagesA as $m) {
|
||||
$this->assertContains(strrev($m), $messagesObject);
|
||||
}
|
||||
foreach ($messagesB as $m) {
|
||||
$this->assertContains(strrev($m), $messagesObject);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue