mirror of
https://github.com/ramsey/uuid.git
synced 2026-06-13 15:46:53 +03:00
fix: update validation to support versions 6 & 7
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user