From 13edf70fc959bdc2ecf68bb2da0a6cf358f016ce Mon Sep 17 00:00:00 2001 From: Ben Ramsey Date: Fri, 1 Apr 2022 16:08:22 -0500 Subject: [PATCH] feat: introduce Variant enum and use instead of constants --- composer.json | 2 +- src/BinaryUtils.php | 12 ++-- src/Guid/Fields.php | 4 +- src/Rfc4122/Fields.php | 3 +- src/Rfc4122/FieldsInterface.php | 3 +- src/Rfc4122/VariantTrait.php | 23 +++---- src/Uuid.php | 28 --------- src/Variant.php | 36 +++++++++++ tests/BinaryUtilsTest.php | 29 +++++++-- tests/ExpectedBehaviorTest.php | 18 ++---- tests/Guid/FieldsTest.php | 11 ++-- tests/Nonstandard/FieldsTest.php | 4 +- tests/Rfc4122/FieldsTest.php | 15 ++--- tests/Rfc4122/VariantTraitTest.php | 3 +- tests/UuidTest.php | 99 +++++++++++++++--------------- 15 files changed, 157 insertions(+), 133 deletions(-) create mode 100644 src/Variant.php diff --git a/composer.json b/composer.json index 3da822e..7e4fec6 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ "guid" ], "require": { - "php": "^8.0", + "php": "^8.1", "ext-ctype": "*", "ext-json": "*", "brick/math": "^0.8 || ^0.9" diff --git a/src/BinaryUtils.php b/src/BinaryUtils.php index fb8ba9a..8283f49 100644 --- a/src/BinaryUtils.php +++ b/src/BinaryUtils.php @@ -31,12 +31,14 @@ class BinaryUtils * * @psalm-pure */ - public static function applyVariant(int $clockSeq): int + public static function applyVariant(int $clockSeq, Variant $variant = Variant::Rfc4122): int { - $clockSeq = $clockSeq & 0x3fff; - $clockSeq |= 0x8000; - - return $clockSeq; + return match ($variant) { + Variant::ReservedNcs => $clockSeq & 0x7fff, + Variant::Rfc4122 => $clockSeq & 0x3fff | 0x8000, + Variant::ReservedMicrosoft => $clockSeq & 0x1fff | 0xc000, + Variant::ReservedFuture => $clockSeq & 0x1fff | 0xe000, + }; } /** diff --git a/src/Guid/Fields.php b/src/Guid/Fields.php index d8a1a2b..eda6e6a 100644 --- a/src/Guid/Fields.php +++ b/src/Guid/Fields.php @@ -21,7 +21,7 @@ use Ramsey\Uuid\Rfc4122\NilTrait; use Ramsey\Uuid\Rfc4122\VariantTrait; use Ramsey\Uuid\Rfc4122\VersionTrait; use Ramsey\Uuid\Type\Hexadecimal; -use Ramsey\Uuid\Uuid; +use Ramsey\Uuid\Variant; use function bin2hex; use function dechex; @@ -189,6 +189,6 @@ final class Fields implements FieldsInterface $variant = $this->getVariant(); - return $variant === Uuid::RFC_4122 || $variant === Uuid::RESERVED_MICROSOFT; + return $variant === Variant::Rfc4122 || $variant === Variant::ReservedMicrosoft; } } diff --git a/src/Rfc4122/Fields.php b/src/Rfc4122/Fields.php index 2ccc20b..0c54a11 100644 --- a/src/Rfc4122/Fields.php +++ b/src/Rfc4122/Fields.php @@ -18,6 +18,7 @@ use Ramsey\Uuid\Exception\InvalidArgumentException; use Ramsey\Uuid\Fields\SerializableFieldsTrait; use Ramsey\Uuid\Type\Hexadecimal; use Ramsey\Uuid\Uuid; +use Ramsey\Uuid\Variant; use function bin2hex; use function dechex; @@ -189,6 +190,6 @@ final class Fields implements FieldsInterface return true; } - return $this->getVariant() === Uuid::RFC_4122; + return $this->getVariant() === Variant::Rfc4122; } } diff --git a/src/Rfc4122/FieldsInterface.php b/src/Rfc4122/FieldsInterface.php index a303525..5b4b801 100644 --- a/src/Rfc4122/FieldsInterface.php +++ b/src/Rfc4122/FieldsInterface.php @@ -16,6 +16,7 @@ namespace Ramsey\Uuid\Rfc4122; use Ramsey\Uuid\Fields\FieldsInterface as BaseFieldsInterface; use Ramsey\Uuid\Type\Hexadecimal; +use Ramsey\Uuid\Variant; /** * RFC 4122 defines fields for a specific variant of UUID @@ -95,7 +96,7 @@ interface FieldsInterface extends BaseFieldsInterface * * @link http://tools.ietf.org/html/rfc4122#section-4.1.1 RFC 4122, § 4.1.1: Variant */ - public function getVariant(): int; + public function getVariant(): Variant; /** * Returns the version diff --git a/src/Rfc4122/VariantTrait.php b/src/Rfc4122/VariantTrait.php index 4c98165..81c0d0d 100644 --- a/src/Rfc4122/VariantTrait.php +++ b/src/Rfc4122/VariantTrait.php @@ -15,7 +15,7 @@ declare(strict_types=1); namespace Ramsey\Uuid\Rfc4122; use Ramsey\Uuid\Exception\InvalidBytesException; -use Ramsey\Uuid\Uuid; +use Ramsey\Uuid\Variant; use function decbin; use function str_pad; @@ -41,18 +41,11 @@ trait VariantTrait /** * Returns the variant identifier, according to RFC 4122, for the given bytes * - * The following values may be returned: - * - * - `0` -- Reserved, NCS backward compatibility. - * - `2` -- The variant specified in RFC 4122. - * - `6` -- Reserved, Microsoft Corporation backward compatibility. - * - `7` -- Reserved for future definition. - * * @link https://tools.ietf.org/html/rfc4122#section-4.1.1 RFC 4122, § 4.1.1: Variant * - * @return int The variant identifier, according to RFC 4122 + * @return Variant The variant identifier, according to RFC 4122 */ - public function getVariant(): int + public function getVariant(): Variant { if (strlen($this->getBytes()) !== 16) { throw new InvalidBytesException('Invalid number of bytes'); @@ -76,13 +69,13 @@ trait VariantTrait $msb = substr($binary, 0, 3); if ($msb === '111') { - $variant = Uuid::RESERVED_FUTURE; + $variant = Variant::ReservedFuture; } elseif ($msb === '110') { - $variant = Uuid::RESERVED_MICROSOFT; - } elseif (strpos($msb, '10') === 0) { - $variant = Uuid::RFC_4122; + $variant = Variant::ReservedMicrosoft; + } elseif (str_starts_with($msb, '10')) { + $variant = Variant::Rfc4122; } else { - $variant = Uuid::RESERVED_NCS; + $variant = Variant::ReservedNcs; } return $variant; diff --git a/src/Uuid.php b/src/Uuid.php index bf9048a..5f6e02e 100644 --- a/src/Uuid.php +++ b/src/Uuid.php @@ -80,34 +80,6 @@ class Uuid implements Rfc4122UuidInterface */ public const NIL = '00000000-0000-0000-0000-000000000000'; - /** - * Variant: reserved, NCS backward compatibility - * - * @link http://tools.ietf.org/html/rfc4122#section-4.1.1 RFC 4122, § 4.1.1: Variant - */ - public const RESERVED_NCS = 0; - - /** - * Variant: the UUID layout specified in RFC 4122 - * - * @link http://tools.ietf.org/html/rfc4122#section-4.1.1 RFC 4122, § 4.1.1: Variant - */ - public const RFC_4122 = 2; - - /** - * Variant: reserved, Microsoft Corporation backward compatibility - * - * @link http://tools.ietf.org/html/rfc4122#section-4.1.1 RFC 4122, § 4.1.1: Variant - */ - public const RESERVED_MICROSOFT = 6; - - /** - * Variant: reserved for future definition - * - * @link http://tools.ietf.org/html/rfc4122#section-4.1.1 RFC 4122, § 4.1.1: Variant - */ - public const RESERVED_FUTURE = 7; - /** * @deprecated Use {@see ValidatorInterface::getPattern()} instead. */ diff --git a/src/Variant.php b/src/Variant.php new file mode 100644 index 0000000..5b7505c --- /dev/null +++ b/src/Variant.php @@ -0,0 +1,36 @@ +assertSame($expectedInt, BinaryUtils::applyVariant($clockSeq)); - $this->assertSame($expectedHex, dechex(BinaryUtils::applyVariant($clockSeq))); + $this->assertSame($expectedInt, BinaryUtils::applyVariant($clockSeq, $variant)); + $this->assertSame($expectedHex, dechex(BinaryUtils::applyVariant($clockSeq, $variant))); } /** @@ -128,108 +129,128 @@ class BinaryUtilsTest extends TestCase } /** - * @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingTraversableTypeHintSpecification + * @return array */ public function provideVariantTestValues(): array { return [ [ 'clockSeq' => 0, + 'variant' => Variant::Rfc4122, 'expectedInt' => 32768, 'expectedHex' => '8000', ], [ 'clockSeq' => 4096, + 'variant' => Variant::Rfc4122, 'expectedInt' => 36864, 'expectedHex' => '9000', ], [ 'clockSeq' => 8192, + 'variant' => Variant::Rfc4122, 'expectedInt' => 40960, 'expectedHex' => 'a000', ], [ 'clockSeq' => 12288, + 'variant' => Variant::Rfc4122, 'expectedInt' => 45056, 'expectedHex' => 'b000', ], [ 'clockSeq' => 4095, + 'variant' => Variant::Rfc4122, 'expectedInt' => 36863, 'expectedHex' => '8fff', ], [ 'clockSeq' => 8191, + 'variant' => Variant::Rfc4122, 'expectedInt' => 40959, 'expectedHex' => '9fff', ], [ 'clockSeq' => 12287, + 'variant' => Variant::Rfc4122, 'expectedInt' => 45055, 'expectedHex' => 'afff', ], [ 'clockSeq' => 16383, + 'variant' => Variant::Rfc4122, 'expectedInt' => 49151, 'expectedHex' => 'bfff', ], [ 'clockSeq' => 16384, + 'variant' => Variant::Rfc4122, 'expectedInt' => 32768, 'expectedHex' => '8000', ], [ 'clockSeq' => 20480, + 'variant' => Variant::Rfc4122, 'expectedInt' => 36864, 'expectedHex' => '9000', ], [ 'clockSeq' => 24576, + 'variant' => Variant::Rfc4122, 'expectedInt' => 40960, 'expectedHex' => 'a000', ], [ 'clockSeq' => 28672, + 'variant' => Variant::Rfc4122, 'expectedInt' => 45056, 'expectedHex' => 'b000', ], [ 'clockSeq' => 32768, + 'variant' => Variant::Rfc4122, 'expectedInt' => 32768, 'expectedHex' => '8000', ], [ 'clockSeq' => 36864, + 'variant' => Variant::Rfc4122, 'expectedInt' => 36864, 'expectedHex' => '9000', ], [ 'clockSeq' => 40960, + 'variant' => Variant::Rfc4122, 'expectedInt' => 40960, 'expectedHex' => 'a000', ], [ 'clockSeq' => 45056, + 'variant' => Variant::Rfc4122, 'expectedInt' => 45056, 'expectedHex' => 'b000', ], [ 'clockSeq' => 36863, + 'variant' => Variant::Rfc4122, 'expectedInt' => 36863, 'expectedHex' => '8fff', ], [ 'clockSeq' => 40959, + 'variant' => Variant::Rfc4122, 'expectedInt' => 40959, 'expectedHex' => '9fff', ], [ 'clockSeq' => 45055, + 'variant' => Variant::Rfc4122, 'expectedInt' => 45055, 'expectedHex' => 'afff', ], [ 'clockSeq' => 49151, + 'variant' => Variant::Rfc4122, 'expectedInt' => 49151, 'expectedHex' => 'bfff', ], diff --git a/tests/ExpectedBehaviorTest.php b/tests/ExpectedBehaviorTest.php index 592a288..8cbe2a3 100644 --- a/tests/ExpectedBehaviorTest.php +++ b/tests/ExpectedBehaviorTest.php @@ -2,18 +2,16 @@ namespace Ramsey\Uuid\Test; -use Ramsey\Uuid\Codec\CodecInterface; use Ramsey\Uuid\Codec\TimestampFirstCombCodec; -use Ramsey\Uuid\Converter\TimeConverterInterface; use Ramsey\Uuid\Generator\CombGenerator; use Ramsey\Uuid\Generator\DefaultTimeGenerator; -use Ramsey\Uuid\Math\BrickMathCalculator; use Ramsey\Uuid\Rfc4122\UuidInterface; use Ramsey\Uuid\Rfc4122\UuidV1; use Ramsey\Uuid\Type\Hexadecimal; use Ramsey\Uuid\Type\Time; use Ramsey\Uuid\Uuid; use Ramsey\Uuid\UuidFactory; +use Ramsey\Uuid\Variant; use stdClass; /** @@ -81,7 +79,7 @@ class ExpectedBehaviorTest extends TestCase . $uuid->getFields()->getNode()->toString() ); - $this->assertSame(2, $uuid->getFields()->getVariant()); + $this->assertSame(Variant::Rfc4122, $uuid->getFields()->getVariant()); $this->assertSame((int) substr($method, -1), $uuid->getFields()->getVersion()); $this->assertTrue(ctype_digit((string) $uuid->getInteger())); } @@ -204,7 +202,7 @@ class ExpectedBehaviorTest extends TestCase $this->assertInstanceOf('Ramsey\Uuid\UuidInterface', $uuid); $this->assertSame($string, $uuid->toString()); $this->assertSame($version, $uuid->getFields()->getVersion()); - $this->assertSame($variant, $uuid->getFields()->getVariant()); + $this->assertSame(Variant::from($variant), $uuid->getFields()->getVariant()); $components = explode('-', $string); @@ -234,7 +232,7 @@ class ExpectedBehaviorTest extends TestCase $this->assertInstanceOf('Ramsey\Uuid\UuidInterface', $uuid); $this->assertSame($string, $uuid->toString()); $this->assertSame($version, $uuid->getFields()->getVersion()); - $this->assertSame($variant, $uuid->getFields()->getVariant()); + $this->assertSame(Variant::from($variant), $uuid->getFields()->getVariant()); $components = explode('-', $string); @@ -264,7 +262,7 @@ class ExpectedBehaviorTest extends TestCase $this->assertInstanceOf('Ramsey\Uuid\UuidInterface', $uuid); $this->assertSame($string, $uuid->toString()); $this->assertSame($version, $uuid->getFields()->getVersion()); - $this->assertSame($variant, $uuid->getFields()->getVariant()); + $this->assertSame(Variant::from($variant), $uuid->getFields()->getVariant()); $components = explode('-', $string); @@ -540,7 +538,7 @@ class ExpectedBehaviorTest extends TestCase $expectedBytes = hex2bin($expectedHex); $this->assertInstanceOf('Ramsey\Uuid\UuidInterface', $uuid); - $this->assertSame(2, $uuid->getFields()->getVariant()); + $this->assertSame(Variant::Rfc4122, $uuid->getFields()->getVariant()); $this->assertSame(4, $uuid->getFields()->getVersion()); $this->assertSame($expectedBytes, $uuid->getBytes()); $this->assertSame($expectedHex, (string) $uuid->getHex()); @@ -562,10 +560,6 @@ class ExpectedBehaviorTest extends TestCase ['NAMESPACE_OID', '6ba7b812-9dad-11d1-80b4-00c04fd430c8'], ['NAMESPACE_X500', '6ba7b814-9dad-11d1-80b4-00c04fd430c8'], ['NIL', '00000000-0000-0000-0000-000000000000'], - ['RESERVED_NCS', 0], - ['RFC_4122', 2], - ['RESERVED_MICROSOFT', 6], - ['RESERVED_FUTURE', 7], ['VALID_PATTERN', '^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$'], ['UUID_TYPE_TIME', 1], ['UUID_TYPE_IDENTIFIER', 2], diff --git a/tests/Guid/FieldsTest.php b/tests/Guid/FieldsTest.php index 4924e55..38616a0 100644 --- a/tests/Guid/FieldsTest.php +++ b/tests/Guid/FieldsTest.php @@ -8,6 +8,7 @@ use Ramsey\Uuid\Exception\InvalidArgumentException; use Ramsey\Uuid\Guid\Fields; use Ramsey\Uuid\Test\TestCase; use Ramsey\Uuid\Type\Hexadecimal; +use Ramsey\Uuid\Variant; use function hex2bin; use function serialize; @@ -132,7 +133,7 @@ class FieldsTest extends TestCase ['b08c6fff7dc5e111cb210800200c9a66', 'getTimeLow', 'ff6f8cb0'], ['b08c6fff7dc5e111cb210800200c9a66', 'getTimeMid', 'c57d'], ['b08c6fff7dc5e111cb210800200c9a66', 'getTimestamp', '1e1c57dff6f8cb0'], - ['b08c6fff7dc5e111cb210800200c9a66', 'getVariant', 6], + ['b08c6fff7dc5e111cb210800200c9a66', 'getVariant', Variant::ReservedMicrosoft], ['b08c6fff7dc5e111cb210800200c9a66', 'getVersion', 1], ['b08c6fff7dc5e111cb210800200c9a66', 'isNil', false], @@ -145,7 +146,7 @@ class FieldsTest extends TestCase ['b08c6fff7dc5e141db210800200c9a66', 'getTimeLow', 'ff6f8cb0'], ['b08c6fff7dc5e141db210800200c9a66', 'getTimeMid', 'c57d'], ['b08c6fff7dc5e141db210800200c9a66', 'getTimestamp', '1e1c57dff6f8cb0'], - ['b08c6fff7dc5e141db210800200c9a66', 'getVariant', 6], + ['b08c6fff7dc5e141db210800200c9a66', 'getVariant', Variant::ReservedMicrosoft], ['b08c6fff7dc5e141db210800200c9a66', 'getVersion', 4], ['b08c6fff7dc5e141db210800200c9a66', 'isNil', false], @@ -158,7 +159,7 @@ class FieldsTest extends TestCase ['b08c6fff7dc5e1318b210800200c9a66', 'getTimeLow', 'ff6f8cb0'], ['b08c6fff7dc5e1318b210800200c9a66', 'getTimeMid', 'c57d'], ['b08c6fff7dc5e1318b210800200c9a66', 'getTimestamp', '1e1c57dff6f8cb0'], - ['b08c6fff7dc5e1318b210800200c9a66', 'getVariant', 2], + ['b08c6fff7dc5e1318b210800200c9a66', 'getVariant', Variant::Rfc4122], ['b08c6fff7dc5e1318b210800200c9a66', 'getVersion', 3], ['b08c6fff7dc5e1318b210800200c9a66', 'isNil', false], @@ -171,7 +172,7 @@ class FieldsTest extends TestCase ['b08c6fff7dc5e1519b210800200c9a66', 'getTimeLow', 'ff6f8cb0'], ['b08c6fff7dc5e1519b210800200c9a66', 'getTimeMid', 'c57d'], ['b08c6fff7dc5e1519b210800200c9a66', 'getTimestamp', '1e1c57dff6f8cb0'], - ['b08c6fff7dc5e1519b210800200c9a66', 'getVariant', 2], + ['b08c6fff7dc5e1519b210800200c9a66', 'getVariant', Variant::Rfc4122], ['b08c6fff7dc5e1519b210800200c9a66', 'getVersion', 5], ['b08c6fff7dc5e1519b210800200c9a66', 'isNil', false], @@ -184,7 +185,7 @@ class FieldsTest extends TestCase ['00000000000000000000000000000000', 'getTimeLow', '00000000'], ['00000000000000000000000000000000', 'getTimeMid', '0000'], ['00000000000000000000000000000000', 'getTimestamp', '000000000000000'], - ['00000000000000000000000000000000', 'getVariant', 0], + ['00000000000000000000000000000000', 'getVariant', Variant::ReservedNcs], ['00000000000000000000000000000000', 'getVersion', null], ['00000000000000000000000000000000', 'isNil', true], ]; diff --git a/tests/Nonstandard/FieldsTest.php b/tests/Nonstandard/FieldsTest.php index 319896a..f5ea533 100644 --- a/tests/Nonstandard/FieldsTest.php +++ b/tests/Nonstandard/FieldsTest.php @@ -8,7 +8,7 @@ use Ramsey\Uuid\Exception\InvalidArgumentException; use Ramsey\Uuid\Nonstandard\Fields; use Ramsey\Uuid\Test\TestCase; use Ramsey\Uuid\Type\Hexadecimal; -use Ramsey\Uuid\Uuid; +use Ramsey\Uuid\Variant; use function hex2bin; use function serialize; @@ -60,7 +60,7 @@ class FieldsTest extends TestCase ['ff6f8cb0-c57d-91e1-0b21-0800200c9a66', 'getTimeLow', 'ff6f8cb0'], ['ff6f8cb0-c57d-91e1-0b21-0800200c9a66', 'getTimeMid', 'c57d'], ['ff6f8cb0-c57d-91e1-0b21-0800200c9a66', 'getTimestamp', '1e1c57dff6f8cb0'], - ['ff6f8cb0-c57d-91e1-0b21-0800200c9a66', 'getVariant', Uuid::RESERVED_NCS], + ['ff6f8cb0-c57d-91e1-0b21-0800200c9a66', 'getVariant', Variant::ReservedNcs], ['ff6f8cb0-c57d-91e1-0b21-0800200c9a66', 'getVersion', null], ['ff6f8cb0-c57d-91e1-0b21-0800200c9a66', 'isNil', false], ]; diff --git a/tests/Rfc4122/FieldsTest.php b/tests/Rfc4122/FieldsTest.php index eb84dee..aefa149 100644 --- a/tests/Rfc4122/FieldsTest.php +++ b/tests/Rfc4122/FieldsTest.php @@ -8,6 +8,7 @@ use Ramsey\Uuid\Exception\InvalidArgumentException; use Ramsey\Uuid\Rfc4122\Fields; use Ramsey\Uuid\Test\TestCase; use Ramsey\Uuid\Type\Hexadecimal; +use Ramsey\Uuid\Variant; use function hex2bin; use function serialize; @@ -123,7 +124,7 @@ class FieldsTest extends TestCase ['ff6f8cb0-c57d-11e1-9b21-0800200c9a66', 'getTimeLow', 'ff6f8cb0'], ['ff6f8cb0-c57d-11e1-9b21-0800200c9a66', 'getTimeMid', 'c57d'], ['ff6f8cb0-c57d-11e1-9b21-0800200c9a66', 'getTimestamp', '1e1c57dff6f8cb0'], - ['ff6f8cb0-c57d-11e1-9b21-0800200c9a66', 'getVariant', 2], + ['ff6f8cb0-c57d-11e1-9b21-0800200c9a66', 'getVariant', Variant::Rfc4122], ['ff6f8cb0-c57d-11e1-9b21-0800200c9a66', 'getVersion', 1], ['ff6f8cb0-c57d-11e1-9b21-0800200c9a66', 'isNil', false], @@ -135,7 +136,7 @@ class FieldsTest extends TestCase ['ff6f8cb0-c57d-41e1-ab21-0800200c9a66', 'getTimeLow', 'ff6f8cb0'], ['ff6f8cb0-c57d-41e1-ab21-0800200c9a66', 'getTimeMid', 'c57d'], ['ff6f8cb0-c57d-41e1-ab21-0800200c9a66', 'getTimestamp', '1e1c57dff6f8cb0'], - ['ff6f8cb0-c57d-41e1-ab21-0800200c9a66', 'getVariant', 2], + ['ff6f8cb0-c57d-41e1-ab21-0800200c9a66', 'getVariant', Variant::Rfc4122], ['ff6f8cb0-c57d-41e1-ab21-0800200c9a66', 'getVersion', 4], ['ff6f8cb0-c57d-41e1-ab21-0800200c9a66', 'isNil', false], @@ -147,7 +148,7 @@ class FieldsTest extends TestCase ['ff6f8cb0-c57d-31e1-bb21-0800200c9a66', 'getTimeLow', 'ff6f8cb0'], ['ff6f8cb0-c57d-31e1-bb21-0800200c9a66', 'getTimeMid', 'c57d'], ['ff6f8cb0-c57d-31e1-bb21-0800200c9a66', 'getTimestamp', '1e1c57dff6f8cb0'], - ['ff6f8cb0-c57d-31e1-bb21-0800200c9a66', 'getVariant', 2], + ['ff6f8cb0-c57d-31e1-bb21-0800200c9a66', 'getVariant', Variant::Rfc4122], ['ff6f8cb0-c57d-31e1-bb21-0800200c9a66', 'getVersion', 3], ['ff6f8cb0-c57d-31e1-bb21-0800200c9a66', 'isNil', false], @@ -159,7 +160,7 @@ class FieldsTest extends TestCase ['ff6f8cb0-c57d-51e1-8b21-0800200c9a66', 'getTimeLow', 'ff6f8cb0'], ['ff6f8cb0-c57d-51e1-8b21-0800200c9a66', 'getTimeMid', 'c57d'], ['ff6f8cb0-c57d-51e1-8b21-0800200c9a66', 'getTimestamp', '1e1c57dff6f8cb0'], - ['ff6f8cb0-c57d-51e1-8b21-0800200c9a66', 'getVariant', 2], + ['ff6f8cb0-c57d-51e1-8b21-0800200c9a66', 'getVariant', Variant::Rfc4122], ['ff6f8cb0-c57d-51e1-8b21-0800200c9a66', 'getVersion', 5], ['ff6f8cb0-c57d-51e1-8b21-0800200c9a66', 'isNil', false], @@ -171,7 +172,7 @@ class FieldsTest extends TestCase ['ff6f8cb0-c57d-61e1-8b21-0800200c9a66', 'getTimeLow', 'ff6f8cb0'], ['ff6f8cb0-c57d-61e1-8b21-0800200c9a66', 'getTimeMid', 'c57d'], ['ff6f8cb0-c57d-61e1-8b21-0800200c9a66', 'getTimestamp', 'ff6f8cb0c57d1e1'], - ['ff6f8cb0-c57d-61e1-8b21-0800200c9a66', 'getVariant', 2], + ['ff6f8cb0-c57d-61e1-8b21-0800200c9a66', 'getVariant', Variant::Rfc4122], ['ff6f8cb0-c57d-61e1-8b21-0800200c9a66', 'getVersion', 6], ['ff6f8cb0-c57d-61e1-8b21-0800200c9a66', 'isNil', false], @@ -183,7 +184,7 @@ class FieldsTest extends TestCase ['00000000-0000-0000-0000-000000000000', 'getTimeLow', '00000000'], ['00000000-0000-0000-0000-000000000000', 'getTimeMid', '0000'], ['00000000-0000-0000-0000-000000000000', 'getTimestamp', '000000000000000'], - ['00000000-0000-0000-0000-000000000000', 'getVariant', 0], + ['00000000-0000-0000-0000-000000000000', 'getVariant', Variant::ReservedNcs], ['00000000-0000-0000-0000-000000000000', 'getVersion', null], ['00000000-0000-0000-0000-000000000000', 'isNil', true], @@ -195,7 +196,7 @@ class FieldsTest extends TestCase ['000001f5-5cde-21ea-8400-0242ac130003', 'getTimeLow', '000001f5'], ['000001f5-5cde-21ea-8400-0242ac130003', 'getTimeMid', '5cde'], ['000001f5-5cde-21ea-8400-0242ac130003', 'getTimestamp', '1ea5cde00000000'], - ['000001f5-5cde-21ea-8400-0242ac130003', 'getVariant', 2], + ['000001f5-5cde-21ea-8400-0242ac130003', 'getVariant', Variant::Rfc4122], ['000001f5-5cde-21ea-8400-0242ac130003', 'getVersion', 2], ['000001f5-5cde-21ea-8400-0242ac130003', 'isNil', false], ]; diff --git a/tests/Rfc4122/VariantTraitTest.php b/tests/Rfc4122/VariantTraitTest.php index c0e2593..17251d2 100644 --- a/tests/Rfc4122/VariantTraitTest.php +++ b/tests/Rfc4122/VariantTraitTest.php @@ -9,6 +9,7 @@ use Ramsey\Uuid\Exception\InvalidBytesException; use Ramsey\Uuid\Rfc4122\Fields; use Ramsey\Uuid\Rfc4122\VariantTrait; use Ramsey\Uuid\Test\TestCase; +use Ramsey\Uuid\Variant; use function hex2bin; use function str_replace; @@ -54,7 +55,7 @@ class VariantTraitTest extends TestCase 'getBytes' => $bytes, ]); - $this->assertSame($expectedVariant, $trait->getVariant()); + $this->assertSame(Variant::from($expectedVariant), $trait->getVariant()); } /** diff --git a/tests/UuidTest.php b/tests/UuidTest.php index 2ca65e6..05fee01 100644 --- a/tests/UuidTest.php +++ b/tests/UuidTest.php @@ -31,6 +31,7 @@ use Ramsey\Uuid\UuidFactory; use Ramsey\Uuid\UuidInterface; use Ramsey\Uuid\Validator\GenericValidator; use Ramsey\Uuid\Validator\ValidatorInterface; +use Ramsey\Uuid\Variant; use stdClass; use function base64_decode; @@ -344,7 +345,7 @@ class UuidTest extends TestCase * * @dataProvider provideVariousVariantUuids */ - public function testGetVariantForVariousVariantUuids(string $uuid, int $variant): void + public function testGetVariantForVariousVariantUuids(string $uuid, Variant $variant): void { /** @var \Ramsey\Uuid\Rfc4122\UuidInterface $uuidInstance */ $uuidInstance = Uuid::fromString($uuid); @@ -357,22 +358,22 @@ class UuidTest extends TestCase public function provideVariousVariantUuids(): array { return [ - ['ff6f8cb0-c57d-11e1-0b21-0800200c9a66', Uuid::RESERVED_NCS], - ['ff6f8cb0-c57d-11e1-1b21-0800200c9a66', Uuid::RESERVED_NCS], - ['ff6f8cb0-c57d-11e1-2b21-0800200c9a66', Uuid::RESERVED_NCS], - ['ff6f8cb0-c57d-11e1-3b21-0800200c9a66', Uuid::RESERVED_NCS], - ['ff6f8cb0-c57d-11e1-4b21-0800200c9a66', Uuid::RESERVED_NCS], - ['ff6f8cb0-c57d-11e1-5b21-0800200c9a66', Uuid::RESERVED_NCS], - ['ff6f8cb0-c57d-11e1-6b21-0800200c9a66', Uuid::RESERVED_NCS], - ['ff6f8cb0-c57d-11e1-7b21-0800200c9a66', Uuid::RESERVED_NCS], - ['ff6f8cb0-c57d-11e1-8b21-0800200c9a66', Uuid::RFC_4122], - ['ff6f8cb0-c57d-11e1-9b21-0800200c9a66', Uuid::RFC_4122], - ['ff6f8cb0-c57d-11e1-ab21-0800200c9a66', Uuid::RFC_4122], - ['ff6f8cb0-c57d-11e1-bb21-0800200c9a66', Uuid::RFC_4122], - ['ff6f8cb0-c57d-11e1-cb21-0800200c9a66', Uuid::RESERVED_MICROSOFT], - ['ff6f8cb0-c57d-11e1-db21-0800200c9a66', Uuid::RESERVED_MICROSOFT], - ['ff6f8cb0-c57d-11e1-eb21-0800200c9a66', Uuid::RESERVED_FUTURE], - ['ff6f8cb0-c57d-11e1-fb21-0800200c9a66', Uuid::RESERVED_FUTURE], + ['ff6f8cb0-c57d-11e1-0b21-0800200c9a66', Variant::ReservedNcs], + ['ff6f8cb0-c57d-11e1-1b21-0800200c9a66', Variant::ReservedNcs], + ['ff6f8cb0-c57d-11e1-2b21-0800200c9a66', Variant::ReservedNcs], + ['ff6f8cb0-c57d-11e1-3b21-0800200c9a66', Variant::ReservedNcs], + ['ff6f8cb0-c57d-11e1-4b21-0800200c9a66', Variant::ReservedNcs], + ['ff6f8cb0-c57d-11e1-5b21-0800200c9a66', Variant::ReservedNcs], + ['ff6f8cb0-c57d-11e1-6b21-0800200c9a66', Variant::ReservedNcs], + ['ff6f8cb0-c57d-11e1-7b21-0800200c9a66', Variant::ReservedNcs], + ['ff6f8cb0-c57d-11e1-8b21-0800200c9a66', Variant::Rfc4122], + ['ff6f8cb0-c57d-11e1-9b21-0800200c9a66', Variant::Rfc4122], + ['ff6f8cb0-c57d-11e1-ab21-0800200c9a66', Variant::Rfc4122], + ['ff6f8cb0-c57d-11e1-bb21-0800200c9a66', Variant::Rfc4122], + ['ff6f8cb0-c57d-11e1-cb21-0800200c9a66', Variant::ReservedMicrosoft], + ['ff6f8cb0-c57d-11e1-db21-0800200c9a66', Variant::ReservedMicrosoft], + ['ff6f8cb0-c57d-11e1-eb21-0800200c9a66', Variant::ReservedFuture], + ['ff6f8cb0-c57d-11e1-fb21-0800200c9a66', Variant::ReservedFuture], ]; } @@ -429,7 +430,7 @@ class UuidTest extends TestCase /** @var UuidV1 $uuid */ $uuid = Uuid::uuid1(); $this->assertInstanceOf(DateTimeInterface::class, $uuid->getDateTime()); - $this->assertSame(2, $uuid->getFields()->getVariant()); + $this->assertSame(Variant::Rfc4122, $uuid->getFields()->getVariant()); $this->assertSame(1, $uuid->getFields()->getVersion()); } @@ -438,7 +439,7 @@ class UuidTest extends TestCase /** @var UuidV1 $uuid */ $uuid = Uuid::uuid1('0800200c9a66', 0x1669); $this->assertInstanceOf(DateTimeInterface::class, $uuid->getDateTime()); - $this->assertSame(2, $uuid->getFields()->getVariant()); + $this->assertSame(Variant::Rfc4122, $uuid->getFields()->getVariant()); $this->assertSame(1, $uuid->getFields()->getVersion()); $this->assertSame('1669', $uuid->getFields()->getClockSeq()->toString()); $this->assertSame('0800200c9a66', $uuid->getFields()->getNode()->toString()); @@ -450,7 +451,7 @@ class UuidTest extends TestCase /** @var UuidV1 $uuid */ $uuid = Uuid::uuid1(new Hexadecimal('0800200c9a66'), 0x1669); $this->assertInstanceOf(DateTimeInterface::class, $uuid->getDateTime()); - $this->assertSame(2, $uuid->getFields()->getVariant()); + $this->assertSame(Variant::Rfc4122, $uuid->getFields()->getVariant()); $this->assertSame(1, $uuid->getFields()->getVersion()); $this->assertSame('1669', $uuid->getFields()->getClockSeq()->toString()); $this->assertSame('0800200c9a66', $uuid->getFields()->getNode()->toString()); @@ -463,7 +464,7 @@ class UuidTest extends TestCase $uuid = Uuid::uuid1('7160355e'); $this->assertInstanceOf(DateTimeInterface::class, $uuid->getDateTime()); - $this->assertSame(2, $uuid->getFields()->getVariant()); + $this->assertSame(Variant::Rfc4122, $uuid->getFields()->getVariant()); $this->assertSame(1, $uuid->getFields()->getVersion()); $this->assertSame('00007160355e', $uuid->getFields()->getNode()->toString()); } @@ -474,7 +475,7 @@ class UuidTest extends TestCase $uuid = Uuid::uuid1(new Hexadecimal('7160355e')); $this->assertInstanceOf(DateTimeInterface::class, $uuid->getDateTime()); - $this->assertSame(2, $uuid->getFields()->getVariant()); + $this->assertSame(Variant::Rfc4122, $uuid->getFields()->getVariant()); $this->assertSame(1, $uuid->getFields()->getVersion()); $this->assertSame('00007160355e', $uuid->getFields()->getNode()->toString()); } @@ -485,7 +486,7 @@ class UuidTest extends TestCase $uuid = Uuid::uuid1('71B0aD5e'); $this->assertInstanceOf(DateTimeInterface::class, $uuid->getDateTime()); - $this->assertSame(2, $uuid->getFields()->getVariant()); + $this->assertSame(Variant::Rfc4122, $uuid->getFields()->getVariant()); $this->assertSame(1, $uuid->getFields()->getVersion()); $this->assertSame('000071b0ad5e', $uuid->getFields()->getNode()->toString()); } @@ -521,7 +522,7 @@ class UuidTest extends TestCase /** @var UuidV1 $uuid */ $uuid = Uuid::uuid1(); $this->assertInstanceOf(DateTimeInterface::class, $uuid->getDateTime()); - $this->assertSame(2, $uuid->getFields()->getVariant()); + $this->assertSame(Variant::Rfc4122, $uuid->getFields()->getVariant()); $this->assertSame(1, $uuid->getFields()->getVersion()); } @@ -530,7 +531,7 @@ class UuidTest extends TestCase /** @var UuidV1 $uuid */ $uuid = Uuid::uuid1(new Hexadecimal((string) (new RandomNodeProvider())->getNode())); $this->assertInstanceOf(DateTimeInterface::class, $uuid->getDateTime()); - $this->assertSame(2, $uuid->getFields()->getVariant()); + $this->assertSame(Variant::Rfc4122, $uuid->getFields()->getVariant()); $this->assertSame(1, $uuid->getFields()->getVersion()); } @@ -539,7 +540,7 @@ class UuidTest extends TestCase /** @var UuidV6 $uuid */ $uuid = Uuid::uuid6(); $this->assertInstanceOf(DateTimeInterface::class, $uuid->getDateTime()); - $this->assertSame(2, $uuid->getFields()->getVariant()); + $this->assertSame(Variant::Rfc4122, $uuid->getFields()->getVariant()); $this->assertSame(6, $uuid->getFields()->getVersion()); } @@ -548,7 +549,7 @@ class UuidTest extends TestCase /** @var UuidV6 $uuid */ $uuid = Uuid::uuid6(new Hexadecimal('0800200c9a66'), 0x1669); $this->assertInstanceOf(DateTimeInterface::class, $uuid->getDateTime()); - $this->assertSame(2, $uuid->getFields()->getVariant()); + $this->assertSame(Variant::Rfc4122, $uuid->getFields()->getVariant()); $this->assertSame(6, $uuid->getFields()->getVersion()); $this->assertSame('1669', $uuid->getFields()->getClockSeq()->toString()); $this->assertSame('0800200c9a66', $uuid->getFields()->getNode()->toString()); @@ -561,7 +562,7 @@ class UuidTest extends TestCase $uuid = Uuid::uuid6(new Hexadecimal('7160355e')); $this->assertInstanceOf(DateTimeInterface::class, $uuid->getDateTime()); - $this->assertSame(2, $uuid->getFields()->getVariant()); + $this->assertSame(Variant::Rfc4122, $uuid->getFields()->getVariant()); $this->assertSame(6, $uuid->getFields()->getVersion()); $this->assertSame('00007160355e', $uuid->getFields()->getNode()->toString()); } @@ -572,7 +573,7 @@ class UuidTest extends TestCase $uuid = Uuid::uuid6(new Hexadecimal('71B0aD5e')); $this->assertInstanceOf(DateTimeInterface::class, $uuid->getDateTime()); - $this->assertSame(2, $uuid->getFields()->getVariant()); + $this->assertSame(Variant::Rfc4122, $uuid->getFields()->getVariant()); $this->assertSame(6, $uuid->getFields()->getVersion()); $this->assertSame('000071b0ad5e', $uuid->getFields()->getNode()->toString()); } @@ -600,7 +601,7 @@ class UuidTest extends TestCase /** @var UuidV6 $uuid */ $uuid = Uuid::uuid6(); $this->assertInstanceOf(DateTimeInterface::class, $uuid->getDateTime()); - $this->assertSame(2, $uuid->getFields()->getVariant()); + $this->assertSame(Variant::Rfc4122, $uuid->getFields()->getVariant()); $this->assertSame(6, $uuid->getFields()->getVersion()); } @@ -609,7 +610,7 @@ class UuidTest extends TestCase /** @var UuidV6 $uuid */ $uuid = Uuid::uuid6(new Hexadecimal((string) (new RandomNodeProvider())->getNode())); $this->assertInstanceOf(DateTimeInterface::class, $uuid->getDateTime()); - $this->assertSame(2, $uuid->getFields()->getVariant()); + $this->assertSame(Variant::Rfc4122, $uuid->getFields()->getVariant()); $this->assertSame(6, $uuid->getFields()->getVersion()); } @@ -632,7 +633,7 @@ class UuidTest extends TestCase /** @var \Ramsey\Uuid\Rfc4122\UuidInterface $uobj2 */ $uobj2 = Uuid::uuid3(Uuid::fromString($ns), $name); - $this->assertSame(2, $uobj1->getFields()->getVariant()); + $this->assertSame(Variant::Rfc4122, $uobj1->getFields()->getVariant()); $this->assertSame(3, $uobj1->getFields()->getVersion()); $this->assertSame(Uuid::fromString($uuid)->toString(), $uobj1->toString()); $this->assertTrue($uobj1->equals($uobj2)); @@ -671,7 +672,7 @@ class UuidTest extends TestCase { /** @var \Ramsey\Uuid\Rfc4122\UuidInterface $uuid */ $uuid = Uuid::uuid4(); - $this->assertSame(2, $uuid->getFields()->getVariant()); + $this->assertSame(Variant::Rfc4122, $uuid->getFields()->getVariant()); $this->assertSame(4, $uuid->getFields()->getVersion()); } @@ -773,7 +774,7 @@ class UuidTest extends TestCase /** @var \Ramsey\Uuid\Rfc4122\UuidInterface $uobj2 */ $uobj2 = Uuid::uuid5(Uuid::fromString($ns), $name); - $this->assertSame(2, $uobj1->getFields()->getVariant()); + $this->assertSame(Variant::Rfc4122, $uobj1->getFields()->getVariant()); $this->assertSame(5, $uobj1->getFields()->getVersion()); $this->assertSame(Uuid::fromString($uuid)->toString(), $uobj1->toString()); $this->assertTrue($uobj1->equals($uobj2)); @@ -1159,7 +1160,7 @@ class UuidTest extends TestCase $this->assertSame($urn, $uuid->getUrn()); $this->assertSame($time, $uuid->getFields()->getTimestamp()->toString()); $this->assertSame($clockSeq, $uuid->getFields()->getClockSeq()->toString()); - $this->assertSame($variant, $uuid->getFields()->getVariant()); + $this->assertSame(Variant::from($variant), $uuid->getFields()->getVariant()); $this->assertSame($version, $uuid->getFields()->getVersion()); } } @@ -1191,7 +1192,7 @@ class UuidTest extends TestCase 'urn' => 'urn:uuid:00000000-0000-0000-0000-000000000000', 'time' => '000000000000000', 'clock_seq' => '0000', - 'variant' => Uuid::RESERVED_NCS, + 'variant' => 0, 'version' => null, ], [ @@ -1211,7 +1212,7 @@ class UuidTest extends TestCase 'urn' => 'urn:uuid:00010203-0405-0607-0809-0a0b0c0d0e0f', 'time' => '607040500010203', 'clock_seq' => '0809', - 'variant' => Uuid::RESERVED_NCS, + 'variant' => 0, 'version' => null, ], [ @@ -1231,7 +1232,7 @@ class UuidTest extends TestCase 'urn' => 'urn:uuid:02d9e6d5-9467-382e-8f9b-9300a64ac3cd', 'time' => '82e946702d9e6d5', 'clock_seq' => '0f9b', - 'variant' => Uuid::RFC_4122, + 'variant' => 2, 'version' => Uuid::UUID_TYPE_HASH_MD5, ], [ @@ -1251,7 +1252,7 @@ class UuidTest extends TestCase 'urn' => 'urn:uuid:12345678-1234-5678-1234-567812345678', 'time' => '678123412345678', 'clock_seq' => '1234', - 'variant' => Uuid::RESERVED_NCS, + 'variant' => 0, 'version' => null, ], [ @@ -1271,7 +1272,7 @@ class UuidTest extends TestCase 'urn' => 'urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8', 'time' => '1d19dad6ba7b810', 'clock_seq' => '00b4', - 'variant' => Uuid::RFC_4122, + 'variant' => 2, 'version' => Uuid::UUID_TYPE_TIME, ], [ @@ -1291,7 +1292,7 @@ class UuidTest extends TestCase 'urn' => 'urn:uuid:6ba7b811-9dad-11d1-80b4-00c04fd430c8', 'time' => '1d19dad6ba7b811', 'clock_seq' => '00b4', - 'variant' => Uuid::RFC_4122, + 'variant' => 2, 'version' => Uuid::UUID_TYPE_TIME, ], [ @@ -1311,7 +1312,7 @@ class UuidTest extends TestCase 'urn' => 'urn:uuid:6ba7b812-9dad-11d1-80b4-00c04fd430c8', 'time' => '1d19dad6ba7b812', 'clock_seq' => '00b4', - 'variant' => Uuid::RFC_4122, + 'variant' => 2, 'version' => Uuid::UUID_TYPE_TIME, ], [ @@ -1331,7 +1332,7 @@ class UuidTest extends TestCase 'urn' => 'urn:uuid:6ba7b814-9dad-11d1-80b4-00c04fd430c8', 'time' => '1d19dad6ba7b814', 'clock_seq' => '00b4', - 'variant' => Uuid::RFC_4122, + 'variant' => 2, 'version' => Uuid::UUID_TYPE_TIME, ], [ @@ -1351,7 +1352,7 @@ class UuidTest extends TestCase 'urn' => 'urn:uuid:7d444840-9dc0-11d1-b245-5ffdce74fad2', 'time' => '1d19dc07d444840', 'clock_seq' => '3245', - 'variant' => Uuid::RFC_4122, + 'variant' => 2, 'version' => Uuid::UUID_TYPE_TIME, ], [ @@ -1371,7 +1372,7 @@ class UuidTest extends TestCase 'urn' => 'urn:uuid:e902893a-9d22-3c7e-a7b8-d6e313b71d9f', 'time' => 'c7e9d22e902893a', 'clock_seq' => '27b8', - 'variant' => Uuid::RFC_4122, + 'variant' => 2, 'version' => Uuid::UUID_TYPE_HASH_MD5, ], [ @@ -1391,7 +1392,7 @@ class UuidTest extends TestCase 'urn' => 'urn:uuid:eb424026-6f54-4ef8-a4d0-bb658a1fc6cf', 'time' => 'ef86f54eb424026', 'clock_seq' => '24d0', - 'variant' => Uuid::RFC_4122, + 'variant' => 2, 'version' => Uuid::UUID_TYPE_RANDOM, ], [ @@ -1411,7 +1412,7 @@ class UuidTest extends TestCase 'urn' => 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6', 'time' => '1d07decf81d4fae', 'clock_seq' => '2765', - 'variant' => Uuid::RFC_4122, + 'variant' => 2, 'version' => Uuid::UUID_TYPE_TIME, ], [ @@ -1431,7 +1432,7 @@ class UuidTest extends TestCase 'urn' => 'urn:uuid:fffefdfc-fffe-fffe-fffe-fffefdfcfbfa', 'time' => 'ffefffefffefdfc', 'clock_seq' => '3ffe', - 'variant' => Uuid::RESERVED_FUTURE, + 'variant' => 7, 'version' => null, ], [ @@ -1451,7 +1452,7 @@ class UuidTest extends TestCase 'urn' => 'urn:uuid:ffffffff-ffff-ffff-ffff-ffffffffffff', 'time' => 'fffffffffffffff', 'clock_seq' => '3fff', - 'variant' => Uuid::RESERVED_FUTURE, + 'variant' => 7, 'version' => null, ], ];