From 5b81e9697307fe1519d5ea887dbb1981402bd772 Mon Sep 17 00:00:00 2001 From: Henrique Moody Date: Fri, 23 Feb 2024 01:31:42 +0100 Subject: [PATCH] Update the validation engine of the "Regex" rule Signed-off-by: Henrique Moody --- library/Rules/Regex.php | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/library/Rules/Regex.php b/library/Rules/Regex.php index cb5956d1c..0d89103b3 100644 --- a/library/Rules/Regex.php +++ b/library/Rules/Regex.php @@ -10,6 +10,7 @@ namespace Respect\Validation\Rules; use Respect\Validation\Message\Template; +use Respect\Validation\Result; use function is_scalar; use function preg_match; @@ -18,27 +19,20 @@ '{{name}} must validate against `{{regex|raw}}`', '{{name}} must not validate against `{{regex|raw}}`', )] -final class Regex extends AbstractRule +final class Regex extends Standard { public function __construct( private readonly string $regex ) { } - public function validate(mixed $input): bool + public function evaluate(mixed $input): Result { + $parameters = ['regex' => $this->regex]; if (!is_scalar($input)) { - return false; + return Result::failed($input, $this)->withParameters($parameters); } - return preg_match($this->regex, (string) $input) > 0; - } - - /** - * @return array - */ - public function getParams(): array - { - return ['regex' => $this->regex]; + return new Result(preg_match($this->regex, (string) $input) === 1, $input, $this, parameters: $parameters); } }