fix: update validation to support versions 6 & 7

This commit is contained in:
Ben Ramsey
2022-09-15 22:14:34 -05:00
parent ae247f1dcd
commit a180174b0e
3 changed files with 27 additions and 5 deletions
+1
View File
@@ -109,6 +109,7 @@ interface FieldsInterface extends BaseFieldsInterface
* 4. Randomly generated UUID
* 5. Name-based UUID hashed with SHA-1
* 6. Reordered time UUID
* 7. Unix Epoch time UUID
*
* This returns `null` if the UUID is not an RFC 4122 variant, since version
* is only meaningful for this variant.
+3 -2
View File
@@ -28,7 +28,7 @@ use function str_replace;
final class Validator implements ValidatorInterface
{
private const VALID_PATTERN = '\A[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-'
. '[1-5]{1}[0-9A-Fa-f]{3}-[ABab89]{1}[0-9A-Fa-f]{3}-[0-9A-Fa-f]{12}\z';
. '[1-7][0-9A-Fa-f]{3}-[ABab89][0-9A-Fa-f]{3}-[0-9A-Fa-f]{12}\z';
/**
* @psalm-return non-empty-string
@@ -43,7 +43,8 @@ final class Validator implements ValidatorInterface
public function validate(string $uuid): bool
{
$uuid = str_replace(['urn:', 'uuid:', 'URN:', 'UUID:', '{', '}'], '', $uuid);
$uuid = strtolower($uuid);
return $uuid === Uuid::NIL || preg_match('/' . self::VALID_PATTERN . '/Dms', $uuid);
return $uuid === Uuid::NIL || $uuid === Uuid::MAX || preg_match('/' . self::VALID_PATTERN . '/Dms', $uuid);
}
}
+23 -3
View File
@@ -30,7 +30,15 @@ class ValidatorTest extends TestCase
$validator = new Validator();
foreach ($variations as $variation) {
$this->assertSame($expected, $validator->validate($variation));
$this->assertSame(
$expected,
$validator->validate($variation),
sprintf(
'Expected "%s" to be %s',
$variation,
$expected ? 'valid' : 'not valid',
),
);
}
}
@@ -40,7 +48,7 @@ class ValidatorTest extends TestCase
public function provideValuesForValidation(): array
{
$hexMutations = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c', 'd', 'e', 'f'];
$trueVersions = [1, 2, 3, 4, 5];
$trueVersions = [1, 2, 3, 4, 5, 6, 7];
$trueVariants = [8, 9, 'a', 'b'];
$testValues = [];
@@ -87,13 +95,25 @@ class ValidatorTest extends TestCase
'value' => "\nff6f8cb0-c57d-11e1-1b21-0800200c9a66\n",
'expected' => false,
],
[
'value' => '00000000-0000-0000-0000-000000000000',
'expected' => true,
],
[
'value' => 'ffffffff-ffff-ffff-ffff-ffffffffffff',
'expected' => true,
],
[
'value' => 'FFFFFFFF-FFFF-FFFF-FFFF-FFFFFFFFFFFF',
'expected' => true,
],
]);
}
public function testGetPattern(): void
{
$expectedPattern = '\A[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-'
. '[1-5]{1}[0-9A-Fa-f]{3}-[ABab89]{1}[0-9A-Fa-f]{3}-[0-9A-Fa-f]{12}\z';
. '[1-7][0-9A-Fa-f]{3}-[ABab89][0-9A-Fa-f]{3}-[0-9A-Fa-f]{12}\z';
$validator = new Validator();