Changes on the Exception model

This commit is contained in:
Alexandre Gomes Gaigalas 2010-12-02 22:23:02 -02:00
parent 42999f796a
commit 7c889b2d4b
30 changed files with 81 additions and 162 deletions

View file

@ -9,12 +9,4 @@ class AllOfException extends ValidationException
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

@ -6,7 +6,7 @@ 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.',
self::INVALID_NONE => 'None of the %d rules must pass. %d of them passed.',
);
}

View file

@ -8,93 +8,56 @@ class ValidationException extends InvalidArgumentException
{
const INVALID = 'Validation_1';
public static $defaultTemplates = array(
self::INVALID => 'Data validation failed'
self::INVALID => 'Data validation failed: "%s"'
);
protected $messageTemplate = 'Data validation failed';
protected $messageTemplate;
protected $related = array();
protected $params = array();
public function __construct($code=null)
public function configure()
{
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->params = array_map(
function($mixed) {
return is_object($mixed) ? get_class($mixed) : strval($mixed);
}, func_get_args()
);
$this->useTemplate($this->chooseTemplate($this->params));
$this->renderMessage();
return $this;
}
public function chooseTemplate()
{
return array_shift(array_keys(static::$defaultTemplates));
}
public function renderMessage()
{
$sprintfParams = $this->params;
array_unshift($sprintfParams, $this->messageTemplate);
$this->message = call_user_func_array('sprintf', $sprintfParams);
}
public function setRelated(array $relatedExceptions)
{
foreach ($relatedExceptions as $e)
$this->addRelated($e, false);
$this->renderMessage();
foreach ($relatedExceptions as $related)
$this->addRelated($related);
return $this;
}
public function getRelated()
public function addRelated(ValidationException $related)
{
return $this->related;
$this->related[] = $related;
}
public function addRelated(ValidationException $relatedException,
$render=true)
public function useTemplate($code)
{
$this->related[] = $relatedException;
if ($render)
$this->renderMessage();
return $this;
$this->messageTemplate = @static::$defaultTemplates[$code];
}
protected function renderMessage()
public function useParams($params)
{
$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

@ -1,31 +0,0 @@
<?php
namespace Respect\Validation\Rules;
use DateTime;
use Respect\Validation\Rules\AbstractRule;
abstract class AbstractDate extends AbstractRule
{
const FORMAT_DEFAULT = DateTime::RFC1036;
protected $format = self::FORMAT_DEFAULT;
protected function setFormat($format=self::FORMAT_DEFAULT)
{
$this->format = $format;
}
protected function getDateObject($date)
{
if ($date instanceof DateTime)
return $date;
else
return new DateTime($date);
}
protected function formatDate(DateTime $date)
{
return $date->format($this->format);
}
}

View file

@ -20,7 +20,7 @@ class AllOf extends AbstractComposite
public function createException()
{
return new AllOfException(AllOfException::INVALID_ALLOF);
return new AllOfException;
}
public function assert($input)
@ -29,7 +29,7 @@ class AllOf extends AbstractComposite
if (!empty($exceptions))
throw $this
->getException()
->setParams(count($exceptions), count($this->rules))
->configure($input, count($exceptions), count($this->rules))
->setRelated($exceptions);
return true;
}

View file

@ -35,12 +35,10 @@ class Alnum extends AbstractRule
public function assert($input)
{
$templateCode = empty($this->additionalChars) ? AlnumException::INVALID_ALNUM : AlnumException::INVALID_ALNUM_CHARS;
if (!$this->validate($input))
throw $this
->getException()
->setMessageTemplateFromCode($templateCode)
->setParams($input, $this->additionalChars);
->configure($input, $this->additionalChars);
return true;
}

View file

@ -35,12 +35,10 @@ class Alpha extends AbstractRule
public function assert($input)
{
$templateCode = empty($this->additionalChars) ? AlphaException::INVALID_ALPHA : AlnumException::INVALID_ALPHA_CHARS;
if (!$this->validate($input))
throw $this
->getException()
->setMessageTemplateFromCode($templateCode)
->setParams($input, $this->additionalChars);
->configure($input, $this->additionalChars);
return true;
}

View file

@ -22,14 +22,14 @@ class AtLeast extends AbstractComposite
if ($this->howMany > (count($validators) - count($exceptions)))
throw $this
->getException()
->setParams(count($exceptions), $this->howMany)
->configure($input, count($exceptions), $this->howMany)
->setRelated($exceptions);
return true;
}
public function createException()
{
return new AtLeastException(AtLeastException::INVALID_ATLEAST);
return new AtLeastException;
}
public function validate($input)
@ -66,7 +66,7 @@ class AtLeast extends AbstractComposite
if (count($exceptions) > (count($validators) - $this->howMany))
throw $this
->getException()
->setParams(count($exceptions), $this->howMany)
->configure($input, count($exceptions), $this->howMany)
->setRelated($exceptions);
}
return false;

View file

@ -62,23 +62,12 @@ class Between extends AbstractRule
{
if (!is_null($this->type))
$this->type->assert($input);
$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))
$validMin = $this->validateMin($input);
$validMax = $this->validateMax($input);
if (!$validMin || !$validMax)
throw $this
->createException()
->setMessageTemplateFromCode(BetweenException::INVALID_BOUNDS)
->setParams($input)
->setRelated($exceptions);
->getException()
->configure($input, $this->min, $this->max, $validMin, $validMax);
return true;
}

View file

@ -19,12 +19,12 @@ class Callback extends AbstractRule
);
$this->callback = $callback;
}
public function createException()
{
return new CallbackException;
}
public function validate($input)
{
return call_user_func($this->callback, $input);
@ -35,7 +35,7 @@ class Callback extends AbstractRule
if (!$this->validate($input))
throw $this
->getException()
->setParams($input, $this->callback);
->configure($input, $this->callback);
return true;
}

View file

@ -2,12 +2,19 @@
namespace Respect\Validation\Rules;
use DateTime;
use Respect\Validation\Rules\AbstractDate;
use Respect\Validation\Exceptions\DateException;
use DateTime;
use Respect\Validation\Rules\AbstractRule;
class Date extends AbstractDate
class Date extends AbstractRule
{
const FORMAT_DEFAULT = DateTime::RFC1036;
protected $format = self::FORMAT_DEFAULT;
protected function formatDate(DateTime $date)
{
return $date->format($this->format);
}
public function __construct($format=null)
{
@ -25,18 +32,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 $this
->getException()
->setParams($input, $this->format);
->configure($input, $this->format);
return true;
}

View file

@ -23,7 +23,7 @@ class Digits extends AbstractRule
if (!$this->validate($input))
throw $this
->getException()
->setParams($input);
->configure($input);
return true;
}

View file

@ -23,7 +23,7 @@ class Float extends AbstractRule
if (!$this->validate($input))
throw $this
->getException()
->setParams($input);
->configure($input);
return true;
}

View file

@ -58,12 +58,11 @@ class HasAttribute extends AllOf
} catch (ReflectionException $e) {
throw $this
->getException()
->setParams($this->attribute);
->configure($input, $this->attribute);
} catch (ValidationException $e) {
throw $this
->getException()
->setMessageTemplateFromCode(HasAttributeException::INVALID_HAS_ATTRIBUTE_RELATED)
->setParams($this->attribute);
->configure($input, $this->attribute);
}
return true;
}

View file

@ -40,7 +40,7 @@ class HasKey extends AllOf
if (!$this->validate($input))
throw $this
->getException()
->setParams($input, $this->key);
->configure($input, $this->key);
return parent::validate(@$input[$this->key]);
}

View file

@ -32,7 +32,7 @@ class HasOptionalAttribute extends HasAttribute
} catch (ValidationException $e) {
throw $this
->getException()
->setParams($input, $this->attribute);
->configure($input, $this->attribute);
}
return true;
}

View file

@ -22,7 +22,7 @@ class Hexa extends AbstractRule
if (!$this->validate($input))
throw $this
->getException()
->setParams($input);
->configure($input);
return true;
}

View file

@ -30,7 +30,7 @@ class Instance extends AbstractRule
if (!$this->validate($input))
throw $this
->getException()
->setParams($input, $this->instance);
->configure($input, $this->instance);
return true;
}

View file

@ -30,7 +30,7 @@ class Ip extends AbstractRule
if (!$this->validate($input))
throw $this
->getException()
->setParams($input);
->configure($input);
return true;
}

View file

@ -23,7 +23,7 @@ class NoWhitespace extends AbstractRule
if (!$this->validate($input))
throw $this
->getException()
->setParams($input);
->configure($input);
return true;
}

View file

@ -6,6 +6,7 @@ use Respect\Validation\Exceptions\NoneOfException;
class NoneOf extends AbstractComposite
{
public function createException()
{
return new NoneOfException;
@ -25,9 +26,12 @@ class NoneOf extends AbstractComposite
public function assert($input)
{
$exceptions = $this->validateRules($input);
if (count($this->getRules()) !== count($exceptions))
$numRules = count($this->getRules());
$numExceptions = count($exceptions);
if ($numRules !== $numExceptions)
throw $this
->getException()
->configure($input, $numRules, $numExceptions)
->setRelated($exceptions);
return true;
}

View file

@ -25,7 +25,7 @@ class NotEmpty extends AbstractRule
if (!$this->validate($input))
throw $this
->getException()
->setParams();
->configure($input);
return true;
}

View file

@ -23,7 +23,7 @@ class NullValue extends AbstractRule
if (!$this->validate($input))
throw $this
->getException()
->setParams($input);
->configure($input);
return true;
}

View file

@ -23,7 +23,7 @@ class Numeric extends AbstractRule
if (!$this->validate($input))
throw $this
->getException()
->setParams($input);
->configure($input);
return true;
}

View file

@ -22,7 +22,7 @@ class Object extends AbstractRule
if (!$this->validate($input))
throw $this
->getException()
->setParams($input);
->configure($input);
return true;
}

View file

@ -19,7 +19,7 @@ class OneOf extends AbstractComposite
if (count($exceptions) === count($validators))
throw $this
->getException()
->setParams(count($validators))
->configure(count($validators))
->setRelated($exceptions);
return true;
}

View file

@ -30,7 +30,7 @@ class Regex extends AbstractRule
if (!$this->validate($input))
throw $this
->getException()
->setParams($input, $this->regex);
->configure($input, $this->regex);
return true;
}

View file

@ -53,7 +53,7 @@ class Sf extends AbstractRule
);
throw $this
->getException()
->setParams($violation->getMessage());
->configure($violation->getMessage());
}
return true;
}

View file

@ -69,7 +69,7 @@ class StringLength extends AbstractRule
if (!$validMin || !$validMax)
throw $this
->getException()
->setParams(
->configure(
$input, $validMin, $validMax, $this->min, $this->max
);
return true;

View file

@ -40,11 +40,11 @@ class Zend extends AbstractRule
if (!$this->validate($input)) {
$exceptions = array();
foreach ($this->zendValidator->getMessages() as $m) {
$exceptions[] = $this->getException()->setParams($m);
$exceptions[] = $this->getException()->configure($m);
}
throw $this
->getException()
->setParams($exceptions);
->configure($exceptions);
}
return true;
}