From c2797773a5b9127f1e820860c484e24e3c302621 Mon Sep 17 00:00:00 2001 From: Fanen Ahua Date: Thu, 19 Mar 2026 15:23:14 +0000 Subject: [PATCH] Allow usage of UUIDs v1 to v8 --- library/Rules/Uuid.php | 8 ++++---- tests/unit/Rules/UuidTest.php | 38 ++++++++++++++++++++++------------- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/library/Rules/Uuid.php b/library/Rules/Uuid.php index 5250842db..905cb1c29 100644 --- a/library/Rules/Uuid.php +++ b/library/Rules/Uuid.php @@ -18,7 +18,7 @@ /** * Validates whether the input is a valid UUID. * - * It also supports validation of specific versions 1, 3, 4 and 5. + * It also supports validation of specific versions 1 to 8. * * @author Dick van der Heiden * @author Henrique Moody @@ -46,7 +46,7 @@ final class Uuid extends AbstractRule public function __construct(?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 ComponentException(sprintf('Only versions 1 to 8 are supported: %d given', $version)); } $this->version = $version; @@ -66,7 +66,7 @@ public function validate($input): bool private function isSupportedVersion(int $version): bool { - return $version >= 1 && $version <= 5 && $version !== 2; + return $version >= 1 && $version <= 8; } private function getPattern(): string @@ -75,6 +75,6 @@ private function getPattern(): string return sprintf(self::PATTERN_FORMAT, $this->version); } - return sprintf(self::PATTERN_FORMAT, '[13-5]'); + return sprintf(self::PATTERN_FORMAT, '[1-8]'); } } diff --git a/tests/unit/Rules/UuidTest.php b/tests/unit/Rules/UuidTest.php index 159cf4a02..e56a2001d 100644 --- a/tests/unit/Rules/UuidTest.php +++ b/tests/unit/Rules/UuidTest.php @@ -30,30 +30,24 @@ final class UuidTest extends RuleTestCase { private const UUID_VERSION_1 = 'e4eaaaf2-d142-11e1-b3e4-080027620cdd'; + private const UUID_VERSION_2 = '000003e8-3702-21f0-9f00-325096b39f47'; private const UUID_VERSION_3 = '11a38b9a-b3da-360f-9353-a5a725514269'; private const UUID_VERSION_4 = '25769c6c-d34d-4bfe-ba98-e0ee856f3e7a'; private const UUID_VERSION_5 = 'c4a760a8-dbcf-5254-a0d9-6a4474bd1b62'; + private const UUID_VERSION_6 = '1f1239e6-c96d-6964-ae1d-9da658bd666c'; + private const UUID_VERSION_7 = '019d0676-4cf1-77c4-834e-2b847eb7d6f3'; + private const UUID_VERSION_8 = '019d0676-dc57-8649-ae91-5913434dbdc2'; /** * @test */ - public function itShouldThrowExceptionWhenVersionIsTwo(): void - { - self::expectException(ComponentException::class); - self::expectExceptionMessage('Only versions 1, 3, 4, and 5 are supported: 2 given'); - - new Uuid(2); - } - /** - * @test - */ - public function itShouldThrowExceptionWhenVersionIsGreaterThanFive(): void + public function itShouldThrowExceptionWhenVersionIsGreaterThanEight(): void { - $version = random_int(6, PHP_INT_MAX); + $version = random_int(9, PHP_INT_MAX); self::expectException(ComponentException::class); - self::expectExceptionMessage('Only versions 1, 3, 4, and 5 are supported: ' . $version . ' given'); + self::expectExceptionMessage('Only versions 1 to 8 are supported: ' . $version . ' given'); new Uuid($version); } @@ -66,7 +60,7 @@ public function itShouldThrowExceptionWhenVersionIsLessThanOne(): void $version = random_int(PHP_INT_MIN, 0); self::expectException(ComponentException::class); - self::expectExceptionMessage('Only versions 1, 3, 4, and 5 are supported: ' . $version . ' given'); + self::expectExceptionMessage('Only versions 1 to 8 are supported: ' . $version . ' given'); new Uuid($version); } @@ -80,13 +74,21 @@ public static function providerForValidInput(): array return [ 'any version with version 1' => [$sut, self::UUID_VERSION_1], + 'any version with version 2' => [$sut, self::UUID_VERSION_2], 'any version with version 3' => [$sut, self::UUID_VERSION_3], 'any version with version 4' => [$sut, self::UUID_VERSION_4], 'any version with version 5' => [$sut, self::UUID_VERSION_5], + 'any version with version 6' => [$sut, self::UUID_VERSION_6], + 'any version with version 7' => [$sut, self::UUID_VERSION_7], + 'any version with version 8' => [$sut, self::UUID_VERSION_8], 'version 1 with version 1' => [new Uuid(1), self::UUID_VERSION_1], + 'version 2 with version 2' => [new Uuid(2), self::UUID_VERSION_2], 'version 3 with version 3' => [new Uuid(3), self::UUID_VERSION_3], 'version 4 with version 4' => [new Uuid(4), self::UUID_VERSION_4], 'version 5 with version 5' => [new Uuid(5), self::UUID_VERSION_5], + 'version 6 with version 6' => [new Uuid(6), self::UUID_VERSION_6], + 'version 7 with version 7' => [new Uuid(7), self::UUID_VERSION_7], + 'version 8 with version 8' => [new Uuid(8), self::UUID_VERSION_8], ]; } @@ -97,9 +99,13 @@ public static function providerForInvalidInput(): array { $sut = new Uuid(); $sutVersion1 = new Uuid(1); + $sutVersion2 = new Uuid(2); $sutVersion3 = new Uuid(3); $sutVersion4 = new Uuid(4); $sutVersion5 = new Uuid(5); + $sutVersion6 = new Uuid(6); + $sutVersion7 = new Uuid(7); + $sutVersion8 = new Uuid(8); return [ 'empty' => [$sut, ''], @@ -110,6 +116,7 @@ public static function providerForInvalidInput(): array 'version 1 with version 3' => [$sutVersion1, self::UUID_VERSION_3], 'version 1 with version 4' => [$sutVersion1, self::UUID_VERSION_4], 'version 1 with version 5' => [$sutVersion1, self::UUID_VERSION_5], + 'version 2 with version 5' => [$sutVersion2, self::UUID_VERSION_5], 'version 3 with version 1' => [$sutVersion3, self::UUID_VERSION_1], 'version 3 with version 4' => [$sutVersion3, self::UUID_VERSION_4], 'version 3 with version 5' => [$sutVersion3, self::UUID_VERSION_5], @@ -119,6 +126,9 @@ public static function providerForInvalidInput(): array 'version 5 with version 1' => [$sutVersion5, self::UUID_VERSION_1], 'version 5 with version 3' => [$sutVersion5, self::UUID_VERSION_3], 'version 5 with version 4' => [$sutVersion5, self::UUID_VERSION_4], + 'version 6 with version 4' => [$sutVersion6, self::UUID_VERSION_4], + 'version 7 with version 3' => [$sutVersion7, self::UUID_VERSION_3], + 'version 8 with version 5' => [$sutVersion8, self::UUID_VERSION_5], 'array' => [$sut, []], 'boolean true' => [$sut, true], 'boolean false' => [$sut, false],