2010-09-23 21:46:46 +02:00
|
|
|
<?php
|
2015-06-08 16:47:14 +02:00
|
|
|
|
2015-06-08 17:09:25 +02:00
|
|
|
/*
|
|
|
|
* This file is part of Respect/Validation.
|
|
|
|
*
|
|
|
|
* (c) Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the "LICENSE.md"
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2010-09-23 21:46:46 +02:00
|
|
|
namespace Respect\Validation;
|
|
|
|
|
|
|
|
use ReflectionClass;
|
2011-02-07 11:52:18 +01:00
|
|
|
use Respect\Validation\Exceptions\AllOfException;
|
|
|
|
use Respect\Validation\Exceptions\ComponentException;
|
|
|
|
use Respect\Validation\Rules\AllOf;
|
2015-07-20 12:16:46 +02:00
|
|
|
use Respect\Validation\Rules\Key;
|
2010-09-23 21:46:46 +02:00
|
|
|
|
2011-05-25 12:34:08 +02:00
|
|
|
/**
|
2015-02-19 20:33:45 +01:00
|
|
|
* @method static Validator age(int $minAge = null, int $maxAge = null)
|
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()
|
2015-08-20 06:17:24 +02:00
|
|
|
* @method static Validator callableType()
|
2015-01-08 03:27:08 +01:00
|
|
|
* @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()
|
2015-05-15 11:24:51 +02:00
|
|
|
* @method static Validator domain(bool $tldCheck = true)
|
2015-01-08 03:27:08 +01:00
|
|
|
* @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-06-16 05:41:49 +02:00
|
|
|
* @method static Validator extension(string $extension)
|
2015-09-06 17:37:08 +02:00
|
|
|
* @method static Validator factor(int $dividend)
|
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-08-20 05:35:43 +02:00
|
|
|
* @method static Validator finite()
|
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-08-20 05:43:17 +02:00
|
|
|
* @method static Validator infinite()
|
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-07-20 12:16:46 +02:00
|
|
|
* @method static Validator keySet(Key $rule...)
|
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)
|
2015-06-16 06:16:29 +02:00
|
|
|
* @method static Validator mimetype(string $mimetype)
|
2015-01-29 12:39:20 +01:00
|
|
|
* @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-08-20 05:55:32 +02:00
|
|
|
* @method static Validator resource()
|
2015-01-08 03:27:08 +01:00
|
|
|
* @method static Validator roman()
|
2015-08-20 06:02:11 +02:00
|
|
|
* @method static Validator scalar()
|
2015-01-08 03:27:08 +01:00
|
|
|
* @method static Validator sf(string $name, array $params = null)
|
2015-06-16 04:55:09 +02:00
|
|
|
* @method static Validator size(string $minSize = null, string $maxSize = null)
|
2015-01-08 03:27:08 +01:00
|
|
|
* @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-02-02 13:50:17 +01:00
|
|
|
* @method static Validator type(string $type)
|
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()
|
2015-09-15 20:35:17 +02:00
|
|
|
* @method static Validator videoUrl(string $service = null)
|
2015-01-08 03:27:08 +01:00
|
|
|
* @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
|
|
|
{
|
2014-05-07 05:51:54 +02:00
|
|
|
protected static $factory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Factory
|
|
|
|
*/
|
|
|
|
protected static function getFactory()
|
|
|
|
{
|
2015-06-08 16:47:14 +02:00
|
|
|
if (!static::$factory instanceof Factory) {
|
2014-05-07 05:51:54 +02:00
|
|
|
static::$factory = new Factory();
|
|
|
|
}
|
|
|
|
|
|
|
|
return static::$factory;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Factory $factory
|
|
|
|
*/
|
|
|
|
public static function setFactory($factory)
|
|
|
|
{
|
|
|
|
static::$factory = $factory;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $rulePrefix
|
|
|
|
* @param bool $prepend
|
|
|
|
*/
|
|
|
|
public static function with($rulePrefix, $prepend = false)
|
|
|
|
{
|
|
|
|
if (false === $prepend) {
|
|
|
|
self::getFactory()->appendRulePrefix($rulePrefix);
|
|
|
|
} else {
|
|
|
|
self::getFactory()->prependRulePrefix($rulePrefix);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2011-04-04 19:37:40 +02:00
|
|
|
try {
|
2014-05-07 05:51:54 +02:00
|
|
|
return static::getFactory()->rule($ruleSpec, $arguments);
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
throw new ComponentException($exception->getMessage(), $exception->getCode(), $exception);
|
2011-04-04 19:37:40 +02:00
|
|
|
}
|
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-08-26 19:30:45 +02:00
|
|
|
protected function createException()
|
2011-02-07 11:52:18 +01:00
|
|
|
{
|
2015-08-26 19:30:45 +02:00
|
|
|
return new AllOfException();
|
2011-02-07 11:52:18 +01:00
|
|
|
}
|
|
|
|
|
2011-05-25 12:34:08 +02:00
|
|
|
/**
|
2015-06-08 16:47:14 +02:00
|
|
|
* Create instance validator.
|
2011-05-25 12:34:08 +02:00
|
|
|
*
|
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
|
|
|
}
|