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>
|
|
|
|
*
|
2019-05-23 16:21:34 +02:00
|
|
|
* For the full copyright and license information, please view the LICENSE file
|
|
|
|
* that was distributed with this source code.
|
2015-06-08 17:09:25 +02:00
|
|
|
*/
|
|
|
|
|
2017-11-19 19:01:31 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2010-09-23 21:46:46 +02:00
|
|
|
namespace Respect\Validation;
|
|
|
|
|
2015-11-06 02:21:42 +01:00
|
|
|
use finfo;
|
2018-08-01 21:44:53 +02:00
|
|
|
use Respect\Validation\Exceptions\ComponentException;
|
2016-04-07 19:11:40 +02:00
|
|
|
use Respect\Validation\Exceptions\ValidationException;
|
2011-02-07 11:52:18 +01:00
|
|
|
use Respect\Validation\Rules\AllOf;
|
2015-07-20 12:16:46 +02:00
|
|
|
use Respect\Validation\Rules\Key;
|
2018-08-19 20:09:32 +02:00
|
|
|
use Symfony\Component\Validator\Constraint;
|
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
2020-07-21 22:54:41 +02:00
|
|
|
|
2019-02-17 19:41:55 +01:00
|
|
|
use function count;
|
2010-09-23 21:46:46 +02:00
|
|
|
|
2011-05-25 12:34:08 +02:00
|
|
|
/**
|
2017-03-27 13:37:47 +02:00
|
|
|
* @method static Validator allOf(Validatable ...$rule)
|
2018-09-24 21:48:41 +02:00
|
|
|
* @method static Validator alnum(string ...$additionalChars)
|
|
|
|
* @method static Validator alpha(string ...$additionalChars)
|
2015-01-08 03:27:08 +01:00
|
|
|
* @method static Validator alwaysInvalid()
|
|
|
|
* @method static Validator alwaysValid()
|
2018-08-22 18:30:48 +02:00
|
|
|
* @method static Validator anyOf(Validatable ...$rule)
|
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)
|
2018-08-22 18:30:48 +02:00
|
|
|
* @method static Validator base(int $base, string $chars = null)
|
2017-07-11 15:28:09 +02:00
|
|
|
* @method static Validator base64()
|
2018-05-31 15:04:28 +02:00
|
|
|
* @method static Validator between($minimum, $maximum)
|
2014-06-25 11:10:31 +02:00
|
|
|
* @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()
|
2018-08-18 15:24:41 +02:00
|
|
|
* @method static Validator call(callable $callable, Validatable $rule)
|
2015-08-20 06:17:24 +02:00
|
|
|
* @method static Validator callableType()
|
2018-04-25 03:44:06 +02:00
|
|
|
* @method static Validator callback(callable $callback)
|
2018-06-10 18:34:14 +02:00
|
|
|
* @method static Validator charset(string ...$charset)
|
2015-01-08 03:27:08 +01:00
|
|
|
* @method static Validator cnh()
|
|
|
|
* @method static Validator cnpj()
|
2019-05-11 19:30:47 +02:00
|
|
|
* @method static Validator control(string ...$additionalChars)
|
2018-09-24 21:48:41 +02:00
|
|
|
* @method static Validator consonant(string ...$additionalChars)
|
2018-04-25 03:44:06 +02:00
|
|
|
* @method static Validator contains($containsValue, bool $identical = false)
|
2018-12-12 15:12:45 +01:00
|
|
|
* @method static Validator containsAny(array $needles, bool $strictCompareArray = false)
|
2015-10-17 22:08:25 +02:00
|
|
|
* @method static Validator countable()
|
2018-06-13 05:01:03 +02:00
|
|
|
* @method static Validator countryCode(string $set = null)
|
2015-10-18 16:02:48 +02:00
|
|
|
* @method static Validator currencyCode()
|
2015-01-08 03:27:08 +01:00
|
|
|
* @method static Validator cpf()
|
2016-04-06 17:26:00 +02:00
|
|
|
* @method static Validator creditCard(string $brand = null)
|
2018-01-29 09:37:40 +01:00
|
|
|
* @method static Validator date(string $format = 'Y-m-d')
|
2017-05-01 08:54:18 +02:00
|
|
|
* @method static Validator dateTime(string $format = null)
|
2018-09-24 21:48:41 +02:00
|
|
|
* @method static Validator digit(string ...$additionalChars)
|
2015-01-08 03:27:08 +01:00
|
|
|
* @method static Validator directory()
|
2015-05-15 11:24:51 +02:00
|
|
|
* @method static Validator domain(bool $tldCheck = true)
|
2018-06-17 01:20:47 +02:00
|
|
|
* @method static Validator each(Validatable $rule)
|
2015-01-08 03:27:08 +01:00
|
|
|
* @method static Validator email()
|
2018-04-25 03:44:06 +02:00
|
|
|
* @method static Validator endsWith($endValue, bool $identical = false)
|
|
|
|
* @method static Validator equals($compareTo)
|
2018-06-23 16:24:48 +02:00
|
|
|
* @method static Validator equivalent($compareTo)
|
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-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()
|
2018-04-25 03:44:06 +02:00
|
|
|
* @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()
|
2018-09-24 21:48:41 +02:00
|
|
|
* @method static Validator graph(string ...$additionalChars)
|
2018-05-31 13:50:58 +02:00
|
|
|
* @method static Validator greaterThan($compareTo)
|
2015-01-08 03:27:08 +01:00
|
|
|
* @method static Validator hexRgbColor()
|
2019-02-04 21:24:26 +01:00
|
|
|
* @method static Validator iban()
|
2018-04-25 03:44:06 +02:00
|
|
|
* @method static Validator identical($value)
|
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()
|
2018-04-25 03:44:06 +02:00
|
|
|
* @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()
|
2018-12-07 19:38:27 +01:00
|
|
|
* @method static Validator ip(string $range = '*', int $options = null)
|
2018-10-02 17:23:35 +02:00
|
|
|
* @method static Validator isbn()
|
2016-08-03 11:05:28 +02:00
|
|
|
* @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)
|
2018-08-22 18:30:48 +02:00
|
|
|
* @method static Validator languageCode(string $set = null)
|
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()
|
2018-05-31 13:18:54 +02:00
|
|
|
* @method static Validator lessThan($compareTo)
|
2017-08-18 13:47:36 +02:00
|
|
|
* @method static Validator luhn()
|
2015-01-08 03:27:08 +01:00
|
|
|
* @method static Validator macAddress()
|
2018-05-31 11:29:05 +02:00
|
|
|
* @method static Validator max($compareTo)
|
2018-05-31 16:23:42 +02:00
|
|
|
* @method static Validator maxAge(int $age, string $format = null)
|
2015-06-16 06:16:29 +02:00
|
|
|
* @method static Validator mimetype(string $mimetype)
|
2018-05-31 16:25:00 +02:00
|
|
|
* @method static Validator min($compareTo)
|
|
|
|
* @method static Validator minAge(int $age, string $format = null)
|
2015-01-08 03:27:08 +01:00
|
|
|
* @method static Validator multiple(int $multipleOf)
|
|
|
|
* @method static Validator negative()
|
2019-02-10 18:02:43 +01:00
|
|
|
* @method static Validator nfeAccessKey()
|
2016-09-26 15:45:22 +02:00
|
|
|
* @method static Validator nif()
|
2019-05-11 23:40:52 +02:00
|
|
|
* @method static Validator nip()
|
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()
|
2019-01-27 01:14:47 +01:00
|
|
|
* @method static Validator notEmoji()
|
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)
|
2015-10-15 16:10:47 +02:00
|
|
|
* @method static Validator optional(Validatable $rule)
|
2015-01-08 03:27:08 +01:00
|
|
|
* @method static Validator perfectSquare()
|
2015-11-26 20:46:51 +01:00
|
|
|
* @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()
|
2019-05-12 00:12:07 +02:00
|
|
|
* @method static Validator polishIdCard()
|
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()
|
2018-09-24 21:48:41 +02:00
|
|
|
* @method static Validator printable(string ...$additionalChars)
|
|
|
|
* @method static Validator punct(string ...$additionalChars)
|
2015-01-08 03:27:08 +01:00
|
|
|
* @method static Validator readable()
|
2015-01-29 12:39:20 +01:00
|
|
|
* @method static Validator regex(string $regex)
|
2015-10-07 17:33:39 +02:00
|
|
|
* @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()
|
2018-08-19 20:09:32 +02:00
|
|
|
* @method static Validator sf(Constraint $constraint, ValidatorInterface $validator = 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()
|
2019-03-04 14:35:28 +01:00
|
|
|
* @method static Validator sorted(string $direction)
|
2018-09-24 21:48:41 +02:00
|
|
|
* @method static Validator space(string ...$additionalChars)
|
2018-04-25 03:44:06 +02:00
|
|
|
* @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)
|
2017-12-02 22:20:48 +01:00
|
|
|
* @method static Validator subset(array $superset)
|
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()
|
2018-10-04 21:45:09 +02:00
|
|
|
* @method static Validator uuid(int $version = null)
|
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)
|
2018-09-24 21:48:41 +02:00
|
|
|
* @method static Validator vowel(string ...$additionalChars)
|
2019-07-03 11:57:04 +02:00
|
|
|
* @method static Validator when(Validatable $if, Validatable $then, Validatable $else = null)
|
2015-01-08 03:27:08 +01:00
|
|
|
* @method static Validator writable()
|
2018-09-24 21:48:41 +02:00
|
|
|
* @method static Validator xdigit(string ...$additionalChars)
|
2015-01-08 03:27:08 +01:00
|
|
|
* @method static Validator yes($useLocale = false)
|
2018-04-25 03:44:06 +02:00
|
|
|
* @method static Validator zend($validator, array $params = null)
|
2018-11-25 17:56:04 +01:00
|
|
|
*
|
|
|
|
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
|
|
|
* @author Henrique Moody <henriquemoody@gmail.com>
|
2011-05-25 12:34:08 +02:00
|
|
|
*/
|
2018-08-01 21:44:53 +02:00
|
|
|
final class Validator extends AllOf
|
2010-09-23 21:46:46 +02:00
|
|
|
{
|
2020-08-27 15:29:46 +02:00
|
|
|
/**
|
|
|
|
* Create instance validator.
|
|
|
|
*/
|
|
|
|
public static function create(): self
|
|
|
|
{
|
|
|
|
return new self();
|
|
|
|
}
|
|
|
|
|
2018-08-01 21:44:53 +02:00
|
|
|
/**
|
2019-04-05 14:33:00 +02:00
|
|
|
* {@inheritDoc}
|
2018-08-01 21:44:53 +02:00
|
|
|
*/
|
2018-01-28 16:14:00 +01:00
|
|
|
public function check($input): void
|
2016-04-07 19:11:40 +02:00
|
|
|
{
|
|
|
|
try {
|
2018-01-28 16:14:00 +01:00
|
|
|
parent::check($input);
|
2016-04-07 19:11:40 +02:00
|
|
|
} catch (ValidationException $exception) {
|
2019-02-09 13:28:08 +01:00
|
|
|
if (count($this->getRules()) == 1 && $this->template) {
|
2018-05-24 07:47:09 +02:00
|
|
|
$exception->updateTemplate($this->template);
|
2016-04-07 19:11:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
throw $exception;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-08 03:27:08 +01:00
|
|
|
/**
|
2018-08-01 21:44:53 +02:00
|
|
|
* Creates a new Validator instance with a rule that was called on the static method.
|
|
|
|
*
|
2019-02-07 21:22:10 +01:00
|
|
|
* @param mixed[] $arguments
|
2015-01-08 03:27:08 +01:00
|
|
|
*
|
2018-08-01 21:44:53 +02:00
|
|
|
* @throws ComponentException
|
2015-01-08 03:27:08 +01:00
|
|
|
*/
|
2018-08-01 21:44:53 +02:00
|
|
|
public static function __callStatic(string $ruleName, array $arguments): self
|
2010-09-25 02:28:03 +02:00
|
|
|
{
|
2018-08-01 21:44:53 +02:00
|
|
|
return self::create()->__call($ruleName, $arguments);
|
2010-09-25 02:28:03 +02:00
|
|
|
}
|
|
|
|
|
2015-01-08 03:27:08 +01:00
|
|
|
/**
|
2018-08-01 21:44:53 +02:00
|
|
|
* Create a new rule by the name of the method and adds the rule to the chain.
|
|
|
|
*
|
2019-02-07 21:22:10 +01:00
|
|
|
* @param mixed[] $arguments
|
2018-08-01 21:44:53 +02:00
|
|
|
*
|
|
|
|
* @throws ComponentException
|
2015-01-08 03:27:08 +01:00
|
|
|
*/
|
2018-08-01 21:44:53 +02:00
|
|
|
public function __call(string $ruleName, array $arguments): self
|
2010-09-25 02:28:03 +02:00
|
|
|
{
|
2019-02-04 00:44:08 +01:00
|
|
|
$this->addRule(Factory::getDefaultInstance()->rule($ruleName, $arguments));
|
|
|
|
|
|
|
|
return $this;
|
2010-09-25 02:28:03 +02:00
|
|
|
}
|
2011-02-10 21:49:48 +01:00
|
|
|
}
|