respect-validation/library/Validator.php
Henrique Moody 8c529c433e
Refactor ValidationException
Make the ValidationException a little bit less mutable than before. All
its dependencies are now passed into the constructor.

This commit also make the Factory pass the translator to the exceptions
allowing to define the translator before the exception gets created.
This change is not the ideal one, later I would like to not need the
Singleton from the Factory to do that, but for now it seems like a good
approach.

One more thing that this commit does is to introduce the "id" for
Exceptions. Key can be either the defined "name" or the name of the rule
that throwed the exception. This method will be handy to identify
exceptions better.

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
2018-05-27 16:12:05 +02:00

235 lines
9.1 KiB
PHP

<?php
/*
* 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;
use finfo;
use ReflectionClass;
use Respect\Validation\Exceptions\ComponentException;
use Respect\Validation\Exceptions\ValidationException;
use Respect\Validation\Rules\AllOf;
use Respect\Validation\Rules\Key;
/**
* @method static Validator allOf(Validatable ...$rule)
* @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 anyOf()
* @method static Validator arrayType()
* @method static Validator arrayVal()
* @method static Validator attribute(string $reference, Validatable $validator = null, bool $mandatory = true)
* @method static Validator base()
* @method static Validator base64()
* @method static Validator between($minimum, $maximum, bool $inclusive = true)
* @method static Validator bic(string $countryCode)
* @method static Validator boolType()
* @method static Validator boolVal()
* @method static Validator bsn()
* @method static Validator call()
* @method static Validator callableType()
* @method static Validator callback(callable $callback)
* @method static Validator charset($charset)
* @method static Validator cnh()
* @method static Validator cnpj()
* @method static Validator consonant(string $additionalChars = null)
* @method static Validator contains($containsValue, bool $identical = false)
* @method static Validator countable()
* @method static Validator countryCode()
* @method static Validator currencyCode()
* @method static Validator cpf()
* @method static Validator creditCard(string $brand = null)
* @method static Validator date(string $format = 'Y-m-d')
* @method static Validator dateTime(string $format = null)
* @method static Validator digit(string $additionalChars = null)
* @method static Validator directory()
* @method static Validator domain(bool $tldCheck = true)
* @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)
* @method static Validator even()
* @method static Validator executable()
* @method static Validator exists()
* @method static Validator extension(string $extension)
* @method static Validator factor(int $dividend)
* @method static Validator falseVal()
* @method static Validator fibonacci()
* @method static Validator file()
* @method static Validator filterVar(int $filter, $options = null)
* @method static Validator finite()
* @method static Validator floatVal()
* @method static Validator floatType()
* @method static Validator graph(string $additionalChars = null)
* @method static Validator hexRgbColor()
* @method static Validator identical($value)
* @method static Validator identityCard(string $countryCode)
* @method static Validator image(finfo $fileInfo = null)
* @method static Validator imei()
* @method static Validator in($haystack, bool $compareIdentical = false)
* @method static Validator infinite()
* @method static Validator instance(string $instanceName)
* @method static Validator intVal()
* @method static Validator intType()
* @method static Validator ip($ipOptions = null)
* @method static Validator iterableType()
* @method static Validator json()
* @method static Validator key(string $reference, Validatable $referenceValidator = null, bool $mandatory = true)
* @method static Validator keyNested(string $reference, Validatable $referenceValidator = null, bool $mandatory = true)
* @method static Validator keySet(Key ...$rule)
* @method static Validator keyValue(string $comparedKey, string $ruleName, string $baseKey)
* @method static Validator languageCode(string $set)
* @method static Validator leapDate(string $format)
* @method static Validator leapYear()
* @method static Validator length(int $min = null, int $max = null, bool $inclusive = true)
* @method static Validator lowercase()
* @method static Validator luhn()
* @method static Validator macAddress()
* @method static Validator max($maxValue, bool $inclusive = true)
* @method static Validator maximumAge(int $age, string $format = null)
* @method static Validator mimetype(string $mimetype)
* @method static Validator min($minValue, bool $inclusive = true)
* @method static Validator minimumAge(int $age, bool $format = null)
* @method static Validator multiple(int $multipleOf)
* @method static Validator negative()
* @method static Validator nif()
* @method static Validator no($useLocale = false)
* @method static Validator noneOf(Validatable ...$rule)
* @method static Validator not(Validatable $rule)
* @method static Validator notBlank()
* @method static Validator notEmpty()
* @method static Validator notOptional()
* @method static Validator noWhitespace()
* @method static Validator nullable(Validatable $rule)
* @method static Validator nullType()
* @method static Validator number()
* @method static Validator numericVal()
* @method static Validator objectType()
* @method static Validator odd()
* @method static Validator oneOf(Validatable ...$rule)
* @method static Validator optional(Validatable $rule)
* @method static Validator perfectSquare()
* @method static Validator pesel()
* @method static Validator phone()
* @method static Validator phpLabel()
* @method static Validator pis()
* @method static Validator positive()
* @method static Validator postalCode(string $countryCode)
* @method static Validator primeNumber()
* @method static Validator prnt(string $additionalChars = null)
* @method static Validator punct(string $additionalChars = null)
* @method static Validator readable()
* @method static Validator regex(string $regex)
* @method static Validator resourceType()
* @method static Validator roman()
* @method static Validator scalarVal()
* @method static Validator sf(string $name, array $params = null)
* @method static Validator size(string $minSize = null, string $maxSize = null)
* @method static Validator slug()
* @method static Validator space(string $additionalChars = null)
* @method static Validator startsWith($startValue, bool $identical = false)
* @method static Validator stringType()
* @method static Validator stringVal()
* @method static Validator subdivisionCode(string $countryCode)
* @method static Validator symbolicLink()
* @method static Validator time(string $format = 'H:i:s')
* @method static Validator tld()
* @method static Validator trueVal()
* @method static Validator type(string $type)
* @method static Validator unique()
* @method static Validator uploaded()
* @method static Validator uppercase()
* @method static Validator url()
* @method static Validator uuid()
* @method static Validator vatin(string $countryCode)
* @method static Validator version()
* @method static Validator videoUrl(string $service = null)
* @method static Validator vowel()
* @method static Validator when(Validatable $if, Validatable $then, Validatable $when = null)
* @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->updateTemplate($this->template);
}
throw $exception;
}
}
/**
* @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);
}
/**
* @param mixed $ruleSpec
* @param array $arguments
*
* @return Validatable
*/
public static function buildRule($ruleSpec, $arguments = [])
{
try {
return Factory::getDefaultInstance()->rule($ruleSpec, $arguments);
} catch (\Exception $exception) {
throw new ComponentException($exception->getMessage(), $exception->getCode(), $exception);
}
}
/**
* @param string $method
* @param array $arguments
*
* @return self
*/
public function __call($method, $arguments)
{
return $this->addRule(static::buildRule($method, $arguments));
}
/**
* Create instance validator.
*
* @return Validator
*/
public static function create()
{
$ref = new ReflectionClass(__CLASS__);
return $ref->newInstanceArgs(func_get_args());
}
}