2010-09-23 21:46:46 +02:00
|
|
|
<?php
|
|
|
|
namespace Respect\Validation;
|
|
|
|
|
|
|
|
use ReflectionClass;
|
2010-10-18 10:40:44 +02:00
|
|
|
use ReflectionException;
|
2011-02-07 11:52:18 +01:00
|
|
|
use Respect\Validation\Exceptions\AllOfException;
|
|
|
|
use Respect\Validation\Exceptions\ComponentException;
|
|
|
|
use Respect\Validation\Rules\AllOf;
|
2010-09-23 21:46:46 +02:00
|
|
|
|
2011-05-25 12:34:08 +02:00
|
|
|
/**
|
2015-01-08 03:27:08 +01:00
|
|
|
* @method static Validator allOf()
|
|
|
|
* @method static Validator alnum(string $additionalChars = null)
|
|
|
|
* @method static Validator alpha(string $additionalChars = null)
|
|
|
|
* @method static Validator alwaysInvalid()
|
|
|
|
* @method static Validator alwaysValid()
|
|
|
|
* @method static Validator arr()
|
|
|
|
* @method static Validator attribute(string $reference, Validatable $validator = null, bool $mandatory = true)
|
2014-06-25 11:10:31 +02:00
|
|
|
* @method static Validator bank(string $countryCode)
|
|
|
|
* @method static Validator bankAccount(string $countryCode)
|
2015-01-08 03:27:08 +01:00
|
|
|
* @method static Validator base()
|
2015-01-29 12:39:20 +01:00
|
|
|
* @method static Validator between(mixed $min = null, mixed $max = null, bool $inclusive = false)
|
2014-06-25 11:10:31 +02:00
|
|
|
* @method static Validator bic(string $countryCode)
|
2015-01-08 03:27:08 +01:00
|
|
|
* @method static Validator bool()
|
|
|
|
* @method static Validator call()
|
|
|
|
* @method static Validator callback(mixed $callback)
|
2015-01-29 12:39:20 +01:00
|
|
|
* @method static Validator charset(mixed $charset)
|
2015-01-08 03:27:08 +01:00
|
|
|
* @method static Validator cnh()
|
|
|
|
* @method static Validator cnpj()
|
|
|
|
* @method static Validator consonant(string $additionalChars = null)
|
|
|
|
* @method static Validator contains(mixed $containsValue, bool $identical = false)
|
|
|
|
* @method static Validator countryCode()
|
|
|
|
* @method static Validator cpf()
|
|
|
|
* @method static Validator creditCard()
|
|
|
|
* @method static Validator date(string $format = null)
|
|
|
|
* @method static Validator digit(string $additionalChars = null)
|
|
|
|
* @method static Validator directory()
|
|
|
|
* @method static Validator domain()
|
|
|
|
* @method static Validator each(Validatable $itemValidator = null, Validatable $keyValidator = null)
|
|
|
|
* @method static Validator email()
|
|
|
|
* @method static Validator endsWith(mixed $endValue, bool $identical = false)
|
2015-01-29 12:39:20 +01:00
|
|
|
* @method static Validator equals(mixed $compareTo, bool $compareIdentical = false)
|
2015-01-08 03:27:08 +01:00
|
|
|
* @method static Validator even()
|
|
|
|
* @method static Validator executable()
|
|
|
|
* @method static Validator exists()
|
2015-01-22 17:47:55 +01:00
|
|
|
* @method static Validator false()
|
2015-01-08 03:27:08 +01:00
|
|
|
* @method static Validator file()
|
2015-01-26 13:10:54 +01:00
|
|
|
* @method static Validator filterVar(int $filter, mixed $options = null)
|
2015-01-08 03:27:08 +01:00
|
|
|
* @method static Validator float()
|
|
|
|
* @method static Validator graph(string $additionalChars = null)
|
|
|
|
* @method static Validator hexRgbColor()
|
2015-01-29 12:39:20 +01:00
|
|
|
* @method static Validator in(mixed $haystack, bool $compareIdentical = false)
|
2015-01-08 03:27:08 +01:00
|
|
|
* @method static Validator instance(string $instanceName)
|
|
|
|
* @method static Validator int()
|
2015-01-29 12:39:20 +01:00
|
|
|
* @method static Validator ip(mixed $ipOptions = null)
|
2015-01-08 03:27:08 +01:00
|
|
|
* @method static Validator json()
|
|
|
|
* @method static Validator key(string $reference, Validatable $referenceValidator = null, bool $mandatory = true)
|
2015-01-29 12:39:20 +01:00
|
|
|
* @method static Validator leapDate(string $format)
|
2015-01-08 03:27:08 +01:00
|
|
|
* @method static Validator leapYear()
|
2015-01-29 12:39:20 +01:00
|
|
|
* @method static Validator length(int $min = null, int $max = null, bool $inclusive = true)
|
2015-01-08 03:27:08 +01:00
|
|
|
* @method static Validator lowercase()
|
|
|
|
* @method static Validator macAddress()
|
2015-01-29 12:39:20 +01:00
|
|
|
* @method static Validator max(mixed $maxValue, bool $inclusive = false)
|
|
|
|
* @method static Validator min(mixed $minValue, bool $inclusive = false)
|
2015-01-08 03:27:08 +01:00
|
|
|
* @method static Validator minimumAge(int $age)
|
|
|
|
* @method static Validator multiple(int $multipleOf)
|
|
|
|
* @method static Validator negative()
|
|
|
|
* @method static Validator no($useLocale = false)
|
|
|
|
* @method static Validator noneOf()
|
|
|
|
* @method static Validator not(Validatable $rule)
|
|
|
|
* @method static Validator notEmpty()
|
|
|
|
* @method static Validator noWhitespace()
|
|
|
|
* @method static Validator nullValue()
|
|
|
|
* @method static Validator numeric()
|
|
|
|
* @method static Validator object()
|
|
|
|
* @method static Validator odd()
|
|
|
|
* @method static Validator oneOf()
|
|
|
|
* @method static Validator perfectSquare()
|
|
|
|
* @method static Validator phone()
|
|
|
|
* @method static Validator positive()
|
2015-01-16 20:15:44 +01:00
|
|
|
* @method static Validator postalCode(string $countryCode)
|
2015-01-08 03:27:08 +01:00
|
|
|
* @method static Validator primeNumber()
|
|
|
|
* @method static Validator prnt(string $additionalChars = null)
|
|
|
|
* @method static Validator punct(string $additionalChars = null)
|
|
|
|
* @method static Validator readable()
|
2015-01-29 12:39:20 +01:00
|
|
|
* @method static Validator regex(string $regex)
|
2015-01-08 03:27:08 +01:00
|
|
|
* @method static Validator roman()
|
|
|
|
* @method static Validator sf(string $name, array $params = null)
|
|
|
|
* @method static Validator slug()
|
|
|
|
* @method static Validator space(string $additionalChars = null)
|
|
|
|
* @method static Validator startsWith(mixed $startValue, bool $identical = false)
|
|
|
|
* @method static Validator string()
|
|
|
|
* @method static Validator symbolicLink()
|
|
|
|
* @method static Validator tld()
|
2015-01-22 17:43:58 +01:00
|
|
|
* @method static Validator true()
|
2015-01-08 03:27:08 +01:00
|
|
|
* @method static Validator uploaded()
|
|
|
|
* @method static Validator uppercase()
|
2015-01-27 13:25:20 +01:00
|
|
|
* @method static Validator url()
|
2015-01-08 03:27:08 +01:00
|
|
|
* @method static Validator version()
|
|
|
|
* @method static Validator vowel()
|
2015-01-17 00:42:57 +01:00
|
|
|
* @method static Validator when(Validatable $if, Validatable $then, Validatable $when = null)
|
2015-01-08 03:27:08 +01:00
|
|
|
* @method static Validator writable()
|
|
|
|
* @method static Validator xdigit(string $additionalChars = null)
|
|
|
|
* @method static Validator yes($useLocale = false)
|
|
|
|
* @method static Validator zend(mixed $validator, array $params = null)
|
2011-05-25 12:34:08 +02:00
|
|
|
*/
|
2010-11-09 04:35:19 +01:00
|
|
|
class Validator extends AllOf
|
2010-09-23 21:46:46 +02:00
|
|
|
{
|
2015-01-08 03:27:08 +01:00
|
|
|
/**
|
|
|
|
* @param string $ruleName
|
|
|
|
* @param array $arguments
|
|
|
|
*
|
|
|
|
* @return Validator
|
|
|
|
*/
|
2011-02-07 11:52:18 +01:00
|
|
|
public static function __callStatic($ruleName, $arguments)
|
2010-09-25 02:28:03 +02:00
|
|
|
{
|
2013-01-14 22:55:03 +01:00
|
|
|
if ('allOf' === $ruleName) {
|
2011-04-27 19:35:12 +02:00
|
|
|
return static::buildRule($ruleName, $arguments);
|
2013-01-14 22:55:03 +01:00
|
|
|
}
|
2011-04-27 19:35:12 +02:00
|
|
|
|
2015-01-08 03:15:35 +01:00
|
|
|
$validator = new static();
|
2013-01-14 22:55:03 +01:00
|
|
|
|
2011-04-27 19:35:12 +02:00
|
|
|
return $validator->__call($ruleName, $arguments);
|
2010-09-25 02:28:03 +02:00
|
|
|
}
|
|
|
|
|
2015-01-08 03:27:08 +01:00
|
|
|
/**
|
|
|
|
* @param mixed $ruleSpec
|
|
|
|
* @param array $arguments
|
|
|
|
*
|
|
|
|
* @return Validatable
|
|
|
|
*/
|
2015-01-08 03:15:35 +01:00
|
|
|
public static function buildRule($ruleSpec, $arguments = array())
|
2010-09-25 02:28:03 +02:00
|
|
|
{
|
2013-01-14 22:55:03 +01:00
|
|
|
if ($ruleSpec instanceof Validatable) {
|
2011-02-07 11:52:18 +01:00
|
|
|
return $ruleSpec;
|
2013-01-14 22:55:03 +01:00
|
|
|
}
|
|
|
|
|
2011-04-04 19:37:40 +02:00
|
|
|
try {
|
2015-01-08 03:15:35 +01:00
|
|
|
$validatorFqn = 'Respect\\Validation\\Rules\\'.ucfirst($ruleSpec);
|
2011-04-04 19:37:40 +02:00
|
|
|
$validatorClass = new ReflectionClass($validatorFqn);
|
2015-01-08 03:15:35 +01:00
|
|
|
$validatorInstance = $validatorClass->newInstanceArgs($arguments);
|
2011-04-04 18:40:47 +02:00
|
|
|
|
2011-04-04 19:37:40 +02:00
|
|
|
return $validatorInstance;
|
|
|
|
} catch (ReflectionException $e) {
|
|
|
|
throw new ComponentException($e->getMessage());
|
|
|
|
}
|
2010-09-25 02:28:03 +02:00
|
|
|
}
|
|
|
|
|
2015-01-08 03:27:08 +01:00
|
|
|
/**
|
|
|
|
* @param string $method
|
|
|
|
* @param array $arguments
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
2011-02-07 11:52:18 +01:00
|
|
|
public function __call($method, $arguments)
|
2010-09-25 02:28:03 +02:00
|
|
|
{
|
2015-01-19 23:12:07 +01:00
|
|
|
if ('not' === $method && empty($arguments)) {
|
|
|
|
return new static(new Rules\Not($this));
|
2013-01-14 22:55:03 +01:00
|
|
|
}
|
2012-07-24 03:06:23 +02:00
|
|
|
|
2012-07-22 15:17:31 +02:00
|
|
|
return $this->addRule(static::buildRule($method, $arguments));
|
2010-09-25 02:28:03 +02:00
|
|
|
}
|
|
|
|
|
2015-01-08 03:27:08 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $input
|
|
|
|
* @param array $extraParams
|
|
|
|
*
|
|
|
|
* @return AllOfException
|
|
|
|
*/
|
2015-01-08 03:15:35 +01:00
|
|
|
public function reportError($input, array $extraParams = array())
|
2011-02-07 11:52:18 +01:00
|
|
|
{
|
2015-01-08 03:15:35 +01:00
|
|
|
$exception = new AllOfException();
|
2011-02-18 04:08:46 +01:00
|
|
|
$input = AllOfException::stringify($input);
|
2015-01-08 03:15:35 +01:00
|
|
|
$name = $this->getName() ?: "\"$input\"";
|
2011-12-27 03:43:22 +01:00
|
|
|
$params = array_merge(
|
2015-01-08 03:15:35 +01:00
|
|
|
$extraParams,
|
|
|
|
get_object_vars($this),
|
|
|
|
get_class_vars(__CLASS__)
|
2011-12-27 03:43:22 +01:00
|
|
|
);
|
2011-02-18 04:08:46 +01:00
|
|
|
$exception->configure($name, $params);
|
2013-01-14 22:55:03 +01:00
|
|
|
if (!is_null($this->template)) {
|
2011-04-17 17:29:48 +02:00
|
|
|
$exception->setTemplate($this->template);
|
2013-01-14 22:55:03 +01:00
|
|
|
}
|
|
|
|
|
2011-02-18 04:08:46 +01:00
|
|
|
return $exception;
|
2011-02-07 11:52:18 +01:00
|
|
|
}
|
|
|
|
|
2011-05-25 12:34:08 +02:00
|
|
|
/**
|
|
|
|
* Create instance validator
|
|
|
|
*
|
2015-01-08 03:27:08 +01:00
|
|
|
* @return Validator
|
2011-05-25 12:34:08 +02:00
|
|
|
*/
|
2013-01-14 22:55:03 +01:00
|
|
|
public static function create()
|
|
|
|
{
|
2011-05-25 12:56:22 +02:00
|
|
|
$ref = new ReflectionClass(__CLASS__);
|
2013-01-14 22:55:03 +01:00
|
|
|
|
2011-05-25 12:34:08 +02:00
|
|
|
return $ref->newInstanceArgs(func_get_args());
|
|
|
|
}
|
2011-02-10 21:49:48 +01:00
|
|
|
}
|