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
|
|
|
/*
|
2023-02-17 05:13:51 +01:00
|
|
|
* Copyright (c) Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
|
|
* SPDX-License-Identifier: MIT
|
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;
|
|
|
|
|
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;
|
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
|
|
|
/**
|
2020-10-04 13:04:20 +02:00
|
|
|
* @mixin StaticValidator
|
2018-11-25 17:56:04 +01:00
|
|
|
*
|
2023-02-13 05:59:06 +01:00
|
|
|
* @author Alexandre Gomes Gaigalas <alganet@gmail.com>
|
2018-11-25 17:56:04 +01:00
|
|
|
* @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
|
|
|
}
|