mirror of
https://github.com/Respect/Validation.git
synced 2024-06-26 17:20:16 +02:00
Changed the Exceptions to Match Rules names
This commit is contained in:
parent
460b744ee1
commit
2408730f48
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class NotAlphanumericException extends InvalidException
|
||||
class AlnumException extends InvalidException
|
||||
{
|
||||
const MSG_NOT_ALPHANUMERIC = 'Alnum_1';
|
||||
const MSG_NOT_ALPHANUMERIC_ADDITIONAL = 'Alnum_2';
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class NotAlphaException extends InvalidException
|
||||
class AlphaException extends InvalidException
|
||||
{
|
||||
const MSG_NOT_ALPHA = 'Alpha_1';
|
||||
const MSG_NOT_ALPHA_ADDITIONAL = 'Alpha_2';
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class NotArrayException extends InvalidException
|
||||
class ArrException extends InvalidException
|
||||
{
|
||||
const MSG_NOT_ARRAY = 'Array_1';
|
||||
protected $messageTemplates = array(
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class NotBetweenException extends InvalidException
|
||||
class BetweenException extends InvalidException
|
||||
{
|
||||
|
||||
protected $min;
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class InvalidDate extends InvalidException
|
||||
class DateException extends InvalidException
|
||||
{
|
||||
const MSG_INVALID_DATE = 'Date_1';
|
||||
const MSG_INVALID_FORMAT = 'Date_2';
|
|
@ -1,23 +0,0 @@
|
|||
<?php
|
||||
|
||||
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
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class NotDigitsException extends InvalidException
|
||||
class DigitsException extends InvalidException
|
||||
{
|
||||
const MSG_NOT_DIGITS = 'Digits_1';
|
||||
protected $messageTemplates = array(
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class NotFloatException extends InvalidException
|
||||
class FloatException extends InvalidException
|
||||
{
|
||||
const MSG_NOT_FLOAT = 'Float_1';
|
||||
protected $messageTemplates = array(
|
|
@ -4,18 +4,18 @@ namespace Respect\Validation\Exceptions;
|
|||
|
||||
use InvalidArgumentException;
|
||||
|
||||
class AttributeNotPresentException extends InvalidException
|
||||
class HasAttributeException extends InvalidException
|
||||
{
|
||||
const MSG_ATTRIBUTE_NOT_PRESENT = 'HasAttribute_1';
|
||||
const MSG_HAS_ATTRIBUTE = 'HasAttribute_1';
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_ATTRIBUTE_NOT_PRESENT => 'Object %s does not have the attribute %s'
|
||||
self::MSG_HAS_ATTRIBUTE => 'Object %s does not have the attribute %s'
|
||||
);
|
||||
|
||||
public function __construct($input, $attributeName)
|
||||
{
|
||||
parent::__construct(
|
||||
sprintf(
|
||||
$this->getMessageTemplate(self::MSG_ATTRIBUTE_NOT_PRESENT),
|
||||
$this->getMessageTemplate(self::MSG_HAS_ATTRIBUTE),
|
||||
$this->getStringRepresentation($input), $attributeName
|
||||
)
|
||||
);
|
|
@ -2,9 +2,7 @@
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
use InvalidArgumentException;
|
||||
|
||||
class KeyNotPresentException extends InvalidException
|
||||
class HasKeyException extends InvalidException
|
||||
{
|
||||
const MSG_KEY_NOT_PRESENT = 'HasKey_1';
|
||||
protected $messageTemplates = array(
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class NotHexadecimalException extends InvalidException
|
||||
class HexaException extends InvalidException
|
||||
{
|
||||
const MSG_NOT_HEXADECIMAL = 'Hexa_1';
|
||||
protected $messageTemplates = array(
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class NotInstanceException extends InvalidException
|
||||
class InstanceException extends InvalidException
|
||||
{
|
||||
const MSG_NOT_INSTANCE = 'Instance_1';
|
||||
protected $messageTemplates = array(
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class InvalidIpException extends InvalidException
|
||||
class IpException extends InvalidException
|
||||
{
|
||||
const MSG_NOT_IP = 'Ip_1';
|
||||
public $options;
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class WhitespaceFoundException extends InvalidException
|
||||
class NoWhitespaceException extends InvalidException
|
||||
{
|
||||
const MSG_WHITESPACE_FOUND = 'NoWhitespace_1';
|
||||
protected $messageTemplates = array(
|
|
@ -2,17 +2,17 @@
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class EmptyStringException extends InvalidException
|
||||
class NotEmptyException extends InvalidException
|
||||
{
|
||||
const MSG_EMPTY_STRING = 'StringNotEmpty_1';
|
||||
const MSG_NOT_EMPTY = 'NotEmpty_1';
|
||||
|
||||
protected $messageTemplates = array(
|
||||
self::MSG_EMPTY_STRING => 'You provided an empty string'
|
||||
self::MSG_NOT_EMPTY => 'You provided an empty value'
|
||||
);
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct($this->getMessageTemplate(self::MSG_EMPTY_STRING));
|
||||
parent::__construct($this->getMessageTemplate(self::MSG_NOT_EMPTY));
|
||||
}
|
||||
|
||||
}
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class NotNullException extends InvalidException
|
||||
class NullValueException extends InvalidException
|
||||
{
|
||||
const MSG_NOT_NULL = 'NullValue_1';
|
||||
protected $messageTemplates = array(
|
|
@ -1,42 +0,0 @@
|
|||
<?php
|
||||
|
||||
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]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class NotNumericException extends InvalidException
|
||||
class NumericException extends InvalidException
|
||||
{
|
||||
|
||||
const MSG_NOT_NUMERIC = 'Numeric_1';
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class NotObjectException extends InvalidException
|
||||
class ObjectException extends InvalidException
|
||||
{
|
||||
const MSG_NOT_OBJECT = 'Object_1';
|
||||
protected $messageTemplates = array(
|
|
@ -3,7 +3,7 @@
|
|||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Rules\AbstractRule;
|
||||
use Respect\Validation\Exceptions\NotAlphanumericException;
|
||||
use Respect\Validation\Exceptions\AlnumException;
|
||||
use Respect\Validation\Exceptions\ComponentException;
|
||||
|
||||
class Alnum extends AbstractRule
|
||||
|
@ -31,7 +31,7 @@ class Alnum extends AbstractRule
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw new NotAlphanumericException($input, $this->additionalChars);
|
||||
throw new AlnumException($input, $this->additionalChars);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Rules\AbstractRule;
|
||||
use Respect\Validation\Exceptions\NotAlphaException;
|
||||
use Respect\Validation\Exceptions\AlphaException;
|
||||
use Respect\Validation\Exceptions\ComponentException;
|
||||
|
||||
class Alpha extends AbstractRule
|
||||
|
@ -31,7 +31,7 @@ class Alpha extends AbstractRule
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw new NotAlphaException($input, $this->additionalChars);
|
||||
throw new AlphaException($input, $this->additionalChars);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Rules\AbstractRule;
|
||||
use Respect\Validation\Exceptions\NotArrayException;
|
||||
use Respect\Validation\Exceptions\ArrException;
|
||||
|
||||
class Arr extends AbstractRule
|
||||
{
|
||||
|
@ -16,7 +16,7 @@ class Arr extends AbstractRule
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw new NotArrayException($input);
|
||||
throw new ArrException($input);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -5,7 +5,7 @@ namespace Respect\Validation\Rules;
|
|||
use \Exception;
|
||||
use Respect\Validation\Validator;
|
||||
use Respect\Validation\Rules\AbstractRule;
|
||||
use Respect\Validation\Exceptions\NotBetweenException;
|
||||
use Respect\Validation\Exceptions\BetweenException;
|
||||
use Respect\Validation\Exceptions\ComponentException;
|
||||
use Respect\Validation\Validatable;
|
||||
|
||||
|
@ -60,7 +60,7 @@ class Between extends AbstractRule
|
|||
$validMin = $this->validateMin($input);
|
||||
$validMax = $this->validateMax($input);
|
||||
if (!$validMin || !$validMax)
|
||||
throw new NotBetweenException(
|
||||
throw new BetweenException(
|
||||
$input,
|
||||
$validMin,
|
||||
$validMax,
|
||||
|
|
|
@ -4,7 +4,7 @@ namespace Respect\Validation\Rules;
|
|||
|
||||
use DateTime;
|
||||
use Respect\Validation\Rules\AbstractDate;
|
||||
use Respect\Validation\Exceptions\InvalidDate;
|
||||
use Respect\Validation\Exceptions\DateException;
|
||||
|
||||
class Date extends AbstractDate
|
||||
{
|
||||
|
@ -29,7 +29,7 @@ class Date extends AbstractDate
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw new InvalidDate($input, $this->format);
|
||||
throw new DateException($input, $this->format);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,61 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Rules\AbstractDate;
|
||||
use Respect\Validation\Exceptions\ComponentException;
|
||||
use Respect\Validation\Exceptions\DateOutOfBoundsException;
|
||||
use Respect\Validation\Exceptions\InvalidDate;
|
||||
use Respect\Validation\Validator;
|
||||
|
||||
class DateBetween extends AbstractDate
|
||||
{
|
||||
|
||||
protected $min;
|
||||
protected $max;
|
||||
|
||||
public function __construct($min, $max, $format=null)
|
||||
{
|
||||
if (!Validator::date()->validate($min))
|
||||
throw new ComponentException(
|
||||
'Invalid min date'
|
||||
);
|
||||
if (!Validator::date()->validate($max))
|
||||
throw new ComponentException(
|
||||
'Invalid max date'
|
||||
);
|
||||
$this->min = $this->getDateObject($min);
|
||||
$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))
|
||||
);
|
||||
if (!is_null($format))
|
||||
$this->setFormat($format);
|
||||
}
|
||||
|
||||
public function assert($input)
|
||||
{
|
||||
$target = $this->getDateObject($input);
|
||||
if (!$this->validate($target))
|
||||
throw new DateOutOfBoundsException(
|
||||
$this->formatDate($target),
|
||||
$this->formatDate($this->min),
|
||||
$this->formatDate($this->max)
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
$target = $this->getDateObject($input);
|
||||
return $target >= $this->min and $target <= $this->max;
|
||||
}
|
||||
|
||||
public function check($input)
|
||||
{
|
||||
return $this->assert($input);
|
||||
}
|
||||
|
||||
}
|
|
@ -3,7 +3,7 @@
|
|||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Rules\AbstractRule;
|
||||
use Respect\Validation\Exceptions\NotDigitsException;
|
||||
use Respect\Validation\Exceptions\DigitsException;
|
||||
|
||||
class Digits extends AbstractRule
|
||||
{
|
||||
|
@ -16,7 +16,7 @@ class Digits extends AbstractRule
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw new NotDigitsException($input);
|
||||
throw new DigitsException($input);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Rules\AbstractRule;
|
||||
use Respect\Validation\Exceptions\NotFloatException;
|
||||
use Respect\Validation\Exceptions\FloatException;
|
||||
|
||||
class Float extends AbstractRule
|
||||
{
|
||||
|
@ -16,7 +16,7 @@ class Float extends AbstractRule
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw new NotFloatException($input);
|
||||
throw new FloatException($input);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Rules\AbstractRule;
|
||||
use Respect\Validation\Exceptions\AttributeNotPresentException;
|
||||
use Respect\Validation\Exceptions\HasAttributeException;
|
||||
use Respect\Validation\Rules\All;
|
||||
use Respect\Validation\Exceptions\ComponentException;
|
||||
|
||||
|
@ -32,7 +32,7 @@ class HasAttribute extends AllOf
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw new AttributeNotPresentException($input, $this->attribute);
|
||||
throw new HasAttributeException($input, $this->attribute);
|
||||
return parent::validate(@$input->{$this->attribute});
|
||||
}
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Rules\AbstractRule;
|
||||
use Respect\Validation\Exceptions\KeyNotPresentException;
|
||||
use Respect\Validation\Exceptions\HasKeyException;
|
||||
use Respect\Validation\Rules\All;
|
||||
use Respect\Validation\Exceptions\ComponentException;
|
||||
use Respect\Validation\Validator;
|
||||
|
@ -33,7 +33,7 @@ class HasKey extends AllOf
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw new KeyNotPresentException($input, $this->key);
|
||||
throw new HasKeyException($input, $this->key);
|
||||
return parent::validate(@$input[$this->key]);
|
||||
}
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Rules\AbstractRule;
|
||||
use Respect\Validation\Exceptions\NotHexadecimalException;
|
||||
use Respect\Validation\Exceptions\HexaException;
|
||||
|
||||
class Hexa extends AbstractRule
|
||||
{
|
||||
|
@ -16,7 +16,7 @@ class Hexa extends AbstractRule
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw new NotHexadecimalException($input);
|
||||
throw new HexaException($input);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Rules\AbstractRule;
|
||||
use Respect\Validation\Exceptions\NotInstanceException;
|
||||
use Respect\Validation\Exceptions\InstanceException;
|
||||
|
||||
class Instance extends AbstractRule
|
||||
{
|
||||
|
@ -23,7 +23,7 @@ class Instance extends AbstractRule
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw new NotInstanceException($input, $this->instance);
|
||||
throw new InstanceException($input, $this->instance);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Rules\AbstractRule;
|
||||
use Respect\Validation\Exceptions\InvalidIpException;
|
||||
use Respect\Validation\Exceptions\IpException;
|
||||
|
||||
class Ip extends AbstractRule
|
||||
{
|
||||
|
@ -23,7 +23,7 @@ class Ip extends AbstractRule
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw new InvalidIpException($input);
|
||||
throw new IpException($input);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Rules\AbstractRule;
|
||||
use Respect\Validation\Exceptions\WhitespaceFoundException;
|
||||
use Respect\Validation\Exceptions\NoWhitespaceException;
|
||||
|
||||
class NoWhitespace extends AbstractRule
|
||||
{
|
||||
|
@ -16,7 +16,7 @@ class NoWhitespace extends AbstractRule
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw new WhitespaceFoundException($input);
|
||||
throw new NoWhitespaceException($input);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -2,22 +2,23 @@
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Exceptions\EmptyStringException;
|
||||
use Respect\Validation\Exceptions\NotEmptyException;
|
||||
use Respect\Validation\Rules\AbstractRule;
|
||||
|
||||
class StringNotEmpty extends AbstractRule
|
||||
class NotEmpty extends AbstractRule
|
||||
{
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
$trimmed = trim($input);
|
||||
return!empty($trimmed);
|
||||
if (is_string($input))
|
||||
$input = trim($input);
|
||||
return!empty($input);
|
||||
}
|
||||
|
||||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw new EmptyStringException();
|
||||
throw new NotEmptyException();
|
||||
return true;
|
||||
}
|
||||
|
|
@ -3,7 +3,7 @@
|
|||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Rules\AbstractRule;
|
||||
use Respect\Validation\Exceptions\NotNullException;
|
||||
use Respect\Validation\Exceptions\NullValueException;
|
||||
|
||||
class NullValue extends AbstractRule
|
||||
{
|
||||
|
@ -16,7 +16,7 @@ class NullValue extends AbstractRule
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw new NotNullException($input);
|
||||
throw new NullValueException($input);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Rules\AbstractRule;
|
||||
use Respect\Validation\Exceptions\NotNumericException;
|
||||
use Respect\Validation\Exceptions\NumericException;
|
||||
|
||||
class Numeric extends AbstractRule
|
||||
{
|
||||
|
@ -16,7 +16,7 @@ class Numeric extends AbstractRule
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw new NotNumericException($input);
|
||||
throw new NumericException($input);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,80 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Validator;
|
||||
use Respect\Validation\Rules\AbstractRule;
|
||||
use Respect\Validation\Rules\Numeric;
|
||||
use Respect\Validation\Exceptions\NotNumericException;
|
||||
use Respect\Validation\Exceptions\NumberOutOfBoundsException;
|
||||
use Respect\Validation\Exceptions\InvalidException;
|
||||
use Respect\Validation\Exceptions\ComponentException;
|
||||
|
||||
class NumericBetween extends AbstractRule
|
||||
{
|
||||
|
||||
public function __construct($min=null, $max=null)
|
||||
{
|
||||
$this->min = $min;
|
||||
$this->max = $max;
|
||||
$paramValidator = Validator::oneOf(
|
||||
Validator::numeric(), Validator::nullValue()
|
||||
);
|
||||
if (!$paramValidator->validate($min))
|
||||
throw new ComponentException(
|
||||
sprintf('%s is not a valid numeric length', $min)
|
||||
);
|
||||
|
||||
if (!$paramValidator->validate($max))
|
||||
throw new ComponentException(
|
||||
sprintf('%s is not a valid numeric length', $max)
|
||||
);
|
||||
|
||||
if (!is_null($min) && !is_null($max) && $min > $max) {
|
||||
throw new ComponentException(
|
||||
sprintf('%s cannot be less than %s for validation', $this->min,
|
||||
$this->max)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function validateMin($input)
|
||||
{
|
||||
return is_null($this->min) || $input >= $this->min;
|
||||
}
|
||||
|
||||
public function validateMax($input)
|
||||
{
|
||||
return is_null($this->max) || $input <= $this->max;
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
return Validator::numeric($input)->validate($input)
|
||||
&& $this->validateMin($input)
|
||||
&& $this->validateMax($input);
|
||||
}
|
||||
|
||||
public function assert($input)
|
||||
{
|
||||
$validNumeric = new Numeric();
|
||||
$validNumeric->assert($input);
|
||||
$validMin = $this->validateMin($input);
|
||||
$validMax = $this->validateMax($input);
|
||||
if (!$validMin || !$validMax)
|
||||
throw new NumberOutOfBoundsException(
|
||||
$input,
|
||||
$validMin,
|
||||
$validMax,
|
||||
$this->min,
|
||||
$this->max
|
||||
);
|
||||
return true;
|
||||
}
|
||||
|
||||
public function check($input)
|
||||
{
|
||||
return $this->assert($input);
|
||||
}
|
||||
|
||||
}
|
|
@ -3,7 +3,7 @@
|
|||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Rules\AbstractRule;
|
||||
use Respect\Validation\Exceptions\NotObjectException;
|
||||
use Respect\Validation\Exceptions\ObjectException;
|
||||
|
||||
class Object extends AbstractRule
|
||||
{
|
||||
|
@ -16,7 +16,7 @@ class Object extends AbstractRule
|
|||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input))
|
||||
throw new NotObjectException($input);
|
||||
throw new ObjectException($input);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -47,7 +47,7 @@ class AbstractCompositeTest extends \PHPUnit_Framework_TestCase
|
|||
public function testBuildValidators()
|
||||
{
|
||||
$this->object->addRules(array(
|
||||
'noWhitespace', 'StringNotEmpty', 'Alnum' => array('__')
|
||||
'noWhitespace', 'NotEmpty', 'Alnum' => array('__')
|
||||
));
|
||||
}
|
||||
|
||||
|
|
|
@ -16,7 +16,7 @@ class AlnumTest extends \PHPUnit_Framework_TestCase
|
|||
|
||||
/**
|
||||
* @dataProvider providerForInvalidAlnum
|
||||
* @expectedException Respect\Validation\Exceptions\NotAlphanumericException
|
||||
* @expectedException Respect\Validation\Exceptions\AlnumException
|
||||
*/
|
||||
public function testAlnumInvalid($invalidAlnum, $aditional)
|
||||
{
|
||||
|
|
|
@ -16,7 +16,7 @@ class AlphaTest extends \PHPUnit_Framework_TestCase
|
|||
|
||||
/**
|
||||
* @dataProvider providerForInvalidAlpha
|
||||
* @expectedException Respect\Validation\Exceptions\NotAlphaException
|
||||
* @expectedException Respect\Validation\Exceptions\AlphaException
|
||||
*/
|
||||
public function testAlphaInvalid($invalidAlpha, $aditional)
|
||||
{
|
||||
|
|
|
@ -23,7 +23,7 @@ class ArrTest extends \PHPUnit_Framework_TestCase
|
|||
|
||||
/**
|
||||
* @dataProvider providerForNotArr
|
||||
* @expectedException Respect\Validation\Exceptions\NotArrayException
|
||||
* @expectedException Respect\Validation\Exceptions\ArrException
|
||||
*/
|
||||
public function testNotArr($input)
|
||||
{
|
||||
|
|
|
@ -70,7 +70,7 @@ class BetweenTest extends \PHPUnit_Framework_TestCase
|
|||
|
||||
/**
|
||||
* @dataProvider providerInvalid
|
||||
* @expectedException Respect\Validation\Exceptions\NotBetweenException
|
||||
* @expectedException Respect\Validation\Exceptions\BetweenException
|
||||
*/
|
||||
public function testNotBetweenBounds($min, $max, $input, $type)
|
||||
{
|
||||
|
|
|
@ -1,72 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use DateTime;
|
||||
|
||||
class DateBetweenTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
/**
|
||||
* @dataProvider providerForValidDates
|
||||
*/
|
||||
public function testBetweenValid($input, $min, $max)
|
||||
{
|
||||
$between = new DateBetween($min, $max);
|
||||
$this->assertTrue($between->validate($input));
|
||||
$this->assertTrue($between->assert($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForInvalidDates
|
||||
* @expectedException Respect\Validation\Exceptions\DateOutOfBoundsException
|
||||
*/
|
||||
public function testBetweenInvalid($input, $min, $max)
|
||||
{
|
||||
|
||||
$between = new DateBetween($min, $max);
|
||||
$this->assertFalse($between->validate($input));
|
||||
$this->assertFalse($between->assert($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForInvalidConfigs
|
||||
* @expectedException Respect\Validation\Exceptions\ComponentException
|
||||
*/
|
||||
public function testInvalidConfigs($min, $max)
|
||||
{
|
||||
|
||||
$between = new DateBetween($min, $max);
|
||||
}
|
||||
|
||||
public function providerForValidDates()
|
||||
{
|
||||
return array(
|
||||
array('now', 'yesterday', 'tomorrow'),
|
||||
array('now', 'now', 'tomorrow'),
|
||||
array('now', 'yesterday', 'now'),
|
||||
array('now', 'now', 'now'),
|
||||
array(new DateTime(), 'yesterday', 'tomorrow'),
|
||||
);
|
||||
}
|
||||
|
||||
public function providerForInvalidDates()
|
||||
{
|
||||
return array(
|
||||
array('now', 'next month', 'next year'),
|
||||
);
|
||||
}
|
||||
|
||||
public function providerForInvalidConfigs()
|
||||
{
|
||||
|
||||
return array(
|
||||
array('tomorrow', 'yesterday'),
|
||||
array(new \stdClass, 'now'),
|
||||
array(array(), 'now'),
|
||||
array('now', array()),
|
||||
array(array(), new \stdClass),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -35,7 +35,7 @@ class DateTest extends \PHPUnit_Framework_TestCase
|
|||
}
|
||||
|
||||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\InvalidDate
|
||||
* @expectedException Respect\Validation\Exceptions\DateException
|
||||
*/
|
||||
public function testDateFormat()
|
||||
{
|
||||
|
@ -44,7 +44,7 @@ class DateTest extends \PHPUnit_Framework_TestCase
|
|||
}
|
||||
|
||||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\InvalidDate
|
||||
* @expectedException Respect\Validation\Exceptions\DateException
|
||||
*/
|
||||
public function testInvalidDateFormat()
|
||||
{
|
||||
|
|
|
@ -23,7 +23,7 @@ class DigitsTest extends \PHPUnit_Framework_TestCase
|
|||
|
||||
/**
|
||||
* @dataProvider providerForNotDigits
|
||||
* @expectedException Respect\Validation\Exceptions\NotDigitsException
|
||||
* @expectedException Respect\Validation\Exceptions\DigitsException
|
||||
*/
|
||||
public function testNotDigits($input)
|
||||
{
|
|
@ -23,7 +23,7 @@ class FloatTest extends \PHPUnit_Framework_TestCase
|
|||
|
||||
/**
|
||||
* @dataProvider providerForNotFloat
|
||||
* @expectedException Respect\Validation\Exceptions\NotFloatException
|
||||
* @expectedException Respect\Validation\Exceptions\FloatException
|
||||
*/
|
||||
public function testNotFloat($input)
|
||||
{
|
||||
|
|
|
@ -14,7 +14,7 @@ class HasAttributeTest extends \PHPUnit_Framework_TestCase
|
|||
}
|
||||
|
||||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\AttributeNotPresentException
|
||||
* @expectedException Respect\Validation\Exceptions\HasAttributeException
|
||||
*/
|
||||
public function testNotNull()
|
||||
{
|
||||
|
|
|
@ -14,7 +14,7 @@ class HasKeyTest extends \PHPUnit_Framework_TestCase
|
|||
}
|
||||
|
||||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\KeyNotPresentException
|
||||
* @expectedException Respect\Validation\Exceptions\HasKeyException
|
||||
*/
|
||||
public function testNotNull()
|
||||
{
|
||||
|
|
|
@ -23,7 +23,7 @@ class HexaTest extends \PHPUnit_Framework_TestCase
|
|||
|
||||
/**
|
||||
* @dataProvider providerForNotHexa
|
||||
* @expectedException Respect\Validation\Exceptions\NotHexadecimalException
|
||||
* @expectedException Respect\Validation\Exceptions\HexaException
|
||||
*/
|
||||
public function testNotHexa($input)
|
||||
{
|
||||
|
|
|
@ -18,7 +18,7 @@ class InstanceTest extends \PHPUnit_Framework_TestCase
|
|||
}
|
||||
|
||||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\NotInstanceException
|
||||
* @expectedException Respect\Validation\Exceptions\InstanceException
|
||||
*/
|
||||
public function testNotInstance()
|
||||
{
|
||||
|
|
|
@ -24,7 +24,7 @@ class IpTest extends \PHPUnit_Framework_TestCase
|
|||
|
||||
/**
|
||||
* @dataProvider providerForNotIp
|
||||
* @expectedException Respect\Validation\Exceptions\InvalidIpException
|
||||
* @expectedException Respect\Validation\Exceptions\IpException
|
||||
*/
|
||||
public function testNotIp($input, $options=null)
|
||||
{
|
||||
|
|
|
@ -18,7 +18,7 @@ class NoWhitespaceTest extends \PHPUnit_Framework_TestCase
|
|||
}
|
||||
|
||||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\WhitespaceFoundException
|
||||
* @expectedException Respect\Validation\Exceptions\NoWhitespaceException
|
||||
*/
|
||||
public function testWhitespace()
|
||||
{
|
||||
|
|
|
@ -2,14 +2,14 @@
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class StringNotEmptyTest extends \PHPUnit_Framework_TestCase
|
||||
class NotEmptyTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
protected $object;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->object = new StringNotEmpty;
|
||||
$this->object = new NotEmpty;
|
||||
}
|
||||
|
||||
public function testStringNotEmpty()
|
||||
|
@ -18,7 +18,7 @@ class StringNotEmptyTest extends \PHPUnit_Framework_TestCase
|
|||
}
|
||||
|
||||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\EmptyStringException
|
||||
* @expectedException Respect\Validation\Exceptions\NotEmptyException
|
||||
*/
|
||||
public function testStringEmpty()
|
||||
{
|
|
@ -18,7 +18,7 @@ class NullValueTest extends \PHPUnit_Framework_TestCase
|
|||
}
|
||||
|
||||
/**
|
||||
* @expectedException Respect\Validation\Exceptions\NotNullException
|
||||
* @expectedException Respect\Validation\Exceptions\NullValueException
|
||||
*/
|
||||
public function testNotNull()
|
||||
{
|
||||
|
|
|
@ -1,85 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class NumericBetweenTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
public function providerValid()
|
||||
{
|
||||
return array(
|
||||
array(0, 1, 0),
|
||||
array(0, 1, 1),
|
||||
array(10, 20, 15),
|
||||
array(10, 20, 20),
|
||||
array(-10, 20, -5),
|
||||
array(-10, 20, 0),
|
||||
);
|
||||
}
|
||||
|
||||
public function providerInvalid()
|
||||
{
|
||||
return array(
|
||||
array(0, 1, 2),
|
||||
array(0, 1, -1),
|
||||
array(10, 20, 999),
|
||||
array(-10, 20, -11),
|
||||
);
|
||||
}
|
||||
|
||||
public function providerInvalidComponentParameters()
|
||||
{
|
||||
return array(
|
||||
array('a', 1, 1),
|
||||
array(0, ' ', 1),
|
||||
);
|
||||
}
|
||||
|
||||
public function providerNotNumeric()
|
||||
{
|
||||
return array(
|
||||
array(10, 20, 'zas a'),
|
||||
array(-10, 20, ' ')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerValid
|
||||
*/
|
||||
public function testNumericBetweenBounds($min, $max, $input)
|
||||
{
|
||||
$o = new NumericBetween($min, $max);
|
||||
$this->assertTrue($o->assert($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerInvalid
|
||||
* @expectedException Respect\Validation\Exceptions\NumberOutOfBoundsException
|
||||
*/
|
||||
public function testNumericNotBetweenBounds($min, $max, $input)
|
||||
{
|
||||
$o = new NumericBetween($min, $max);
|
||||
$this->assertTrue($o->assert($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerInvalidComponentParameters
|
||||
* @expectedException Respect\Validation\Exceptions\ComponentException
|
||||
*/
|
||||
public function testComponentParameters($min, $max, $input)
|
||||
{
|
||||
$o = new NumericBetween($min, $max);
|
||||
$this->assertTrue($o->assert($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerNotNumeric
|
||||
* @expectedException Respect\Validation\Exceptions\NotNumericException
|
||||
*/
|
||||
public function testNotNumeric($min, $max, $input)
|
||||
{
|
||||
$o = new NumericBetween($min, $max);
|
||||
$this->assertTrue($o->assert($input));
|
||||
}
|
||||
|
||||
}
|
|
@ -23,7 +23,7 @@ class NumericTest extends \PHPUnit_Framework_TestCase
|
|||
|
||||
/**
|
||||
* @dataProvider providerForNotNumeric
|
||||
* @expectedException Respect\Validation\Exceptions\NotNumericException
|
||||
* @expectedException Respect\Validation\Exceptions\NumericException
|
||||
*/
|
||||
public function testNotNumeric($input)
|
||||
{
|
||||
|
|
|
@ -23,7 +23,7 @@ class ObjectTest extends \PHPUnit_Framework_TestCase
|
|||
|
||||
/**
|
||||
* @dataProvider providerForNotObject
|
||||
* @expectedException Respect\Validation\Exceptions\NotObjectException
|
||||
* @expectedException Respect\Validation\Exceptions\ObjectException
|
||||
*/
|
||||
public function testNotObject($input)
|
||||
{
|
||||
|
|
|
@ -7,44 +7,43 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
|
||||
public function testValidateSimple()
|
||||
{
|
||||
$v = Validator::stringNotEmpty()->validate('foo');
|
||||
$v = Validator::notEmpty()->validate('foo');
|
||||
$this->assertTrue($v);
|
||||
}
|
||||
|
||||
public function testValidateArguments()
|
||||
{
|
||||
$v = Validator::dateBetween('yesterday', 'tomorrow')->validate('now');
|
||||
$v = Validator::between(10, 20)->validate(15);
|
||||
$this->assertTrue($v);
|
||||
}
|
||||
|
||||
public function testValidateFluent()
|
||||
{
|
||||
$v = Validator::dateBetween('yesterday', 'tomorrow')->validate('now');
|
||||
$v = Validator::between(10, 20)->validate(15);
|
||||
$this->assertTrue($v);
|
||||
}
|
||||
|
||||
public function testValidateFluentChain()
|
||||
{
|
||||
$v = Validator::dateBetween('yesterday', 'tomorrow')->stringNotEmpty()
|
||||
->assert('now');
|
||||
$v = Validator::between(10, 20)->notEmpty()
|
||||
->assert(15);
|
||||
$this->assertTrue($v);
|
||||
}
|
||||
|
||||
public function testValidatorComposite()
|
||||
{
|
||||
$v = Validator::oneOf(
|
||||
Validator::stringNotEmpty(),
|
||||
Validator::dateBetween('+2 years', '+3 years')
|
||||
)->validate('now');
|
||||
Validator::notEmpty(), Validator::between(10, 20)
|
||||
)->validate(15);
|
||||
$this->assertTrue($v);
|
||||
}
|
||||
|
||||
public function testValidatorCompositeTwitterUsername()
|
||||
{
|
||||
$v = Validator::alnum('_')
|
||||
->noWhitespace()
|
||||
->stringLength(1, 15)
|
||||
->assert('alganet');
|
||||
->noWhitespace()
|
||||
->stringLength(1, 15)
|
||||
->assert('alganet');
|
||||
$this->assertTrue($v);
|
||||
}
|
||||
|
||||
|
@ -54,9 +53,9 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
public function testValidatorCompositeTwitterUsernameInvalid()
|
||||
{
|
||||
$v = Validator::alnum('_')
|
||||
->noWhitespace()
|
||||
->stringLength(1, 15)
|
||||
->assert('#$% #odjfubgihdbfgihbdfighb');
|
||||
->noWhitespace()
|
||||
->stringLength(1, 15)
|
||||
->assert('#$% #odjfubgihdbfgihbdfighb');
|
||||
$this->assertTrue($v);
|
||||
}
|
||||
|
||||
|
@ -69,15 +68,15 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
|
|||
$target->name = 'Alexandre';
|
||||
|
||||
$validator = Validator::object()
|
||||
->oneOf(
|
||||
Validator::hasAttribute('screen_name',
|
||||
Validator::alnum('_')->noWhitespace()),
|
||||
Validator::hasAttribute('id', Validator::numeric())
|
||||
)
|
||||
->hasAttribute('created_at', Validator::date())
|
||||
->hasAttribute('name', $v160 = Validator::stringLength(1, 160))
|
||||
->hasOptionalAttribute('description', $v160)
|
||||
->hasOptionalAttribute('location', $v160);
|
||||
->oneOf(
|
||||
Validator::hasAttribute('screen_name',
|
||||
Validator::alnum('_')->noWhitespace()),
|
||||
Validator::hasAttribute('id', Validator::numeric())
|
||||
)
|
||||
->hasAttribute('created_at', Validator::date())
|
||||
->hasAttribute('name', $v160 = Validator::stringLength(1, 160))
|
||||
->hasOptionalAttribute('description', $v160)
|
||||
->hasOptionalAttribute('location', $v160);
|
||||
try {
|
||||
$validator->assert($target);
|
||||
} catch (InvalidException $e) {
|
||||
|
|
Loading…
Reference in a new issue