Skip to content

Commit

Permalink
Update the validation engine of the "Uuid" rule
Browse files Browse the repository at this point in the history
Signed-off-by: Henrique Moody <[email protected]>
  • Loading branch information
henriquemoody committed Mar 24, 2024
1 parent ea605b6 commit 67888e1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 23 deletions.
32 changes: 13 additions & 19 deletions library/Rules/Uuid.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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__';

Expand All @@ -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<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;
return new Result(preg_match($this->getPattern(), $input) > 0, $input, $this, $parameters, $template);
}

private function isSupportedVersion(int $version): bool
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/Rules/UuidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 67888e1

Please sign in to comment.