mirror of
https://github.com/Respect/Validation.git
synced 2024-06-29 10:40:24 +02:00
Update the validation engine of the "Uuid" rule
Signed-off-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
ea605b61b6
commit
67888e1b8d
|
@ -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
|
||||||
|
|
|
@ -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);
|
||||||
|
|
Loading…
Reference in a new issue