Guess I woke up today a little obsessive. Sorted all the class declarations/methods/properties/namespace imports in alphabetic/visibility order.

This commit is contained in:
Alexandre 2011-02-07 08:52:18 -02:00
parent 4b4390eb31
commit 58c16154e4
29 changed files with 458 additions and 451 deletions

View file

@ -2,11 +2,11 @@
namespace Respect\Validation;
use \RecursiveArrayIterator;
use Respect\Validation\Exceptions\ValidationException;
use RecursiveArrayIterator;
use Respect\Validation\Exceptions\AbstractCompositeException;
use Respect\Validation\Exceptions\ValidationException;
class ExceptionIterator extends RecursiveArrayIterator
class ExceptionIterator extends RecursiveArrayIterator
{
protected $fullRelated;

View file

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

View file

@ -2,10 +2,10 @@
namespace Respect\Validation\Exceptions;
use \InvalidArgumentException;
use \Exception;
use \RecursiveIteratorIterator;
use \RecursiveTreeIterator;
use Exception;
use InvalidArgumentException;
use RecursiveIteratorIterator;
use RecursiveTreeIterator;
use Respect\Validation\ExceptionIterator;
use Respect\Validation\Reportable;
@ -17,21 +17,11 @@ class ValidationException extends InvalidArgumentException
public static $defaultTemplates = array(
'Data validation failed: "%s"'
);
protected $related = array();
protected $params = array();
protected $id = '';
protected $params = array();
protected $related = array();
protected $template = '';
public function getParams()
{
return $this->params;
}
public function setTemplate($template)
{
$this->template = $template;
}
public static function create($input=null)
{
$i = new static;
@ -41,6 +31,22 @@ class ValidationException extends InvalidArgumentException
return $i;
}
public function __toString()
{
return $this->getMainMessage();
}
public function addRelated(ValidationException $related)
{
$this->related[] = $related;
return $this;
}
public function chooseTemplate()
{
return key(static::$defaultTemplates);
}
public function configure($input=null)
{
$this->message = $this->getMainMessage();
@ -50,6 +56,92 @@ class ValidationException extends InvalidArgumentException
return $this;
}
public function findRelated()
{
$target = $this;
$path = func_get_args();
while (!empty($path) && $target !== false)
$target = $this->getRelatedByName(array_shift($path));
return $target;
}
public function getFullMessage()
{
$message = array();
foreach ($this->getIterator(false, self::ITERATE_TREE) as $m)
$message[] = $m;
return implode(PHP_EOL, $message);
}
public function getId()
{
return $this->id;
}
public function getIterator($full=false, $mode=self::ITERATE_ALL)
{
$exceptionIterator = new ExceptionIterator($this, $full);
if ($mode == self::ITERATE_ALL)
return new RecursiveIteratorIterator($exceptionIterator, 1);
else
return new RecursiveTreeIterator($exceptionIterator);
}
public function getMainMessage()
{
$sprintfParams = $this->params;
if (empty($sprintfParams))
return $this->message;
array_unshift($sprintfParams, $this->getTemplate());
return call_user_func_array('sprintf', $sprintfParams);
}
public function getParams()
{
return $this->params;
}
public function getRelated()
{
return $this->related;
}
public function getRelatedByName($name)
{
foreach ($this->getIterator(true) as $e)
if ($e->getId() === $name)
return $e;
return false;
}
public function getTemplate()
{
if (!empty($this->template))
return $this->template;
$templateKey = call_user_func_array(
array($this, 'chooseTemplate'), $this->params
);
return $this->template = static::$defaultTemplates[$templateKey];
}
public function setId($id)
{
$this->id = $id;
return $this;
}
public function setRelated(array $relatedExceptions)
{
foreach ($relatedExceptions as $related)
$this->addRelated($related);
return $this;
}
public function setTemplate($template)
{
$this->template = $template;
}
protected function guessId()
{
if (!empty($this->id))
@ -68,96 +160,4 @@ class ValidationException extends InvalidArgumentException
$param = get_class($param);
}
public function chooseTemplate()
{
return key(static::$defaultTemplates);
}
public function getFullMessage()
{
$message = array();
foreach ($this->getIterator(false, self::ITERATE_TREE) as $m)
$message[] = $m;
return implode(PHP_EOL, $message);
}
public function setId($id)
{
$this->id = $id;
return $this;
}
public function getId()
{
return $this->id;
}
public function getRelatedByName($name)
{
foreach ($this->getIterator(true) as $e)
if ($e->getId() === $name)
return $e;
return false;
}
public function getIterator($full=false, $mode=self::ITERATE_ALL)
{
$exceptionIterator = new ExceptionIterator($this, $full);
if ($mode == self::ITERATE_ALL)
return new RecursiveIteratorIterator($exceptionIterator, 1);
else
return new RecursiveTreeIterator($exceptionIterator);
}
public function findRelated()
{
$target = $this;
$path = func_get_args();
while (!empty($path) && $target !== false)
$target = $this->getRelatedByName(array_shift($path));
return $target;
}
public function getMainMessage()
{
$sprintfParams = $this->params;
if (empty($sprintfParams))
return $this->message;
array_unshift($sprintfParams, $this->getTemplate());
return call_user_func_array('sprintf', $sprintfParams);
}
public function getRelated()
{
return $this->related;
}
public function setRelated(array $relatedExceptions)
{
foreach ($relatedExceptions as $related)
$this->addRelated($related);
return $this;
}
public function addRelated(ValidationException $related)
{
$this->related[] = $related;
return $this;
}
public function getTemplate()
{
if (!empty($this->template))
return $this->template;
$templateKey = call_user_func_array(
array($this, 'chooseTemplate'), $this->params
);
return $this->template = static::$defaultTemplates[$templateKey];
}
public function __toString()
{
return $this->getMainMessage();
}
}

View file

@ -3,10 +3,10 @@
namespace Respect\Validation\Rules;
use Exception;
use Respect\Validation\Validator;
use Respect\Validation\Validatable;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Exceptions\ValidationException;
use Respect\Validation\Validatable;
use Respect\Validation\Validator;
abstract class AbstractComposite extends AbstractRule implements Validatable
{
@ -18,18 +18,6 @@ abstract class AbstractComposite extends AbstractRule implements Validatable
$this->addRules(func_get_args());
}
public function setName($name)
{
foreach ($this->getRules() as $r)
$r->setName($name);
return parent::setName($name);
}
protected function appendRule(Validatable $validator)
{
$this->rules[spl_object_hash($validator)] = $validator;
}
public function addRule($validator, $arguments=array())
{
$this->appendRule(
@ -37,20 +25,6 @@ abstract class AbstractComposite extends AbstractRule implements Validatable
);
}
public function hasRule($validator)
{
if (empty($this->rules))
return false;
if ($validator instanceof Valitatable)
return isset($this->rules[spl_object_hash($validator)]);
else
return (boolean) array_filter(
$this->rules,
function($v) use ($validator) {
return (integer) ($v instanceof $validator);
});
}
public function addRules(array $validators, $prefix='')
{
foreach ($validators as $validatorKey => $validatorSpec) {
@ -82,6 +56,32 @@ abstract class AbstractComposite extends AbstractRule implements Validatable
return $this->rules;
}
public function hasRule($validator)
{
if (empty($this->rules))
return false;
if ($validator instanceof Valitatable)
return isset($this->rules[spl_object_hash($validator)]);
else
return (boolean) array_filter(
$this->rules,
function($v) use ($validator) {
return (integer) ($v instanceof $validator);
});
}
public function setName($name)
{
foreach ($this->getRules() as $r)
$r->setName($name);
return parent::setName($name);
}
protected function appendRule(Validatable $validator)
{
$this->rules[spl_object_hash($validator)] = $validator;
}
protected function validateRules($input)
{
$validators = $this->getRules();

View file

@ -14,6 +14,10 @@ abstract class AbstractRelated extends AbstractRule implements Validatable
protected $reference = '';
protected $referenceValidator;
abstract protected function hasReference($input);
abstract protected function getReferenceValue($input);
public function __construct($reference, Validatable $validator=null,
$mandatory=true)
{
@ -22,26 +26,6 @@ abstract class AbstractRelated extends AbstractRule implements Validatable
$this->mandatory = $mandatory;
}
abstract protected function hasReference($input);
abstract protected function getReferenceValue($input);
public function reportError($input, array $relatedExceptions=array())
{
return parent::reportError($input, $relatedExceptions, $this->reference,
!is_null($relatedExceptions))->setId($this->reference);
}
public function validate($input)
{
if ($this->mandatory && !$this->hasReference($input))
return false;
if (!is_null($this->referenceValidator))
return $this->referenceValidator
->validate($this->getReferenceValue($input));
return true;
}
public function assert($input)
{
if ($this->mandatory && !$this->hasReference($input))
@ -70,4 +54,20 @@ abstract class AbstractRelated extends AbstractRule implements Validatable
return true;
}
public function reportError($input, array $relatedExceptions=array())
{
return parent::reportError($input, $relatedExceptions, $this->reference,
!is_null($relatedExceptions))->setId($this->reference);
}
public function validate($input)
{
if ($this->mandatory && !$this->hasReference($input))
return false;
if (!is_null($this->referenceValidator))
return $this->referenceValidator
->validate($this->getReferenceValue($input));
return true;
}
}

View file

@ -14,18 +14,7 @@ abstract class AbstractRule implements Validatable
public function __construct()
{
}
public function setName($name)
{
$this->name = $name;
return $this;
}
public function getName()
{
return $this->name;
//a constructor is required for ReflectionClass
}
public function __invoke($input)
@ -33,6 +22,18 @@ abstract class AbstractRule implements Validatable
return $this->validate($input);
}
public function assert($input)
{
if (!$this->validate($input))
throw $this->reportError($input);
return true;
}
public function check($input)
{
return $this->assert($input);
}
public function createException()
{
$currentFQN = get_called_class();
@ -46,29 +47,16 @@ abstract class AbstractRule implements Validatable
return $this->exception;
}
public function getName()
{
return $this->name;
}
public function hasException()
{
return!empty($this->exception);
}
public function setException(ValidationException $e)
{
$this->exception = $e;
return $this;
}
public function assert($input)
{
if (!$this->validate($input))
throw $this->reportError($input);
return true;
}
public function check($input)
{
return $this->assert($input);
}
public function reportError($input, array $relatedExceptions=array())
{
if ($this->hasException())
@ -83,4 +71,16 @@ abstract class AbstractRule implements Validatable
return $exception;
}
public function setException(ValidationException $e)
{
$this->exception = $e;
return $this;
}
public function setName($name)
{
$this->name = $name;
return $this;
}
}

View file

@ -5,14 +5,6 @@ namespace Respect\Validation\Rules;
class AllOf extends AbstractComposite
{
public function validate($input)
{
foreach ($this->getRules() as $rule)
if (!$rule->validate())
return false;
return true;
}
public function assert($input)
{
$exceptions = $this->validateRules($input);
@ -29,4 +21,12 @@ class AllOf extends AbstractComposite
$v->check($input);
}
public function validate($input)
{
foreach ($this->getRules() as $rule)
if (!$rule->validate())
return false;
return true;
}
}

View file

@ -19,6 +19,13 @@ class Alpha extends AbstractRule
$this->additionalChars = $additionalChars;
}
public function assert($input)
{
if (!$this->validate($input))
throw $this->reportError($input, array(), $this->additionalChars);
return true;
}
public function validate($input)
{
return is_string($input) && preg_match(
@ -27,11 +34,4 @@ class Alpha extends AbstractRule
);
}
public function assert($input)
{
if (!$this->validate($input))
throw $this->reportError($input, array(), $this->additionalChars);
return true;
}
}

View file

@ -2,8 +2,8 @@
namespace Respect\Validation\Rules;
use Countable;
use ArrayAccess;
use Countable;
use Traversable;
class Arr extends AbstractRule

View file

@ -25,24 +25,6 @@ class AtLeast extends AbstractComposite
return true;
}
public function validate($input)
{
$validators = $this->getRules();
$pass = 0;
foreach ($validators as $v) {
try {
$v->check($input);
$pass++;
} catch (ValidationException $e) {
//no need to do anything here. We just wanna count
//how many rules passed
}
if ($pass >= $this->howMany)
return true;
}
return false;
}
public function check($input)
{
$validators = $this->getRules();
@ -64,4 +46,22 @@ class AtLeast extends AbstractComposite
return false;
}
public function validate($input)
{
$validators = $this->getRules();
$pass = 0;
foreach ($validators as $v) {
try {
$v->check($input);
$pass++;
} catch (ValidationException $e) {
//no need to do anything here. We just wanna count
//how many rules passed
}
if ($pass >= $this->howMany)
return true;
}
return false;
}
}

View file

@ -3,8 +3,8 @@
namespace Respect\Validation\Rules;
use ReflectionProperty;
use Respect\Validation\Validatable;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Validatable;
class Attribute extends AbstractRelated
{
@ -20,11 +20,6 @@ class Attribute extends AbstractRelated
$this->setName($reference);
}
protected function hasReference($input)
{
return @property_exists($input, $this->reference);
}
protected function getReferenceValue($input)
{
$propertyMirror = new ReflectionProperty($input, $this->reference);
@ -32,4 +27,9 @@ class Attribute extends AbstractRelated
return $propertyMirror->getValue($input);
}
protected function hasReference($input)
{
return @property_exists($input, $this->reference);
}
}

View file

@ -5,14 +5,14 @@ namespace Respect\Validation\Rules;
class Call extends AbstractRelated
{
protected function hasReference($input)
{
return is_callable($this->reference);
}
protected function getReferenceValue($input)
{
return call_user_func($this->reference, $input);
}
protected function hasReference($input)
{
return is_callable($this->reference);
}
}

View file

@ -19,6 +19,13 @@ class Date extends AbstractRule
$this->format = $format;
}
public function assert($input)
{
if (!$this->validate($input))
throw $this->reportError($input, array(), $this->format);
return true;
}
public function validate($input)
{
if ($input instanceof DateTime)
@ -31,11 +38,4 @@ class Date extends AbstractRule
return date($this->format, strtotime($input)) == $input;
}
public function assert($input)
{
if (!$this->validate($input))
throw $this->reportError($input, array(), $this->format);
return true;
}
}

View file

@ -18,21 +18,6 @@ class Each extends AbstractRule
$this->keyValidator = $keyValidator;
}
public function validate($input)
{
if (empty($input))
return true;
if (!is_array($input) || $input instanceof Traversable)
return false;
foreach ($input as $key => $item) {
if (isset($this->itemValidator) && !$this->itemValidator->validate($item))
return false;
if (isset($this->keyValidator) && !$this->keyValidator->validate($key))
return false;
}
return true;
}
public function assert($input)
{
if (empty($input))
@ -67,8 +52,27 @@ class Each extends AbstractRule
public function check($input)
{
foreach ($input as $item)
$this->itemValidator->check($item);
foreach ($input as $item) {
if (isset($this->itemValidator))
$this->itemValidator->check($item);
if (isset($this->keyValidator))
$this->keyValidator->check($item);
}
return true;
}
public function validate($input)
{
if (empty($input))
return true;
if (!is_array($input) || $input instanceof Traversable)
return false;
foreach ($input as $key => $item) {
if (isset($this->itemValidator) && !$this->itemValidator->validate($item))
return false;
if (isset($this->keyValidator) && !$this->keyValidator->validate($key))
return false;
}
return true;
}

View file

@ -5,8 +5,8 @@ namespace Respect\Validation\Rules;
class Equals extends AbstractRule
{
protected $param = null;
protected $identical = false;
protected $param = null;
public function __construct($param, $identical=false)
{
@ -14,6 +14,13 @@ class Equals extends AbstractRule
$this->identical = $identical;
}
public function assert($input)
{
if (!$this->validate($input))
throw $this->reportError($input, array(), $this->param,
$this->identical);
}
public function validate($input)
{
if ($this->identical)
@ -22,11 +29,4 @@ class Equals extends AbstractRule
return $input == $this->param;
}
public function assert($input)
{
if (!$this->validate($input))
throw $this->reportError($input, array(), $this->param,
$this->identical);
}
}

View file

@ -14,6 +14,14 @@ class In extends AbstractRule
$this->strict = $strict;
}
public function assert($input)
{
if (!$this->validate($input))
throw $this->reportError($input, array(),
print_r($this->options, true), $this->strict);
return true;
}
public function validate($input)
{
if (is_array($this->options))
@ -27,12 +35,4 @@ class In extends AbstractRule
return false;
}
public function assert($input)
{
if (!$this->validate($input))
throw $this->reportError($input, array(),
print_r($this->options, true), $this->strict);
return true;
}
}

View file

@ -12,11 +12,6 @@ class Instance extends AbstractRule
$this->instance = $instance;
}
public function validate($input)
{
return $input instanceof $this->instance;
}
public function assert($input)
{
if (!$this->validate($input))
@ -24,4 +19,9 @@ class Instance extends AbstractRule
return true;
}
public function validate($input)
{
return $input instanceof $this->instance;
}
}

View file

@ -2,8 +2,8 @@
namespace Respect\Validation\Rules;
use Respect\Validation\Validatable;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Validatable;
class Key extends AbstractRelated
{
@ -19,14 +19,14 @@ class Key extends AbstractRelated
$this->setName($reference);
}
protected function hasReference($input)
{
return array_key_exists($this->reference, $input);
}
protected function getReferenceValue($input)
{
return @$input[$this->reference];
}
protected function hasReference($input)
{
return array_key_exists($this->reference, $input);
}
}

View file

@ -3,12 +3,12 @@
namespace Respect\Validation\Rules;
use Countable;
use Respect\Validation\Rules\AbstractRule;
use Respect\Validation\Exceptions\LengthException;
use Respect\Validation\Validator;
use Respect\Validation\Exceptions\NotNumericException;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Exceptions\InvalidException;
use Respect\Validation\Exceptions\LengthException;
use Respect\Validation\Exceptions\NotNumericException;
use Respect\Validation\Rules\AbstractRule;
use Respect\Validation\Validator;
class Length extends AbstractRule
{
@ -40,6 +40,18 @@ class Length extends AbstractRule
}
}
public function assert($input)
{
if (!$this->validate($input))
throw $this->reportError($input, array(), $this->min, $this->max);
return true;
}
public function validate($input)
{
return $this->validateMin($input) && $this->validateMax($input);
}
protected function extractLength($input)
{
if (is_string($input))
@ -50,7 +62,7 @@ class Length extends AbstractRule
return false;
}
public function validateMin($input)
protected function validateMin($input)
{
$length = $this->extractLength($input);
if (is_null($this->min))
@ -61,7 +73,7 @@ class Length extends AbstractRule
return $length > $this->min;
}
public function validateMax($input)
protected function validateMax($input)
{
$length = $this->extractLength($input);
if (is_null($this->max))
@ -72,16 +84,4 @@ class Length extends AbstractRule
return $length < $this->max;
}
public function validate($input)
{
return $this->validateMin($input) && $this->validateMax($input);
}
public function assert($input)
{
if (!$this->validate($input))
throw $this->reportError($input, array(), $this->min, $this->max);
return true;
}
}

View file

@ -5,8 +5,8 @@ namespace Respect\Validation\Rules;
class Max extends AbstractRule
{
protected $max;
protected $inclusive;
protected $max;
public function __construct($maxValue, $inclusive=false)
{
@ -14,14 +14,6 @@ class Max extends AbstractRule
$this->inclusive = $inclusive;
}
public function validate($input)
{
if ($this->inclusive)
return $input <= $this->max;
else
return $input < $this->max;
}
public function assert($input)
{
if (!$this->validate($input))
@ -30,4 +22,12 @@ class Max extends AbstractRule
return true;
}
public function validate($input)
{
if ($this->inclusive)
return $input <= $this->max;
else
return $input < $this->max;
}
}

View file

@ -5,8 +5,8 @@ namespace Respect\Validation\Rules;
class Min extends AbstractRule
{
protected $min;
protected $inclusive;
protected $min;
public function __construct($minValue, $inclusive=false)
{
@ -14,14 +14,6 @@ class Min extends AbstractRule
$this->inclusive = $inclusive;
}
public function validate($input)
{
if ($this->inclusive)
return $input >= $this->min;
else
return $input > $this->min;
}
public function assert($input)
{
if (!$this->validate($input))
@ -30,4 +22,12 @@ class Min extends AbstractRule
return true;
}
public function validate($input)
{
if ($this->inclusive)
return $input >= $this->min;
else
return $input > $this->min;
}
}

View file

@ -5,17 +5,6 @@ namespace Respect\Validation\Rules;
class NoneOf extends AbstractComposite
{
public function validate($input)
{
$validators = $this->getRules();
return count($validators) === count(array_filter(
$validators,
function($v) use($input) {
return!$v->validate($input);
}
));
}
public function assert($input)
{
$exceptions = $this->validateRules($input);
@ -27,4 +16,15 @@ class NoneOf extends AbstractComposite
return true;
}
public function validate($input)
{
$validators = $this->getRules();
return count($validators) === count(array_filter(
$validators,
function($v) use($input) {
return!$v->validate($input);
}
));
}
}

View file

@ -12,11 +12,6 @@ class Regex extends AbstractRule
$this->regex = $regex;
}
public function validate($input)
{
return preg_match("/{$this->regex}/", $input);
}
public function assert($input)
{
if (!$this->validate($input))
@ -24,4 +19,9 @@ class Regex extends AbstractRule
return true;
}
public function validate($input)
{
return preg_match("/{$this->regex}/", $input);
}
}

View file

@ -8,10 +8,10 @@ use Symfony\Component\Validator\ConstraintViolation;
class Sf extends AbstractRule
{
protected $messages = array();
protected $constraint;
protected $validator;
protected $name;
protected $messages = array();
protected $validator;
public function __construct($name, $params=array())
{
@ -25,14 +25,6 @@ class Sf extends AbstractRule
$this->constraint = $sfMirrorConstraint->newInstance();
}
public function validate($input)
{
$validatorName = 'Symfony\Component\Validator\Constraints\\'
. $this->name . 'Validator';
$this->validator = new $validatorName;
return $this->validator->isValid($input, $this->constraint);
}
public function assert($input)
{
if (!$this->validate($input)) {
@ -48,4 +40,12 @@ class Sf extends AbstractRule
return true;
}
public function validate($input)
{
$validatorName = 'Symfony\Component\Validator\Constraints\\'
. $this->name . 'Validator';
$this->validator = new $validatorName;
return $this->validator->isValid($input, $this->constraint);
}
}

View file

@ -22,11 +22,6 @@ class Zend extends AbstractRule
$this->zendValidator = $zendMirror->newInstance();
}
public function validate($input)
{
return $this->zendValidator->isValid($input);
}
public function assert($input)
{
if (!$this->validate($input)) {
@ -39,4 +34,9 @@ class Zend extends AbstractRule
return true;
}
public function validate($input)
{
return $this->zendValidator->isValid($input);
}
}

View file

@ -9,18 +9,20 @@ interface Validatable
public function assert($input);
public function validate($input);
public function check($input);
public function createException();
public function getException();
public function getName();
public function reportError($input, array $relatedExceptions=array());
public function setException(ValidationException $e);
public function setName($name);
public function getName();
public function validate($input);
}

View file

@ -2,68 +2,18 @@
namespace Respect\Validation;
use Respect\Validation\Rules\AllOf;
use ReflectionClass;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Exceptions\AllOfException;
use ReflectionException;
use Respect\Validation\Exceptions\AllOfException;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Rules\AllOf;
class Validator extends AllOf
{
const ERR_INTERFACE = '%s does not implement the Respect\Validator\Validatable interface required for validators';
protected $ruleName;
protected $arguments = array();
protected function setRuleName($ruleName)
{
$this->ruleName = $ruleName;
}
protected function setArguments(array $arguments)
{
$this->arguments = $arguments;
}
protected function addArgument($argument)
{
$this->arguments[] = $argument;
}
public function __get($property)
{
$this->applyParts(func_get_args());
return $this;
}
protected function applyParts($parts)
{
foreach ($parts as $a) {
if (!isset($this->ruleName))
$this->setRuleName($a);
else
$this->addArgument($a);
}
$this->checkForCompleteRule();
}
public function __call($method, $arguments)
{
array_unshift($arguments, $method);
$this->applyParts($arguments);
return $this;
}
protected function checkForCompleteRule()
{
if (!isset($this->ruleName))
return;
$this->addRule(
static::buildRule($this->ruleName, $this->arguments)
);
$this->ruleName = null;
$this->arguments = array();
}
protected $ruleName;
public static function __callStatic($ruleName, $arguments)
{
@ -74,17 +24,6 @@ class Validator extends AllOf
return $validator;
}
protected static function getRuleClassname($ruleName)
{
$ruleFqn = explode('\\', get_called_class());
array_pop($ruleFqn);
$ruleFqn[] = 'Rules';
$ruleFqn[] = $ruleName;
$ruleFqn = array_map('ucfirst', $ruleFqn);
$ruleFqn = implode('\\', $ruleFqn);
return $ruleFqn;
}
public static function buildRule($ruleSpec, $arguments=array())
{
if ($ruleSpec instanceof Validatable) {
@ -113,9 +52,70 @@ class Validator extends AllOf
return $validatorInstance;
}
public function __call($method, $arguments)
{
array_unshift($arguments, $method);
$this->applyParts($arguments);
return $this;
}
public function __get($property)
{
$this->applyParts(func_get_args());
return $this;
}
public function createException()
{
return new AllOfException;
}
protected static function getRuleClassname($ruleName)
{
$ruleFqn = explode('\\', get_called_class());
array_pop($ruleFqn);
$ruleFqn[] = 'Rules';
$ruleFqn[] = $ruleName;
$ruleFqn = array_map('ucfirst', $ruleFqn);
$ruleFqn = implode('\\', $ruleFqn);
return $ruleFqn;
}
protected function addArgument($argument)
{
$this->arguments[] = $argument;
}
protected function applyParts($parts)
{
foreach ($parts as $a) {
if (!isset($this->ruleName))
$this->setRuleName($a);
else
$this->addArgument($a);
}
$this->checkForCompleteRule();
}
protected function checkForCompleteRule()
{
if (!isset($this->ruleName))
return;
$this->addRule(
static::buildRule($this->ruleName, $this->arguments)
);
$this->ruleName = null;
$this->arguments = array();
}
protected function setArguments(array $arguments)
{
$this->arguments = $arguments;
}
protected function setRuleName($ruleName)
{
$this->ruleName = $ruleName;
}
}

View file

@ -10,8 +10,8 @@
<email>alexandre@gaigalas.net</email>
<active>yes</active>
</lead>
<date>2011-02-06</date>
<time>23:12:33</time>
<date>2011-02-07</date>
<time>08:52:08</time>
<version>
<release>0.1.0</release>
<api>0.1.0</api>
@ -37,7 +37,7 @@ First Version
<file baseinstalldir="Respect/Validation" md5sum="901b1589c84ceb4fe459ac6a71ba510b" name="Exceptions/BetweenException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="1524106b844fc93a52e1d826f1c7515a" name="Exceptions/CallbackException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="85cfc4028d19470a3ea4a6c2d619c1ad" name="Exceptions/CallException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="dccb83d6730328467bd0935caea42b04" name="Exceptions/ComponentException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="b22f51d15eb7d8fffceadcaf237948f0" name="Exceptions/ComponentException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="fcaf859fc01b157181d2386aec2a997d" name="Exceptions/DateException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="b835e4fee3c6361b8b6738beced252b8" name="Exceptions/DigitsException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="81b44bb5999ecc906a282742db267453" name="Exceptions/EachException.php" role="php" />
@ -65,37 +65,37 @@ First Version
<file baseinstalldir="Respect/Validation" md5sum="79c0e89e0cf71e98e2f88c8b4c7d8025" name="Exceptions/RegexException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="9d312b877f954696df3059eaa7c567b3" name="Exceptions/SfException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="fa46f1c072118cd86349fde9f18b3306" name="Exceptions/StringException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="5218b3fbb69301510722e59f9f4e75e8" name="Exceptions/ValidationException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="1d121dab0e182946acc7749b1d5aba78" name="Exceptions/ValidationException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="f82ca20630380b0b9d6335a2942d1c84" name="Exceptions/ZendException.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="4d1cdd1c36712cbb63d389609ab871ff" name="Rules/AbstractComposite.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="abaca0c4f1e41e7a9a75b62bb68664b4" name="Rules/AbstractRelated.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="595116795bb82d76dfb248af823d33a9" name="Rules/AbstractRule.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="9aec5cb0ecbd63e2df4d3c6fc1544f14" name="Rules/AllOf.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="2753b5b068425432e786c4e30df464fe" name="Rules/AbstractComposite.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="07aa083d1394817149cf4a6db5ea629f" name="Rules/AbstractRelated.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="cc6a9b9c7c6c43ab1daef9a8f67f67ca" name="Rules/AbstractRule.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="1ac46f8a36089242442a4a82799716b8" name="Rules/AllOf.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="a431d0ef3c154cb2c5fe7a07bbd56acf" name="Rules/Alnum.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="53e41c07b79bb2183b64fb30818c8ec5" name="Rules/Alpha.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="ea3e92cc6b0f2cc18eb66bf2506ecd88" name="Rules/Arr.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="1f6761b039050df857634cdf72794789" name="Rules/AtLeast.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="908141afa67173d6df644cf484ca1c19" name="Rules/Attribute.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="c74366a999c22fa514abd1a5d8ace61c" name="Rules/Alpha.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="25f54c2c1f3d756afb759261c164f8b6" name="Rules/Arr.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="1a03ead07f42b92f344e0fef3bd0ca9b" name="Rules/AtLeast.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="2e41115c4fdbae34471d2bc325111807" name="Rules/Attribute.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="0e5a580ff3090d106df1e02c5882ef57" name="Rules/Between.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="d51ab5f4b302720d988d39f84e145851" name="Rules/Call.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="3df410c62d6cf08f39536431651baf04" name="Rules/Call.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="b6837ae1fd632cedab0a460fd0ed8ea2" name="Rules/Callback.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="8485d68277dc87c19dfc61327782fbd9" name="Rules/Date.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="83640edc58e55a503834de0fad9fa2f1" name="Rules/Date.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="f6cee7a9c23415c07318af3dbcdba94b" name="Rules/Digits.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="f9e382e06b82ef2db9ad6d086e8be650" name="Rules/Each.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="9045e8059dffe685c1f4876203990e6f" name="Rules/Equals.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="6610a5d8ae836534280d161c59dc2f97" name="Rules/Each.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="37988625634f04ef761c865ee7c9d266" name="Rules/Equals.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="3374c2cf568efc7326468193878792bd" name="Rules/Float.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="cf8c4770ebb3021bd1737b677c7270c9" name="Rules/Hexa.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="2c93ab42a84ed9a1bec7d256567e8a1b" name="Rules/In.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="e64f06f3b0c2078c87655dc740846293" name="Rules/Instance.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="7a18b24d454962cdf562c16379fd78a7" name="Rules/In.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="ee1ad7f5bb96ea3006c282a952064333" name="Rules/Instance.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="fbeec7c032013e067e72c9704ba8537f" name="Rules/Int.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="3a083becb83c264973b097207d6e4c7a" name="Rules/Ip.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="28a39db3f5c31b649a840bc2889cc9ff" name="Rules/Key.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="627c89ec77e5611833b1da875d5b669e" name="Rules/Length.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="00c17c8f4d3562a3202674b61a4f9a91" name="Rules/Max.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="82b066b058fcf54b48cd9ad363f60e03" name="Rules/Min.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="6e092aa8436eeecea533b8627bf8792f" name="Rules/Key.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="c5606ad837364c7154fda7e81fea67f6" name="Rules/Length.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="ef8ab55b74bb62a4cd223ac5d142b1ed" name="Rules/Max.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="9ee612680c890ca6bf972683beebb1c0" name="Rules/Min.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="1e7ba06c790b75a168a98e3972d38aa6" name="Rules/MostOf.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="ecf7ebf9af5c2c963dbde65efc5cfad2" name="Rules/Negative.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="3475bf1fa01eaf9244eaaacb1004bd1e" name="Rules/NoneOf.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="4f9f5feb5c72b0d436a5c02ffddd8465" name="Rules/NoneOf.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="0aaf71cbd1de8bcd583e560fc80042f5" name="Rules/NotEmpty.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="a8a1d7a823cd22f3446b8db96f30f2fc" name="Rules/NoWhitespace.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="c5d8a9ee171e6acf3bdab26ef08350c2" name="Rules/NullValue.php" role="php" />
@ -103,13 +103,13 @@ First Version
<file baseinstalldir="Respect/Validation" md5sum="8a6462204bffbb3eead42a1b92b7d17e" name="Rules/Object.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="05dd2d2108851f2ba1237ffb9be51ec7" name="Rules/OneOf.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="2b9e4d588eefda35f48b2360dcf5b36d" name="Rules/Positive.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="38c92ea81d8bce37761026dcb0876032" name="Rules/Regex.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="15a7336192a82ef5ee5e4ddcdeafefc1" name="Rules/Sf.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="006fe4fa5620a00fca02b03603a1da05" name="Rules/Regex.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="e67ee6162bbf07e28ddb0f6f3cc55047" name="Rules/Sf.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="d3cf890b95664760156f902207242250" name="Rules/String.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="f95e0018ccc2ea97ece90c41f7da90d3" name="Rules/Zend.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="28957781dcb20a937fc8643672e848c6" name="ExceptionIterator.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="34b7d721ae2edac1fbc10c02bc936028" name="Validatable.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="2d08eae5e1809dda0b843ee4d8c37828" name="Validator.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="8f8b0eb1acf16eb5e07f4391218340b1" name="Rules/Zend.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="183d34b3e0d1bab169b43a150dc1cdd1" name="ExceptionIterator.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="e2df3f7c6104d57ff0cea651c6cc80c3" name="Validatable.php" role="php" />
<file baseinstalldir="Respect/Validation" md5sum="13211c7a903f978e3ffe75845d34cc66" name="Validator.php" role="php" />
</dir>
</contents>
<dependencies>
@ -133,7 +133,7 @@ First Version
<release>alpha</release>
<api>alpha</api>
</stability>
<date>2011-02-06</date>
<date>2011-02-07</date>
<license uri="http://www.opensource.org/licenses/bsd-license.php">BSD Style</license>
<notes>
First Version

View file

@ -9,6 +9,7 @@
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
verbose="true"
testSuiteLoaderClass="PHPUnit_Runner_StandardTestSuiteLoader">
</phpunit>