Very unstable commit. Lot of changes to the exception model.

This commit is contained in:
Alexandre Gomes Gaigalas 2010-12-02 19:04:10 -02:00
parent 1c15766fcd
commit 42999f796a
93 changed files with 641 additions and 737 deletions

View file

@ -0,0 +1,20 @@
<?php
namespace Respect\Validation\Exceptions;
class AllOfException extends ValidationException
{
const INVALID_ALLOF= 'AllOf_1';
public static $defaultTemplates = array(
self::INVALID_ALLOF => '%d of the %d required rules did not passed',
);
protected function renderMessage()
{
if (1 === count($this->related))
$this->message = array_shift($this->related)->getMessage();
else
parent::renderMessage();
}
}

View file

@ -2,24 +2,13 @@
namespace Respect\Validation\Exceptions;
class AlnumException extends InvalidException
class AlnumException extends ValidationException
{
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")'
const INVALID_ALNUM = 'Alnum_1';
const INVALID_ALNUM_CHARS = 'Alnum_2';
public static $defaultTemplates = array(
self::INVALID_ALNUM => '"%s" does not contain only letters and digits',
self::INVALID_ALNUM_CHARS => '"%s" does not contain only letters, digits and "%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
)
);
}
}

View file

@ -2,24 +2,13 @@
namespace Respect\Validation\Exceptions;
class AlphaException extends InvalidException
class AlphaException extends ValidationException
{
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")'
const INVALID_ALPHA = 'Alpha_1';
const INVALID_ALPHA_CHARS = 'Alpha_2';
public static $defaultTemplates = array(
self::INVALID_ALPHA => '"%s" does not contain only letters',
self::INVALID_ALPHA_CHARS => '"%s" does not contain only letters and "%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
)
);
}
}

View file

@ -1,22 +0,0 @@
<?php
namespace Respect\Validation\Exceptions;
class ArrException extends InvalidException
{
const MSG_NOT_ARRAY = 'Array_1';
protected $messageTemplates = array(
self::MSG_NOT_ARRAY => '%s is not an array'
);
public function __construct($input)
{
parent::__construct(
sprintf(
$this->getMessageTemplate(self::MSG_NOT_ARRAY),
$this->getStringRepresentation($input)
)
);
}
}

View file

@ -0,0 +1,12 @@
<?php
namespace Respect\Validation\Exceptions;
class AtLeastException extends ValidationException
{
const INVALID_ATLEAST = 'AtLeast_1';
public static $defaultTemplates = array(
self::INVALID_ATLEAST => '%d of the %d mandatory rules are invalid',
);
}

View file

@ -2,42 +2,15 @@
namespace Respect\Validation\Exceptions;
class BetweenException extends InvalidException
class BetweenException extends ValidationException
{
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',
const INVALID_LESS= 'Between_1';
const INVALID_MORE= 'Between_2';
const INVALID_BOUNDS= 'Between_3';
public static $defaultTemplates = array(
self::INVALID_LESS => '"%s" is less than "%s"',
self::INVALID_MORE => '"%s" is more than "%s"',
self::INVALID_BOUNDS => '"%s" is out of bounds',
);
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]);
}
}
}

View file

@ -2,26 +2,11 @@
namespace Respect\Validation\Exceptions;
use Exception;
class CallbackException extends InvalidException
class CallbackException extends ValidationException
{
const MSG_CALLBACK = 'Callback_1';
protected $messageTemplates = array(
self::MSG_CALLBACK => '%s does not validate against the provided callback %s.'
const INVALID_CALLBACK= 'Callback_1';
public static $defaultTemplates = array(
self::INVALID_CALLBACK => '"%s" is invalid',
);
public function __construct($input, $callback)
{
parent::__construct(
$this->getMessageTemplate(
sprintf(
self::MSG_CALLBACK,
$this->getStringRepresentation($input),
$this->getStringRepresentation($callback)
)
)
);
}
}

View file

@ -2,9 +2,9 @@
namespace Respect\Validation\Exceptions;
use InvalidArgumentException;
use \Exception;
class ComponentException extends InvalidArgumentException
class ComponentException extends Exception
{
}

View file

@ -2,23 +2,11 @@
namespace Respect\Validation\Exceptions;
class DateException extends InvalidException
class DateException extends ValidationException
{
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',
const INVALID_DATE= 'Date_1';
public static $defaultTemplates = array(
self::INVALID_DATE => '"%s" is not a valid date (format: %s)',
);
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
)
);
}
}

View file

@ -2,18 +2,11 @@
namespace Respect\Validation\Exceptions;
class DigitsException extends InvalidException
class DigitsException extends ValidationException
{
const MSG_NOT_DIGITS = 'Digits_1';
protected $messageTemplates = array(
self::MSG_NOT_DIGITS => '%s does not contain only digits'
const INVALID_DIGITS= 'Digits_1';
public static $defaultTemplates = array(
self::INVALID_DIGITS => '"%s" does not contain only digits',
);
public function __construct($input)
{
parent::__construct(
sprintf($this->getMessageTemplate(self::MSG_NOT_DIGITS), $input)
);
}
}

View file

@ -2,18 +2,11 @@
namespace Respect\Validation\Exceptions;
class FloatException extends InvalidException
class FloatException extends ValidationException
{
const MSG_NOT_FLOAT = 'Float_1';
protected $messageTemplates = array(
self::MSG_NOT_FLOAT => '%s is not a valid float number'
const INVALID_= 'Float_1';
public static $defaultTemplates = array(
self::INVALID_ => '"%s" is not a valid float',
);
public function __construct($input)
{
parent::__construct(
sprintf($this->getMessageTemplate(self::MSG_NOT_FLOAT), $input)
);
}
}

View file

@ -2,23 +2,13 @@
namespace Respect\Validation\Exceptions;
use InvalidArgumentException;
class HasAttributeException extends InvalidException
class HasAttributeException extends ValidationException
{
const MSG_HAS_ATTRIBUTE = 'HasAttribute_1';
protected $messageTemplates = array(
self::MSG_HAS_ATTRIBUTE => 'Object %s does not have the attribute %s'
const INVALID_HAS_ATTRIBUTE= 'HasAttribute_1';
const INVALID_HAS_ATTRIBUTE_RELATED = 'HasAttribute_2';
public static $defaultTemplates = array(
self::INVALID_HAS_ATTRIBUTE => '"%s" is not present',
self::INVALID_HAS_ATTRIBUTE_RELATED => '"%s" is invalid',
);
public function __construct($input, $attributeName)
{
parent::__construct(
sprintf(
$this->getMessageTemplate(self::MSG_HAS_ATTRIBUTE),
$this->getStringRepresentation($input), $attributeName
)
);
}
}

View file

@ -2,21 +2,13 @@
namespace Respect\Validation\Exceptions;
class HasKeyException extends InvalidException
class HasKeyException extends ValidationException
{
const MSG_KEY_NOT_PRESENT = 'HasKey_1';
protected $messageTemplates = array(
self::MSG_KEY_NOT_PRESENT => 'Array %s does not have the key %s'
const INVALID_HAS_KEY= 'HasKey_1';
const INVALID_HAS_KEY_RELATED = 'HasKey_2';
public static $defaultTemplates = array(
self::INVALID_HAS_KEY => '"%s" is not present',
self::INVALID_HAS_KEY_RELATED => '"%s" is invalid',
);
public function __construct($input, $keyName)
{
parent::__construct(
sprintf(
$this->getMessageTemplate(self::MSG_KEY_NOT_PRESENT),
$this->getStringRepresentation($input), $keyName
)
);
}
}

View file

@ -0,0 +1,14 @@
<?php
namespace Respect\Validation\Exceptions;
class HasOptionalAttributeException extends ValidationException
{
const INVALID_HAS_ATTRIBUTE= 'HasAttribute_1';
const INVALID_HAS_ATTRIBUTE_RELATED = 'HasAttribute_2';
public static $defaultTemplates = array(
self::INVALID_HAS_ATTRIBUTE => '"%s" is not present',
self::INVALID_HAS_ATTRIBUTE_RELATED => '"%s" is invalid',
);
}

View file

@ -2,20 +2,11 @@
namespace Respect\Validation\Exceptions;
class HexaException extends InvalidException
class HexaException extends ValidationException
{
const MSG_NOT_HEXADECIMAL = 'Hexa_1';
protected $messageTemplates = array(
self::MSG_NOT_HEXADECIMAL => '%s is not a valid hexadecimal number'
const INVALID_HEXA= 'Hexa_1';
public static $defaultTemplates = array(
self::INVALID_HEXA => '"%s" is not a valid hexadecimal number',
);
public function __construct($input)
{
parent::__construct(
sprintf(
$this->getMessageTemplate(self::MSG_NOT_HEXADECIMAL), $input
)
);
}
}

View file

@ -2,22 +2,11 @@
namespace Respect\Validation\Exceptions;
class InstanceException extends InvalidException
class InstanceException extends ValidationException
{
const MSG_NOT_INSTANCE = 'Instance_1';
protected $messageTemplates = array(
self::MSG_NOT_INSTANCE => '%s is not an instance %s'
const INVALID_INSTANCE= 'Instance_1';
public static $defaultTemplates = array(
self::INVALID_INSTANCE => '"%s" is not an instance "%s"',
);
public function __construct($input, $instance)
{
parent::__construct(
sprintf(
$this->getMessageTemplate(self::MSG_NOT_INSTANCE),
$this->getStringRepresentation($input),
$this->getStringRepresentation($instance)
)
);
}
}

View file

@ -1,71 +0,0 @@
<?php
namespace Respect\Validation\Exceptions;
use Exception;
use InvalidArgumentException;
class InvalidException extends InvalidArgumentException
{
protected $exceptions = array();
public function __construct($spec)
{
if (is_string($spec)) {
$message = $spec;
} elseif (is_array($spec)) {
$messages = array();
foreach ($spec as $m) {
if ($m instanceof Exception) {
$messages[] = $m->getMessage();
$this->addException($m);
}
}
$message = implode(PHP_EOL, $messages);
}
parent::__construct($message);
}
protected function addException($e)
{
$this->exceptions[] = $e;
}
public function getExceptions()
{
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);
}
}

View file

@ -2,21 +2,11 @@
namespace Respect\Validation\Exceptions;
class IpException extends InvalidException
class IpException extends ValidationException
{
const MSG_NOT_IP = 'Ip_1';
public $options;
protected $messageTemplates = array(
self::MSG_NOT_IP => '%s is not a valid IP address'
const INVALID_IP= 'Ip_1';
public static $defaultTemplates = array(
self::INVALID_IP => '"%s" is not a valid IP address',
);
public function __construct($input)
{
parent::__construct(
sprintf(
$this->getMessageTemplate(self::MSG_NOT_IP), $input
)
);
}
}

View file

@ -0,0 +1,12 @@
<?php
namespace Respect\Validation\Exceptions;
class MostOfException extends ValidationException
{
const INVALID_MOST= 'Most_1';
public static $defaultTemplates = array(
self::INVALID_MOST => '%d of the %d required rules did not passed',
);
}

View file

@ -2,19 +2,11 @@
namespace Respect\Validation\Exceptions;
class NoWhitespaceException extends InvalidException
class NoWhitespaceException extends ValidationException
{
const MSG_WHITESPACE_FOUND = 'NoWhitespace_1';
protected $messageTemplates = array(
self::MSG_WHITESPACE_FOUND => '%s contains spaces, tabs, line breaks or other not allowed charaters.'
const INVALID_NO_WHITESPACE= 'NoWhitespace_1';
public static $defaultTemplates = array(
self::INVALID_NO_WHITESPACE => '"%s" contains whitespace',
);
public function __construct($input)
{
parent::__construct(
sprintf($this->getMessageTemplate(self::MSG_WHITESPACE_FOUND),
$input)
);
}
}

View file

@ -0,0 +1,12 @@
<?php
namespace Respect\Validation\Exceptions;
class NoneOfException extends ValidationException
{
const INVALID_NONE= 'None_1';
public static $defaultTemplates = array(
self::INVALID_NONE => 'None of the %n rules must pass. % of them passed.',
);
}

View file

@ -2,17 +2,11 @@
namespace Respect\Validation\Exceptions;
class NotEmptyException extends InvalidException
class NotEmptyException extends ValidationException
{
const MSG_NOT_EMPTY = 'NotEmpty_1';
protected $messageTemplates = array(
self::MSG_NOT_EMPTY => 'You provided an empty value'
const INVALID_NOT_EMPTY= 'NotEmpty_1';
public static $defaultTemplates = array(
self::INVALID_NOT_EMPTY => 'The provided value is empty',
);
public function __construct()
{
parent::__construct($this->getMessageTemplate(self::MSG_NOT_EMPTY));
}
}

View file

@ -2,18 +2,11 @@
namespace Respect\Validation\Exceptions;
class NullValueException extends InvalidException
class NullValueException extends ValidationException
{
const MSG_NOT_NULL = 'NullValue_1';
protected $messageTemplates = array(
self::MSG_NOT_NULL => '%s is not null'
const INVALID_NULL_VALUE= 'NullValue_1';
public static $defaultTemplates = array(
self::INVALID_NULL_VALUE => '"%s" is not a null value',
);
public function __construct($input)
{
parent::__construct(
sprintf($this->getMessageTemplate(self::MSG_NOT_NULL), $input)
);
}
}

View file

@ -2,19 +2,11 @@
namespace Respect\Validation\Exceptions;
class NumericException extends InvalidException
class NumericException extends ValidationException
{
const MSG_NOT_NUMERIC = 'Numeric_1';
protected $messageTemplates = array(
self::MSG_NOT_NUMERIC => '%s is not a numeric value'
const INVALID_NUMERIC= 'Numeric_1';
public static $defaultTemplates = array(
self::INVALID_NUMERIC => '"%s" is not a numeric value',
);
public function __construct($input)
{
parent::__construct(
sprintf($this->getMessageTemplate(self::MSG_NOT_NUMERIC),
$input)
);
}
}

View file

@ -2,21 +2,11 @@
namespace Respect\Validation\Exceptions;
class ObjectException extends InvalidException
class ObjectException extends ValidationException
{
const MSG_NOT_OBJECT = 'Object_1';
protected $messageTemplates = array(
self::MSG_NOT_OBJECT => '%s is not an object'
const INVALID_OBJECT= 'Object_1';
public static $defaultTemplates = array(
self::INVALID_OBJECT => '"%s" is not an object',
);
public function __construct($input)
{
parent::__construct(
sprintf(
$this->getMessageTemplate(self::MSG_NOT_OBJECT),
$this->getStringRepresentation($input)
)
);
}
}

View file

@ -0,0 +1,12 @@
<?php
namespace Respect\Validation\Exceptions;
class OneOfException extends ValidationException
{
const INVALID_ONE_OF= 'OneOf_1';
public static $defaultTemplates = array(
self::INVALID_ONE_OF => 'None of the %d rules passed',
);
}

View file

@ -2,23 +2,11 @@
namespace Respect\Validation\Exceptions;
use Exception;
class RegexException extends InvalidException
class RegexException extends ValidationException
{
const MSG_REGEX = 'Regex_1';
protected $messageTemplates = array(
self::MSG_REGEX => '%s does not validate against the provided regular expression: %s.'
const INVALID_REGEX= 'Regex_1';
public static $defaultTemplates = array(
self::INVALID_REGEX => '"%s" did not validated against the "%s" expression',
);
public function __construct($input, $regex)
{
parent::__construct(
sprintf(
$this->getMessageTemplate(self::MSG_REGEX),
$this->getStringRepresentation($input), $regex
)
);
}
}

View file

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

View file

@ -2,39 +2,13 @@
namespace Respect\Validation\Exceptions;
class StringLengthException extends InvalidException
class StringLengthException extends ValidationException
{
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'
const INVALID_LESS= 'StringLength_1';
const INVALID_MORE= 'StringLength_2';
public static $defaultTemplates = array(
self::INVALID_LESS => '"%s" is shorter than the specified minimum of %n characters',
self::INVALID_MORE => '"%s" is longer than the specified minimum of %n 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]);
}
}
}

View file

@ -1,22 +0,0 @@
<?php
namespace Respect\Validation\Exceptions;
class TraversableException extends InvalidException
{
const MSG_NOT_TRAVERSABLE = 'Traversable_1';
protected $messageTemplates = array(
self::MSG_NOT_TRAVERSABLE => '%s is not traversable'
);
public function __construct($input)
{
parent::__construct(
sprintf(
$this->getMessageTemplate(self::MSG_NOT_TRAVERSABLE),
$this->getStringRepresentation($input)
)
);
}
}

View file

@ -0,0 +1,100 @@
<?php
namespace Respect\Validation\Exceptions;
use \InvalidArgumentException;
class ValidationException extends InvalidArgumentException
{
const INVALID = 'Validation_1';
public static $defaultTemplates = array(
self::INVALID => 'Data validation failed'
);
protected $messageTemplate = 'Data validation failed';
protected $related = array();
protected $params = array();
public function __construct($code=null)
{
if (!is_null($code))
$this->setMessageTemplateFromCode($code);
}
public function getMessageTemplate()
{
return $this->messageTemplate;
}
public function setMessageTemplate($messageTemplate)
{
$this->messageTemplate = $messageTemplate;
return $this;
}
public function setMessageTemplateFromCode($code)
{
$this->setMessageTemplate($this->getTemplate($code));
return $this;
}
public function getTemplate($code)
{
return @static::$defaultTemplates[$code];
}
public function setParams()
{
$messageParameters = func_get_args();
foreach ($messageParameters as &$par)
$par = static::stringify($par);
$this->params = $messageParameters;
$this->renderMessage();
return $this;
}
public function setRelated(array $relatedExceptions)
{
foreach ($relatedExceptions as $e)
$this->addRelated($e, false);
$this->renderMessage();
return $this;
}
public function getRelated()
{
return $this->related;
}
public function addRelated(ValidationException $relatedException,
$render=true)
{
$this->related[] = $relatedException;
if ($render)
$this->renderMessage();
return $this;
}
protected function renderMessage()
{
$relatedMessages = array();
$params = $this->params;
array_unshift($params, $this->messageTemplate);
$this->message = @call_user_func_array('sprintf', $params);
foreach ($this->related as $n => $related) {
$relatedMessage = "-" . $related->getMessage();
$relatedMessage = str_replace("\n", "\n ", $relatedMessage);
$relatedMessages[] = $relatedMessage;
}
if (!empty($relatedMessages))
$this->message .= "\n" . implode("\n", $relatedMessages);
}
protected static function stringify($mixed)
{
if (is_object($mixed))
return get_class($mixed);
else
return strval($mixed);
}
}

View file

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

View file

@ -6,7 +6,7 @@ use Respect\Validation\Validatable;
use Respect\Validation\Validator;
use Respect\Validation\Rules\AbstractRule;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Exceptions\InvalidException;
use Respect\Validation\Exceptions\ValidationException;
use Exception;
abstract class AbstractComposite extends AbstractRule
@ -83,7 +83,7 @@ abstract class AbstractComposite extends AbstractRule
foreach ($validators as $v)
try {
$v->assert($input);
} catch (InvalidException $e) {
} catch (ValidationException $e) {
$exceptions[] = $e;
}
return $exceptions;

View file

@ -3,13 +3,28 @@
namespace Respect\Validation\Rules;
use Respect\Validation\Validatable;
use Respect\Validation\Exceptions\ValidationException;
abstract class AbstractRule implements Validatable
{
protected $exception;
public function __invoke($input)
{
return $this->validate($input);
}
public function createException()
{
return new ValidationException;
}
public function getException()
{
if (is_null($this->exception))
$this->exception = $this->createException();
return $this->exception;
}
}

View file

@ -2,7 +2,7 @@
namespace Respect\Validation\Rules;
use Respect\Validation\Exceptions\InvalidException;
use Respect\Validation\Exceptions\AllOfException;
class AllOf extends AbstractComposite
{
@ -18,18 +18,26 @@ class AllOf extends AbstractComposite
));
}
public function createException()
{
return new AllOfException(AllOfException::INVALID_ALLOF);
}
public function assert($input)
{
$exceptions = $this->validateRules($input);
if (!empty($exceptions))
throw new InvalidException($exceptions);
throw $this
->getException()
->setParams(count($exceptions), count($this->rules))
->setRelated($exceptions);
return true;
}
public function check($input)
{
foreach ($this->getRules() as $v)
$v->assert($input);
$v->check($input);
}
}

View file

@ -20,6 +20,11 @@ class Alnum extends AbstractRule
$this->additionalChars = $additionalChars;
}
public function createException()
{
return new AlnumException;
}
public function validate($input)
{
return is_string($input) && preg_match(
@ -30,8 +35,12 @@ class Alnum extends AbstractRule
public function assert($input)
{
$templateCode = empty($this->additionalChars) ? AlnumException::INVALID_ALNUM : AlnumException::INVALID_ALNUM_CHARS;
if (!$this->validate($input))
throw new AlnumException($input, $this->additionalChars);
throw $this
->getException()
->setMessageTemplateFromCode($templateCode)
->setParams($input, $this->additionalChars);
return true;
}

View file

@ -20,6 +20,11 @@ class Alpha extends AbstractRule
$this->additionalChars = $additionalChars;
}
public function createException()
{
return new AlphaException;
}
public function validate($input)
{
return is_string($input) && preg_match(
@ -30,8 +35,12 @@ class Alpha extends AbstractRule
public function assert($input)
{
$templateCode = empty($this->additionalChars) ? AlphaException::INVALID_ALPHA : AlnumException::INVALID_ALPHA_CHARS;
if (!$this->validate($input))
throw new AlphaException($input, $this->additionalChars);
throw $this
->getException()
->setMessageTemplateFromCode($templateCode)
->setParams($input, $this->additionalChars);
return true;
}

View file

@ -1,21 +0,0 @@
<?php
namespace Respect\Validation\Rules;
use Respect\Validation\Rules\AbstractRule;
use Respect\Validation\Exceptions\ArrException;
class Arr extends Traversable
{
protected function isTraversable($input)
{
return is_array($input);
}
protected function buildException($input)
{
return new ArrException($input);
}
}

View file

@ -2,7 +2,7 @@
namespace Respect\Validation\Rules;
use Respect\Validation\Exceptions\InvalidException;
use Respect\Validation\Exceptions\AtLeastException;
class AtLeast extends AbstractComposite
{
@ -20,19 +20,27 @@ class AtLeast extends AbstractComposite
$validators = $this->getRules();
$exceptions = $this->validateRules($input);
if ($this->howMany > (count($validators) - count($exceptions)))
throw new InvalidException($exceptions);
throw $this
->getException()
->setParams(count($exceptions), $this->howMany)
->setRelated($exceptions);
return true;
}
public function createException()
{
return new AtLeastException(AtLeastException::INVALID_ATLEAST);
}
public function validate($input)
{
$validators = $this->getRules();
$pass = 0;
foreach ($validators as $v) {
try {
$v->assert($input);
$v->check($input);
$pass++;
} catch (InvalidException $e) {
} catch (ValidationException $e) {
}
if ($pass >= $this->howMany)
@ -48,15 +56,18 @@ class AtLeast extends AbstractComposite
$exceptions = array();
foreach ($validators as $v) {
try {
$v->assert($input);
$v->check($input);
$pass++;
} catch (InvalidException $e) {
} catch (ValidationException $e) {
$exceptions[] = $e;
}
if ($pass >= $this->howMany)
return true;
if (count($exceptions) > (count($validators) - $this->howMany))
throw new InvalidException($e);
throw $this
->getException()
->setParams(count($exceptions), $this->howMany)
->setRelated($exceptions);
}
return false;
}

View file

@ -2,12 +2,12 @@
namespace Respect\Validation\Rules;
use \Exception;
use Respect\Validation\Validator;
use Respect\Validation\Rules\AbstractRule;
use Respect\Validation\Exceptions\BetweenException;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Validatable;
use Respect\Validation\Exceptions\ValidationException;
class Between extends AbstractRule
{
@ -29,13 +29,18 @@ class Between extends AbstractRule
try {
$type->assert($min);
$type->assert($max);
} catch (Exception $e) {
} catch (ValidationException $e) {
throw new ComponentException(
$e->getMessage()
);
}
}
public function createException()
{
return new BetweenException;
}
public function validateMin($input)
{
return is_null($this->min) || $input >= $this->min;
@ -57,16 +62,23 @@ class Between extends AbstractRule
{
if (!is_null($this->type))
$this->type->assert($input);
$validMin = $this->validateMin($input);
$validMax = $this->validateMax($input);
if (!$validMin || !$validMax)
throw new BetweenException(
$input,
$validMin,
$validMax,
$this->min,
$this->max
);
$exceptions = array();
if (!$this->validateMin($input))
$exceptions[] = $this
->createException()
->setMessageTemplateFromCode(BetweenException::INVALID_LESS)
->setParams($input, $this->min);
if (!$this->validateMax($input))
$exceptions[] = $this
->createException()
->setMessageTemplateFromCode(BetweenException::INVALID_MORE)
->setParams($input, $this->max);
if (!empty($exceptions))
throw $this
->createException()
->setMessageTemplateFromCode(BetweenException::INVALID_BOUNDS)
->setParams($input)
->setRelated($exceptions);
return true;
}

View file

@ -19,6 +19,11 @@ class Callback extends AbstractRule
);
$this->callback = $callback;
}
public function createException()
{
return new CallbackException;
}
public function validate($input)
{
@ -28,7 +33,9 @@ class Callback extends AbstractRule
public function assert($input)
{
if (!$this->validate($input))
throw new CallbackException($input, $this->callback);
throw $this
->getException()
->setParams($input, $this->callback);
return true;
}

View file

@ -25,11 +25,18 @@ class Date extends AbstractDate
else
return date($this->format, strtotime($input)) == $input;
}
public function createException()
{
return new DateException;
}
public function assert($input)
{
if (!$this->validate($input))
throw new DateException($input, $this->format);
throw $this
->getException()
->setParams($input, $this->format);
return true;
}

View file

@ -8,6 +8,11 @@ use Respect\Validation\Exceptions\DigitsException;
class Digits extends AbstractRule
{
public function createException()
{
return new DigitsException;
}
public function validate($input)
{
return ctype_digit((string) $input);
@ -16,7 +21,9 @@ class Digits extends AbstractRule
public function assert($input)
{
if (!$this->validate($input))
throw new DigitsException($input);
throw $this
->getException()
->setParams($input);
return true;
}

View file

@ -7,6 +7,11 @@ use Respect\Validation\Exceptions\FloatException;
class Float extends AbstractRule
{
public function createException()
{
return new FloatException;
}
public function validate($input)
{
@ -16,7 +21,9 @@ class Float extends AbstractRule
public function assert($input)
{
if (!$this->validate($input))
throw new FloatException($input);
throw $this
->getException()
->setParams($input);
return true;
}

View file

@ -5,6 +5,7 @@ namespace Respect\Validation\Rules;
use Respect\Validation\Exceptions\HasAttributeException;
use Respect\Validation\Rules\AllOf;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Exceptions\ValidationException;
use Respect\Validation\Validatable;
use \ReflectionProperty;
use \ReflectionException;
@ -25,6 +26,11 @@ class HasAttribute extends AllOf
$this->addRule($attributeValidator);
}
public function createException()
{
return new HasAttributeException(HasAttributeException::INVALID_HAS_ATTRIBUTE);
}
protected function getAttributeValue($input)
{
$propertyMirror = new ReflectionProperty($input, $this->attribute);
@ -45,8 +51,20 @@ class HasAttribute extends AllOf
public function assert($input)
{
if (!$this->validate($input))
throw new HasAttributeException($input, $this->attribute);
try {
parent::assert(
$this->getAttributeValue($input)
);
} catch (ReflectionException $e) {
throw $this
->getException()
->setParams($this->attribute);
} catch (ValidationException $e) {
throw $this
->getException()
->setMessageTemplateFromCode(HasAttributeException::INVALID_HAS_ATTRIBUTE_RELATED)
->setParams($this->attribute);
}
return true;
}

View file

@ -23,6 +23,11 @@ class HasKey extends AllOf
if (!is_null($valueValidator))
$this->addRule($valueValidator);
}
public function createException()
{
return new HasKeyException;
}
public function validate($input)
{
@ -33,7 +38,9 @@ class HasKey extends AllOf
public function assert($input)
{
if (!$this->validate($input))
throw new HasKeyException($input, $this->key);
throw $this
->getException()
->setParams($input, $this->key);
return parent::validate(@$input[$this->key]);
}

View file

@ -3,6 +3,7 @@
namespace Respect\Validation\Rules;
use Respect\Validation\Rules\HasAttribute;
use Respect\Validation\Exceptions\HasOptionalAttributeException;
use \ReflectionProperty;
use \ReflectionException;
@ -20,4 +21,25 @@ class HasOptionalAttribute extends HasAttribute
}
}
public function assert($input)
{
try {
parent::assert(
$this->getAttributeValue($input)
);
} catch (ReflectionException $e) {
return true;
} catch (ValidationException $e) {
throw $this
->getException()
->setParams($input, $this->attribute);
}
return true;
}
public function createException()
{
return new HasOptionalAttributeException;
}
}

View file

@ -3,6 +3,7 @@
namespace Respect\Validation\Rules;
use Respect\Validation\Rules\HasKey;
use Respect\Validation\Exceptions\HasOptionalKeyException;
class HasOptionalKey extends HasKey
{
@ -13,4 +14,9 @@ class HasOptionalKey extends HasKey
|| parent::validate($input[$this->key]);
}
public function createException()
{
return new HasOptionalKeyException;
}
}

View file

@ -7,6 +7,10 @@ use Respect\Validation\Exceptions\HexaException;
class Hexa extends AbstractRule
{
public function createException()
{
return new HexaException;
}
public function validate($input)
{
@ -16,7 +20,9 @@ class Hexa extends AbstractRule
public function assert($input)
{
if (!$this->validate($input))
throw new HexaException($input);
throw $this
->getException()
->setParams($input);
return true;
}

View file

@ -10,6 +10,11 @@ class Instance extends AbstractRule
public $instance;
public function createException()
{
return new InstanceException;
}
public function __construct($instance)
{
$this->instance = $instance;
@ -23,7 +28,9 @@ class Instance extends AbstractRule
public function assert($input)
{
if (!$this->validate($input))
throw new InstanceException($input, $this->instance);
throw $this
->getException()
->setParams($input, $this->instance);
return true;
}

View file

@ -13,6 +13,11 @@ class Ip extends AbstractRule
$this->options = $options;
}
public function createException()
{
return new IpException;
}
public function validate($input)
{
return filter_var(
@ -23,7 +28,9 @@ class Ip extends AbstractRule
public function assert($input)
{
if (!$this->validate($input))
throw new IpException($input);
throw $this
->getException()
->setParams($input);
return true;
}

View file

@ -2,7 +2,7 @@
namespace Respect\Validation\Rules;
use Respect\Validation\Exceptions\InvalidException;
use Respect\Validation\Exceptions\MostOfException;
class MostOf extends AtLeast
{
@ -12,5 +12,10 @@ class MostOf extends AtLeast
$this->howMany = ceil(func_num_args() / 2);
$this->addRules(func_get_args());
}
public function createException()
{
return new MostOfException;
}
}

View file

@ -8,6 +8,11 @@ use Respect\Validation\Exceptions\NoWhitespaceException;
class NoWhitespace extends AbstractRule
{
public function createException()
{
return new NoWhitespaceException;
}
public function validate($input)
{
return preg_match('#^\S+$#', $input);
@ -16,7 +21,9 @@ class NoWhitespace extends AbstractRule
public function assert($input)
{
if (!$this->validate($input))
throw new NoWhitespaceException($input);
throw $this
->getException()
->setParams($input);
return true;
}

View file

@ -2,10 +2,14 @@
namespace Respect\Validation\Rules;
use Respect\Validation\Exceptions\InvalidException;
use Respect\Validation\Exceptions\NoneOfException;
class NoneOf extends AbstractComposite
{
public function createException()
{
return new NoneOfException;
}
public function validate($input)
{
@ -22,7 +26,9 @@ class NoneOf extends AbstractComposite
{
$exceptions = $this->validateRules($input);
if (count($this->getRules()) !== count($exceptions))
throw new InvalidException($exceptions);
throw $this
->getException()
->setRelated($exceptions);
return true;
}

View file

@ -8,6 +8,11 @@ use Respect\Validation\Rules\AbstractRule;
class NotEmpty extends AbstractRule
{
public function createException()
{
return new NotEmptyException;
}
public function validate($input)
{
if (is_string($input))
@ -18,7 +23,9 @@ class NotEmpty extends AbstractRule
public function assert($input)
{
if (!$this->validate($input))
throw new NotEmptyException();
throw $this
->getException()
->setParams();
return true;
}

View file

@ -8,6 +8,11 @@ use Respect\Validation\Exceptions\NullValueException;
class NullValue extends AbstractRule
{
public function createException()
{
return new NullValueException;
}
public function validate($input)
{
return is_null($input);
@ -16,7 +21,9 @@ class NullValue extends AbstractRule
public function assert($input)
{
if (!$this->validate($input))
throw new NullValueException($input);
throw $this
->getException()
->setParams($input);
return true;
}

View file

@ -8,6 +8,11 @@ use Respect\Validation\Exceptions\NumericException;
class Numeric extends AbstractRule
{
public function createException()
{
return new NumericException;
}
public function validate($input)
{
return is_numeric($input);
@ -16,7 +21,9 @@ class Numeric extends AbstractRule
public function assert($input)
{
if (!$this->validate($input))
throw new NumericException($input);
throw $this
->getException()
->setParams($input);
return true;
}

View file

@ -7,6 +7,10 @@ use Respect\Validation\Exceptions\ObjectException;
class Object extends AbstractRule
{
public function createException()
{
return new ObjectException;
}
public function validate($input)
{
@ -16,7 +20,9 @@ class Object extends AbstractRule
public function assert($input)
{
if (!$this->validate($input))
throw new ObjectException($input);
throw $this
->getException()
->setParams($input);
return true;
}

View file

@ -2,17 +2,25 @@
namespace Respect\Validation\Rules;
use Respect\Validation\Exceptions\InvalidException;
use Respect\Validation\Exceptions\OneOfException;
class OneOf extends AbstractComposite
{
public function createException()
{
return new OneOfException(OneOfException::INVALID_ONE_OF);
}
public function assert($input)
{
$validators = $this->getRules();
$exceptions = $this->validateRules($input);
if (count($exceptions) === count($validators))
throw new InvalidException($exceptions);
throw $this
->getException()
->setParams(count($validators))
->setRelated($exceptions);
return true;
}

View file

@ -15,6 +15,11 @@ class Regex extends AbstractRule
$this->regex = $regex;
}
public function createException()
{
return new RegexException;
}
public function validate($input)
{
return preg_match("/{$this->regex}/", $input);
@ -23,7 +28,9 @@ class Regex extends AbstractRule
public function assert($input)
{
if (!$this->validate($input))
throw new RegexException($input, $this->regex);
throw $this
->getException()
->setParams($input, $this->regex);
return true;
}

View file

@ -5,7 +5,7 @@ namespace Respect\Validation\Rules;
use ReflectionClass;
use Respect\Validation\Rules\AbstractRule;
use Respect\Validation\Exceptions\CallbackException;
use Respect\Validation\Exceptions\InvalidException;
use Respect\Validation\Exceptions\SfException;
use Symfony\Component\Validator\ConstraintViolation;
class Sf extends AbstractRule
@ -28,6 +28,11 @@ class Sf extends AbstractRule
$this->constraint = $sfMirrorConstraint->newInstance();
}
public function createException()
{
return new SfException;
}
public function validate($input)
{
$validatorName = 'Symfony\Component\Validator\Constraints\\'
@ -46,7 +51,9 @@ class Sf extends AbstractRule
'',
$input
);
throw new InvalidException($violation->getMessage());
throw $this
->getException()
->setParams($violation->getMessage());
}
return true;
}

View file

@ -40,6 +40,11 @@ class StringLength extends AbstractRule
}
}
public function createException()
{
return new StringLengthException;
}
public function validateMin($input)
{
$input = mb_strlen($input);
@ -62,12 +67,10 @@ class StringLength extends AbstractRule
$validMin = $this->validateMin($input);
$validMax = $this->validateMax($input);
if (!$validMin || !$validMax)
throw new StringLengthException(
$input,
$validMin,
$validMax,
$this->min,
$this->max
throw $this
->getException()
->setParams(
$input, $validMin, $validMax, $this->min, $this->max
);
return true;
}

View file

@ -1,71 +0,0 @@
<?php
namespace Respect\Validation\Rules;
use Respect\Validation\Rules\AbstractRule;
use Respect\Validation\Exceptions\TraversableException;
use Respect\Validation\Validatable;
use \Traversable as Tvsable;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Exceptions\InvalidException;
class Traversable extends AllOf
{
protected $itemValidator;
public function __construct(Validatable $itemValidator=null)
{
$this->itemValidator = $itemValidator;
}
protected function isTraversable($input)
{
return is_array($input) || $input instanceof Tvsable;
}
protected function buildException($input)
{
return new TraversableException($input);
}
public function validate($input)
{
if (!$this->isTraversable($input))
return false;
if (!is_null($this->itemValidator))
foreach ($input as $item)
if (!$this->itemValidator->validate($item))
return false;
return true;
}
public function assert($input)
{
$exceptions = array();
if (!$this->isTraversable($input))
$exceptions[] = $this->buildException($input);
if (!is_null($this->itemValidator))
foreach ($input as $item)
try {
$this->itemValidator->assert($item);
} catch (InvalidException $e) {
$exceptions[] = $e;
}
if (!empty($exceptions))
throw $this->buildException($exceptions);
return true;
}
public function check($input)
{
if (!$this->isTraversable($input))
throw $this->buildException($exceptions);
if (!is_null($this->itemValidator))
foreach ($input as $item)
if (!$this->itemValidator->check($item))
return false;
return true;
}
}

View file

@ -5,7 +5,7 @@ namespace Respect\Validation\Rules;
use ReflectionClass;
use Respect\Validation\Rules\AbstractRule;
use Respect\Validation\Exceptions\CallbackException;
use Respect\Validation\Exceptions\InvalidException;
use Respect\Validation\Exceptions\ZendException;
class Zend extends AbstractRule
{
@ -25,6 +25,11 @@ class Zend extends AbstractRule
$this->zendValidator = $zendMirror->newInstance();
}
public function createException()
{
return new ZendException;
}
public function validate($input)
{
return $this->zendValidator->isValid($input);
@ -35,9 +40,11 @@ class Zend extends AbstractRule
if (!$this->validate($input)) {
$exceptions = array();
foreach ($this->zendValidator->getMessages() as $m) {
$exceptions[] = new InvalidException($m);
$exceptions[] = $this->getException()->setParams($m);
}
throw new InvalidException($exceptions);
throw $this
->getException()
->setParams($exceptions);
}
return true;
}

View file

@ -25,7 +25,7 @@ class AllOfTest extends \PHPUnit_Framework_TestCase
}
/**
* @expectedException Respect\Validation\Exceptions\InvalidException
* @expectedException Respect\Validation\Exceptions\ValidationException
*/
public function testInvalid()
{

View file

@ -16,7 +16,7 @@ class AlnumTest extends \PHPUnit_Framework_TestCase
/**
* @dataProvider providerForInvalidAlnum
* @expectedException Respect\Validation\Exceptions\AlnumException
* @expectedException Respect\Validation\Exceptions\ValidationException
*/
public function testAlnumInvalid($invalidAlnum, $aditional)
{

View file

@ -16,7 +16,7 @@ class AlphaTest extends \PHPUnit_Framework_TestCase
/**
* @dataProvider providerForInvalidAlpha
* @expectedException Respect\Validation\Exceptions\AlphaException
* @expectedException Respect\Validation\Exceptions\ValidationException
*/
public function testAlphaInvalid($invalidAlpha, $aditional)
{

View file

@ -1,54 +0,0 @@
<?php
namespace Respect\Validation\Rules;
class ArrTest extends \PHPUnit_Framework_TestCase
{
protected $object;
protected function setUp()
{
$this->object = new Arr;
}
/**
* @dataProvider providerForArr
*
*/
public function testArr($input)
{
$this->assertTrue($this->object->assert($input));
}
/**
* @dataProvider providerForNotArr
* @expectedException Respect\Validation\Exceptions\ArrException
*/
public function testNotArr($input)
{
$this->assertTrue($this->object->assert($input));
}
public function providerForArr()
{
return array(
array(array()),
array(array(1, 2, 3)),
array(array(1 => 2)),
);
}
public function providerForNotArr()
{
return array(
array(null),
array(new \stdClass),
array(new \ArrayObject(array(1 => 2))),
array(' '),
array(12321),
array(''),
);
}
}

View file

@ -24,7 +24,7 @@ class AtLeastTest extends \PHPUnit_Framework_TestCase
}
/**
* @expectedException Respect\Validation\Exceptions\InvalidException
* @expectedException Respect\Validation\Exceptions\ValidationException
*/
public function testInvalid()
{

View file

@ -70,7 +70,7 @@ class BetweenTest extends \PHPUnit_Framework_TestCase
/**
* @dataProvider providerInvalid
* @expectedException Respect\Validation\Exceptions\BetweenException
* @expectedException Respect\Validation\Exceptions\ValidationException
*/
public function testNotBetweenBounds($min, $max, $input, $type)
{
@ -90,7 +90,7 @@ class BetweenTest extends \PHPUnit_Framework_TestCase
/**
* @dataProvider providerInvalidInput
* @expectedException Respect\Validation\Exceptions\InvalidException
* @expectedException Respect\Validation\Exceptions\ValidationException
*/
public function testInvalidInput($min, $max, $input, $type)
{

View file

@ -14,7 +14,7 @@ class CallbackTest extends \PHPUnit_Framework_TestCase
}
/**
* @expectedException Respect\Validation\Exceptions\CallbackException
* @expectedException Respect\Validation\Exceptions\ValidationException
*/
public function testCallbackNot()
{

View file

@ -35,7 +35,7 @@ class DateTest extends \PHPUnit_Framework_TestCase
}
/**
* @expectedException Respect\Validation\Exceptions\DateException
* @expectedException Respect\Validation\Exceptions\ValidationException
*/
public function testDateFormat()
{
@ -44,7 +44,7 @@ class DateTest extends \PHPUnit_Framework_TestCase
}
/**
* @expectedException Respect\Validation\Exceptions\DateException
* @expectedException Respect\Validation\Exceptions\ValidationException
*/
public function testInvalidDateFormat()
{

View file

@ -23,7 +23,7 @@ class DigitsTest extends \PHPUnit_Framework_TestCase
/**
* @dataProvider providerForNotDigits
* @expectedException Respect\Validation\Exceptions\DigitsException
* @expectedException Respect\Validation\Exceptions\ValidationException
*/
public function testNotDigits($input)
{

View file

@ -23,7 +23,7 @@ class FloatTest extends \PHPUnit_Framework_TestCase
/**
* @dataProvider providerForNotFloat
* @expectedException Respect\Validation\Exceptions\FloatException
* @expectedException Respect\Validation\Exceptions\ValidationException
*/
public function testNotFloat($input)
{

View file

@ -21,7 +21,7 @@ class HasAttributeTest extends \PHPUnit_Framework_TestCase
}
/**
* @expectedException Respect\Validation\Exceptions\HasAttributeException
* @expectedException Respect\Validation\Exceptions\ValidationException
*/
public function testNotNull()
{

View file

@ -14,7 +14,7 @@ class HasKeyTest extends \PHPUnit_Framework_TestCase
}
/**
* @expectedException Respect\Validation\Exceptions\HasKeyException
* @expectedException Respect\Validation\Exceptions\ValidationException
*/
public function testNotNull()
{

View file

@ -23,7 +23,7 @@ class HexaTest extends \PHPUnit_Framework_TestCase
/**
* @dataProvider providerForNotHexa
* @expectedException Respect\Validation\Exceptions\HexaException
* @expectedException Respect\Validation\Exceptions\ValidationException
*/
public function testNotHexa($input)
{

View file

@ -18,7 +18,7 @@ class InstanceTest extends \PHPUnit_Framework_TestCase
}
/**
* @expectedException Respect\Validation\Exceptions\InstanceException
* @expectedException Respect\Validation\Exceptions\ValidationException
*/
public function testNotInstance()
{

View file

@ -24,7 +24,7 @@ class IpTest extends \PHPUnit_Framework_TestCase
/**
* @dataProvider providerForNotIp
* @expectedException Respect\Validation\Exceptions\IpException
* @expectedException Respect\Validation\Exceptions\ValidationException
*/
public function testNotIp($input, $options=null)
{

View file

@ -24,7 +24,7 @@ class MostOfTest extends \PHPUnit_Framework_TestCase
}
/**
* @expectedException Respect\Validation\Exceptions\InvalidException
* @expectedException Respect\Validation\Exceptions\ValidationException
*/
public function testInvalid()
{

View file

@ -18,7 +18,7 @@ class NoWhitespaceTest extends \PHPUnit_Framework_TestCase
}
/**
* @expectedException Respect\Validation\Exceptions\NoWhitespaceException
* @expectedException Respect\Validation\Exceptions\ValidationException
*/
public function testWhitespace()
{

View file

@ -24,7 +24,7 @@ class NoneOfTest extends \PHPUnit_Framework_TestCase
}
/**
* @expectedException Respect\Validation\Exceptions\InvalidException
* @expectedException Respect\Validation\Exceptions\ValidationException
*/
public function testInvalid()
{

View file

@ -18,7 +18,7 @@ class NotEmptyTest extends \PHPUnit_Framework_TestCase
}
/**
* @expectedException Respect\Validation\Exceptions\NotEmptyException
* @expectedException Respect\Validation\Exceptions\ValidationException
*/
public function testStringEmpty()
{

View file

@ -18,7 +18,7 @@ class NullValueTest extends \PHPUnit_Framework_TestCase
}
/**
* @expectedException Respect\Validation\Exceptions\NullValueException
* @expectedException Respect\Validation\Exceptions\ValidationException
*/
public function testNotNull()
{

View file

@ -23,7 +23,7 @@ class NumericTest extends \PHPUnit_Framework_TestCase
/**
* @dataProvider providerForNotNumeric
* @expectedException Respect\Validation\Exceptions\NumericException
* @expectedException Respect\Validation\Exceptions\ValidationException
*/
public function testNotNumeric($input)
{

View file

@ -23,7 +23,7 @@ class ObjectTest extends \PHPUnit_Framework_TestCase
/**
* @dataProvider providerForNotObject
* @expectedException Respect\Validation\Exceptions\ObjectException
* @expectedException Respect\Validation\Exceptions\ValidationException
*/
public function testNotObject($input)
{

View file

@ -24,7 +24,7 @@ class OneOfTest extends \PHPUnit_Framework_TestCase
}
/**
* @expectedException Respect\Validation\Exceptions\InvalidException
* @expectedException Respect\Validation\Exceptions\ValidationException
*/
public function testInvalid()
{

View file

@ -12,7 +12,7 @@ class RegexTest extends \PHPUnit_Framework_TestCase
}
/**
* @expectedException Respect\Validation\Exceptions\RegexException
* @expectedException Respect\Validation\Exceptions\ValidationException
*/
public function testRegexNot()
{

View file

@ -19,7 +19,7 @@ class SfTest extends \PHPUnit_Framework_TestCase
}
/**
* @expectedException Respect\Validation\Exceptions\InvalidException
* @expectedException Respect\Validation\Exceptions\ValidationException
*/
public function testParamsNot()
{

View file

@ -16,7 +16,7 @@ class StringLengthTest extends \PHPUnit_Framework_TestCase
/**
* @dataProvider providerForInvalidLenght
* @expectedException Respect\Validation\Exceptions\StringLengthException
* @expectedException Respect\Validation\Exceptions\ValidationException
*/
public function testStringLengthInvalid($string, $min, $max)
{

View file

@ -1,73 +0,0 @@
<?php
namespace Respect\Validation\Rules;
class TraversableTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider providerForTraversable
*
*/
public function testTraversable($input)
{
$v = new Traversable;
$this->assertTrue($v->assert($input));
}
/**
* @dataProvider providerForNotTraversable
* @expectedException Respect\Validation\Exceptions\TraversableException
*/
public function testNotTraversable($input)
{
$v = new Traversable;
$this->assertTrue($v->assert($input));
}
public function testTraversableItemValidator()
{
$v = new Traversable(new StringLength(5, 10));
$this->assertTrue(
$v->assert(
array('alganet'), array('kingolabs'), array('respect')
)
);
}
/**
* @dataProvider providerForNotTraversable
* @expectedException Respect\Validation\Exceptions\TraversableException
*/
public function testTraversableItemValidatorFalse()
{
$v = new Traversable(new StringLength(15, 30));
$this->assertFalse(
$v->assert(
array('alganet'), array('kingolabs'), array('respect')
)
);
}
public function providerForTraversable()
{
return array(
array(array()),
array(array(1, 2, 3)),
array(array(1 => 2)),
array(new \ArrayObject(array(1 => 2))),
);
}
public function providerForNotTraversable()
{
return array(
array(null),
array(new \stdClass),
array(' '),
array(12321),
array(''),
);
}
}

View file

@ -24,7 +24,7 @@ class ZendTest extends \PHPUnit_Framework_TestCase
}
/**
* @expectedException Respect\Validation\Exceptions\InvalidException
* @expectedException Respect\Validation\Exceptions\ValidationException
*/
public function testSimpleNot()
{
@ -39,7 +39,7 @@ class ZendTest extends \PHPUnit_Framework_TestCase
}
/**
* @expectedException Respect\Validation\Exceptions\InvalidException
* @expectedException Respect\Validation\Exceptions\ValidationException
*/
public function testParamsNot()
{

View file

@ -48,7 +48,7 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
}
/**
* @expectedException Respect\Validation\Exceptions\InvalidException
* @expectedException Respect\Validation\Exceptions\ValidationException
*/
public function testValidatorCompositeTwitterUsernameInvalid()
{
@ -63,18 +63,23 @@ class ValidatorTest extends \PHPUnit_Framework_TestCase
{
$target = new \stdClass;
$target->id = 13;
$target->created_at = '2009-10-10';
$target->name = 'Alexandre';
$target->sex = 'foo';
$target->id = 49549;
$validator = Validator::object()
->oneOf(
Validator::hasAttribute('screen_name',
Validator::alnum('_')->noWhitespace()),
Validator::hasAttribute('id', Validator::numeric())
Validator::hasAttribute('id',
Validator::numeric()
->between(1, 15))
)
->hasAttribute('created_at', Validator::date())
->hasAttribute('name', $v160 = Validator::stringLength(1, 160))
->hasAttribute('sex',
Validator::allOf(
Validator::hexa(), Validator::float(), Validator::numeric()
))
->hasOptionalAttribute('description', $v160)
->hasOptionalAttribute('location', $v160);
try {