Moved assert() to AbstractRule, improved extensibility for rules moving Exception creation to createException()

This commit is contained in:
Alexandre Gomes Gaigalas 2010-12-06 17:02:21 -02:00
parent 92d7c1e919
commit 9a16edf053
33 changed files with 41 additions and 117 deletions

View file

@ -32,8 +32,6 @@ abstract class AbstractRelated extends AbstractRule implements Validatable
abstract protected function getReferenceValue($input);
abstract protected function createException();
protected function reportError($input, ValidationException $related=null)
{
$e = $this->getException();

View file

@ -15,6 +15,14 @@ abstract class AbstractRule implements Validatable
return $this->validate($input);
}
protected function createException()
{
$currentFQN = get_called_class();
$exceptionFQN = str_replace('\\Rules\\', '\\Exceptions\\', $currentFQN);
$exceptionFQN .= 'Exception';
return new $exceptionFQN;
}
public function getException()
{
return $this->exception;
@ -25,6 +33,14 @@ abstract class AbstractRule implements Validatable
$this->exception = $e;
}
public function assert($input)
{
if (!$this->validate($input))
throw $this->getException() ? : $this->createException()
->configure($input);
return true;
}
public function check($input)
{
return $this->assert($input);

View file

@ -38,8 +38,6 @@ abstract class AbstractVector extends AbstractRule
return $e;
}
abstract protected function createException();
public function assert($input)
{
if (!is_array($input) || $input instanceof Traversable)

View file

@ -24,7 +24,7 @@ class AllOf extends AbstractComposite
$exceptions = $this->validateRules($input);
$numRules = count($this->rules);
if (!empty($exceptions))
throw $this->getException() ? : AllOfException::create()
throw $this->getException() ? : $this->createException()
->setRelated($exceptions)
->configure(
$input, count($exceptions), $numRules, $numRules

View file

@ -15,7 +15,7 @@ class Alnum extends Alpha
public function assert($input)
{
if (!$this->validate($input))
throw $this->getException() ? : AlnumException::create()
throw $this->getException() ? : $this->createException()
->configure($input, $this->additionalChars);
return true;
}

View file

@ -32,7 +32,7 @@ class Alpha extends AbstractRule
public function assert($input)
{
if (!$this->validate($input))
throw $this->getException() ? : AlphaException::create()
throw $this->getException() ? : $this->createException()
->configure($input, $this->additionalChars);
return true;
}

View file

@ -14,12 +14,4 @@ class Arr extends AbstractRule
return is_array($input) || $input instanceof ArrayObject;
}
public function assert($input)
{
if (!$this->validate($input))
throw $this->getException() ? : ArrException::create()
->configure($input);
return true;
}
}

View file

@ -22,7 +22,7 @@ class AtLeast extends AbstractComposite
$numRules = count($validators);
$numExceptions = count($exceptions);
if ($this->howMany > ($numRules - $numExceptions))
throw $this->getException() ? : AtLeastException::create()
throw $this->getException() ? : $this->createException()
->configure(
$input, count($exceptions), $this->howMany, $numRules
)->setRelated($exceptions);
@ -62,7 +62,7 @@ class AtLeast extends AbstractComposite
if ($pass >= $this->howMany)
return true;
if (count($exceptions) > (count($validators) - $this->howMany))
throw $this->getException() ? : AtLeastException::create()
throw $this->getException() ? : $this->createException()
->setRelated($exceptions)
->configure($input, count($exceptions), $this->howMany);
}

View file

@ -21,9 +21,4 @@ class Attribute extends AbstractRelated
return $propertyMirror->getValue($input);
}
protected function createException()
{
return AttributeException::create();
}
}

View file

@ -30,7 +30,7 @@ class Between extends AllOf
try {
parent::assert($input);
} catch (ValidationException $e) {
throw $this->getException() ? : BetweenException::create()
throw $this->getException() ? : $this->createException()
->addRelated($e)
->configure($input);
}

View file

@ -28,7 +28,7 @@ class Callback extends AbstractRule
public function assert($input)
{
if (!$this->validate($input))
throw $this->getException() ? : CallbackException::create()
throw $this->getException() ? : $this->createException()
->configure($input, $this->callback);
return true;
}

View file

@ -36,7 +36,7 @@ class Date extends AbstractRule
public function assert($input)
{
if (!$this->validate($input))
throw $this->getException() ? : DateException::create()
throw $this->getException() ? : $this->createException()
->configure($input, $this->format);
return true;
}

View file

@ -13,11 +13,4 @@ class Digits extends AbstractRule
return ctype_digit((string) $input);
}
public function assert($input)
{
if (!$this->validate($input))
throw $this->getException() ? : DigitsException::create($input);
return true;
}
}

View file

@ -6,10 +6,5 @@ use Respect\Validation\Exceptions\EachException;
class Each extends AbstractVector
{
protected function createException()
{
return EachException::create();
}
}

View file

@ -13,12 +13,4 @@ class Float extends AbstractRule
return filter_var($input, FILTER_VALIDATE_FLOAT);
}
public function assert($input)
{
if (!$this->validate($input))
throw $this->getException() ? : FloatException::create()
->configure($input);
return true;
}
}

View file

@ -13,12 +13,4 @@ class Hexa extends AbstractRule
return ctype_xdigit($input);
}
public function assert($input)
{
if (!$this->validate($input))
throw $this->getException() ? : HexaException::create()
->configure($input);
return true;
}
}

View file

@ -23,7 +23,7 @@ class Instance extends AbstractRule
public function assert($input)
{
if (!$this->validate($input))
throw $this->getException() ? : InstanceException::create()
throw $this->getException() ? : $this->createException()
->configure($input, $this->instance);
return true;
}

View file

@ -20,12 +20,4 @@ class Ip extends AbstractRule
);
}
public function assert($input)
{
if (!$this->validate($input))
throw $this->getException() ? : IpException::create()
->configure($input);
return true;
}
}

View file

@ -19,9 +19,4 @@ class Key extends AbstractRelated
return @$input[$this->reference];
}
protected function createException()
{
return KeyException::create();
}
}

View file

@ -23,7 +23,7 @@ class Max extends AbstractRule
public function assert($input)
{
if (!$this->validate($input))
throw $this->getException() ? : MaxException::create()
throw $this->getException() ? : $this->createException()
->configure($input, $this->max);
return true;
}

View file

@ -23,7 +23,7 @@ class Min extends AbstractRule
public function assert($input)
{
if (!$this->validate($input))
throw $this->getException() ? : MinException::create()
throw $this->getException() ? : $this->createException()
->configure($input, $this->min);
return true;
}

View file

@ -13,12 +13,4 @@ class NoWhitespace extends AbstractRule
return preg_match('#^\S+$#', $input);
}
public function assert($input)
{
if (!$this->validate($input))
throw $this->getException() ? : NoWhitespaceException::create()
->configure($input);
return true;
}
}

View file

@ -24,7 +24,7 @@ class NoneOf extends AbstractComposite
$numRules = count($this->getRules());
$numExceptions = count($exceptions);
if ($numRules !== $numExceptions)
throw $this->getException() ? : NoneOfException::create()
throw $this->getException() ? : $this->createException()
->configure($input, $numExceptions, 0, $numRules)
->setRelated($exceptions);
return true;

View file

@ -15,12 +15,4 @@ class NotEmpty extends AbstractRule
return!empty($input);
}
public function assert($input)
{
if (!$this->validate($input))
throw $this->getException() ? : NotEmptyException::create()
->configure($input);
return true;
}
}

View file

@ -13,12 +13,4 @@ class NullValue extends AbstractRule
return is_null($input);
}
public function assert($input)
{
if (!$this->validate($input))
throw $this->getException() ? : NullValueException::create()
->configure($input);
return true;
}
}

View file

@ -13,12 +13,4 @@ class Numeric extends AbstractRule
return is_numeric($input);
}
public function assert($input)
{
if (!$this->validate($input))
throw $this->getException() ? : NumericException::create()
->configure($input);
return true;
}
}

View file

@ -13,12 +13,4 @@ class Object extends AbstractRule
return is_object($input);
}
public function assert($input)
{
if (!$this->validate($input))
throw $this->getException() ? : ObjectException::create()
->configure($input);
return true;
}
}

View file

@ -14,7 +14,7 @@ class OneOf extends AbstractComposite
$numRules = count($validators);
$numExceptions = count($exceptions);
if ($numExceptions === $numRules)
throw $this->getException() ? : OneOfException::create()
throw $this->getException() ? : $this->createException()
->configure($input, $numExceptions, 1, $numRules)
->setRelated($exceptions);
return true;

View file

@ -23,7 +23,7 @@ class Regex extends AbstractRule
public function assert($input)
{
if (!$this->validate($input))
throw $this->getException() ? : RegexException::create()
throw $this->getException() ? : $this->createException()
->configure($input, $this->regex);
return true;
}

View file

@ -46,7 +46,7 @@ class Sf extends AbstractRule
'',
$input
);
throw $this->getException() ? : SfException::create()
throw $this->getException() ? : $this->createException()
->configure($violation->getMessage());
}
return true;

View file

@ -62,7 +62,7 @@ class StringLength extends AbstractRule
$validMin = $this->validateMin($input);
$validMax = $this->validateMax($input);
if (!$validMin || !$validMax)
throw $this->getException() ? : StringLengthException::create()
throw $this->getException() ? : $this->createException()
->configure(
$input, $this->min, $this->max, $validMin, $validMax
);

View file

@ -35,9 +35,9 @@ class Zend extends AbstractRule
if (!$this->validate($input)) {
$exceptions = array();
foreach ($this->zendValidator->getMessages() as $m) {
$exceptions[] = ZendException::create()->configure($m);
$exceptions[] = $this->createException()->configure($m);
}
throw $this->getException() ? : ZendException::create()
throw $this->getException() ? : $this->createException()
->configure($exceptions);
}
return true;

View file

@ -5,6 +5,7 @@ namespace Respect\Validation;
use Respect\Validation\Rules\AllOf;
use ReflectionClass;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Exceptions\AllOfException;
use ReflectionException;
class Validator extends AllOf
@ -121,4 +122,9 @@ class Validator extends AllOf
return $validatorInstance;
}
protected function createException()
{
return new AllOfException;
}
}