respect-validation/library/Validator.php

235 lines
9.1 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.
*/
declare(strict_types=1);
namespace Respect\Validation;
2015-11-06 02:21:42 +01:00
use finfo;
use ReflectionClass;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Exceptions\ValidationException;
use Respect\Validation\Rules\AllOf;
2015-07-20 12:16:46 +02:00
use Respect\Validation\Rules\Key;
/**
2017-03-27 13:37:47 +02:00
* @method static Validator allOf(Validatable ...$rule)
2015-01-08 03:27:08 +01:00
* @method static Validator alnum(string $additionalChars = null)
* @method static Validator alpha(string $additionalChars = null)
* @method static Validator alwaysInvalid()
* @method static Validator alwaysValid()
2017-02-05 12:23:22 +01:00
* @method static Validator anyOf()
2015-10-17 22:51:42 +02:00
* @method static Validator arrayType()
2017-02-05 12:23:22 +01:00
* @method static Validator arrayVal()
2015-01-08 03:27:08 +01:00
* @method static Validator attribute(string $reference, Validatable $validator = null, bool $mandatory = true)
* @method static Validator base()
2017-07-11 15:28:09 +02:00
* @method static Validator base64()
2018-05-19 16:46:58 +02:00
* @method static Validator between($minimum, $maximum, bool $inclusive = true)
* @method static Validator bic(string $countryCode)
2015-10-07 16:30:29 +02:00
* @method static Validator boolType()
2015-10-21 06:02:31 +02:00
* @method static Validator boolVal()
2015-10-16 21:46:08 +02:00
* @method static Validator bsn()
2015-01-08 03:27:08 +01:00
* @method static Validator call()
2015-08-20 06:17:24 +02:00
* @method static Validator callableType()
* @method static Validator callback(callable $callback)
* @method static Validator charset($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($containsValue, bool $identical = false)
2015-10-17 22:08:25 +02:00
* @method static Validator countable()
2015-01-08 03:27:08 +01:00
* @method static Validator countryCode()
2015-10-18 16:02:48 +02:00
* @method static Validator currencyCode()
2015-01-08 03:27:08 +01:00
* @method static Validator cpf()
* @method static Validator creditCard(string $brand = null)
2018-01-29 09:37:40 +01:00
* @method static Validator date(string $format = 'Y-m-d')
* @method static Validator dateTime(string $format = null)
2015-01-08 03:27:08 +01:00
* @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($endValue, bool $identical = false)
* @method static Validator equals($compareTo)
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-10-07 16:35:39 +02:00
* @method static Validator falseVal()
2016-01-23 11:45:40 +01:00
* @method static Validator fibonacci()
2015-01-08 03:27:08 +01:00
* @method static Validator file()
* @method static Validator filterVar(int $filter, $options = null)
2015-08-20 05:35:43 +02:00
* @method static Validator finite()
2015-10-07 16:42:37 +02:00
* @method static Validator floatVal()
2015-10-17 20:17:07 +02:00
* @method static Validator floatType()
2015-01-08 03:27:08 +01:00
* @method static Validator graph(string $additionalChars = null)
* @method static Validator hexRgbColor()
* @method static Validator identical($value)
* @method static Validator identityCard(string $countryCode)
2015-11-06 02:21:42 +01:00
* @method static Validator image(finfo $fileInfo = null)
2015-10-23 03:45:56 +02:00
* @method static Validator imei()
* @method static Validator in($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)
2015-10-07 16:46:57 +02:00
* @method static Validator intVal()
2015-10-17 04:43:01 +02:00
* @method static Validator intType()
* @method static Validator ip($ipOptions = null)
* @method static Validator iterableType()
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-10-05 21:20:04 +02:00
* @method static Validator keyNested(string $reference, Validatable $referenceValidator = null, bool $mandatory = true)
2017-03-27 23:52:34 +02:00
* @method static Validator keySet(Key ...$rule)
2015-10-08 18:54:42 +02:00
* @method static Validator keyValue(string $comparedKey, string $ruleName, string $baseKey)
2015-10-26 23:59:48 +01:00
* @method static Validator languageCode(string $set)
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()
2017-08-18 13:47:36 +02:00
* @method static Validator luhn()
2015-01-08 03:27:08 +01:00
* @method static Validator macAddress()
* @method static Validator max($maxValue, bool $inclusive = true)
* @method static Validator maximumAge(int $age, string $format = null)
2015-06-16 06:16:29 +02:00
* @method static Validator mimetype(string $mimetype)
* @method static Validator min($minValue, bool $inclusive = true)
2017-07-31 12:57:27 +02:00
* @method static Validator minimumAge(int $age, bool $format = null)
2015-01-08 03:27:08 +01:00
* @method static Validator multiple(int $multipleOf)
* @method static Validator negative()
* @method static Validator nif()
2015-01-08 03:27:08 +01:00
* @method static Validator no($useLocale = false)
2017-03-27 13:37:47 +02:00
* @method static Validator noneOf(Validatable ...$rule)
2015-01-08 03:27:08 +01:00
* @method static Validator not(Validatable $rule)
2015-10-14 06:02:53 +02:00
* @method static Validator notBlank()
2015-01-08 03:27:08 +01:00
* @method static Validator notEmpty()
2015-10-15 15:31:45 +02:00
* @method static Validator notOptional()
2015-01-08 03:27:08 +01:00
* @method static Validator noWhitespace()
2018-01-18 12:15:51 +01:00
* @method static Validator nullable(Validatable $rule)
2015-10-07 17:28:06 +02:00
* @method static Validator nullType()
2016-07-21 15:09:38 +02:00
* @method static Validator number()
2016-11-06 18:45:44 +01:00
* @method static Validator numericVal()
2015-10-07 17:31:32 +02:00
* @method static Validator objectType()
2015-01-08 03:27:08 +01:00
* @method static Validator odd()
2017-03-27 13:37:47 +02:00
* @method static Validator oneOf(Validatable ...$rule)
* @method static Validator optional(Validatable $rule)
2015-01-08 03:27:08 +01:00
* @method static Validator perfectSquare()
* @method static Validator pesel()
2015-01-08 03:27:08 +01:00
* @method static Validator phone()
2016-03-04 17:46:07 +01:00
* @method static Validator phpLabel()
2017-03-13 22:51:55 +01:00
* @method static Validator pis()
2015-01-08 03:27:08 +01:00
* @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)
* @method static Validator resourceType()
2015-01-08 03:27:08 +01:00
* @method static Validator roman()
2015-10-07 17:35:25 +02:00
* @method static Validator scalarVal()
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($startValue, bool $identical = false)
2015-10-07 16:52:03 +02:00
* @method static Validator stringType()
2017-02-04 15:07:22 +01:00
* @method static Validator stringVal()
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()
2018-01-30 09:29:45 +01:00
* @method static Validator time(string $format = 'H:i:s')
2015-01-08 03:27:08 +01:00
* @method static Validator tld()
2015-10-07 16:38:24 +02:00
* @method static Validator trueVal()
2015-02-02 13:50:17 +01:00
* @method static Validator type(string $type)
2017-04-03 13:31:26 +02:00
* @method static Validator unique()
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()
2017-05-16 10:59:55 +02:00
* @method static Validator uuid()
* @method static Validator vatin(string $countryCode)
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($validator, array $params = null)
*/
class Validator extends AllOf
{
public function check($input): void
{
try {
parent::check($input);
} catch (ValidationException $exception) {
if (1 == count($this->getRules()) && $this->template) {
$exception->setTemplate($this->template);
}
throw $exception;
}
}
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
*/
2015-10-18 03:44:47 +02:00
public static function buildRule($ruleSpec, $arguments = [])
{
2011-04-04 19:37:40 +02:00
try {
return Factory::getDefaultInstance()->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)
{
return $this->addRule(static::buildRule($method, $arguments));
}
/**
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());
}
}