mirror of
https://github.com/Respect/Validation.git
synced 2024-06-04 14:52:17 +02:00
Fixed coding standards.
Coding standards must be follow PSR-1 and PSR-2.
This commit is contained in:
parent
80ed90d92c
commit
6c007df1d1
|
@ -18,10 +18,11 @@ class ExceptionIterator extends RecursiveArrayIterator
|
|||
|
||||
public function hasChildren()
|
||||
{
|
||||
if (!$this->current() instanceof AbstractNestedException)
|
||||
if (!$this->current() instanceof AbstractNestedException) {
|
||||
return false;
|
||||
else
|
||||
} else {
|
||||
return (boolean) $this->current()->getRelated($this->fullRelated);
|
||||
}
|
||||
}
|
||||
|
||||
public function getChildren()
|
||||
|
@ -30,4 +31,3 @@ class ExceptionIterator extends RecursiveArrayIterator
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -21,15 +21,17 @@ class AbstractGroupedException extends AbstractNestedException
|
|||
{
|
||||
$numRules = $this->getParam('passed');
|
||||
$numFailed = count($this->getRelated());
|
||||
|
||||
return $numRules === $numFailed ? static::NONE : static::SOME;
|
||||
}
|
||||
|
||||
public function getParams()
|
||||
{
|
||||
if (1 === count($this->related))
|
||||
if (1 === count($this->related)) {
|
||||
return current($this->related)->getParams();
|
||||
else
|
||||
} else {
|
||||
return parent::getParams();
|
||||
}
|
||||
}
|
||||
|
||||
public function getTemplate()
|
||||
|
@ -37,11 +39,12 @@ class AbstractGroupedException extends AbstractNestedException
|
|||
$parentTemplate = parent::getTemplate();
|
||||
$isEmpty = empty($this->template);
|
||||
|
||||
if (!$isEmpty && $this->template != $parentTemplate)
|
||||
if (!$isEmpty && $this->template != $parentTemplate) {
|
||||
return $this->template;
|
||||
if ($isEmpty && 1 === count($this->related))
|
||||
} if ($isEmpty && 1 === count($this->related)) {
|
||||
return current($this->related)->getTemplate();
|
||||
else
|
||||
} else {
|
||||
return $parentTemplate;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,6 +16,7 @@ class AbstractNestedException extends ValidationException
|
|||
public function addRelated(ValidationException $related)
|
||||
{
|
||||
$this->related[spl_object_hash($related)] = $related;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
@ -29,12 +30,14 @@ class AbstractNestedException extends ValidationException
|
|||
|
||||
$e = $this->findRelated($path);
|
||||
|
||||
if (is_object($e) && !$numericKey)
|
||||
if (is_object($e) && !$numericKey) {
|
||||
$e->setTemplate($value);
|
||||
}
|
||||
|
||||
$path = str_replace('.', '_', $path);
|
||||
$messages[$path] = $e ? $e->getMainMessage() : '';
|
||||
}
|
||||
|
||||
return $messages;
|
||||
}
|
||||
|
||||
|
@ -43,8 +46,10 @@ class AbstractNestedException extends ValidationException
|
|||
$target = $this;
|
||||
$path = explode('.', $path);
|
||||
|
||||
while (!empty($path) && $target !== false)
|
||||
while (!empty($path) && $target !== false) {
|
||||
$target = $target->getRelatedByName(array_shift($path));
|
||||
}
|
||||
|
||||
return $target;
|
||||
}
|
||||
|
||||
|
@ -52,43 +57,50 @@ class AbstractNestedException extends ValidationException
|
|||
{
|
||||
$exceptionIterator = new ExceptionIterator($this, $full);
|
||||
|
||||
if ($mode == self::ITERATE_ALL)
|
||||
if ($mode == self::ITERATE_ALL) {
|
||||
return new RecursiveIteratorIterator($exceptionIterator, 1);
|
||||
else
|
||||
} else {
|
||||
return new RecursiveTreeIterator($exceptionIterator);
|
||||
}
|
||||
}
|
||||
|
||||
public function getFullMessage()
|
||||
{
|
||||
$message = array();
|
||||
$iterator = $this->getIterator(false, self::ITERATE_TREE);
|
||||
foreach ($iterator as $m)
|
||||
foreach ($iterator as $m) {
|
||||
$message[] = $m;
|
||||
}
|
||||
|
||||
return implode(PHP_EOL, $message);
|
||||
}
|
||||
|
||||
public function getRelated($full=false)
|
||||
{
|
||||
if (!$full && 1 === count($this->related)
|
||||
&& current($this->related) instanceof AbstractNestedException)
|
||||
if (!$full && 1 === count($this->related)
|
||||
&& current($this->related) instanceof AbstractNestedException) {
|
||||
return current($this->related)->getRelated();
|
||||
else
|
||||
} else {
|
||||
return $this->related;
|
||||
}
|
||||
}
|
||||
|
||||
public function getRelatedByName($name)
|
||||
{
|
||||
foreach ($this->getIterator(true) as $e)
|
||||
if ($e->getId() === $name || $e->getName() === $name)
|
||||
foreach ($this->getIterator(true) as $e) {
|
||||
if ($e->getId() === $name || $e->getName() === $name) {
|
||||
return $e;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function setRelated(array $relatedExceptions)
|
||||
{
|
||||
foreach ($relatedExceptions as $related)
|
||||
foreach ($relatedExceptions as $related) {
|
||||
$this->addRelated($related);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
|
|
@ -17,4 +17,3 @@ class AllOfException extends AbstractGroupedException
|
|||
);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -17,4 +17,3 @@ class AlnumException extends AlphaException
|
|||
);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -23,4 +23,3 @@ class AlphaException extends ValidationException
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class AlwaysInalidException extends ValidationException
|
||||
class AlwaysInvalidException extends ValidationException
|
||||
{
|
||||
|
||||
public static $defaultTemplates = array(
|
||||
|
@ -15,4 +15,3 @@ class AlwaysInalidException extends ValidationException
|
|||
);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -15,4 +15,3 @@ class AlwaysValidException extends ValidationException
|
|||
);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -15,4 +15,3 @@ class ArrException extends ValidationException
|
|||
);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -23,4 +23,3 @@ class AttributeException extends AbstractNestedException
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -4,6 +4,5 @@ namespace Respect\Validation\Exceptions;
|
|||
|
||||
class BaseException extends ValidationException
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -25,17 +25,19 @@ class BetweenException extends AbstractNestedException
|
|||
{
|
||||
$params['minValue'] = static::stringify($params['minValue']);
|
||||
$params['maxValue'] = static::stringify($params['maxValue']);
|
||||
|
||||
return parent::configure($name, $params);
|
||||
}
|
||||
|
||||
public function chooseTemplate()
|
||||
{
|
||||
if (!$this->getParam('minValue'))
|
||||
if (!$this->getParam('minValue')) {
|
||||
return static::GREATER;
|
||||
elseif (!$this->getParam('maxValue'))
|
||||
} elseif (!$this->getParam('maxValue')) {
|
||||
return static::LOWER;
|
||||
else
|
||||
} else {
|
||||
return static::BOTH;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -15,4 +15,3 @@ class BoolException extends ValidationException
|
|||
);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -4,6 +4,5 @@ namespace Respect\Validation\Exceptions;
|
|||
|
||||
class CallException extends AbstractGroupedException
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -15,4 +15,3 @@ class CharsetException extends ValidationException
|
|||
);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -14,4 +14,4 @@ class CnhException extends ValidationException
|
|||
)
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,6 +6,5 @@ use Exception;
|
|||
|
||||
class ComponentException extends Exception
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -15,4 +15,3 @@ class ContainsException extends ValidationException
|
|||
);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -15,4 +15,3 @@ class CountryCodeException extends ValidationException
|
|||
);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -22,6 +22,7 @@ class DateException extends ValidationException
|
|||
$params['format'] = date(
|
||||
$params['format'], strtotime('2005-12-30 01:02:03')
|
||||
);
|
||||
|
||||
return parent::configure($name, $params);
|
||||
}
|
||||
|
||||
|
|
|
@ -17,4 +17,3 @@ class DigitsException extends AlphaException
|
|||
);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -15,4 +15,3 @@ class EachException extends AbstractNestedException
|
|||
);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -14,4 +14,4 @@ class EmailException extends ValidationException
|
|||
)
|
||||
);
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,4 +15,3 @@ class EndsWithException extends ValidationException
|
|||
);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -15,4 +15,3 @@ class EvenException extends ValidationException
|
|||
);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -15,4 +15,3 @@ class FloatException extends ValidationException
|
|||
);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -15,4 +15,3 @@ class HexaException extends ValidationException
|
|||
);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -15,4 +15,3 @@ class InException extends ValidationException
|
|||
);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -15,4 +15,3 @@ class IntException extends ValidationException
|
|||
);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -24,10 +24,11 @@ class IpException extends ValidationException
|
|||
$range = $params['networkRange'];
|
||||
$message = $range['min'];
|
||||
|
||||
if (isset($range['max']))
|
||||
if (isset($range['max'])) {
|
||||
$message .= '-' . $range['max'];
|
||||
else
|
||||
} else {
|
||||
$message .= '/' . long2ip($range['mask']);
|
||||
}
|
||||
|
||||
$params['range'] = $message;
|
||||
}
|
||||
|
@ -37,11 +38,11 @@ class IpException extends ValidationException
|
|||
|
||||
public function chooseTemplate()
|
||||
{
|
||||
if (!$this->getParam('networkRange'))
|
||||
if (!$this->getParam('networkRange')) {
|
||||
return static::STANDARD;
|
||||
else
|
||||
} else {
|
||||
return static::NETWORK_RANGE;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -25,18 +25,19 @@ class LengthException extends ValidationException
|
|||
{
|
||||
$params['minValue'] = static::stringify($params['minValue']);
|
||||
$params['maxValue'] = static::stringify($params['maxValue']);
|
||||
|
||||
return parent::configure($name, $params);
|
||||
}
|
||||
|
||||
public function chooseTemplate()
|
||||
{
|
||||
if (!$this->getParam('minValue'))
|
||||
if (!$this->getParam('minValue')) {
|
||||
return static::GREATER;
|
||||
elseif (!$this->getParam('maxValue'))
|
||||
} elseif (!$this->getParam('maxValue')) {
|
||||
return static::LOWER;
|
||||
else
|
||||
} else {
|
||||
return static::BOTH;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -15,4 +15,3 @@ class LowercaseException extends ValidationException
|
|||
);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -15,4 +15,3 @@ class MacAddressException extends ValidationException
|
|||
);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -4,7 +4,7 @@ namespace Respect\Validation\Exceptions;
|
|||
|
||||
class MinimumAgeException extends ValidationException
|
||||
{
|
||||
|
||||
|
||||
public static $defaultTemplates = array(
|
||||
self::MODE_DEFAULT => array(
|
||||
self::STANDARD => 'The age must be {{age}} years or more.',
|
||||
|
|
|
@ -4,6 +4,5 @@ namespace Respect\Validation\Exceptions;
|
|||
|
||||
class MostOfException extends AtLeastException
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ namespace Respect\Validation\Exceptions;
|
|||
|
||||
class MultipleException extends ValidationException
|
||||
{
|
||||
|
||||
|
||||
public static $defaultTemplates = array(
|
||||
self::MODE_DEFAULT => array(
|
||||
self::STANDARD => '{{name}} must be multiple of {{multipleOf}}',
|
||||
|
@ -15,5 +15,3 @@ class MultipleException extends ValidationException
|
|||
);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -15,4 +15,3 @@ class NoWhitespaceException extends ValidationException
|
|||
);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -23,4 +23,3 @@ class NotEmptyException extends ValidationException
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -15,4 +15,3 @@ class NumericException extends ValidationException
|
|||
);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -15,4 +15,3 @@ class ObjectException extends ValidationException
|
|||
);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -15,4 +15,3 @@ class OneOfException extends AbstractNestedException
|
|||
);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -15,4 +15,3 @@ class PerfectSquareException extends ValidationException
|
|||
);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -15,4 +15,3 @@ class RegexException extends ValidationException
|
|||
);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -15,4 +15,3 @@ class RomanException extends ValidationException
|
|||
);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -15,4 +15,3 @@ class StartsWithException extends ValidationException
|
|||
);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -15,4 +15,3 @@ class StringException extends ValidationException
|
|||
);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -15,4 +15,3 @@ class TldException extends ValidationException
|
|||
);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -15,4 +15,3 @@ class UppercaseException extends ValidationException
|
|||
);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -29,32 +29,35 @@ class ValidationException extends InvalidArgumentException
|
|||
{
|
||||
return preg_replace_callback(
|
||||
'/{{(\w+)}}/',
|
||||
function($match) use($vars) {
|
||||
function($match) use ($vars) {
|
||||
return isset($vars[$match[1]]) ? $vars[$match[1]] : $match[0];
|
||||
}, $template
|
||||
},
|
||||
$template
|
||||
);
|
||||
}
|
||||
|
||||
public static function stringify($value)
|
||||
{
|
||||
if (is_string($value))
|
||||
if (is_string($value)) {
|
||||
return $value;
|
||||
elseif (is_array($value))
|
||||
} elseif (is_array($value)) {
|
||||
return 'Array'; //FIXME
|
||||
elseif (is_object($value))
|
||||
} elseif (is_object($value)) {
|
||||
return static::stringifyObject($value);
|
||||
else
|
||||
} else {
|
||||
return (string) $value;
|
||||
}
|
||||
}
|
||||
|
||||
public static function stringifyObject($value)
|
||||
{
|
||||
if (method_exists($value, '__toString'))
|
||||
if (method_exists($value, '__toString')) {
|
||||
return (string) $value;
|
||||
elseif ($value instanceof DateTime)
|
||||
} elseif ($value instanceof DateTime) {
|
||||
return $value->format('Y-m-d H:i:s');
|
||||
else
|
||||
} else {
|
||||
return "Object of class " . get_class($value);
|
||||
}
|
||||
}
|
||||
|
||||
public function __toString()
|
||||
|
@ -73,6 +76,7 @@ class ValidationException extends InvalidArgumentException
|
|||
$this->setParams($params);
|
||||
$this->message = $this->getMainMessage();
|
||||
$this->setId($this->guessId());
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
@ -91,8 +95,10 @@ class ValidationException extends InvalidArgumentException
|
|||
$vars = $this->getParams();
|
||||
$vars['name'] = $this->getName();
|
||||
$template = $this->getTemplate();
|
||||
if(isset($vars['translator']) && is_callable($vars['translator']))
|
||||
if (isset($vars['translator']) && is_callable($vars['translator'])) {
|
||||
$template = call_user_func($vars['translator'], $template);
|
||||
}
|
||||
|
||||
return static::format($template, $vars);
|
||||
}
|
||||
|
||||
|
@ -108,10 +114,11 @@ class ValidationException extends InvalidArgumentException
|
|||
|
||||
public function getTemplate()
|
||||
{
|
||||
if (!empty($this->template))
|
||||
if (!empty($this->template)) {
|
||||
return $this->template;
|
||||
else
|
||||
} else {
|
||||
return $this->template = $this->buildTemplate();
|
||||
}
|
||||
}
|
||||
|
||||
public function hasParam($name)
|
||||
|
@ -122,12 +129,14 @@ class ValidationException extends InvalidArgumentException
|
|||
public function setId($id)
|
||||
{
|
||||
$this->id = $id;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setName($name)
|
||||
{
|
||||
$this->name = static::stringify($name);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
@ -135,43 +144,51 @@ class ValidationException extends InvalidArgumentException
|
|||
{
|
||||
$this->mode = $mode;
|
||||
$this->template = $this->buildTemplate();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setParam($key, $value)
|
||||
{
|
||||
$this->params[$key] = ($key == 'translator') ? $value : static::stringify($value);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setParams(array $params)
|
||||
{
|
||||
foreach ($params as $key => $value)
|
||||
foreach ($params as $key => $value) {
|
||||
$this->setParam($key, $value);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setTemplate($template)
|
||||
{
|
||||
$this->template = $template;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function buildTemplate()
|
||||
{
|
||||
$templateKey = $this->chooseTemplate();
|
||||
|
||||
return static::$defaultTemplates[$this->mode][$templateKey];
|
||||
}
|
||||
|
||||
protected function guessId()
|
||||
{
|
||||
if (!empty($this->id) && $this->id != 'validation')
|
||||
if (!empty($this->id) && $this->id != 'validation') {
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
$classParts = explode('\\', get_called_class());
|
||||
$id = end($classParts);
|
||||
$id = lcfirst(str_replace('Exception', '', $id));
|
||||
|
||||
return $id;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -15,4 +15,3 @@ class ZendException extends AbstractNestedException
|
|||
);
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Exceptions\ValidationException;
|
||||
use Respect\Validation\Sanitizable;
|
||||
use Respect\Validation\Validatable;
|
||||
use Respect\Validation\Validator;
|
||||
|
||||
|
@ -19,10 +18,11 @@ abstract class AbstractComposite extends AbstractRule
|
|||
|
||||
public function addRule($validator, $arguments=array())
|
||||
{
|
||||
if (!$validator instanceof Validatable)
|
||||
if (!$validator instanceof Validatable) {
|
||||
$this->appendRule(Validator::buildRule($validator, $arguments));
|
||||
else
|
||||
} else {
|
||||
$this->appendRule($validator);
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
@ -34,15 +34,17 @@ abstract class AbstractComposite extends AbstractRule
|
|||
|
||||
public function addRules(array $validators)
|
||||
{
|
||||
foreach ($validators as $key => $spec)
|
||||
if ($spec instanceof Validatable)
|
||||
foreach ($validators as $key => $spec) {
|
||||
if ($spec instanceof Validatable) {
|
||||
$this->appendRule($spec);
|
||||
elseif (is_numeric($key) && is_array($spec))
|
||||
} elseif (is_numeric($key) && is_array($spec)) {
|
||||
$this->addRules($spec);
|
||||
elseif (is_array($spec))
|
||||
} elseif (is_array($spec)) {
|
||||
$this->addRule($key, $spec);
|
||||
else
|
||||
} else {
|
||||
$this->addRule($spec);
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
@ -54,16 +56,21 @@ abstract class AbstractComposite extends AbstractRule
|
|||
|
||||
public function hasRule($validator)
|
||||
{
|
||||
if (empty($this->rules))
|
||||
if (empty($this->rules)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($validator instanceof Validatable)
|
||||
if ($validator instanceof Validatable) {
|
||||
return isset($this->rules[spl_object_hash($validator)]);
|
||||
}
|
||||
|
||||
if (is_string($validator))
|
||||
foreach ($this->rules as $rule)
|
||||
if (get_class($rule) == __NAMESPACE__ . '\\' . $validator)
|
||||
if (is_string($validator)) {
|
||||
foreach ($this->rules as $rule) {
|
||||
if (get_class($rule) == __NAMESPACE__ . '\\' . $validator) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
@ -77,12 +84,14 @@ abstract class AbstractComposite extends AbstractRule
|
|||
{
|
||||
$validators = $this->getRules();
|
||||
$exceptions = array();
|
||||
foreach ($validators as $v)
|
||||
foreach ($validators as $v) {
|
||||
try {
|
||||
$v->assert($input);
|
||||
} catch (ValidationException $e) {
|
||||
$exceptions[] = $e;
|
||||
}
|
||||
}
|
||||
|
||||
return $exceptions;
|
||||
}
|
||||
|
||||
|
|
|
@ -38,4 +38,4 @@ abstract class AbstractCountryInfo extends AbstractRule
|
|||
return in_array(strtolower($input), $this->tldList);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -29,10 +29,11 @@ abstract class AbstractRelated extends AbstractRule implements Validatable
|
|||
{
|
||||
$hasReference = $this->hasReference($input);
|
||||
|
||||
if ($this->mandatory && !$hasReference)
|
||||
if ($this->mandatory && !$hasReference) {
|
||||
throw $this->reportError($input, array('hasReference' => false));
|
||||
elseif ((!$this->mandatory && !$hasReference) || !$this->validator)
|
||||
} elseif ((!$this->mandatory && !$hasReference) || !$this->validator) {
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
return $this->validator->assert($this->getReferenceValue($input));
|
||||
|
@ -47,10 +48,11 @@ abstract class AbstractRelated extends AbstractRule implements Validatable
|
|||
{
|
||||
$hasReference = $this->hasReference($input);
|
||||
|
||||
if ($this->mandatory && !$hasReference)
|
||||
if ($this->mandatory && !$hasReference) {
|
||||
throw $this->reportError($input, array('hasReference' => false));
|
||||
elseif ((!$this->mandatory && !$hasReference) || !$this->validator)
|
||||
} elseif ((!$this->mandatory && !$hasReference) || !$this->validator) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $this->validator->check($this->getReferenceValue($input));
|
||||
}
|
||||
|
@ -59,13 +61,13 @@ abstract class AbstractRelated extends AbstractRule implements Validatable
|
|||
{
|
||||
$hasReference = $this->hasReference($input);
|
||||
|
||||
if ($this->mandatory && !$hasReference)
|
||||
if ($this->mandatory && !$hasReference) {
|
||||
return false;
|
||||
elseif (!$this->mandatory && !$hasReference)
|
||||
} elseif (!$this->mandatory && !$hasReference) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return is_null($this->validator)
|
||||
|| $this->validator->validate($this->getReferenceValue($input));
|
||||
return (is_null($this->validator) || $this->validator->validate($this->getReferenceValue($input)));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
namespace Respect\Validation\Rules;
|
||||
|
||||
use Respect\Validation\Validatable;
|
||||
use Respect\Validation\Validator;
|
||||
use Respect\Validation\Exceptions\ValidationException;
|
||||
|
||||
abstract class AbstractRule implements Validatable
|
||||
|
@ -24,18 +23,21 @@ abstract class AbstractRule implements Validatable
|
|||
return $this->validate($input);
|
||||
}
|
||||
|
||||
public function addOr() {
|
||||
public function addOr()
|
||||
{
|
||||
$rules = func_get_args();
|
||||
array_unshift($rules, $this);
|
||||
|
||||
return new OneOf($rules);
|
||||
}
|
||||
|
||||
public function assert($input)
|
||||
{
|
||||
if ($this->validate($input))
|
||||
if ($this->validate($input)) {
|
||||
return true;
|
||||
else
|
||||
} else {
|
||||
throw $this->reportError($input);
|
||||
}
|
||||
}
|
||||
|
||||
public function check($input)
|
||||
|
@ -58,20 +60,24 @@ abstract class AbstractRule implements Validatable
|
|||
compact('input')
|
||||
);
|
||||
$exception->configure($name, $params);
|
||||
if (!is_null($this->template))
|
||||
if (!is_null($this->template)) {
|
||||
$exception->setTemplate($this->template);
|
||||
}
|
||||
|
||||
return $exception;
|
||||
}
|
||||
|
||||
public function setName($name)
|
||||
{
|
||||
$this->name = $name;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setTemplate($template)
|
||||
{
|
||||
$this->template = $template;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
@ -80,8 +86,8 @@ abstract class AbstractRule implements Validatable
|
|||
$currentFQN = get_called_class();
|
||||
$exceptionFQN = str_replace('\\Rules\\', '\\Exceptions\\', $currentFQN);
|
||||
$exceptionFQN .= 'Exception';
|
||||
|
||||
return new $exceptionFQN;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -15,24 +15,32 @@ class AllOf extends AbstractComposite
|
|||
'failed' => $numExceptions,
|
||||
'passed' => $numRules - $numExceptions
|
||||
);
|
||||
if (!empty($exceptions))
|
||||
if (!empty($exceptions)) {
|
||||
throw $this->reportError($input, $summary)->setRelated($exceptions);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function check($input)
|
||||
{
|
||||
foreach ($this->getRules() as $v)
|
||||
if (!$v->check($input))
|
||||
foreach ($this->getRules() as $v) {
|
||||
if (!$v->check($input)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
foreach ($this->getRules() as $v)
|
||||
if (!$v->validate($input))
|
||||
foreach ($this->getRules() as $v) {
|
||||
if (!$v->validate($input)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -12,24 +12,23 @@ class Alpha extends AbstractRule
|
|||
|
||||
public function __construct($additionalChars='')
|
||||
{
|
||||
if (!is_string($additionalChars))
|
||||
throw new ComponentException(
|
||||
'Invalid list of additional characters to be loaded'
|
||||
);
|
||||
if (!is_string($additionalChars)) {
|
||||
throw new ComponentException('Invalid list of additional characters to be loaded');
|
||||
}
|
||||
$this->additionalChars = $additionalChars;
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
if (!is_scalar($input))
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -11,10 +11,10 @@ class Attribute extends AbstractRelated
|
|||
|
||||
public function __construct($reference, Validatable $validator=null, $mandatory=true)
|
||||
{
|
||||
if (!is_string($reference) || empty($reference))
|
||||
throw new ComponentException(
|
||||
'Invalid attribute/property name'
|
||||
);
|
||||
if (!is_string($reference) || empty($reference)) {
|
||||
throw new ComponentException('Invalid attribute/property name');
|
||||
}
|
||||
|
||||
parent::__construct($reference, $validator, $mandatory);
|
||||
}
|
||||
|
||||
|
@ -22,6 +22,7 @@ class Attribute extends AbstractRelated
|
|||
{
|
||||
$propertyMirror = new ReflectionProperty($input, $this->reference);
|
||||
$propertyMirror->setAccessible(true);
|
||||
|
||||
return $propertyMirror->getValue($input);
|
||||
}
|
||||
|
||||
|
@ -31,4 +32,3 @@ class Attribute extends AbstractRelated
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -12,23 +12,22 @@ class Base extends AbstractRule
|
|||
|
||||
public function __construct($base=null, $chars=null)
|
||||
{
|
||||
if (!is_null($chars)) $this->chars = $chars;
|
||||
if (!is_null($chars)) {
|
||||
$this->chars = $chars;
|
||||
}
|
||||
|
||||
$max = strlen($this->chars);
|
||||
if (!is_numeric($base) || $base > $max)
|
||||
throw new BaseException(
|
||||
sprintf(
|
||||
'a base between 1 and %s is required', $max
|
||||
)
|
||||
);
|
||||
if (!is_numeric($base) || $base > $max) {
|
||||
throw new BaseException(sprintf('a base between 1 and %s is required', $max));
|
||||
}
|
||||
$this->base = $base;
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
$valid = substr($this->chars, 0, $this->base);
|
||||
return (boolean)preg_match("@^[$valid]+$@", (string)$input);
|
||||
|
||||
return (boolean) preg_match("@^[$valid]+$@", (string) $input);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -14,17 +14,17 @@ class Between extends AllOf
|
|||
{
|
||||
$this->minValue = $min;
|
||||
$this->maxValue = $max;
|
||||
if (!is_null($min) && !is_null($max) && $min > $max)
|
||||
throw new ComponentException(
|
||||
sprintf(
|
||||
'%s cannot be less than %s for validation', $min, $max
|
||||
)
|
||||
);
|
||||
if (!is_null($min))
|
||||
if (!is_null($min) && !is_null($max) && $min > $max) {
|
||||
throw new ComponentException(sprintf('%s cannot be less than %s for validation', $min, $max));
|
||||
}
|
||||
|
||||
if (!is_null($min)) {
|
||||
$this->addRule(new Min($min, $inclusive));
|
||||
if (!is_null($max))
|
||||
}
|
||||
|
||||
if (!is_null($max)) {
|
||||
$this->addRule(new Max($max, $inclusive));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -11,10 +11,10 @@ class Callback extends AbstractRule
|
|||
|
||||
public function __construct($callback)
|
||||
{
|
||||
if (!is_callable($callback))
|
||||
throw new ComponentException(
|
||||
'Invalid callback'
|
||||
);
|
||||
if (!is_callable($callback)) {
|
||||
throw new ComponentException('Invalid callback');
|
||||
}
|
||||
|
||||
$this->callback = $callback;
|
||||
}
|
||||
|
||||
|
@ -24,4 +24,3 @@ class Callback extends AbstractRule
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -33,4 +33,3 @@ class Charset extends AbstractRule
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -1,31 +1,35 @@
|
|||
<?php
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class Cnh extends AbstractRule
|
||||
class Cnh extends AbstractRule
|
||||
{
|
||||
public function validate($input)
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
$ret = false;
|
||||
|
||||
if ( ( strlen( $input = preg_replace( '/[^\d]/' , '' , $input ) ) == 11 ) &&
|
||||
( str_repeat( $input[1] , 11 ) != $input ) ) {
|
||||
$dsc = 0;
|
||||
if ((strlen($input = preg_replace('/[^\d]/', '', $input)) == 11 )
|
||||
&& (str_repeat($input[1], 11) != $input)) {
|
||||
|
||||
for ( $i = 0 , $j = 9 , $v = 0 ; $i < 9 ; ++$i , --$j ) {
|
||||
$dsc = 0;
|
||||
for ($i = 0 , $j = 9, $v = 0; $i < 9; ++$i, --$j) {
|
||||
$v += (int) $input[$i] * $j;
|
||||
}
|
||||
|
||||
if ( ( $vl1 = $v % 11 ) >= 10 ) {
|
||||
if (($vl1 = $v % 11) >= 10 ) {
|
||||
$vl1 = 0;
|
||||
$dsc = 2;
|
||||
}
|
||||
|
||||
for ( $i = 0 , $j = 1 , $v = 0 ; $i < 9 ; ++$i , ++$j ) {
|
||||
for ($i = 0, $j = 1, $v = 0; $i < 9; ++$i, ++$j) {
|
||||
$v += (int) $input[$i] * $j;
|
||||
}
|
||||
$vl2 = ( $x = ( $v % 11 ) ) >= 10 ? 0 : $x - $dsc;
|
||||
$ret = sprintf( '%d%d' , $vl1 , $vl2 ) == substr( $input , -2 );
|
||||
|
||||
$vl2 = ($x = ($v % 11)) >= 10 ? 0 : $x - $dsc;
|
||||
$ret = sprintf('%d%d', $vl1, $vl2) == substr($input, -2);
|
||||
}
|
||||
|
||||
return $ret;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -2,27 +2,32 @@
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class Cnpj extends AbstractRule
|
||||
class Cnpj extends AbstractRule
|
||||
{
|
||||
public function validate($input)
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
//Code ported from jsfromhell.com
|
||||
|
||||
$c = preg_replace('/\D/', '', $input);
|
||||
$b = array(6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2);
|
||||
|
||||
if (strlen($c) != 14)
|
||||
|
||||
if (strlen($c) != 14) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for ($i = 0, $n = 0; $i < 12; $n += $c[$i] * $b[++$i]);
|
||||
|
||||
if ($c[12] != ((($n %= 11) < 2) ? 0 : 11 - $n))
|
||||
|
||||
if ($c[12] != ((($n %= 11) < 2) ? 0 : 11 - $n)) {
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
for ($i = 0, $n = 0; $i <= 12; $n += $c[$i] * $b[$i++]);
|
||||
|
||||
if ($c[13] != ((($n %= 11) < 2) ? 0 : 11 - $n))
|
||||
|
||||
if ($c[13] != ((($n %= 11) < 2) ? 0 : 11 - $n)) {
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -9,4 +9,3 @@ class Consonants extends Alpha
|
|||
public $stringFormat = '/^(\s|[b-df-hj-np-tv-zB-DF-HJ-NP-TV-Z])*$/';
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -16,25 +16,28 @@ class Contains extends AbstractRule
|
|||
|
||||
public function validate($input)
|
||||
{
|
||||
if ($this->identical)
|
||||
if ($this->identical) {
|
||||
return $this->validateIdentical($input);
|
||||
|
||||
}
|
||||
|
||||
return $this->validateEquals($input);
|
||||
}
|
||||
|
||||
protected function validateEquals($input)
|
||||
{
|
||||
if (is_array($input))
|
||||
if (is_array($input)) {
|
||||
return in_array($this->containsValue, $input);
|
||||
|
||||
}
|
||||
|
||||
return false !== mb_stripos($input, $this->containsValue, 0, mb_detect_encoding($input));
|
||||
}
|
||||
|
||||
protected function validateIdentical($input)
|
||||
{
|
||||
if (is_array($input))
|
||||
if (is_array($input)) {
|
||||
return in_array($this->containsValue, $input, true);
|
||||
|
||||
}
|
||||
|
||||
return false !== mb_strpos($input, $this->containsValue, 0, mb_detect_encoding($input));
|
||||
}
|
||||
|
||||
|
|
|
@ -24,7 +24,7 @@ class CountryCode extends AbstractRule
|
|||
'SY', 'SZ', 'TC', 'TD', 'TF', 'TG', 'TH', 'TJ', 'TK', 'TL', 'TM', 'TN', 'TO', 'TR',
|
||||
'TT', 'TV', 'TW', 'TZ', 'UA', 'UG', 'UM', 'US', 'UY', 'UZ', 'VA', 'VC', 'VE', 'VG',
|
||||
'VI', 'VN', 'VU', 'WF', 'WS', 'YE', 'YT', 'ZA', 'ZM', 'ZW'
|
||||
);
|
||||
);
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
|
|
|
@ -2,30 +2,33 @@
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class Cpf extends AbstractRule
|
||||
class Cpf extends AbstractRule
|
||||
{
|
||||
public function validate($input)
|
||||
public function validate($input)
|
||||
{
|
||||
// Code ported from jsfromhell.com
|
||||
|
||||
$c = preg_replace('/\D/', '', $input);
|
||||
|
||||
if (strlen($c) != 11)
|
||||
return false;
|
||||
|
||||
if (preg_match("/^{$c[0]}{11}$/", $c))
|
||||
if (strlen($c) != 11) {
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
if (preg_match("/^{$c[0]}{11}$/", $c)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for ($s = 10, $n = 0, $i = 0; $s >= 2; $n += $c[$i++] * $s--);
|
||||
|
||||
if ($c[9] != ((($n %= 11) < 2) ? 0 : 11 - $n))
|
||||
|
||||
if ($c[9] != ((($n %= 11) < 2) ? 0 : 11 - $n)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for ($s = 11, $n = 0, $i = 0; $s >= 2; $n += $c[$i++] * $s--);
|
||||
|
||||
if ($c[10] != ((($n %= 11) < 2) ? 0 : 11 - $n))
|
||||
if ($c[10] != ((($n %= 11) < 2) ? 0 : 11 - $n)) {
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,8 +8,10 @@ class CreditCard extends AbstractRule
|
|||
public function validate($input)
|
||||
{
|
||||
$input = preg_replace('([^0-9])', '', $input);
|
||||
if(!empty($input))
|
||||
if (!empty($input)) {
|
||||
return $this->verifyMod10($input);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -17,14 +19,11 @@ class CreditCard extends AbstractRule
|
|||
{
|
||||
$sum = 0;
|
||||
$input = strrev($input);
|
||||
for ($i = 0; $i < strlen($input); $i++)
|
||||
{
|
||||
for ($i = 0; $i < strlen($input); $i++) {
|
||||
$current = substr($input, $i, 1);
|
||||
if ($i % 2 == 1)
|
||||
{
|
||||
if ($i % 2 == 1) {
|
||||
$current *= 2;
|
||||
if ($current > 9)
|
||||
{
|
||||
if ($current > 9) {
|
||||
$firstDigit = $current % 10;
|
||||
$secondDigit = ($current - $firstDigit) / 10;
|
||||
$current = $firstDigit + $secondDigit;
|
||||
|
@ -32,6 +31,7 @@ class CreditCard extends AbstractRule
|
|||
}
|
||||
$sum += $current;
|
||||
}
|
||||
|
||||
return ($sum % 10 == 0);
|
||||
}
|
||||
|
||||
|
|
|
@ -16,12 +16,13 @@ class Date extends AbstractRule
|
|||
|
||||
public function validate($input)
|
||||
{
|
||||
if ($input instanceof DateTime)
|
||||
if ($input instanceof DateTime) {
|
||||
return true;
|
||||
elseif (!is_string($input))
|
||||
} elseif (!is_string($input)) {
|
||||
return false;
|
||||
elseif (is_null($this->format))
|
||||
} elseif (is_null($this->format)) {
|
||||
return false !== strtotime($input);
|
||||
}
|
||||
|
||||
$dateFromFormat = DateTime::createFromFormat($this->format, $input);
|
||||
|
||||
|
@ -30,4 +31,3 @@ class Date extends AbstractRule
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -9,4 +9,3 @@ class Digits extends Alpha
|
|||
public $stringFormat = '/^\s*[0-9]+([0-9]|\s)*$/';
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -7,11 +7,11 @@ class Directory extends AbstractRule
|
|||
|
||||
public function validate($input)
|
||||
{
|
||||
if ($input instanceof \SplFileInfo) {
|
||||
return $input->isDir();
|
||||
}
|
||||
if ($input instanceof \SplFileInfo) {
|
||||
return $input->isDir();
|
||||
}
|
||||
|
||||
return (is_string($input) && is_dir($input));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -25,35 +25,46 @@ class Domain extends AbstractComposite
|
|||
$this->domainLength = new Length(3, null);
|
||||
$this->end = new Tld();
|
||||
$this->otherParts = new AllOf(
|
||||
new Alnum('-'),
|
||||
new Not(new StartsWith('-'))
|
||||
new Alnum('-'),
|
||||
new Not(new StartsWith('-'))
|
||||
);
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
if ($this->ip->validate($input))
|
||||
if ($this->ip->validate($input)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!$this->whitespace->validate($input)
|
||||
|| !$this->dot->validate($input)
|
||||
|| !$this->domainLength->validate($input))
|
||||
|| !$this->domainLength->validate($input)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$parts = explode('.', $input);
|
||||
if (count($parts) < 2)
|
||||
if (count($parts) < 2) {
|
||||
return false;
|
||||
if (!$this->end->validate(array_pop($parts)))
|
||||
}
|
||||
|
||||
if (!$this->end->validate(array_pop($parts))) {
|
||||
return false;
|
||||
foreach ($parts as $p)
|
||||
if (!$this->otherParts->validate($p))
|
||||
}
|
||||
|
||||
foreach ($parts as $p) {
|
||||
if (!$this->otherParts->validate($p)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function assert($input)
|
||||
{
|
||||
if ($this->ip->validate($input))
|
||||
if ($this->ip->validate($input)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$e = array();
|
||||
|
||||
|
@ -64,13 +75,18 @@ class Domain extends AbstractComposite
|
|||
|
||||
$parts = explode('.', $input);
|
||||
|
||||
if (count($parts) >= 2)
|
||||
if (count($parts) >= 2) {
|
||||
$this->collectAssertException($e, $this->end, array_pop($parts));
|
||||
foreach ($parts as $p)
|
||||
}
|
||||
|
||||
foreach ($parts as $p) {
|
||||
$this->collectAssertException($e, $this->otherParts, $p);
|
||||
}
|
||||
|
||||
if (count($e))
|
||||
if (count($e)) {
|
||||
throw $this->reportError($input)->setRelated($e);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -85,8 +101,10 @@ class Domain extends AbstractComposite
|
|||
|
||||
public function check($input)
|
||||
{
|
||||
if ($this->ip->validate($input))
|
||||
if ($this->ip->validate($input)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$this->whitespace->check($input);
|
||||
$this->dot->check($input);
|
||||
$this->domainLength->check($input);
|
||||
|
@ -94,13 +112,15 @@ class Domain extends AbstractComposite
|
|||
|
||||
$parts = explode('.', $input);
|
||||
|
||||
if (count($parts) >= 2)
|
||||
if (count($parts) >= 2) {
|
||||
$this->end->check(array_pop($parts));
|
||||
foreach ($parts as $p)
|
||||
}
|
||||
|
||||
foreach ($parts as $p) {
|
||||
$this->otherParts->check($p);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -23,46 +23,57 @@ class Each extends AbstractRule
|
|||
{
|
||||
$exceptions = array();
|
||||
|
||||
if (!is_array($input) || $input instanceof Traversable)
|
||||
if (!is_array($input) || $input instanceof Traversable) {
|
||||
throw $this->reportError($input);
|
||||
}
|
||||
|
||||
if (empty($input))
|
||||
if (empty($input)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
foreach ($input as $key => $item) {
|
||||
if (isset($this->itemValidator))
|
||||
if (isset($this->itemValidator)) {
|
||||
try {
|
||||
$this->itemValidator->assert($item);
|
||||
} catch (ValidationException $e) {
|
||||
$exceptions[] = $e;
|
||||
}
|
||||
if (isset($this->keyValidator))
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($this->keyValidator)) {
|
||||
try {
|
||||
$this->keyValidator->assert($key);
|
||||
} catch (ValidationException $e) {
|
||||
$exceptions[] = $e;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($exceptions))
|
||||
if (!empty($exceptions)) {
|
||||
throw $this->reportError($input)->setRelated($exceptions);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function check($input)
|
||||
{
|
||||
if (empty($input))
|
||||
if (empty($input)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!is_array($input) || $input instanceof Traversable)
|
||||
if (!is_array($input) || $input instanceof Traversable) {
|
||||
throw $this->reportError($input);
|
||||
}
|
||||
|
||||
foreach ($input as $key => $item) {
|
||||
if (isset($this->itemValidator))
|
||||
if (isset($this->itemValidator)) {
|
||||
$this->itemValidator->check($item);
|
||||
if (isset($this->keyValidator))
|
||||
}
|
||||
|
||||
if (isset($this->keyValidator)) {
|
||||
$this->keyValidator->check($key);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
|
@ -70,21 +81,25 @@ class Each extends AbstractRule
|
|||
|
||||
public function validate($input)
|
||||
{
|
||||
if (!is_array($input) || $input instanceof Traversable)
|
||||
if (!is_array($input) || $input instanceof Traversable) {
|
||||
return false;
|
||||
|
||||
if (empty($input))
|
||||
}
|
||||
|
||||
if (empty($input)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
foreach ($input as $key => $item) {
|
||||
if (isset($this->itemValidator) && !$this->itemValidator->validate($item))
|
||||
if (isset($this->itemValidator) && !$this->itemValidator->validate($item)) {
|
||||
return false;
|
||||
if (isset($this->keyValidator) && !$this->keyValidator->validate($key))
|
||||
}
|
||||
|
||||
if (isset($this->keyValidator) && !$this->keyValidator->validate($key)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -10,4 +10,4 @@ class Email extends AbstractRule
|
|||
return is_string($input) && filter_var($input, FILTER_VALIDATE_EMAIL);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,29 +16,31 @@ class EndsWith extends AbstractRule
|
|||
|
||||
public function validate($input)
|
||||
{
|
||||
if ($this->identical)
|
||||
if ($this->identical) {
|
||||
return $this->validateIdentical($input);
|
||||
else
|
||||
} else {
|
||||
return $this->validateEquals($input);
|
||||
}
|
||||
}
|
||||
|
||||
protected function validateEquals($input)
|
||||
{
|
||||
if (is_array($input))
|
||||
if (is_array($input)) {
|
||||
return end($input) == $this->endValue;
|
||||
else
|
||||
} else {
|
||||
return mb_strripos($input, $this->endValue, -1, $enc = mb_detect_encoding($input))
|
||||
=== mb_strlen($input, $enc) - mb_strlen($this->endValue, $enc) ;
|
||||
}
|
||||
}
|
||||
|
||||
protected function validateIdentical($input)
|
||||
{
|
||||
if (is_array($input))
|
||||
if (is_array($input)) {
|
||||
return end($input) === $this->endValue;
|
||||
else
|
||||
} else {
|
||||
return mb_strrpos($input, $this->endValue, 0, $enc = mb_detect_encoding($input))
|
||||
=== mb_strlen($input, $enc) - mb_strlen($this->endValue, $enc);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -21,11 +21,11 @@ class Equals extends AbstractRule
|
|||
|
||||
public function validate($input)
|
||||
{
|
||||
if ($this->compareIdentical)
|
||||
if ($this->compareIdentical) {
|
||||
return $input === $this->compareTo;
|
||||
else
|
||||
} else {
|
||||
return $input == $this->compareTo;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -2,7 +2,6 @@
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
|
||||
class Even extends AbstractRule
|
||||
{
|
||||
|
||||
|
|
|
@ -11,4 +11,3 @@ class Float extends AbstractRule
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -21,19 +21,21 @@ class In extends AbstractRule
|
|||
|
||||
public function validate($input)
|
||||
{
|
||||
if (is_array($this->haystack))
|
||||
if (is_array($this->haystack)) {
|
||||
return in_array($input, $this->haystack, $this->compareIdentical);
|
||||
|
||||
if (!is_string($this->haystack))
|
||||
}
|
||||
|
||||
if (!is_string($this->haystack)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$enc = mb_detect_encoding($input);
|
||||
|
||||
if ($this->compareIdentical)
|
||||
|
||||
if ($this->compareIdentical) {
|
||||
return mb_strpos($this->haystack, $input, 0, $enc) !== false;
|
||||
|
||||
}
|
||||
|
||||
return mb_stripos($this->haystack, $input, 0, $enc) !== false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -15,6 +15,7 @@ class Ip extends AbstractRule
|
|||
{
|
||||
if (is_int($ipOptions)) {
|
||||
$this->ipOptions = $ipOptions;
|
||||
|
||||
return ;
|
||||
}
|
||||
|
||||
|
@ -24,19 +25,21 @@ class Ip extends AbstractRule
|
|||
protected function parseRange($input)
|
||||
{
|
||||
if ($input === null || $input == '*' || $input == '*.*.*.*'
|
||||
|| $input == '0.0.0.0-255.255.255.255')
|
||||
|| $input == '0.0.0.0-255.255.255.255') {
|
||||
return null;
|
||||
}
|
||||
|
||||
$range = array('min' => null, 'max' => null, 'mask' => null);
|
||||
|
||||
if (strpos($input, '-') !== false)
|
||||
if (strpos($input, '-') !== false) {
|
||||
list($range['min'], $range['max']) = explode('-', $input);
|
||||
elseif (strpos($input, '*') !== false) {
|
||||
} elseif (strpos($input, '*') !== false) {
|
||||
$this->parseRangeUsingWildcards($input, $range);
|
||||
} elseif (strpos($input, '/') !== false) {
|
||||
$this->parseRangeUsingCidr($input, $range);
|
||||
} else
|
||||
} else {
|
||||
throw new ComponentException('Invalid network range');
|
||||
}
|
||||
|
||||
if (!$this->verifyAddress($range['min'])) {
|
||||
throw new ComponentException('Invalid network range');
|
||||
|
@ -103,8 +106,9 @@ class Ip extends AbstractRule
|
|||
|
||||
protected function verifyNetwork($input)
|
||||
{
|
||||
if ($this->networkRange === null)
|
||||
if ($this->networkRange === null) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (isset($this->networkRange['mask'])) {
|
||||
return $this->belongsToSubnet($input);
|
||||
|
|
|
@ -5,11 +5,9 @@ namespace Respect\Validation\Rules;
|
|||
class Json extends AbstractRule
|
||||
{
|
||||
|
||||
public function validate($input)
|
||||
public function validate($input)
|
||||
{
|
||||
return (bool) (json_decode($input));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -10,10 +10,9 @@ class Key extends AbstractRelated
|
|||
|
||||
public function __construct($reference, Validatable $referenceValidator=null, $mandatory=true)
|
||||
{
|
||||
if (!is_string($reference) || empty($reference))
|
||||
throw new ComponentException(
|
||||
'Invalid array key name'
|
||||
);
|
||||
if (!is_string($reference) || empty($reference)) {
|
||||
throw new ComponentException('Invalid array key name');
|
||||
}
|
||||
parent::__construct($reference, $referenceValidator, $mandatory);
|
||||
}
|
||||
|
||||
|
|
|
@ -16,17 +16,16 @@ class LeapDate extends AbstractRule
|
|||
|
||||
public function validate($input)
|
||||
{
|
||||
if (is_string($input))
|
||||
if (is_string($input)) {
|
||||
$date = DateTime::createFromFormat($this->format, $input);
|
||||
elseif ($input instanceof DateTime)
|
||||
} elseif ($input instanceof DateTime) {
|
||||
$date = $input;
|
||||
else
|
||||
return false;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Dates that aren't leap will aways be rounded
|
||||
return $date->format('m-d') == '02-29';
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -8,19 +8,19 @@ class LeapYear extends AbstractRule
|
|||
{
|
||||
public function validate($year)
|
||||
{
|
||||
if (is_numeric($year))
|
||||
$year = (int) $year;
|
||||
elseif (is_string($year))
|
||||
if (is_numeric($year)) {
|
||||
$year = (int) $year;
|
||||
} elseif (is_string($year)) {
|
||||
$year = (int) date('Y', strtotime($year));
|
||||
elseif ($year instanceof DateTime)
|
||||
} elseif ($year instanceof DateTime) {
|
||||
$year = (int) $year->format('Y');
|
||||
else
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
$date = strtotime(sprintf('%d-02-29', $year));
|
||||
|
||||
return (bool) date('L', $date);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -5,7 +5,6 @@ namespace Respect\Validation\Rules;
|
|||
use Countable;
|
||||
use Respect\Validation\Exceptions\ComponentException;
|
||||
use Respect\Validation\Rules\AbstractRule;
|
||||
use Respect\Validation\Validator;
|
||||
|
||||
class Length extends AbstractRule
|
||||
{
|
||||
|
@ -20,15 +19,17 @@ class Length extends AbstractRule
|
|||
$this->maxValue = $max;
|
||||
$this->inclusive = $inclusive;
|
||||
$paramValidator = new OneOf(new Numeric, new NullValue);
|
||||
if (!$paramValidator->validate($min))
|
||||
if (!$paramValidator->validate($min)) {
|
||||
throw new ComponentException(
|
||||
sprintf('%s is not a valid numeric length', $min)
|
||||
);
|
||||
}
|
||||
|
||||
if (!$paramValidator->validate($max))
|
||||
if (!$paramValidator->validate($max)) {
|
||||
throw new ComponentException(
|
||||
sprintf('%s is not a valid numeric length', $max)
|
||||
);
|
||||
}
|
||||
|
||||
if (!is_null($min) && !is_null($max) && $min > $max) {
|
||||
throw new ComponentException(
|
||||
|
@ -40,28 +41,32 @@ class Length extends AbstractRule
|
|||
public function validate($input)
|
||||
{
|
||||
$length = $this->extractLength($input);
|
||||
|
||||
return $this->validateMin($length) && $this->validateMax($length);
|
||||
}
|
||||
|
||||
protected function extractLength($input)
|
||||
{
|
||||
if (is_string($input))
|
||||
if (is_string($input)) {
|
||||
return mb_strlen($input, mb_detect_encoding($input));
|
||||
elseif (is_array($input) || $input instanceof Countable)
|
||||
} elseif (is_array($input) || $input instanceof Countable) {
|
||||
return count($input);
|
||||
elseif (is_object($input))
|
||||
} elseif (is_object($input)) {
|
||||
return count(get_object_vars($input));
|
||||
else
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
protected function validateMin($length)
|
||||
{
|
||||
if (is_null($this->minValue))
|
||||
if (is_null($this->minValue)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($this->inclusive)
|
||||
if ($this->inclusive) {
|
||||
return $length >= $this->minValue;
|
||||
}
|
||||
|
||||
return $length > $this->minValue;
|
||||
}
|
||||
|
@ -80,4 +85,3 @@ class Length extends AbstractRule
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -10,4 +10,4 @@ class Lowercase extends AbstractRule
|
|||
return $input === mb_strtolower($input, mb_detect_encoding($input));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,8 +6,8 @@ class MacAddress extends AbstractRule
|
|||
{
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
{
|
||||
return !empty($input) && preg_match('/^(([0-9a-fA-F]{2}-){5}|([0-9a-fA-F]{2}:){5})[0-9a-fA-F]{2}$/', $input);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -16,10 +16,11 @@ class Max extends AbstractRule
|
|||
|
||||
public function validate($input)
|
||||
{
|
||||
if ($this->inclusive)
|
||||
if ($this->inclusive) {
|
||||
return $input <= $this->maxValue;
|
||||
else
|
||||
} else {
|
||||
return $input < $this->maxValue;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -16,11 +16,11 @@ class Min extends AbstractRule
|
|||
|
||||
public function validate($input)
|
||||
{
|
||||
if ($this->inclusive)
|
||||
if ($this->inclusive) {
|
||||
return $input >= $this->minValue;
|
||||
else
|
||||
} else {
|
||||
return $input > $this->minValue;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -9,7 +9,7 @@ class MinimumAge extends AbstractRule
|
|||
|
||||
public $age = null;
|
||||
public $format = null;
|
||||
|
||||
|
||||
public function __construct($age, $format=null)
|
||||
{
|
||||
$this->age = $age;
|
||||
|
@ -18,20 +18,21 @@ class MinimumAge extends AbstractRule
|
|||
|
||||
public function validate($input)
|
||||
{
|
||||
if (!is_int($this->age))
|
||||
if (!is_int($this->age)) {
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
if ($input instanceof DateTime) {
|
||||
$birthday = new \DateTime('now - '.$this->age.' year');
|
||||
|
||||
return $birthday > $input;
|
||||
}
|
||||
elseif (!is_string($input) || (is_null($this->format) && false === strtotime($input)))
|
||||
} elseif (!is_string($input) || (is_null($this->format) && false === strtotime($input))) {
|
||||
return false;
|
||||
else {
|
||||
} else {
|
||||
$age = ((date('Ymd') - date('Ymd', strtotime($input))) / 10000);
|
||||
|
||||
return $age >= $this->age;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -11,11 +11,12 @@ class Multiple extends AbstractRule
|
|||
{
|
||||
$this->multipleOf = $multipleOf;
|
||||
}
|
||||
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
if ($this->multipleOf == 0)
|
||||
if ($this->multipleOf == 0) {
|
||||
return ($input == 0);
|
||||
}
|
||||
|
||||
return ($input % $this->multipleOf == 0);
|
||||
}
|
||||
|
|
|
@ -11,4 +11,3 @@ class Negative extends AbstractRule
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -10,18 +10,22 @@ class NoneOf extends AbstractComposite
|
|||
$exceptions = $this->validateRules($input);
|
||||
$numRules = count($this->getRules());
|
||||
$numExceptions = count($exceptions);
|
||||
if ($numRules !== $numExceptions)
|
||||
if ($numRules !== $numExceptions) {
|
||||
throw $this->reportError($input)->setRelated($exceptions);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
foreach ($this->getRules() as $rule)
|
||||
if ($rule->validate($input))
|
||||
foreach ($this->getRules() as $rule) {
|
||||
if ($rule->validate($input)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -12,24 +12,27 @@ class Not extends AbstractRule
|
|||
|
||||
public function __construct(Validatable $rule)
|
||||
{
|
||||
if ($rule instanceof AbstractComposite)
|
||||
if ($rule instanceof AbstractComposite) {
|
||||
$rule = $this->absorbComposite($rule);
|
||||
}
|
||||
|
||||
$this->rule = $rule;
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
if ($this->rule instanceof AbstractComposite)
|
||||
if ($this->rule instanceof AbstractComposite) {
|
||||
return $this->rule->validate($input);
|
||||
}
|
||||
|
||||
return!$this->rule->validate($input);
|
||||
}
|
||||
|
||||
public function assert($input)
|
||||
{
|
||||
if ($this->rule instanceof AbstractComposite)
|
||||
if ($this->rule instanceof AbstractComposite) {
|
||||
return $this->rule->assert($input);
|
||||
}
|
||||
|
||||
try {
|
||||
$this->rule->assert($input);
|
||||
|
@ -48,11 +51,13 @@ class Not extends AbstractRule
|
|||
$rules = $clone->getRules();
|
||||
$clone->removeRules();
|
||||
|
||||
foreach ($rules as &$r)
|
||||
if ($r instanceof AbstractComposite)
|
||||
foreach ($rules as &$r) {
|
||||
if ($r instanceof AbstractComposite) {
|
||||
$clone->addRule($this->absorbComposite($r));
|
||||
else
|
||||
} else {
|
||||
$clone->addRule(new static($r));
|
||||
}
|
||||
}
|
||||
|
||||
return $clone;
|
||||
}
|
||||
|
|
|
@ -7,8 +7,10 @@ class NotEmpty extends AbstractRule
|
|||
|
||||
public function validate($input)
|
||||
{
|
||||
if (is_string($input))
|
||||
if (is_string($input)) {
|
||||
$input = trim($input);
|
||||
}
|
||||
|
||||
return !empty($input);
|
||||
}
|
||||
|
||||
|
|
|
@ -14,35 +14,43 @@ class OneOf extends AbstractComposite
|
|||
$numRules = count($validators);
|
||||
$numExceptions = count($exceptions);
|
||||
$numPassed = $numRules - $numExceptions;
|
||||
if ($numExceptions === $numRules)
|
||||
if ($numExceptions === $numRules) {
|
||||
throw $this->reportError($input)->setRelated($exceptions);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
foreach ($this->getRules() as $v)
|
||||
if ($v->validate($input))
|
||||
foreach ($this->getRules() as $v) {
|
||||
if ($v->validate($input)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function check($input)
|
||||
{
|
||||
foreach ($this->getRules() as $v)
|
||||
foreach ($this->getRules() as $v) {
|
||||
try {
|
||||
if ($v->check($input))
|
||||
if ($v->check($input)) {
|
||||
return true;
|
||||
}
|
||||
} catch (ValidationException $e) {
|
||||
if (!isset($firstException))
|
||||
if (!isset($firstException)) {
|
||||
$firstException = $e;
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($firstException))
|
||||
}
|
||||
|
||||
if (isset($firstException)) {
|
||||
throw $firstException;
|
||||
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -7,5 +7,5 @@ class PerfectSquare extends AbstractRule
|
|||
{
|
||||
return is_numeric($input) && sqrt($input) * sqrt($input) == $input;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -4,20 +4,29 @@ namespace Respect\Validation\Rules;
|
|||
|
||||
class PrimeNumber extends AbstractRule
|
||||
{
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
if (is_numeric($input) && $input > 0) {
|
||||
$cont = 0;
|
||||
for ($i=1;$i<=$input;$i++)
|
||||
if (($input % $i)==0)
|
||||
|
||||
for ($i=1; $i<=$input; $i++) {
|
||||
if (($input % $i) == 0) {
|
||||
$cont = $cont + 1;
|
||||
if ($cont <= 2)
|
||||
}
|
||||
}
|
||||
|
||||
if ($cont <= 2) {
|
||||
$input = 1;
|
||||
else
|
||||
} else {
|
||||
$input = 0;
|
||||
}
|
||||
|
||||
} else {
|
||||
$input = 0;
|
||||
}
|
||||
|
||||
return (boolean) $input;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -18,4 +18,3 @@ class Regex extends AbstractRule
|
|||
}
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -6,9 +6,8 @@ class Roman extends AbstractRule
|
|||
{
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
{
|
||||
return (boolean) preg_match('/^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/', $input);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue