Update the validation engine of the "Uuid" rule

Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
Henrique Moody 2024-03-24 17:31:36 +01:00
parent ea605b61b6
commit 67888e1b8d
No known key found for this signature in database
GPG key ID: 221E9281655813A6
2 changed files with 17 additions and 23 deletions

View file

@ -9,8 +9,10 @@ declare(strict_types=1);
namespace Respect\Validation\Rules; namespace Respect\Validation\Rules;
use Respect\Validation\Exceptions\ComponentException; use Respect\Validation\Exceptions\InvalidRuleConstructorException;
use Respect\Validation\Message\Template; use Respect\Validation\Message\Template;
use Respect\Validation\Result;
use Respect\Validation\Rules\Core\Standard;
use function is_string; use function is_string;
use function preg_match; use function preg_match;
@ -26,7 +28,7 @@ use function sprintf;
'{{name}} must not be a valid UUID version {{version|raw}}', '{{name}} must not be a valid UUID version {{version|raw}}',
self::TEMPLATE_VERSION, self::TEMPLATE_VERSION,
)] )]
final class Uuid extends AbstractRule final class Uuid extends Standard
{ {
public const TEMPLATE_VERSION = '__version__'; public const TEMPLATE_VERSION = '__version__';
@ -36,30 +38,22 @@ final class Uuid extends AbstractRule
private readonly ?int $version = null private readonly ?int $version = null
) { ) {
if ($version !== null && !$this->isSupportedVersion($version)) { 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)) { if (!is_string($input)) {
return false; return Result::failed($input, $this, $parameters, $template);
} }
return preg_match($this->getPattern(), $input) > 0; return new Result(preg_match($this->getPattern(), $input) > 0, $input, $this, $parameters, $template);
}
/**
* @return array<string, mixed>
*/
public function getParams(): array
{
return ['version' => $this->version];
}
protected function getStandardTemplate(mixed $input): string
{
return $this->version ? self::TEMPLATE_VERSION : self::TEMPLATE_STANDARD;
} }
private function isSupportedVersion(int $version): bool private function isSupportedVersion(int $version): bool

View file

@ -12,7 +12,7 @@ namespace Respect\Validation\Rules;
use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\Attributes\Test;
use Respect\Validation\Exceptions\ComponentException; use Respect\Validation\Exceptions\InvalidRuleConstructorException;
use Respect\Validation\Test\RuleTestCase; use Respect\Validation\Test\RuleTestCase;
use stdClass; use stdClass;
@ -33,7 +33,7 @@ final class UuidTest extends RuleTestCase
#[Test] #[Test]
public function itShouldThrowExceptionWhenVersionIsTwo(): void 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'); self::expectExceptionMessage('Only versions 1, 3, 4, and 5 are supported: 2 given');
new Uuid(2); new Uuid(2);
@ -44,7 +44,7 @@ final class UuidTest extends RuleTestCase
{ {
$version = random_int(6, PHP_INT_MAX); $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'); self::expectExceptionMessage('Only versions 1, 3, 4, and 5 are supported: ' . $version . ' given');
new Uuid($version); new Uuid($version);
@ -55,7 +55,7 @@ final class UuidTest extends RuleTestCase
{ {
$version = random_int(PHP_INT_MIN, 0); $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'); self::expectExceptionMessage('Only versions 1, 3, 4, and 5 are supported: ' . $version . ' given');
new Uuid($version); new Uuid($version);