Skip to content

Commit a180174

Browse files
committed
fix: update validation to support versions 6 & 7
1 parent ae247f1 commit a180174

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

src/Rfc4122/FieldsInterface.php

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ public function getVariant(): int;
109109
* 4. Randomly generated UUID
110110
* 5. Name-based UUID hashed with SHA-1
111111
* 6. Reordered time UUID
112+
* 7. Unix Epoch time UUID
112113
*
113114
* This returns `null` if the UUID is not an RFC 4122 variant, since version
114115
* is only meaningful for this variant.

src/Rfc4122/Validator.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
final class Validator implements ValidatorInterface
2929
{
3030
private const VALID_PATTERN = '\A[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-'
31-
. '[1-5]{1}[0-9A-Fa-f]{3}-[ABab89]{1}[0-9A-Fa-f]{3}-[0-9A-Fa-f]{12}\z';
31+
. '[1-7][0-9A-Fa-f]{3}-[ABab89][0-9A-Fa-f]{3}-[0-9A-Fa-f]{12}\z';
3232

3333
/**
3434
* @psalm-return non-empty-string
@@ -43,7 +43,8 @@ public function getPattern(): string
4343
public function validate(string $uuid): bool
4444
{
4545
$uuid = str_replace(['urn:', 'uuid:', 'URN:', 'UUID:', '{', '}'], '', $uuid);
46+
$uuid = strtolower($uuid);
4647

47-
return $uuid === Uuid::NIL || preg_match('/' . self::VALID_PATTERN . '/Dms', $uuid);
48+
return $uuid === Uuid::NIL || $uuid === Uuid::MAX || preg_match('/' . self::VALID_PATTERN . '/Dms', $uuid);
4849
}
4950
}

tests/Rfc4122/ValidatorTest.php

+23-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,15 @@ public function testValidate(string $value, bool $expected): void
3030
$validator = new Validator();
3131

3232
foreach ($variations as $variation) {
33-
$this->assertSame($expected, $validator->validate($variation));
33+
$this->assertSame(
34+
$expected,
35+
$validator->validate($variation),
36+
sprintf(
37+
'Expected "%s" to be %s',
38+
$variation,
39+
$expected ? 'valid' : 'not valid',
40+
),
41+
);
3442
}
3543
}
3644

@@ -40,7 +48,7 @@ public function testValidate(string $value, bool $expected): void
4048
public function provideValuesForValidation(): array
4149
{
4250
$hexMutations = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c', 'd', 'e', 'f'];
43-
$trueVersions = [1, 2, 3, 4, 5];
51+
$trueVersions = [1, 2, 3, 4, 5, 6, 7];
4452
$trueVariants = [8, 9, 'a', 'b'];
4553

4654
$testValues = [];
@@ -87,13 +95,25 @@ public function provideValuesForValidation(): array
8795
'value' => "\nff6f8cb0-c57d-11e1-1b21-0800200c9a66\n",
8896
'expected' => false,
8997
],
98+
[
99+
'value' => '00000000-0000-0000-0000-000000000000',
100+
'expected' => true,
101+
],
102+
[
103+
'value' => 'ffffffff-ffff-ffff-ffff-ffffffffffff',
104+
'expected' => true,
105+
],
106+
[
107+
'value' => 'FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF',
108+
'expected' => true,
109+
],
90110
]);
91111
}
92112

93113
public function testGetPattern(): void
94114
{
95115
$expectedPattern = '\A[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-'
96-
. '[1-5]{1}[0-9A-Fa-f]{3}-[ABab89]{1}[0-9A-Fa-f]{3}-[0-9A-Fa-f]{12}\z';
116+
. '[1-7][0-9A-Fa-f]{3}-[ABab89][0-9A-Fa-f]{3}-[0-9A-Fa-f]{12}\z';
97117

98118
$validator = new Validator();
99119

0 commit comments

Comments
 (0)