From 67888e1b8d59e17104dc2b86d8e21a0d60a909ce Mon Sep 17 00:00:00 2001 From: Henrique Moody Date: Sun, 24 Mar 2024 17:31:36 +0100 Subject: [PATCH] Update the validation engine of the "Uuid" rule Signed-off-by: Henrique Moody --- library/Rules/Uuid.php | 32 +++++++++++++------------------- tests/unit/Rules/UuidTest.php | 8 ++++---- 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/library/Rules/Uuid.php b/library/Rules/Uuid.php index 0592a6e56..8957a65c9 100644 --- a/library/Rules/Uuid.php +++ b/library/Rules/Uuid.php @@ -9,8 +9,10 @@ namespace Respect\Validation\Rules; -use Respect\Validation\Exceptions\ComponentException; +use Respect\Validation\Exceptions\InvalidRuleConstructorException; use Respect\Validation\Message\Template; +use Respect\Validation\Result; +use Respect\Validation\Rules\Core\Standard; use function is_string; use function preg_match; @@ -26,7 +28,7 @@ '{{name}} must not be a valid UUID version {{version|raw}}', self::TEMPLATE_VERSION, )] -final class Uuid extends AbstractRule +final class Uuid extends Standard { public const TEMPLATE_VERSION = '__version__'; @@ -36,30 +38,22 @@ public function __construct( private readonly ?int $version = null ) { if ($version !== null && !$this->isSupportedVersion($version)) { - throw new ComponentException(sprintf('Only versions 1, 3, 4, and 5 are supported: %d given', $version)); + throw new InvalidRuleConstructorException( + 'Only versions 1, 3, 4, and 5 are supported: %d given', + (string) $version + ); } } - public function validate(mixed $input): bool + public function evaluate(mixed $input): Result { + $template = $this->version ? self::TEMPLATE_VERSION : self::TEMPLATE_STANDARD; + $parameters = ['version' => $this->version]; if (!is_string($input)) { - return false; + return Result::failed($input, $this, $parameters, $template); } - return preg_match($this->getPattern(), $input) > 0; - } - - /** - * @return array - */ - public function getParams(): array - { - return ['version' => $this->version]; - } - - protected function getStandardTemplate(mixed $input): string - { - return $this->version ? self::TEMPLATE_VERSION : self::TEMPLATE_STANDARD; + return new Result(preg_match($this->getPattern(), $input) > 0, $input, $this, $parameters, $template); } private function isSupportedVersion(int $version): bool diff --git a/tests/unit/Rules/UuidTest.php b/tests/unit/Rules/UuidTest.php index 849f2a862..e7a716f6c 100644 --- a/tests/unit/Rules/UuidTest.php +++ b/tests/unit/Rules/UuidTest.php @@ -12,7 +12,7 @@ use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\Attributes\Test; -use Respect\Validation\Exceptions\ComponentException; +use Respect\Validation\Exceptions\InvalidRuleConstructorException; use Respect\Validation\Test\RuleTestCase; use stdClass; @@ -33,7 +33,7 @@ final class UuidTest extends RuleTestCase #[Test] public function itShouldThrowExceptionWhenVersionIsTwo(): void { - self::expectException(ComponentException::class); + self::expectException(InvalidRuleConstructorException::class); self::expectExceptionMessage('Only versions 1, 3, 4, and 5 are supported: 2 given'); new Uuid(2); @@ -44,7 +44,7 @@ public function itShouldThrowExceptionWhenVersionIsGreaterThanFive(): void { $version = random_int(6, PHP_INT_MAX); - self::expectException(ComponentException::class); + self::expectException(InvalidRuleConstructorException::class); self::expectExceptionMessage('Only versions 1, 3, 4, and 5 are supported: ' . $version . ' given'); new Uuid($version); @@ -55,7 +55,7 @@ public function itShouldThrowExceptionWhenVersionIsLessThanOne(): void { $version = random_int(PHP_INT_MIN, 0); - self::expectException(ComponentException::class); + self::expectException(InvalidRuleConstructorException::class); self::expectExceptionMessage('Only versions 1, 3, 4, and 5 are supported: ' . $version . ' given'); new Uuid($version);