respect-validation/library/Validator.php

231 lines
8.2 KiB
PHP
Raw Normal View History

<?php
2015-06-08 16:47:14 +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.
*/
namespace Respect\Validation;
use ReflectionClass;
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;
/**
* @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)
* @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)
* @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()
* @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()
* @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()
2015-09-21 18:28:59 +02:00
* @method static Validator subdivisionCode(string $countryCode)
2015-01-08 03:27:08 +01:00
* @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()
* @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)
*/
class Validator extends AllOf
{
protected static $factory;
/**
* @return Factory
*/
protected static function getFactory()
{
2015-06-08 16:47:14 +02:00
if (!static::$factory instanceof Factory) {
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
*/
public static function __callStatic($ruleName, $arguments)
{
if ('allOf' === $ruleName) {
return static::buildRule($ruleName, $arguments);
}
$validator = new static();
return $validator->__call($ruleName, $arguments);
}
2015-01-08 03:27:08 +01:00
/**
* @param mixed $ruleSpec
* @param array $arguments
*
* @return Validatable
*/
public static function buildRule($ruleSpec, $arguments = array())
{
2011-04-04 19:37:40 +02:00
try {
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
}
}
2015-01-08 03:27:08 +01:00
/**
* @param string $method
* @param array $arguments
*
* @return self
*/
public function __call($method, $arguments)
{
if ('not' === $method && empty($arguments)) {
return new static(new Rules\Not($this));
}
return $this->addRule(static::buildRule($method, $arguments));
}
protected function createException()
{
return new AllOfException();
}
/**
2015-06-08 16:47:14 +02:00
* Create instance validator.
*
2015-01-08 03:27:08 +01:00
* @return Validator
*/
public static function create()
{
2011-05-25 12:56:22 +02:00
$ref = new ReflectionClass(__CLASS__);
return $ref->newInstanceArgs(func_get_args());
}
}