respect-validation/library/Validator.php

231 lines
9.4 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 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;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Validator\ValidatorInterface;
/**
2017-03-27 13:37:47 +02:00
* @method static Validator allOf(Validatable ...$rule)
* @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()
* @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)
* @method static Validator base(int $base, string $chars = null)
2017-07-11 15:28:09 +02:00
* @method static Validator base64()
* @method static Validator between($minimum, $maximum)
* @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()
* @method static Validator call(callable $callable, Validatable $rule)
2015-08-20 06:17:24 +02:00
* @method static Validator callableType()
* @method static Validator callback(callable $callback)
* @method static Validator charset(string ...$charset)
2015-01-08 03:27:08 +01:00
* @method static Validator cnh()
* @method static Validator cnpj()
* @method static Validator cntrl(string ...$additionalChars)
* @method static Validator consonant(string ...$additionalChars)
* @method static Validator contains($containsValue, bool $identical = false)
* @method static Validator containsAny(array $needles, bool $strictCompareArray = false)
2015-10-17 22:08:25 +02:00
* @method static Validator countable()
* @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()
* @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)
* @method static Validator digit(string ...$additionalChars)
2015-01-08 03:27:08 +01:00
* @method static Validator directory()
* @method static Validator domain(bool $tldCheck = true)
* @method static Validator each(Validatable $rule)
2015-01-08 03:27:08 +01:00
* @method static Validator email()
* @method static Validator endsWith($endValue, bool $identical = false)
* @method static Validator equals($compareTo)
* @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()
* @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()
* @method static Validator graph(string ...$additionalChars)
* @method static Validator greaterThan($compareTo)
2015-01-08 03:27:08 +01:00
* @method static Validator hexRgbColor()
* @method static Validator iban()
* @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(string $range = '*', int $options = null)
* @method static Validator isbn()
* @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)
* @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()
* @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()
* @method static Validator max($compareTo)
* @method static Validator maxAge(int $age, string $format = null)
2015-06-16 06:16:29 +02:00
* @method static Validator mimetype(string $mimetype)
* @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()
* @method static Validator nfeAccessKey()
* @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()
* @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)
* @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 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)
* @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()
* @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()
* @method static Validator space(string ...$additionalChars)
* @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)
* @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()
* @method static Validator uuid(int $version = null)
* @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)
* @method static Validator vowel(string ...$additionalChars)
* @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)
2015-01-08 03:27:08 +01:00
* @method static Validator yes($useLocale = false)
* @method static Validator zend($validator, array $params = null)
*
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
* @author Henrique Moody <henriquemoody@gmail.com>
*/
final class Validator extends AllOf
{
/**
* {@inheritdoc}
*/
public function check($input): void
{
try {
parent::check($input);
} catch (ValidationException $exception) {
if (count($this->getRules()) == 1 && $this->template) {
$exception->updateTemplate($this->template);
}
throw $exception;
}
}
2015-01-08 03:27:08 +01:00
/**
* Creates a new Validator instance with a rule that was called on the static method.
*
* @param mixed[] $arguments
2015-01-08 03:27:08 +01:00
*
* @throws ComponentException
2015-01-08 03:27:08 +01:00
*/
public static function __callStatic(string $ruleName, array $arguments): self
{
return self::create()->__call($ruleName, $arguments);
}
2015-01-08 03:27:08 +01:00
/**
* Create a new rule by the name of the method and adds the rule to the chain.
*
* @param mixed[] $arguments
*
* @throws ComponentException
2015-01-08 03:27:08 +01:00
*/
public function __call(string $ruleName, array $arguments): self
{
$this->addRule(Factory::getDefaultInstance()->rule($ruleName, $arguments));
return $this;
}
/**
2015-06-08 16:47:14 +02:00
* Create instance validator.
*/
public static function create(): self
{
return new self();
}
}