mirror of
https://github.com/Respect/Validation.git
synced 2024-05-29 11:52:34 +02:00
Cleanup, fixed AbstractRelatedException inheritance problems.
This commit is contained in:
parent
b13ef8f072
commit
47e8c47e9e
|
@ -3,8 +3,7 @@
|
|||
namespace Respect\Validation;
|
||||
|
||||
use RecursiveArrayIterator;
|
||||
use Respect\Validation\Exceptions\AbstractCompositeException;
|
||||
use Respect\Validation\Exceptions\ValidationException;
|
||||
use Respect\Validation\Exceptions\AbstractNestedException;
|
||||
|
||||
class ExceptionIterator extends RecursiveArrayIterator
|
||||
{
|
||||
|
@ -19,7 +18,7 @@ class ExceptionIterator extends RecursiveArrayIterator
|
|||
|
||||
public function hasChildren()
|
||||
{
|
||||
if (!$this->current() instanceof AbstractCompositeException)
|
||||
if (!$this->current() instanceof AbstractNestedException)
|
||||
return false;
|
||||
else
|
||||
return (boolean) $this->current()->getRelated($this->fullRelated);
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class AbstractGroupedException extends AbstractNestedException
|
||||
{
|
||||
const NONE = 0;
|
||||
const SOME = 1;
|
||||
public static $defaultTemplates = array(
|
||||
self::NONE => 'All of the required rules must pass for {{name}}',
|
||||
self::SOME => 'These rules must pass for {{name}}',
|
||||
);
|
||||
|
||||
public function chooseTemplate()
|
||||
{
|
||||
$numRules = $this->getParam('passed');
|
||||
$numFailed = count($this->getRelated());
|
||||
return $numRules === $numFailed ? static::NONE : static::SOME;
|
||||
}
|
||||
|
||||
public function getParams()
|
||||
{
|
||||
if (1 === count($this->related))
|
||||
return $this->related[0]->getParams();
|
||||
else
|
||||
return parent::getParams();
|
||||
}
|
||||
|
||||
public function getRelated($full=false)
|
||||
{
|
||||
if ($full || 1 !== count($this->related))
|
||||
return $this->related;
|
||||
elseif ($this->related[0] instanceof AbstractNestedException)
|
||||
return $this->related[0]->getRelated();
|
||||
else
|
||||
return array();
|
||||
}
|
||||
|
||||
public function getTemplate()
|
||||
{
|
||||
if (1 === count($this->related))
|
||||
return $this->related[0]->getTemplate();
|
||||
else
|
||||
return parent::getTemplate();
|
||||
}
|
||||
|
||||
}
|
|
@ -6,66 +6,12 @@ use RecursiveIteratorIterator;
|
|||
use RecursiveTreeIterator;
|
||||
use Respect\Validation\ExceptionIterator;
|
||||
|
||||
class AbstractCompositeException extends ValidationException
|
||||
class AbstractNestedException extends ValidationException
|
||||
{
|
||||
const ITERATE_TREE = 1;
|
||||
const ITERATE_ALL = 2;
|
||||
const NONE = 0;
|
||||
const SOME = 1;
|
||||
|
||||
protected $related = array();
|
||||
public static $defaultTemplates = array(
|
||||
self::NONE => 'All of the required rules must pass for {{name}}',
|
||||
self::SOME => 'These rules must pass for {{name}}',
|
||||
);
|
||||
|
||||
public function chooseTemplate()
|
||||
{
|
||||
$numRules = $this->getParam('passed');
|
||||
$numFailed = count($this->getRelated());
|
||||
return $numRules === $numFailed ? static::NONE : static::SOME;
|
||||
}
|
||||
|
||||
public function getFullMessage()
|
||||
{
|
||||
$message = array();
|
||||
foreach ($this->getIterator(false, self::ITERATE_TREE) as $m)
|
||||
$message[] = $m;
|
||||
return implode(PHP_EOL, $message);
|
||||
}
|
||||
|
||||
public function setContext($context)
|
||||
{
|
||||
parent::setContext($context);
|
||||
foreach ($this->related as $r)
|
||||
$r->setContext($context);
|
||||
}
|
||||
|
||||
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 getRelatedByName($name)
|
||||
{
|
||||
foreach ($this->getIterator(true) as $e) {
|
||||
if ($e->getId() === $name || $e->getName() === $name)
|
||||
return $e;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function addRelated(ValidationException $related)
|
||||
{
|
||||
|
@ -73,42 +19,63 @@ class AbstractCompositeException extends ValidationException
|
|||
return $this;
|
||||
}
|
||||
|
||||
public function setName($name)
|
||||
public function findRelated()
|
||||
{
|
||||
return parent::setName($name);
|
||||
$target = $this;
|
||||
$path = func_get_args();
|
||||
|
||||
while (!empty($path) && $target !== false)
|
||||
$target = $this->getRelatedByName(array_shift($path));
|
||||
return $target;
|
||||
}
|
||||
|
||||
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 getFullMessage()
|
||||
{
|
||||
$message = array();
|
||||
|
||||
foreach ($this->getIterator(false, self::ITERATE_TREE) as $m)
|
||||
$message[] = $m;
|
||||
return implode(PHP_EOL, $message);
|
||||
}
|
||||
|
||||
public function getRelated($full=false)
|
||||
{
|
||||
return $this->related;
|
||||
}
|
||||
|
||||
public function getRelatedByName($name)
|
||||
{
|
||||
foreach ($this->getIterator(true) as $e)
|
||||
if ($e->getId() === $name || $e->getName() === $name)
|
||||
return $e;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function setContext($context)
|
||||
{
|
||||
parent::setContext($context);
|
||||
|
||||
foreach ($this->related as $r)
|
||||
$r->setContext($context);
|
||||
}
|
||||
|
||||
public function setRelated(array $relatedExceptions)
|
||||
{
|
||||
foreach ($relatedExceptions as $related)
|
||||
$this->addRelated($related);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getRelated($full=false)
|
||||
{
|
||||
if ($full || 1 !== count($this->related))
|
||||
return $this->related;
|
||||
elseif ($this->related[0] instanceof self)
|
||||
return $this->related[0]->getRelated();
|
||||
else
|
||||
return array();
|
||||
}
|
||||
|
||||
public function getTemplate()
|
||||
{
|
||||
if (1 === count($this->related))
|
||||
return $this->related[0]->getTemplate();
|
||||
else
|
||||
return parent::getTemplate();
|
||||
}
|
||||
|
||||
public function getParams()
|
||||
{
|
||||
if (1 === count($this->related))
|
||||
return $this->related[0]->getParams();
|
||||
else
|
||||
return parent::getParams();
|
||||
}
|
||||
|
||||
}
|
|
@ -1,38 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class AbstractRelatedException extends AbstractCompositeException
|
||||
{
|
||||
|
||||
public function chooseTemplate()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
//TODO cleanup this inheritances
|
||||
public function getRelated($full=false)
|
||||
{
|
||||
return $this->related;
|
||||
}
|
||||
|
||||
//TODO cleanup this inheritances
|
||||
public function getParams()
|
||||
{
|
||||
return $this->params;
|
||||
}
|
||||
|
||||
//TODO cleanup this inheritances
|
||||
public function getTemplate()
|
||||
{
|
||||
if (!empty($this->template))
|
||||
return $this->template;
|
||||
$templateKey = $this->chooseTemplate();
|
||||
if (is_null($this->context))
|
||||
$this->template = static::$defaultTemplates[$templateKey];
|
||||
else
|
||||
$this->template = $this->context->getTemplate($this, $templateKey);
|
||||
return $this->template;
|
||||
}
|
||||
|
||||
}
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class AllOfException extends AbstractCompositeException
|
||||
class AllOfException extends AbstractGroupedException
|
||||
{
|
||||
|
||||
public static $defaultTemplates = array(
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class AtLeastException extends AbstractCompositeException
|
||||
class AtLeastException extends AbstractGroupedException
|
||||
{
|
||||
|
||||
public static $defaultTemplates = array(
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class AttributeException extends AbstractRelatedException
|
||||
class AttributeException extends AbstractNestedException
|
||||
{
|
||||
const NOT_PRESENT = 0;
|
||||
const INVALID = 1;
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class BetweenException extends AbstractRelatedException
|
||||
class BetweenException extends AbstractNestedException
|
||||
{
|
||||
const BOTH = 0;
|
||||
const LOWER = 1;
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class CallException extends AbstractCompositeException
|
||||
class CallException extends AbstractGroupedException
|
||||
{
|
||||
|
||||
}
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class CallbackException extends AbstractRelatedException
|
||||
class CallbackException extends AbstractNestedException
|
||||
{
|
||||
|
||||
public static $defaultTemplates = array(
|
||||
|
|
|
@ -13,8 +13,9 @@ class DateException extends ValidationException
|
|||
|
||||
public function configure($name, array $params=array())
|
||||
{
|
||||
$params['format'] = date($params['format'],
|
||||
strtotime('2005-12-30 01:02:03'));
|
||||
$params['format'] = date(
|
||||
$params['format'], strtotime('2005-12-30 01:02:03')
|
||||
);
|
||||
return parent::configure($name, $params);
|
||||
}
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class EachException extends AbstractRelatedException
|
||||
class EachException extends AbstractNestedException
|
||||
{
|
||||
|
||||
public static $defaultTemplates = array(
|
||||
|
|
|
@ -2,15 +2,12 @@
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class NoneOfException extends AbstractCompositeException
|
||||
class NoneOfException extends AbstractNestedException
|
||||
{
|
||||
|
||||
public static $defaultTemplates = array(
|
||||
self::STANDARD => 'None of these rules must pass for {{name}}',
|
||||
);
|
||||
|
||||
public function chooseTemplate() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class OneOfException extends AbstractRelatedException
|
||||
class OneOfException extends AbstractNestedException
|
||||
{
|
||||
|
||||
public static $defaultTemplates = array(
|
||||
|
|
|
@ -63,7 +63,7 @@ class ValidationException extends InvalidArgumentException
|
|||
$this->setName($name);
|
||||
$this->setParams($params);
|
||||
$this->message = $this->getMainMessage();
|
||||
$this->guessId();
|
||||
$this->setId($this->guessId());
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
@ -89,11 +89,6 @@ class ValidationException extends InvalidArgumentException
|
|||
return $this->hasParam($name) ? $this->params[$name] : false;
|
||||
}
|
||||
|
||||
public function hasParam($name)
|
||||
{
|
||||
return isset($this->params[$name]);
|
||||
}
|
||||
|
||||
public function getParams()
|
||||
{
|
||||
return $this->params;
|
||||
|
@ -103,12 +98,13 @@ class ValidationException extends InvalidArgumentException
|
|||
{
|
||||
if (!empty($this->template))
|
||||
return $this->template;
|
||||
$templateKey = $this->chooseTemplate();
|
||||
if (is_null($this->context))
|
||||
$this->template = static::$defaultTemplates[$templateKey];
|
||||
else
|
||||
$this->template = $this->context->getTemplate($this, $templateKey);
|
||||
return $this->template;
|
||||
return $this->buildTemplate();
|
||||
}
|
||||
|
||||
public function hasParam($name)
|
||||
{
|
||||
return isset($this->params[$name]);
|
||||
}
|
||||
|
||||
public function setContext($context)
|
||||
|
@ -139,13 +135,22 @@ class ValidationException extends InvalidArgumentException
|
|||
$this->template = $template;
|
||||
}
|
||||
|
||||
protected function buildTemplate()
|
||||
{
|
||||
$templateKey = $this->chooseTemplate();
|
||||
if (is_null($this->context))
|
||||
$this->template = static::$defaultTemplates[$templateKey];
|
||||
else
|
||||
$this->template = $this->context->getTemplate($this, $templateKey);
|
||||
}
|
||||
|
||||
protected function guessId()
|
||||
{
|
||||
if (!empty($this->id))
|
||||
return;
|
||||
$id = end(explode('\\', get_called_class()));
|
||||
$id = lcfirst(str_replace('Exception', '', $id));
|
||||
$this->setId($id);
|
||||
return $id;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class ZendException extends AbstractRelatedException
|
||||
class ZendException extends AbstractNestedException
|
||||
{
|
||||
|
||||
public static $defaultTemplates = array(
|
||||
|
|
|
@ -2,9 +2,6 @@
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Exception;
|
||||
use Respect\Validation\Exceptions\AbstractCompositeException;
|
||||
use Respect\Validation\Exceptions\ComponentException;
|
||||
use Respect\Validation\Exceptions\ValidationException;
|
||||
use Respect\Validation\Validatable;
|
||||
use Respect\Validation\Validator;
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Validatable;
|
||||
use Respect\Validation\Exceptions\AbstractCompositeException;
|
||||
use Respect\Validation\Exceptions\ValidationException;
|
||||
|
||||
abstract class AbstractRelated extends AbstractRule implements Validatable
|
||||
|
|
|
@ -14,7 +14,7 @@ abstract class AbstractRule implements Validatable
|
|||
|
||||
public function __construct()
|
||||
{
|
||||
//a constructor is required for ReflectionClass
|
||||
//a constructor is required for ReflectionClass::newInstance()
|
||||
}
|
||||
|
||||
public function __invoke($input)
|
||||
|
@ -42,10 +42,7 @@ abstract class AbstractRule implements Validatable
|
|||
|
||||
public function reportError($input, array $extraParams=array())
|
||||
{
|
||||
$currentFQN = get_called_class();
|
||||
$exceptionFQN = str_replace('\\Rules\\', '\\Exceptions\\', $currentFQN);
|
||||
$exceptionFQN .= 'Exception';
|
||||
$exception = new $exceptionFQN;
|
||||
$exception = $this->createException();
|
||||
$input = ValidationException::stringify($input);
|
||||
$name = $this->getName() ? : "\"$input\"";
|
||||
$params = array_merge($extraParams, get_object_vars($this));
|
||||
|
@ -59,4 +56,12 @@ abstract class AbstractRule implements Validatable
|
|||
return $this;
|
||||
}
|
||||
|
||||
protected function createException()
|
||||
{
|
||||
$currentFQN = get_called_class();
|
||||
$exceptionFQN = str_replace('\\Rules\\', '\\Exceptions\\', $currentFQN);
|
||||
$exceptionFQN .= 'Exception';
|
||||
return new $exceptionFQN;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -23,9 +23,12 @@ class Alpha extends AbstractRule
|
|||
{
|
||||
if (!is_scalar($input))
|
||||
return false;
|
||||
|
||||
$input = (string) $input;
|
||||
$cleanInput = str_replace(str_split($this->additionalChars), '', $input);
|
||||
|
||||
return ($cleanInput !== $input && $cleanInput === '')
|
||||
|| preg_match($this->stringFormat, $cleanInput);
|
||||
|| preg_match($this->stringFormat, $cleanInput);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -33,20 +33,22 @@ class AtLeast extends AbstractComposite
|
|||
public function check($input)
|
||||
{
|
||||
$validators = $this->getRules();
|
||||
$pass = 0;
|
||||
$exceptions = array();
|
||||
$numRules = count($validators);
|
||||
$numPassed = 0;
|
||||
$maxExceptions = $numRules - $this->howMany;
|
||||
foreach ($validators as $v) {
|
||||
try {
|
||||
$v->check($input);
|
||||
$pass++;
|
||||
if (++$numPassed >= $this->howMany)
|
||||
return true;
|
||||
if (count($exceptions) > $maxExceptions)
|
||||
throw $this->reportError(
|
||||
$input,
|
||||
array('passed' => $numPassed))->setRelated($exceptions);
|
||||
} catch (ValidationException $e) {
|
||||
$exceptions[] = $e;
|
||||
}
|
||||
if ($pass >= $this->howMany)
|
||||
return true;
|
||||
if (count($exceptions) > ($numPassed = count($validators) - $this->howMany))
|
||||
throw $this->reportError($input, array('passed' => $numPassed))
|
||||
->setRelated($exceptions);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
@ -54,18 +56,17 @@ class AtLeast extends AbstractComposite
|
|||
public function validate($input)
|
||||
{
|
||||
$validators = $this->getRules();
|
||||
$pass = 0;
|
||||
foreach ($validators as $v) {
|
||||
$numPassed = 0;
|
||||
foreach ($validators as $v)
|
||||
try {
|
||||
$v->check($input);
|
||||
$pass++;
|
||||
if (++$numPassed >= $this->howMany)
|
||||
return true;
|
||||
} catch (ValidationException $e) {
|
||||
//no need to do anything here. We just wanna count
|
||||
//how many rules passed
|
||||
//empty catch block is nasty, i know, but no need to do
|
||||
//anything here. We just wanna count how many rules passed
|
||||
}
|
||||
if ($pass >= $this->howMany)
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
@ -27,7 +27,7 @@ class Attribute extends AbstractRelated
|
|||
|
||||
public function hasReference($input)
|
||||
{
|
||||
return @property_exists($input, $this->reference);
|
||||
return is_object($input) && property_exists($input, $this->reference);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -23,12 +23,12 @@ class Date extends AbstractRule
|
|||
{
|
||||
if ($input instanceof DateTime)
|
||||
return true;
|
||||
if (!is_string($input))
|
||||
elseif (!is_string($input))
|
||||
return false;
|
||||
if (is_null($this->format))
|
||||
return (boolean) strtotime($input);
|
||||
elseif (is_null($this->format))
|
||||
return false !== strtotime($input);
|
||||
else
|
||||
return date($this->format, strtotime($input)) == $input;
|
||||
return $input === date($this->format, strtotime($input));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -20,40 +20,43 @@ class Each extends AbstractRule
|
|||
|
||||
public function assert($input)
|
||||
{
|
||||
$exceptions = array();
|
||||
|
||||
if (empty($input))
|
||||
return true;
|
||||
if (is_object($input))
|
||||
elseif (is_object($input))
|
||||
$input = get_object_vars($input);
|
||||
if (!is_array($input) || $input instanceof Traversable)
|
||||
elseif (!is_array($input) || $input instanceof Traversable)
|
||||
throw $this->reportError($input);
|
||||
$exceptions = array();
|
||||
foreach ($input as $key => $item) {
|
||||
if (isset($this->itemValidator))
|
||||
try {
|
||||
$this->itemValidator->assert($item);
|
||||
} catch (ValidationException $e) {
|
||||
$exceptions[] = $e;
|
||||
}
|
||||
if (isset($this->keyValidator))
|
||||
try {
|
||||
$this->keyValidator->assert($item);
|
||||
} catch (ValidationException $e) {
|
||||
$exceptions[] = $e;
|
||||
}
|
||||
}
|
||||
else
|
||||
foreach ($input as $key => $item)
|
||||
if (isset($this->itemValidator))
|
||||
try {
|
||||
$this->itemValidator->assert($item);
|
||||
} catch (ValidationException $e) {
|
||||
$exceptions[] = $e;
|
||||
}
|
||||
elseif (isset($this->keyValidator))
|
||||
try {
|
||||
$this->keyValidator->assert($item);
|
||||
} catch (ValidationException $e) {
|
||||
$exceptions[] = $e;
|
||||
}
|
||||
|
||||
if (!empty($exceptions))
|
||||
throw $this->reportError($input)->setRelated($exceptions);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function check($input)
|
||||
{
|
||||
foreach ($input as $item) {
|
||||
foreach ($input as $item)
|
||||
if (isset($this->itemValidator))
|
||||
$this->itemValidator->check($item);
|
||||
if (isset($this->keyValidator))
|
||||
elseif (isset($this->keyValidator))
|
||||
$this->keyValidator->check($item);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -61,14 +64,15 @@ class Each extends AbstractRule
|
|||
{
|
||||
if (empty($input))
|
||||
return true;
|
||||
if (!is_array($input) || $input instanceof Traversable)
|
||||
elseif (!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;
|
||||
}
|
||||
else
|
||||
foreach ($input as $key => $item)
|
||||
if (isset($this->itemValidator) && !$this->itemValidator->validate($item))
|
||||
return false;
|
||||
elseif (isset($this->keyValidator) && !$this->keyValidator->validate($key))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -16,10 +16,6 @@ class In extends AbstractRule
|
|||
|
||||
public function reportError($input, array $extraParams=array())
|
||||
{
|
||||
if (is_array($this->haystack))
|
||||
$haystack = implode(',', $this->haystack);
|
||||
else
|
||||
$haystack = $this->haystack;
|
||||
return parent::reportError($input, $extraParams);
|
||||
}
|
||||
|
||||
|
@ -27,13 +23,12 @@ class In extends AbstractRule
|
|||
{
|
||||
if (is_array($this->haystack))
|
||||
return in_array($input, $this->haystack, $this->compareIdentical);
|
||||
elseif (is_string($this->haystack))
|
||||
if ($this->haystack)
|
||||
return mb_strpos($this->haystack, $input) !== false;
|
||||
else
|
||||
return mb_stripos($this->haystacko, $input) !== false;
|
||||
else
|
||||
elseif (!is_string($this->haystack))
|
||||
return false;
|
||||
elseif ($this->compareIdentical)
|
||||
return mb_strpos($this->haystack, $input) !== false;
|
||||
else
|
||||
return mb_stripos($this->haystack, $input) !== false;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -8,8 +8,7 @@ use Respect\Validation\Validatable;
|
|||
class Key extends AbstractRelated
|
||||
{
|
||||
|
||||
public function __construct($reference,
|
||||
Validatable $referenceValidator=null, $mandatory=true)
|
||||
public function __construct($reference, Validatable $referenceValidator=null, $mandatory=true)
|
||||
{
|
||||
if (!is_string($reference) || empty($reference))
|
||||
throw new ComponentException(
|
||||
|
@ -20,7 +19,7 @@ class Key extends AbstractRelated
|
|||
|
||||
public function getReferenceValue($input)
|
||||
{
|
||||
return @$input[$this->reference];
|
||||
return $input[$this->reference];
|
||||
}
|
||||
|
||||
public function hasReference($input)
|
||||
|
|
|
@ -39,7 +39,8 @@ class Length extends AbstractRule
|
|||
|
||||
public function validate($input)
|
||||
{
|
||||
return $this->validateMin($input) && $this->validateMax($input);
|
||||
$length = $this->extractLength($input);
|
||||
return $this->validateMin($length) && $this->validateMax($length);
|
||||
}
|
||||
|
||||
protected function extractLength($input)
|
||||
|
@ -52,23 +53,21 @@ class Length extends AbstractRule
|
|||
return false;
|
||||
}
|
||||
|
||||
protected function validateMin($input)
|
||||
protected function validateMin($length)
|
||||
{
|
||||
$length = $this->extractLength($input);
|
||||
if (is_null($this->minValue))
|
||||
return true;
|
||||
if ($this->inclusive)
|
||||
elseif ($this->inclusive)
|
||||
return $length >= $this->minValue;
|
||||
else
|
||||
return $length > $this->minValue;
|
||||
}
|
||||
|
||||
protected function validateMax($input)
|
||||
protected function validateMax($length)
|
||||
{
|
||||
$length = $this->extractLength($input);
|
||||
if (is_null($this->maxValue))
|
||||
return true;
|
||||
if ($this->inclusive)
|
||||
elseif ($this->inclusive)
|
||||
return $length <= $this->maxValue;
|
||||
else
|
||||
return $length < $this->maxValue;
|
||||
|
|
|
@ -17,13 +17,10 @@ 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);
|
||||
}
|
||||
));
|
||||
foreach ($this->getRules() as $rule)
|
||||
if ($rule->validate($input))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -9,7 +9,7 @@ class NotEmpty extends AbstractRule
|
|||
{
|
||||
if (is_string($input))
|
||||
$input = trim($input);
|
||||
return!empty($input);
|
||||
return !empty($input);
|
||||
}
|
||||
|
||||
}
|
|
@ -25,4 +25,12 @@ class OneOf extends AbstractComposite
|
|||
return false;
|
||||
}
|
||||
|
||||
public function check($input)
|
||||
{
|
||||
foreach ($this->getRules() as $v)
|
||||
if ($v->check($input))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -26,19 +26,22 @@ class Zend extends AbstractRule
|
|||
|
||||
public function assert($input)
|
||||
{
|
||||
if (!$this->validate($input)) {
|
||||
$exceptions = array();
|
||||
foreach ($this->zendValidator->getMessages() as $m) {
|
||||
$exceptions = array();
|
||||
$validator = clone $this->zendValidator;
|
||||
|
||||
if ($validator->isValid($input))
|
||||
return true;
|
||||
else
|
||||
foreach ($validator->getMessages() as $m)
|
||||
$exceptions[] = $this->reportError($m, get_object_vars($this));
|
||||
}
|
||||
throw $this->reportError($input)->setRelated($exceptions);
|
||||
}
|
||||
return true;
|
||||
|
||||
throw $this->reportError($input)->setRelated($exceptions);
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
return $this->zendValidator->isValid($input);
|
||||
$validator = clone $this->zendValidator;
|
||||
return $validator->isValid($input);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -10,7 +10,6 @@ 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 $arguments = array();
|
||||
protected $ruleName;
|
||||
|
@ -26,30 +25,19 @@ class Validator extends AllOf
|
|||
|
||||
public static function buildRule($ruleSpec, $arguments=array())
|
||||
{
|
||||
if ($ruleSpec instanceof Validatable) {
|
||||
if ($ruleSpec instanceof Validatable)
|
||||
return $ruleSpec;
|
||||
}
|
||||
if (is_object($ruleSpec))
|
||||
throw new ComponentException(
|
||||
sprintf(static::ERR_INTERFACE, get_class($ruleSpec))
|
||||
);
|
||||
$validatorFqn = static::getRuleClassname($ruleSpec);
|
||||
try {
|
||||
$validatorClass = new ReflectionClass($validatorFqn);
|
||||
} catch (ReflectionException $e) {
|
||||
throw new ComponentException($e->getMessage());
|
||||
}
|
||||
$implementedInterface = $validatorClass->implementsInterface(
|
||||
'Respect\Validation\Validatable'
|
||||
);
|
||||
if (!$implementedInterface)
|
||||
throw new ComponentException(
|
||||
sprintf(static::ERR_INTERFACE, $validatorFqn)
|
||||
);
|
||||
$validatorInstance = $validatorClass->newInstanceArgs(
|
||||
$arguments
|
||||
);
|
||||
return $validatorInstance;
|
||||
else
|
||||
try {
|
||||
$validatorFqn = static::getRuleClassname($ruleSpec);
|
||||
$validatorClass = new ReflectionClass($validatorFqn);
|
||||
$validatorInstance = $validatorClass->newInstanceArgs(
|
||||
$arguments
|
||||
);
|
||||
return $validatorInstance;
|
||||
} catch (ReflectionException $e) {
|
||||
throw new ComponentException($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public function __call($method, $arguments)
|
||||
|
|
|
@ -10,8 +10,8 @@
|
|||
<email>alexandre@gaigalas.net</email>
|
||||
<active>yes</active>
|
||||
</lead>
|
||||
<date>2011-02-19</date>
|
||||
<time>17:47:25</time>
|
||||
<date>2011-02-20</date>
|
||||
<time>13:59:44</time>
|
||||
<version>
|
||||
<release>0.1.0</release>
|
||||
<api>0.1.0</api>
|
||||
|
@ -28,21 +28,21 @@ First Version
|
|||
<dir baseinstalldir="Respect/Validation" name="/">
|
||||
<file baseinstalldir="Respect/Validation" md5sum="4ef5c6ff21a7382ac26d74354a9c2ece" name="Contexts/AbstractContext.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="a99e9fcc3f89d82a4b6dfd0fbe487014" name="Contexts/Form.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="7e43dac02018fdfc40846622bcc253f9" name="Exceptions/AbstractCompositeException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="93478182b3fbebb69e23f48eff09f1f7" name="Exceptions/AbstractRelatedException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="3ca12be55a230b4a1096b680714b3222" name="Exceptions/AllOfException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="ef1b18cf77f1806f777f0600abde19c1" name="Exceptions/AbstractGroupedException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="040df90f709ce6780c8c4be2f5b708f8" name="Exceptions/AbstractNestedException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="3e4da4e7b629a4889f87517506664bf3" name="Exceptions/AllOfException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="ae818fe09029e856ed02ce70273da0f5" name="Exceptions/AlnumException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="a4e7f3830a04d92bf48ae2e88bb8d488" name="Exceptions/AlphaException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="023aeb25a9aca6311c81e96aa4abf4f8" name="Exceptions/ArrException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="307d62a5000949c783dcec2c6b27b578" name="Exceptions/AtLeastException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="8c0b761a132dc1e1182f8a4f87684218" name="Exceptions/AttributeException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="2593e98d0240ef3ca91dfb7099f5d71b" name="Exceptions/BetweenException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="b6c595b7fcb32a829bf04377fb2baf99" name="Exceptions/CallbackException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="f63902e4f4c6a626bb3dc65997db33d3" name="Exceptions/CallException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="285d100d9a91fc4f16ccf2e38bc30639" name="Exceptions/AtLeastException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="5fd812255cc8116f01a7c2d871db5c0e" name="Exceptions/AttributeException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="1c585fb793a9ef955d2582fa81a8240e" name="Exceptions/BetweenException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="887d77c80cbc7176186d62485fb1630b" name="Exceptions/CallbackException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="f5a187f5f40048b7e24022ba8c1c7196" name="Exceptions/CallException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="b22f51d15eb7d8fffceadcaf237948f0" name="Exceptions/ComponentException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="94bd17781e0cdc8e1d901dd1409d99ec" name="Exceptions/DateException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="f4b82cd945205c262506688eada72c91" name="Exceptions/DateException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="294cc49907860578e0afc8f6b67849c0" name="Exceptions/DigitsException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="f74d89c59037c225fc59191f4e601615" name="Exceptions/EachException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="17457e33110d00c69cd2fc14ff73a125" name="Exceptions/EachException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="48dda2bfad790b78fd0952b3d1425415" name="Exceptions/EqualsException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="9aa8e37c1e8d11335fbdf073eb40c31f" name="Exceptions/FloatException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="d5b41b6bd10891ed842c2285f0975c6d" name="Exceptions/HexaException.php" role="php" />
|
||||
|
@ -56,62 +56,62 @@ First Version
|
|||
<file baseinstalldir="Respect/Validation" md5sum="aa9cba4e18c3084fc118cad99bd5b143" name="Exceptions/MinException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="5f7e12383771ce84388845282ac648d8" name="Exceptions/MostOfException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="d6892816424411abf0f873c11d13c28f" name="Exceptions/NegativeException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="774a5c5df499cfecd58d22b22322419e" name="Exceptions/NoneOfException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="70cd4c73328d82880a1624d3567900bf" name="Exceptions/NoneOfException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="372a75be4ae60271e882674190e94a3b" name="Exceptions/NotEmptyException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="700f78b12af170fd83a7828019a7e501" name="Exceptions/NoWhitespaceException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="dee4d709a8359d8afb6b4fed4d1def65" name="Exceptions/NullValueException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="4b8c8c6a3ce99d706e9e7040872f1f48" name="Exceptions/NumericException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="d3b1289ccb22f11759ce19dc92fbe98d" name="Exceptions/ObjectException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="c01b0defc26ea5771bb1fb0cad37bdc3" name="Exceptions/OneOfException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="11c198026c2988b550f9ecf1406f07e1" name="Exceptions/OneOfException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="7df34686076be3531c9bbfca3926728b" name="Exceptions/PositiveException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="cb0971a4379482977ddf22a675581255" name="Exceptions/RegexException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="d0498462a420d57ca74fa47e3c8c6194" name="Exceptions/SfException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="a9d0825a244e0b7d6412777c01367650" name="Exceptions/StringException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="87d7f2f8ff53aa98b0a06f4623994033" name="Exceptions/ValidationException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="250fe508ea6c6fa437d8cd37ac1dc697" name="Exceptions/ZendException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="f2dcb5631134935d3d981313cf296e54" name="Rules/AbstractComposite.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="7f6273711c504bc72f2aad6dc499d15c" name="Rules/AbstractRelated.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="46719f6b47886cf026271f0ab6736453" name="Rules/AbstractRule.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="a5b6a3c68b826aef74b6fe19c696b2c0" name="Exceptions/ValidationException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="f7294294fd18031fd9ad746ca4945369" name="Exceptions/ZendException.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="67eecfc191e3e8d2b7137ba7bac0c574" name="Rules/AbstractComposite.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="a2e9210c6722fa39592ee89d9719df3f" name="Rules/AbstractRelated.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="198d2f1acfe9d09185c934a668783cb8" name="Rules/AbstractRule.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="38749eea751bc65585b121cdf2452b9c" name="Rules/AllOf.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="7b002c1fab94b46f85c5cb79c50a3c2d" name="Rules/Alnum.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="1f552b16151f61c07ef84a58d7b00754" name="Rules/Alpha.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="499cad6ac04b7f775cae65ff23bd7c6e" name="Rules/Alpha.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="25f54c2c1f3d756afb759261c164f8b6" name="Rules/Arr.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="9eba4fdf3110d9302b2a80271a3c1f21" name="Rules/AtLeast.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="39a085f3e4292b002e8bc2f73dc3e926" name="Rules/Attribute.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="9bf907f29fedce62683ba48ccff8ebe0" name="Rules/AtLeast.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="099870a584374ddbff431ad5e504e5ed" name="Rules/Attribute.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="ca4e19a40d5b248a915198c10cde2630" name="Rules/Between.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="952c2d9d0f61c1613d43554a0fd17e42" name="Rules/Call.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="fc648db054e8001969516e07bd331a26" name="Rules/Callback.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="f460a19d4b469051add54992877ef23e" name="Rules/Date.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="94f6e04b93cbc45c04207b0ab32e4d98" name="Rules/Date.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="9f72c097ae08a41b0b612799b01d7465" name="Rules/Digits.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="08975a3debe889e0188e2d71816ec04b" name="Rules/Each.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="9a20af2507bce58028134aa45d2c375d" name="Rules/Each.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="01d224d7f90af4df2ff2eacb6b1c64b4" 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="bbbe54813d655580145eed1f871c9162" name="Rules/In.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="4c35027222aae11d181fe23830fca01f" name="Rules/In.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="34c42f6e0ca06eea6549f0e02e222604" name="Rules/Instance.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="fbeec7c032013e067e72c9704ba8537f" name="Rules/Int.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="e2a7b74c52fb1471488bafcfa1029184" name="Rules/Ip.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="4c2c89e577f1295118a8e31b7f4f4938" name="Rules/Key.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="c492374621fb02d2cb5fc8ac519eac2a" name="Rules/Length.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="288b1f5cc8e643643503c5e8aeb286fe" name="Rules/Key.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="608bc816bf602c7e804c1361bc02e7f6" name="Rules/Length.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="bae9afe4982f0f8234de4cee675c84da" name="Rules/Max.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="9c3f0d53300b2fbb9cc4721e141bd7be" 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="95d2397abf99304388c03f725d5d10c4" name="Rules/NoneOf.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="0aaf71cbd1de8bcd583e560fc80042f5" name="Rules/NotEmpty.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="27c5207a71b9bfabc8baf2c60e701889" name="Rules/NoneOf.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="e6f25a27e2bda69903d501fe94b17ffe" 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" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="34ed73b15119f9fa8e54d61009fcf0a4" name="Rules/Numeric.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="8a6462204bffbb3eead42a1b92b7d17e" name="Rules/Object.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="d65551272c52774e0067b2318c234654" name="Rules/OneOf.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="67bfc60e4ef4a796a471a3edee9a24bc" name="Rules/OneOf.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="2b9e4d588eefda35f48b2360dcf5b36d" name="Rules/Positive.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="c6cc0755ba8b36aebaa6ad7670fc5124" name="Rules/Regex.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="fad11bb57927795ef56cd97e57b5bb50" name="Rules/Sf.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="d3cf890b95664760156f902207242250" name="Rules/String.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="c7e1a15f54d98c301bef8ef504cd3989" name="Rules/Zend.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="24e5e73d13d0e855059f33b441af66f5" name="ExceptionIterator.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="1b8f6f1f600788e30d8d56a4a3de905a" name="Rules/Zend.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="707527a26df31bbf09896ecb50573a47" name="ExceptionIterator.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="13a221cd7ccc263afb1605371c8b7a50" name="Validatable.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="8e152b2358c2cabce815a28bd69baccc" name="Validator.php" role="php" />
|
||||
<file baseinstalldir="Respect/Validation" md5sum="2f8c0b76a6fb39943e61fa041f4f227a" name="Validator.php" role="php" />
|
||||
</dir>
|
||||
</contents>
|
||||
<dependencies>
|
||||
|
@ -135,7 +135,7 @@ First Version
|
|||
<release>alpha</release>
|
||||
<api>alpha</api>
|
||||
</stability>
|
||||
<date>2011-02-19</date>
|
||||
<date>2011-02-20</date>
|
||||
<license uri="http://www.opensource.org/licenses/bsd-license.php">BSD Style</license>
|
||||
<notes>
|
||||
First Version
|
||||
|
|
|
@ -1,31 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation;
|
||||
|
||||
use Respect\Validation\Validator as v;
|
||||
use Respect\Validation\Exceptions\ValidationException;
|
||||
|
||||
class CombinedMessagesTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
public function testIntPositiveBetween()
|
||||
{
|
||||
$int = v::int()->positive()->between(0, 256)->setName('Meu Campo');
|
||||
try {
|
||||
$int->assert(null);
|
||||
} catch (ValidationException $e) {
|
||||
//echo $e->getFullMessage() . PHP_EOL;
|
||||
}
|
||||
}
|
||||
|
||||
public function testScreenName()
|
||||
{
|
||||
$int = v::alnum('_')->noWhitespace()->length(1, 15);
|
||||
try {
|
||||
$int->assert(null);
|
||||
} catch (ValidationException $e) {
|
||||
//echo $e->getFullMessage() . PHP_EOL;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -11,11 +11,10 @@ class NegativeTest extends \PHPUnit_Framework_TestCase
|
|||
* Toggle this to show an example of all validation
|
||||
* messages on the PHPUnit console.
|
||||
*/
|
||||
|
||||
protected $showMessages = false;
|
||||
protected $targetName = 'My Field';
|
||||
|
||||
protected function genMessage($validator, $invalidValue)
|
||||
protected function doTestValidator($validator, $invalidValue)
|
||||
{
|
||||
try {
|
||||
$validator->assert($invalidValue);
|
||||
|
@ -34,165 +33,165 @@ class NegativeTest extends \PHPUnit_Framework_TestCase
|
|||
|
||||
public function testAlnum()
|
||||
{
|
||||
$this->genMessage(v::alnum(), '#');
|
||||
$this->genMessage(v::alnum('_'), '#');
|
||||
$this->doTestValidator(v::alnum(), '#');
|
||||
$this->doTestValidator(v::alnum('_'), '#');
|
||||
}
|
||||
|
||||
public function testAlpha()
|
||||
{
|
||||
$this->genMessage(v::alpha(), '#');
|
||||
$this->genMessage(v::alpha('.'), '#');
|
||||
$this->doTestValidator(v::alpha(), '#');
|
||||
$this->doTestValidator(v::alpha('.'), '#');
|
||||
}
|
||||
|
||||
public function testArr()
|
||||
{
|
||||
$this->genMessage(v::arr(), '#');
|
||||
$this->doTestValidator(v::arr(), '#');
|
||||
}
|
||||
|
||||
public function testAttribute()
|
||||
{
|
||||
$this->genMessage(v::attribute("foo", v::int()),
|
||||
$this->doTestValidator(v::attribute("foo", v::int()),
|
||||
(object) array("foo" => "bar"));
|
||||
$this->genMessage(v::attribute("foo", v::string()), null);
|
||||
$this->doTestValidator(v::attribute("foo", v::string()), null);
|
||||
}
|
||||
|
||||
public function testBetween()
|
||||
{
|
||||
$this->genMessage(v::between(5, 15), 999);
|
||||
$this->genMessage(v::between('a', 'f'), 15951);
|
||||
$this->genMessage(v::between(new \DateTime('now'),
|
||||
$this->doTestValidator(v::between(5, 15), 999);
|
||||
$this->doTestValidator(v::between('a', 'f'), 15951);
|
||||
$this->doTestValidator(v::between(new \DateTime('now'),
|
||||
new \DateTime('tomorrow')), new \DateTime('yesterday'));
|
||||
}
|
||||
|
||||
public function testCall()
|
||||
{
|
||||
$this->genMessage(v::call('implode', v::int()), array('x', 2, 3, 4));
|
||||
$this->doTestValidator(v::call('implode', v::int()), array('x', 2, 3, 4));
|
||||
}
|
||||
|
||||
public function testCallback()
|
||||
{
|
||||
$this->genMessage(v::callback('is_string'), 123);
|
||||
$this->doTestValidator(v::callback('is_string'), 123);
|
||||
}
|
||||
|
||||
public function testDate()
|
||||
{
|
||||
$this->genMessage(v::date('Y-m-d'), '2010-30-10');
|
||||
$this->genMessage(v::date(), 'Jan 310 2008');
|
||||
$this->doTestValidator(v::date('Y-m-d'), '2010-30-10');
|
||||
$this->doTestValidator(v::date(), 'Jan 310 2008');
|
||||
}
|
||||
|
||||
public function testDigits()
|
||||
{
|
||||
$this->genMessage(v::digits(), 'x02384');
|
||||
$this->doTestValidator(v::digits(), 'x02384');
|
||||
}
|
||||
|
||||
public function testEach()
|
||||
{
|
||||
$this->genMessage(v::each(v::hexa()), array('AF', 'P1', '09'));
|
||||
$this->doTestValidator(v::each(v::hexa()), array('AF', 'P1', '09'));
|
||||
}
|
||||
|
||||
public function testEquals()
|
||||
{
|
||||
$this->genMessage(v::equals('foobar'), 'aaar');
|
||||
$this->doTestValidator(v::equals('foobar'), 'aaar');
|
||||
}
|
||||
|
||||
public function testFloat()
|
||||
{
|
||||
$this->genMessage(v::float(), 'dance');
|
||||
$this->doTestValidator(v::float(), 'dance');
|
||||
}
|
||||
|
||||
public function testHexa()
|
||||
{
|
||||
$this->genMessage(v::hexa(), 'PPAFAF');
|
||||
$this->doTestValidator(v::hexa(), 'PPAFAF');
|
||||
}
|
||||
|
||||
public function testIn()
|
||||
{
|
||||
$this->genMessage(v::in(array(1, 1, 2, 3, 5, 8)), 9845984);
|
||||
$this->doTestValidator(v::in(array(1, 1, 2, 3, 5, 8)), 9845984);
|
||||
}
|
||||
|
||||
public function testInstance()
|
||||
{
|
||||
$this->genMessage(v::instance('\ss'), new \stdClass);
|
||||
$this->doTestValidator(v::instance('\ss'), new \stdClass);
|
||||
}
|
||||
|
||||
public function testInt()
|
||||
{
|
||||
$this->genMessage(v::int(), 15.48);
|
||||
$this->doTestValidator(v::int(), 15.48);
|
||||
}
|
||||
|
||||
public function testIp()
|
||||
{
|
||||
$this->genMessage(v::ip(), '200.999.220.222');
|
||||
$this->doTestValidator(v::ip(), '200.999.220.222');
|
||||
}
|
||||
|
||||
public function testLength()
|
||||
{
|
||||
$this->genMessage(v::length(5, 10), 'foobarbalzobihbiy');
|
||||
$this->genMessage(v::length(2, 3), array(1, 2, 3, 4, 5));
|
||||
$this->genMessage(v::length(null, 3), array(1, 2, 3, 4, 5));
|
||||
$this->genMessage(v::length(15, null), array(1, 2, 3, 4, 5));
|
||||
$this->doTestValidator(v::length(5, 10), 'foobarbalzobihbiy');
|
||||
$this->doTestValidator(v::length(2, 3), array(1, 2, 3, 4, 5));
|
||||
$this->doTestValidator(v::length(null, 3), array(1, 2, 3, 4, 5));
|
||||
$this->doTestValidator(v::length(15, null), array(1, 2, 3, 4, 5));
|
||||
}
|
||||
|
||||
public function testMax()
|
||||
{
|
||||
$this->genMessage(v::max(5), 9854);
|
||||
$this->genMessage(v::max(5, true), 9854);
|
||||
$this->doTestValidator(v::max(5), 9854);
|
||||
$this->doTestValidator(v::max(5, true), 9854);
|
||||
}
|
||||
|
||||
public function testMin()
|
||||
{
|
||||
$this->genMessage(v::min(5), -9514);
|
||||
$this->genMessage(v::min(5, true), -9514);
|
||||
$this->doTestValidator(v::min(5), -9514);
|
||||
$this->doTestValidator(v::min(5, true), -9514);
|
||||
}
|
||||
|
||||
public function testNegative()
|
||||
{
|
||||
$this->genMessage(v::negative(), 5);
|
||||
$this->doTestValidator(v::negative(), 5);
|
||||
}
|
||||
|
||||
public function testPositive()
|
||||
{
|
||||
$this->genMessage(v::positive(), -3);
|
||||
$this->doTestValidator(v::positive(), -3);
|
||||
}
|
||||
|
||||
public function testNoWhitespace()
|
||||
{
|
||||
$this->genMessage(v::noWhitespace(), 'a bc');
|
||||
$this->doTestValidator(v::noWhitespace(), 'a bc');
|
||||
}
|
||||
|
||||
public function testNotEmpty()
|
||||
{
|
||||
$this->genMessage(v::notEmpty(), '');
|
||||
$this->doTestValidator(v::notEmpty(), '');
|
||||
}
|
||||
|
||||
public function testNullValue()
|
||||
{
|
||||
$this->genMessage(v::nullValue(), true);
|
||||
$this->doTestValidator(v::nullValue(), true);
|
||||
}
|
||||
|
||||
public function testNumeric()
|
||||
{
|
||||
$this->genMessage(v::numeric(), null);
|
||||
$this->doTestValidator(v::numeric(), null);
|
||||
}
|
||||
|
||||
public function testObject()
|
||||
{
|
||||
$this->genMessage(v::object(), null);
|
||||
$this->doTestValidator(v::object(), null);
|
||||
}
|
||||
|
||||
public function testRegex()
|
||||
{
|
||||
$this->genMessage(v::regex('^[a-f]+$'), 'abcdxxxef');
|
||||
$this->doTestValidator(v::regex('^[a-f]+$'), 'abcdxxxef');
|
||||
}
|
||||
|
||||
public function testString()
|
||||
{
|
||||
$this->genMessage(v::string(), null);
|
||||
$this->doTestValidator(v::string(), null);
|
||||
}
|
||||
|
||||
public function testAllOf()
|
||||
{
|
||||
$this->genMessage(v::allOf(
|
||||
$this->doTestValidator(v::allOf(
|
||||
v::string(), //any string
|
||||
v::length(5, 20), //between 5 and 20 chars
|
||||
v::noWhitespace() //no whitespace allowed
|
||||
|
@ -200,7 +199,7 @@ class NegativeTest extends \PHPUnit_Framework_TestCase
|
|||
|
||||
//same as
|
||||
|
||||
$this->genMessage(v::string()
|
||||
$this->doTestValidator(v::string()
|
||||
->length(5, 20)
|
||||
->noWhitespace(), '# #');
|
||||
}
|
||||
|
@ -212,9 +211,9 @@ class NegativeTest extends \PHPUnit_Framework_TestCase
|
|||
v::float()->negative(), //negative float or;
|
||||
v::nullValue() //null
|
||||
);
|
||||
$this->genMessage($v, '');
|
||||
$this->genMessage($v, '');
|
||||
$this->genMessage($v, '');
|
||||
$this->doTestValidator($v, '');
|
||||
$this->doTestValidator($v, '');
|
||||
$this->doTestValidator($v, '');
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue