From 8c0a06654e8eb3112e8c4268e2b4b3ad410fd900 Mon Sep 17 00:00:00 2001 From: Ben Ramsey Date: Fri, 12 Aug 2022 20:10:51 -0500 Subject: [PATCH] feat: introduce Version enum and use instead of constants --- docs/customize/ordered-time-codec.rst | 2 +- docs/customize/timestamp-first-comb-codec.rst | 2 +- docs/nonstandard/guid.rst | 4 +- docs/nonstandard/other.rst | 4 +- docs/nonstandard/version6.rst | 2 +- docs/quickstart.rst | 2 +- docs/reference.rst | 2 + docs/reference/helper.rst | 12 +- docs/reference/rfc4122-fieldsinterface.rst | 5 +- docs/reference/rfc4122-version.rst | 87 +++++++++++ docs/reference/uuid.rst | 40 ----- docs/reference/variant.rst | 32 ++++ docs/requirements.txt | 6 +- docs/rfc4122/version1.rst | 8 +- docs/rfc4122/version2.rst | 2 +- docs/rfc4122/version4.rst | 2 +- src/BinaryUtils.php | 8 +- src/Codec/OrderedTimeCodec.php | 6 +- src/Guid/Fields.php | 5 +- src/Nonstandard/Fields.php | 3 +- src/Nonstandard/UuidV6.php | 3 +- src/Rfc4122/Fields.php | 9 +- src/Rfc4122/FieldsInterface.php | 2 +- src/Rfc4122/UuidBuilder.php | 12 +- src/Rfc4122/UuidV1.php | 2 +- src/Rfc4122/UuidV2.php | 2 +- src/Rfc4122/UuidV3.php | 2 +- src/Rfc4122/UuidV4.php | 2 +- src/Rfc4122/UuidV5.php | 2 +- src/Rfc4122/Version.php | 139 ++++++++++++++++++ src/Rfc4122/VersionTrait.php | 5 +- src/Uuid.php | 46 ------ src/UuidFactory.php | 23 +-- src/Variant.php | 22 ++- tests/BinaryUtilsTest.php | 35 ++--- tests/ExpectedBehaviorTest.php | 16 +- tests/FunctionsTest.php | 13 +- tests/Generator/PeclUuidNameGeneratorTest.php | 3 +- .../Generator/PeclUuidRandomGeneratorTest.php | 3 +- tests/Generator/PeclUuidTimeGeneratorTest.php | 3 +- tests/Guid/FieldsTest.php | 11 +- tests/Nonstandard/UuidV6Test.php | 21 ++- tests/Rfc4122/FieldsTest.php | 22 +-- tests/Rfc4122/UuidBuilderTest.php | 39 +---- tests/Rfc4122/UuidV1Test.php | 21 ++- tests/Rfc4122/UuidV2Test.php | 19 ++- tests/Rfc4122/UuidV3Test.php | 19 ++- tests/Rfc4122/UuidV4Test.php | 19 ++- tests/Rfc4122/UuidV5Test.php | 19 ++- tests/UuidTest.php | 111 +++++++------- 50 files changed, 526 insertions(+), 353 deletions(-) create mode 100644 docs/reference/rfc4122-version.rst create mode 100644 docs/reference/variant.rst create mode 100644 src/Rfc4122/Version.php diff --git a/docs/customize/ordered-time-codec.rst b/docs/customize/ordered-time-codec.rst index bad166f..39df127 100644 --- a/docs/customize/ordered-time-codec.rst +++ b/docs/customize/ordered-time-codec.rst @@ -41,7 +41,7 @@ a proper sort order. printf( "UUID: %s\nVersion: %d\nDate: %s\nNode: %s\nBytes: %s\n", $orderedTimeUuid->toString(), - $orderedTimeUuid->getFields()->getVersion(), + $orderedTimeUuid->getFields()->getVersion()?->value, $orderedTimeUuid->getDateTime()->format('r'), $orderedTimeUuid->getFields()->getNode()->toString(), bin2hex($orderedTimeUuid->getBytes()) diff --git a/docs/customize/timestamp-first-comb-codec.rst b/docs/customize/timestamp-first-comb-codec.rst index 00f3377..448e2ef 100644 --- a/docs/customize/timestamp-first-comb-codec.rst +++ b/docs/customize/timestamp-first-comb-codec.rst @@ -41,7 +41,7 @@ coming after the previously-created one, in a proper sort order. printf( "UUID: %s\nVersion: %d\nBytes: %s\n", $timestampFirstComb->toString(), - $timestampFirstComb->getFields()->getVersion(), + $timestampFirstComb->getFields()->getVersion()?->value, bin2hex($timestampFirstComb->getBytes()) ); diff --git a/docs/nonstandard/guid.rst b/docs/nonstandard/guid.rst index 4b491da..455a454 100644 --- a/docs/nonstandard/guid.rst +++ b/docs/nonstandard/guid.rst @@ -48,7 +48,7 @@ This is best explained by example. "Class: %s\nGUID: %s\nVersion: %d\nBytes: %s\n", get_class($guid), $guid->toString(), - $guid->getFields()->getVersion(), + $guid->getFields()->getVersion()?->value, bin2hex($guid->getBytes()) ); @@ -104,7 +104,7 @@ can take the GUID string representation and convert it into a standard UUID. "Class: %s\nUUID: %s\nVersion: %d\nBytes: %s\n", get_class($uuid), $uuid->toString(), - $uuid->getFields()->getVersion(), + $uuid->getFields()->getVersion()?->value, bin2hex($uuid->getBytes()) ); diff --git a/docs/nonstandard/other.rst b/docs/nonstandard/other.rst index f7e5a41..59f487d 100644 --- a/docs/nonstandard/other.rst +++ b/docs/nonstandard/other.rst @@ -27,8 +27,8 @@ as a :php:class:`Ramsey\\Uuid\\Nonstandard\\Uuid`. "Class: %s\nUUID: %s\nVersion: %d\nVariant: %s\n", get_class($uuid), $uuid->toString(), - $uuid->getFields()->getVersion(), - $uuid->getFields()->getVariant() + $uuid->getFields()->getVersion()?->value, + $uuid->getFields()->getVariant()?->value ); This will create a Nonstandard\\Uuid from the given string and print out a few diff --git a/docs/nonstandard/version6.rst b/docs/nonstandard/version6.rst index aca5443..7fc8065 100644 --- a/docs/nonstandard/version6.rst +++ b/docs/nonstandard/version6.rst @@ -49,7 +49,7 @@ In all other ways, version 6 UUIDs function like version 1 UUIDs. printf( "UUID: %s\nVersion: %d\nDate: %s\nNode: %s\n", $uuid->toString(), - $uuid->getFields()->getVersion(), + $uuid->getFields()->getVersion()?->value, $uuid->getDateTime()->format('r'), $uuid->getFields()->getNode()->toString() ); diff --git a/docs/quickstart.rst b/docs/quickstart.rst index 6cd310c..c66f356 100644 --- a/docs/quickstart.rst +++ b/docs/quickstart.rst @@ -57,7 +57,7 @@ the static generation methods. printf( "UUID: %s\nVersion: %d\n", $uuid->toString(), - $uuid->getFields()->getVersion() + $uuid->getFields()->getVersion()?->value ); This will return an instance of :php:class:`Ramsey\\Uuid\\Rfc4122\\UuidV4`. diff --git a/docs/reference.rst b/docs/reference.rst index ee88433..7ddcb47 100644 --- a/docs/reference.rst +++ b/docs/reference.rst @@ -18,6 +18,7 @@ Reference reference/rfc4122-uuidv3 reference/rfc4122-uuidv4 reference/rfc4122-uuidv5 + reference/rfc4122-version reference/nonstandard-uuidv6 reference/guid-fields reference/guid-guid @@ -30,3 +31,4 @@ Reference reference/name-based-namespaces reference/calculators reference/validators + reference/variant diff --git a/docs/reference/helper.rst b/docs/reference/helper.rst index 9c410e1..560bc72 100644 --- a/docs/reference/helper.rst +++ b/docs/reference/helper.rst @@ -7,7 +7,7 @@ Helper Functions ramsey/uuid additionally provides the following helper functions, which return only the string standard representation of a UUID. -.. php:function:: Ramsey\\Uuid\\v1([$node[, $clockSeq]]) +.. php:function:: Ramsey\Uuid\v1([$node[, $clockSeq]]) Generates a string standard representation of a version 1, time-based UUID. @@ -16,7 +16,7 @@ only the string standard representation of a UUID. :returns: A string standard representation of a version 1 UUID :returntype: string -.. php:function:: Ramsey\\Uuid\\v2($localDomain[, $localIdentifier[, $node[, $clockSeq]]]) +.. php:function:: Ramsey\Uuid\v2($localDomain[, $localIdentifier[, $node[, $clockSeq]]]) Generates a string standard representation of a version 2, DCE Security UUID. @@ -27,7 +27,7 @@ only the string standard representation of a UUID. :returns: A string standard representation of a version 2 UUID :returntype: string -.. php:function:: Ramsey\\Uuid\\v3($ns, $name) +.. php:function:: Ramsey\Uuid\v3($ns, $name) Generates a string standard representation of a version 3, name-based (MD5) UUID. @@ -36,14 +36,14 @@ only the string standard representation of a UUID. :returns: A string standard representation of a version 3 UUID :returntype: string -.. php:function:: Ramsey\\Uuid\\v4() +.. php:function:: Ramsey\Uuid\v4() Generates a string standard representation of a version 4, random UUID. :returns: A string standard representation of a version 4 UUID :returntype: string -.. php:function:: Ramsey\\Uuid\\v5($ns, $name) +.. php:function:: Ramsey\Uuid\v5($ns, $name) Generates a string standard representation of a version 5, name-based (SHA-1) UUID. @@ -52,7 +52,7 @@ only the string standard representation of a UUID. :returns: A string standard representation of a version 5 UUID :returntype: string -.. php:function:: Ramsey\\Uuid\\v6([$node[, $clockSeq]]) +.. php:function:: Ramsey\Uuid\v6([$node[, $clockSeq]]) Generates a string standard representation of a version 6, ordered-time UUID. diff --git a/docs/reference/rfc4122-fieldsinterface.rst b/docs/reference/rfc4122-fieldsinterface.rst index 44ec64d..e956c65 100644 --- a/docs/reference/rfc4122-fieldsinterface.rst +++ b/docs/reference/rfc4122-fieldsinterface.rst @@ -60,12 +60,13 @@ Rfc4122\\FieldsInterface the value ``2``. :returns: The UUID variant. - :returntype: ``int`` + :returntype: Ramsey\\Uuid\\Variant .. php:method:: getVersion() :returns: The UUID version. - :returntype: ``int`` + :returntype: Ramsey\\Uuid\\Rfc4122\\Version + :returntype: ``null`` .. php:method:: isNil() diff --git a/docs/reference/rfc4122-version.rst b/docs/reference/rfc4122-version.rst new file mode 100644 index 0000000..20757e1 --- /dev/null +++ b/docs/reference/rfc4122-version.rst @@ -0,0 +1,87 @@ +.. _reference.rfc4122.version: + +================ +Rfc4122\\Version +================ + +.. php:namespace:: Ramsey\Uuid\Rfc4122 + +.. php:enum:: Version : int + + The version number describes how the UUID was generated. + + .. php:case:: Time : 1 + + A version 1 UUID uses a timestamp based on the Gregorian calendar epoch, + along with the MAC address (or *node*) for a network interface on the + local machine. For more details, see :ref:`rfc4122.version1`. + + .. php:case:: DceSecurity : 2 + + Like a version 1 UUID, a version 2 UUID uses the current time, along + with the MAC address (or *node*) for a network interface on the local + machine. Additionally, a version 2 UUID replaces the low part of the + time field with a local identifier such as the user ID or group ID of + the local account that created the UUID. For more details, see + :ref:`rfc4122.version2`. + + .. php:case:: HashMd5 : 3 + + Name-based UUIDs combine a namespace with a name. This way, the UUIDs + are unique to the namespace they're created in. Version 3 UUIDs use the + MD5 hashing algorithm to combine the namespace and name. For more + details, see :ref:`rfc4122.version3`. + + .. php:case:: Random : 4 + + Version 4 UUIDs are randomly-generated identifiers. For more details, + see :ref:`rfc4122.version4`. + + .. php:case:: HashSha1 : 5 + + Name-based UUIDs combine a namespace with a name. This way, the UUIDs + are unique to the namespace they're created in. Version 5 UUIDs use the + SHA-1 hashing algorithm to combine the namespace and name. For more + details, see :ref:`rfc4122.version5`. + + .. php:case:: ReorderedTime : 6 + + .. php:case:: UnixTime : 7 + + .. php:case:: Custom : 8 + + .. php:const:: V1 + + An alias for :php:case:`Ramsey\\Uuid\\Rfc4122\\Version::Time`. + + .. php:const:: V2 + + An alias for :php:case:`Ramsey\\Uuid\\Rfc4122\\Version::DceSecurity`. + + .. php:const:: V3 + + An alias for :php:case:`Ramsey\\Uuid\\Rfc4122\\Version::HashMd4`. + + .. php:const:: V4 + + An alias for :php:case:`Ramsey\\Uuid\\Rfc4122\\Version::Random`. + + .. php:const:: V5 + + An alias for :php:case:`Ramsey\\Uuid\\Rfc4122\\Version::HashSha1`. + + .. php:const:: V6 + + An alias for :php:case:`Ramsey\\Uuid\\Rfc4122\\Version::ReorderedTime`. + + .. php:const:: V7 + + An alias for :php:case:`Ramsey\\Uuid\\Rfc4122\\Version::UnixTime`. + + .. php:const:: V8 + + An alias for :php:case:`Ramsey\\Uuid\\Rfc4122\\Version::Custom`. + + .. php:const:: Peabody + + An alias for :php:case:`Ramsey\\Uuid\\Rfc4122\\Version::ReorderedTime`. diff --git a/docs/reference/uuid.rst b/docs/reference/uuid.rst index f3c2f32..52cf87f 100644 --- a/docs/reference/uuid.rst +++ b/docs/reference/uuid.rst @@ -12,30 +12,6 @@ the ramsey/uuid library. .. php:class:: Uuid - .. php:const:: UUID_TYPE_TIME - - :ref:`rfc4122.version1` UUID. - - .. php:const:: UUID_TYPE_DCE_SECURITY - - :ref:`rfc4122.version2` UUID. - - .. php:const:: UUID_TYPE_HASH_MD5 - - :ref:`rfc4122.version3` UUID. - - .. php:const:: UUID_TYPE_RANDOM - - :ref:`rfc4122.version4` UUID. - - .. php:const:: UUID_TYPE_HASH_SHA1 - - :ref:`rfc4122.version5` UUID. - - .. php:const:: UUID_TYPE_PEABODY - - :ref:`nonstandard.version6` UUID. - .. php:const:: NAMESPACE_DNS The name string is a fully-qualified domain name. @@ -69,22 +45,6 @@ the ramsey/uuid library. DCE Security organization domain. - .. php:const:: RESERVED_NCS - - Variant identifier: reserved, NCS backward compatibility. - - .. php:const:: RFC_4122 - - Variant identifier: the UUID layout specified in RFC 4122. - - .. php:const:: RESERVED_MICROSOFT - - Variant identifier: reserved, Microsoft Corporation backward compatibility. - - .. php:const:: RESERVED_FUTURE - - Variant identifier: reserved for future definition. - .. php:staticmethod:: uuid1([$node[, $clockSeq]]) Generates a version 1, time-based UUID. See :ref:`rfc4122.version1`. diff --git a/docs/reference/variant.rst b/docs/reference/variant.rst new file mode 100644 index 0000000..6b83b87 --- /dev/null +++ b/docs/reference/variant.rst @@ -0,0 +1,32 @@ +.. _reference.variant: + +======= +Variant +======= + +.. php:namespace:: Ramsey\Uuid + +.. php:enum:: Variant : int + + The variant number describes the layout of the UUID. UUIDs generated + according to the layout defined in `RFC 4122`_ will always have a variant + value set to ``2``. In ramsey/uuid, this is the enum value + :php:case:`Ramsey\\Uuid\\Variant::Rfc4122`. + + .. php:case:: ReservedNcs : 0 + + Reserved for NCS backward compatibility. + + .. php:case:: Rfc4122 : 2 + + The RFC 4122 variant. + + .. php:case:: ReservedMicrosoft : 6 + + Reserved for Microsoft Corporation backward compatibility. + + .. php:case:: ReservedFuture : 7 + + Reserved for future definition. + +.. _RFC 4122: https://www.rfc-editor.org/rfc/rfc4122 diff --git a/docs/requirements.txt b/docs/requirements.txt index 767ec4d..132abd8 100644 --- a/docs/requirements.txt +++ b/docs/requirements.txt @@ -1,3 +1,3 @@ -Sphinx==2.4.3 -sphinx-rtd-theme==0.4.3 -sphinxcontrib-phpdomain==0.7.0 +Sphinx==4.5.0 +sphinx-rtd-theme==1.0.0 +sphinxcontrib-phpdomain==0.9.0 diff --git a/docs/rfc4122/version1.rst b/docs/rfc4122/version1.rst index e0bf719..226b3ca 100644 --- a/docs/rfc4122/version1.rst +++ b/docs/rfc4122/version1.rst @@ -4,8 +4,9 @@ Version 1: Time-based ===================== -A version 1 UUID uses the current time, along with the MAC address (or *node*) -for a network interface on the local machine. This serves two purposes: +A version 1 UUID uses a timestamp based on the `Gregorian calendar epoch`_, +along with the MAC address (or *node*) for a network interface on the local +machine. This serves two purposes: 1. You can know *when* the identifier was created. 2. You can know *where* the identifier was created. @@ -37,7 +38,7 @@ will generate a random node. printf( "UUID: %s\nVersion: %d\nDate: %s\nNode: %s\n", $uuid->toString(), - $uuid->getFields()->getVersion(), + $uuid->getFields()->getVersion()?->value, $uuid->getDateTime()->format('r'), $uuid->getFields()->getNode()->toString() ); @@ -179,6 +180,7 @@ If you do not need an identifier with a timestamp value embedded in it, see :ref:`rfc4122.version4` to learn about random UUIDs. +.. _Gregorian calendar epoch: https://en.wikipedia.org/wiki/Gregorian_calendar .. _RFC 4122: https://tools.ietf.org/html/rfc4122 .. _RFC 4122, section 4.5: https://tools.ietf.org/html/rfc4122#section-4.5 .. _section 4.2 of RFC 4122: https://tools.ietf.org/html/rfc4122#section-4.2 diff --git a/docs/rfc4122/version2.rst b/docs/rfc4122/version2.rst index 44800c4..46a8543 100644 --- a/docs/rfc4122/version2.rst +++ b/docs/rfc4122/version2.rst @@ -44,7 +44,7 @@ will generate a random node. printf( "UUID: %s\nVersion: %d\nDate: %s\nNode: %s\nDomain: %s\nID: %s\n", $uuid->toString(), - $uuid->getFields()->getVersion(), + $uuid->getFields()->getVersion()?->value, $uuid->getDateTime()->format('r'), $uuid->getFields()->getNode()->toString(), $uuid->getLocalDomainName(), diff --git a/docs/rfc4122/version4.rst b/docs/rfc4122/version4.rst index f4606eb..25cbb5f 100644 --- a/docs/rfc4122/version4.rst +++ b/docs/rfc4122/version4.rst @@ -20,7 +20,7 @@ information, then a version 4 UUID might be perfect for your needs. printf( "UUID: %s\nVersion: %d\n", $uuid->toString(), - $uuid->getFields()->getVersion() + $uuid->getFields()->getVersion()?->value ); This will generate a version 4 UUID and print out its string representation. diff --git a/src/BinaryUtils.php b/src/BinaryUtils.php index 8283f49..0ae8e88 100644 --- a/src/BinaryUtils.php +++ b/src/BinaryUtils.php @@ -14,6 +14,8 @@ declare(strict_types=1); namespace Ramsey\Uuid; +use Ramsey\Uuid\Rfc4122\Version; + /** * Provides binary math utilities */ @@ -48,17 +50,17 @@ class BinaryUtils * * @param int $timeHi The value of the 16-bit `time_hi_and_version` field * before the RFC 4122 version is applied - * @param int $version The RFC 4122 version to apply to the `time_hi` field + * @param Version $version The RFC 4122 version to apply to the `time_hi` field * * @return int The 16-bit time_hi field of the timestamp multiplexed with * the UUID version number * * @psalm-pure */ - public static function applyVersion(int $timeHi, int $version): int + public static function applyVersion(int $timeHi, Version $version): int { $timeHi = $timeHi & 0x0fff; - $timeHi |= $version << 12; + $timeHi |= $version->value << 12; return $timeHi; } diff --git a/src/Codec/OrderedTimeCodec.php b/src/Codec/OrderedTimeCodec.php index ea6e5bf..80eda69 100644 --- a/src/Codec/OrderedTimeCodec.php +++ b/src/Codec/OrderedTimeCodec.php @@ -17,7 +17,7 @@ namespace Ramsey\Uuid\Codec; use Ramsey\Uuid\Exception\InvalidArgumentException; use Ramsey\Uuid\Exception\UnsupportedOperationException; use Ramsey\Uuid\Rfc4122\FieldsInterface as Rfc4122FieldsInterface; -use Ramsey\Uuid\Uuid; +use Ramsey\Uuid\Rfc4122\Version; use Ramsey\Uuid\UuidInterface; use function strlen; @@ -55,7 +55,7 @@ class OrderedTimeCodec extends StringCodec { if ( !($uuid->getFields() instanceof Rfc4122FieldsInterface) - || $uuid->getFields()->getVersion() !== Uuid::UUID_TYPE_TIME + || $uuid->getFields()->getVersion() !== Version::Time ) { throw new InvalidArgumentException( 'Expected RFC 4122 version 1 (time-based) UUID' @@ -96,7 +96,7 @@ class OrderedTimeCodec extends StringCodec if ( !($uuid->getFields() instanceof Rfc4122FieldsInterface) - || $uuid->getFields()->getVersion() !== Uuid::UUID_TYPE_TIME + || $uuid->getFields()->getVersion() !== Version::Time ) { throw new UnsupportedOperationException( 'Attempting to decode a non-time-based UUID using ' diff --git a/src/Guid/Fields.php b/src/Guid/Fields.php index 094cdce..18e3762 100644 --- a/src/Guid/Fields.php +++ b/src/Guid/Fields.php @@ -19,6 +19,7 @@ use Ramsey\Uuid\Fields\SerializableFieldsTrait; use Ramsey\Uuid\Rfc4122\FieldsInterface; use Ramsey\Uuid\Rfc4122\NilTrait; use Ramsey\Uuid\Rfc4122\VariantTrait; +use Ramsey\Uuid\Rfc4122\Version; use Ramsey\Uuid\Rfc4122\VersionTrait; use Ramsey\Uuid\Type\Hexadecimal; use Ramsey\Uuid\Variant; @@ -175,7 +176,7 @@ final class Fields implements FieldsInterface return new Hexadecimal($node); } - public function getVersion(): ?int + public function getVersion(): ?Version { if ($this->isNil()) { return null; @@ -184,7 +185,7 @@ final class Fields implements FieldsInterface /** @var array $parts */ $parts = unpack('n*', $this->bytes); - return ((int) $parts[4] >> 4) & 0x00f; + return Version::tryFrom(((int) $parts[4] >> 4) & 0x00f); } private function isCorrectVariant(): bool diff --git a/src/Nonstandard/Fields.php b/src/Nonstandard/Fields.php index a85a147..25c61b0 100644 --- a/src/Nonstandard/Fields.php +++ b/src/Nonstandard/Fields.php @@ -18,6 +18,7 @@ use Ramsey\Uuid\Exception\InvalidArgumentException; use Ramsey\Uuid\Fields\SerializableFieldsTrait; use Ramsey\Uuid\Rfc4122\FieldsInterface; use Ramsey\Uuid\Rfc4122\VariantTrait; +use Ramsey\Uuid\Rfc4122\Version; use Ramsey\Uuid\Type\Hexadecimal; use function bin2hex; @@ -136,7 +137,7 @@ final class Fields implements FieldsInterface return new Hexadecimal($timestamp); } - public function getVersion(): ?int + public function getVersion(): ?Version { return null; } diff --git a/src/Nonstandard/UuidV6.php b/src/Nonstandard/UuidV6.php index 2bccdf2..0f6729b 100644 --- a/src/Nonstandard/UuidV6.php +++ b/src/Nonstandard/UuidV6.php @@ -25,6 +25,7 @@ use Ramsey\Uuid\Lazy\LazyUuidFromString; use Ramsey\Uuid\Rfc4122\FieldsInterface as Rfc4122FieldsInterface; use Ramsey\Uuid\Rfc4122\UuidInterface; use Ramsey\Uuid\Rfc4122\UuidV1; +use Ramsey\Uuid\Rfc4122\Version; use Ramsey\Uuid\TimeBasedUuidInterface; use Ramsey\Uuid\Uuid; use Throwable; @@ -63,7 +64,7 @@ final class UuidV6 extends Uuid implements UuidInterface, TimeBasedUuidInterface CodecInterface $codec, TimeConverterInterface $timeConverter ) { - if ($fields->getVersion() !== Uuid::UUID_TYPE_PEABODY) { + if ($fields->getVersion() !== Version::ReorderedTime) { throw new InvalidArgumentException( 'Fields used to create a UuidV6 must represent a ' . 'version 6 (ordered-time) UUID' diff --git a/src/Rfc4122/Fields.php b/src/Rfc4122/Fields.php index 99a6539..fe236b3 100644 --- a/src/Rfc4122/Fields.php +++ b/src/Rfc4122/Fields.php @@ -17,7 +17,6 @@ namespace Ramsey\Uuid\Rfc4122; 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; @@ -155,13 +154,13 @@ final class Fields implements FieldsInterface { /** @var non-empty-string $timestamp */ $timestamp = match ($this->getVersion()) { - Uuid::UUID_TYPE_DCE_SECURITY => sprintf( + Version::DceSecurity => sprintf( '%03x%04s%08s', hexdec($this->getTimeHiAndVersion()->toString()) & 0x0fff, $this->getTimeMid()->toString(), '' ), - Uuid::UUID_TYPE_PEABODY => sprintf( + Version::ReorderedTime => sprintf( '%08s%04s%03x', $this->getTimeLow()->toString(), $this->getTimeMid()->toString(), @@ -178,7 +177,7 @@ final class Fields implements FieldsInterface return new Hexadecimal($timestamp); } - public function getVersion(): ?int + public function getVersion(): ?Version { if ($this->isNil()) { return null; @@ -187,7 +186,7 @@ final class Fields implements FieldsInterface /** @var array $parts */ $parts = unpack('n*', $this->bytes); - return (int) $parts[4] >> 12; + return Version::tryFrom((int) $parts[4] >> 12); } private function isCorrectVariant(): bool diff --git a/src/Rfc4122/FieldsInterface.php b/src/Rfc4122/FieldsInterface.php index 5b4b801..bd962aa 100644 --- a/src/Rfc4122/FieldsInterface.php +++ b/src/Rfc4122/FieldsInterface.php @@ -115,7 +115,7 @@ interface FieldsInterface extends BaseFieldsInterface * * @link http://tools.ietf.org/html/rfc4122#section-4.1.3 RFC 4122, § 4.1.3: Version */ - public function getVersion(): ?int; + public function getVersion(): ?Version; /** * Returns true if these fields represent a nil UUID diff --git a/src/Rfc4122/UuidBuilder.php b/src/Rfc4122/UuidBuilder.php index 95c104d..c92cde6 100644 --- a/src/Rfc4122/UuidBuilder.php +++ b/src/Rfc4122/UuidBuilder.php @@ -66,17 +66,17 @@ class UuidBuilder implements UuidBuilderInterface } switch ($fields->getVersion()) { - case 1: + case Version::Time: return new UuidV1($fields, $this->numberConverter, $codec, $this->timeConverter); - case 2: + case Version::DceSecurity: return new UuidV2($fields, $this->numberConverter, $codec, $this->timeConverter); - case 3: + case Version::HashMd5: return new UuidV3($fields, $this->numberConverter, $codec, $this->timeConverter); - case 4: + case Version::Random: return new UuidV4($fields, $this->numberConverter, $codec, $this->timeConverter); - case 5: + case Version::HashSha1: return new UuidV5($fields, $this->numberConverter, $codec, $this->timeConverter); - case 6: + case Version::ReorderedTime: return new UuidV6($fields, $this->numberConverter, $codec, $this->timeConverter); } diff --git a/src/Rfc4122/UuidV1.php b/src/Rfc4122/UuidV1.php index 90004b7..a64d61c 100644 --- a/src/Rfc4122/UuidV1.php +++ b/src/Rfc4122/UuidV1.php @@ -55,7 +55,7 @@ final class UuidV1 extends Uuid implements UuidInterface, TimeBasedUuidInterface CodecInterface $codec, TimeConverterInterface $timeConverter ) { - if ($fields->getVersion() !== Uuid::UUID_TYPE_TIME) { + if ($fields->getVersion() !== Version::Time) { throw new InvalidArgumentException( 'Fields used to create a UuidV1 must represent a ' . 'version 1 (time-based) UUID' diff --git a/src/Rfc4122/UuidV2.php b/src/Rfc4122/UuidV2.php index 0588ee5..2ebcd43 100644 --- a/src/Rfc4122/UuidV2.php +++ b/src/Rfc4122/UuidV2.php @@ -64,7 +64,7 @@ final class UuidV2 extends Uuid implements UuidInterface CodecInterface $codec, TimeConverterInterface $timeConverter ) { - if ($fields->getVersion() !== Uuid::UUID_TYPE_DCE_SECURITY) { + if ($fields->getVersion() !== Version::DceSecurity) { throw new InvalidArgumentException( 'Fields used to create a UuidV2 must represent a ' . 'version 2 (DCE Security) UUID' diff --git a/src/Rfc4122/UuidV3.php b/src/Rfc4122/UuidV3.php index deaa54e..e16bed6 100644 --- a/src/Rfc4122/UuidV3.php +++ b/src/Rfc4122/UuidV3.php @@ -46,7 +46,7 @@ final class UuidV3 extends Uuid implements UuidInterface CodecInterface $codec, TimeConverterInterface $timeConverter ) { - if ($fields->getVersion() !== Uuid::UUID_TYPE_HASH_MD5) { + if ($fields->getVersion() !== Version::HashMd5) { throw new InvalidArgumentException( 'Fields used to create a UuidV3 must represent a ' . 'version 3 (name-based, MD5-hashed) UUID' diff --git a/src/Rfc4122/UuidV4.php b/src/Rfc4122/UuidV4.php index 2e57246..1c9e139 100644 --- a/src/Rfc4122/UuidV4.php +++ b/src/Rfc4122/UuidV4.php @@ -46,7 +46,7 @@ final class UuidV4 extends Uuid implements UuidInterface CodecInterface $codec, TimeConverterInterface $timeConverter ) { - if ($fields->getVersion() !== Uuid::UUID_TYPE_RANDOM) { + if ($fields->getVersion() !== Version::Random) { throw new InvalidArgumentException( 'Fields used to create a UuidV4 must represent a ' . 'version 4 (random) UUID' diff --git a/src/Rfc4122/UuidV5.php b/src/Rfc4122/UuidV5.php index 2ef6ab3..27bd16f 100644 --- a/src/Rfc4122/UuidV5.php +++ b/src/Rfc4122/UuidV5.php @@ -46,7 +46,7 @@ final class UuidV5 extends Uuid implements UuidInterface CodecInterface $codec, TimeConverterInterface $timeConverter ) { - if ($fields->getVersion() !== Uuid::UUID_TYPE_HASH_SHA1) { + if ($fields->getVersion() !== Version::HashSha1) { throw new InvalidArgumentException( 'Fields used to create a UuidV5 must represent a ' . 'version 5 (named-based, SHA1-hashed) UUID' diff --git a/src/Rfc4122/Version.php b/src/Rfc4122/Version.php new file mode 100644 index 0000000..35ded30 --- /dev/null +++ b/src/Rfc4122/Version.php @@ -0,0 +1,139 @@ + + * @license http://opensource.org/licenses/MIT MIT + */ + +declare(strict_types=1); + +namespace Ramsey\Uuid\Rfc4122; + +/** + * The version number describes how the UUID was generated + * + * The version number has the following meaning: + * + * 1. Time-based UUID + * 2. DCE security UUID + * 3. Name-based UUID hashed with MD5 + * 4. Randomly generated UUID + * 5. Name-based UUID hashed with SHA-1 + * 6. Reordered time-based UUID + * 7. Unix Epoch time-based UUID + * 8. Implementation-specific custom UUID + * + * @link http://tools.ietf.org/html/rfc4122#section-4.1.3 RFC 4122, § 4.1.3: Version + * + * phpcs:disable Generic.NamingConventions.UpperCaseConstantName.ClassConstantNotUpperCase + */ +enum Version: int +{ + /** + * Version 1 (time-based) UUID + * + * @link https://datatracker.ietf.org/doc/html/rfc4122#section-4.1.3 RFC 4122, § 4.1.3 + */ + case Time = 1; + + /** + * Version 2 (DCE Security) UUID + * + * @link https://datatracker.ietf.org/doc/html/rfc4122#section-4.1.3 RFC 4122, § 4.1.3 + */ + case DceSecurity = 2; + + /** + * Version 3 (name-based and hashed with MD5) UUID + * + * @link https://datatracker.ietf.org/doc/html/rfc4122#section-4.1.3 RFC 4122, § 4.1.3 + */ + case HashMd5 = 3; + + /** + * Version 4 (random) UUID + * + * @link https://datatracker.ietf.org/doc/html/rfc4122#section-4.1.3 RFC 4122, § 4.1.3 + */ + case Random = 4; + + /** + * Version 5 (name-based and hashed with SHA1) UUID + * + * @link https://datatracker.ietf.org/doc/html/rfc4122#section-4.1.3 RFC 4122, § 4.1.3 + */ + case HashSha1 = 5; + + /** + * Version 6 (reordered time-based) UUID + * + * @link https://datatracker.ietf.org/doc/html/draft-peabody-dispatch-new-uuid-format-04#section-4 draft-peabody-dispatch-new-uuid-format-04, § 4 + * @link https://github.com/uuid6/uuid6-ietf-draft UUID version 6 IETF draft + * @link http://gh.peabody.io/uuidv6/ "Version 6" UUIDs + */ + case ReorderedTime = 6; + + /** + * Version 7 (Unix Epoch time-based) UUID + * + * @link https://datatracker.ietf.org/doc/html/draft-peabody-dispatch-new-uuid-format-04#section-4 draft-peabody-dispatch-new-uuid-format-04, § 4 + */ + case UnixTime = 7; + + /** + * Version 8 (implementation-specific custom) UUID + * + * @link https://datatracker.ietf.org/doc/html/draft-peabody-dispatch-new-uuid-format-04#section-4 draft-peabody-dispatch-new-uuid-format-04, § 4 + */ + case Custom = 8; + + /** + * Alias for {@see self::Time} + */ + public const V1 = self::Time; + + /** + * Alias for {@see self::DceSecurity} + */ + public const V2 = self::DceSecurity; + + /** + * Alias for {@see self::HashMd5} + */ + public const V3 = self::HashMd5; + + /** + * Alias for {@see self::Random} + */ + public const V4 = self::Random; + + /** + * Alias for {@see self::HashSha1} + */ + public const V5 = self::HashSha1; + + /** + * Alias for {@see self::ReorderedTime} + */ + public const V6 = self::ReorderedTime; + + /** + * Alias for {@see self::UnixTime} + */ + public const V7 = self::UnixTime; + + /** + * Alias for {@see self::Custom} + */ + public const V8 = self::Custom; + + /** + * Alias for {@see self::ReorderedTime} + */ + public const Peabody = self::ReorderedTime; +} diff --git a/src/Rfc4122/VersionTrait.php b/src/Rfc4122/VersionTrait.php index f4b2eea..9b20163 100644 --- a/src/Rfc4122/VersionTrait.php +++ b/src/Rfc4122/VersionTrait.php @@ -24,7 +24,7 @@ trait VersionTrait /** * Returns the version */ - abstract public function getVersion(): ?int; + abstract public function getVersion(): ?Version; /** * Returns true if these fields represent a nil UUID @@ -43,7 +43,8 @@ trait VersionTrait } return match ($this->getVersion()) { - 1, 2, 3, 4, 5, 6 => true, + Version::Time, Version::DceSecurity, Version::HashMd5, + Version::Random, Version::HashSha1, Version::ReorderedTime => true, default => false, }; } diff --git a/src/Uuid.php b/src/Uuid.php index 215a034..074278e 100644 --- a/src/Uuid.php +++ b/src/Uuid.php @@ -80,52 +80,6 @@ class Uuid implements Rfc4122UuidInterface */ public const NIL = '00000000-0000-0000-0000-000000000000'; - /** - * Version 1 (time-based) UUID - * - * @link https://tools.ietf.org/html/rfc4122#section-4.1.3 RFC 4122, § 4.1.3: Version - */ - public const UUID_TYPE_TIME = 1; - - /** - * Version 2 (DCE Security) UUID - * - * @link https://tools.ietf.org/html/rfc4122#section-4.1.3 RFC 4122, § 4.1.3: Version - */ - public const UUID_TYPE_DCE_SECURITY = 2; - - /** - * Version 3 (name-based and hashed with MD5) UUID - * - * @link https://tools.ietf.org/html/rfc4122#section-4.1.3 RFC 4122, § 4.1.3: Version - */ - public const UUID_TYPE_HASH_MD5 = 3; - - /** - * Version 4 (random) UUID - * - * @link https://tools.ietf.org/html/rfc4122#section-4.1.3 RFC 4122, § 4.1.3: Version - */ - public const UUID_TYPE_RANDOM = 4; - - /** - * Version 5 (name-based and hashed with SHA1) UUID - * - * @link https://tools.ietf.org/html/rfc4122#section-4.1.3 RFC 4122, § 4.1.3: Version - */ - public const UUID_TYPE_HASH_SHA1 = 5; - - /** - * Version 6 (ordered-time) UUID - * - * This is named `UUID_TYPE_PEABODY`, since the specification is still in - * draft form, and the primary author/editor's name is Brad Peabody. - * - * @link https://github.com/uuid6/uuid6-ietf-draft UUID version 6 IETF draft - * @link http://gh.peabody.io/uuidv6/ "Version 6" UUIDs - */ - public const UUID_TYPE_PEABODY = 6; - /** * DCE Security principal domain * diff --git a/src/UuidFactory.php b/src/UuidFactory.php index 1d06721..a16e52e 100644 --- a/src/UuidFactory.php +++ b/src/UuidFactory.php @@ -27,6 +27,7 @@ use Ramsey\Uuid\Generator\TimeGeneratorInterface; use Ramsey\Uuid\Lazy\LazyUuidFromString; use Ramsey\Uuid\Provider\NodeProviderInterface; use Ramsey\Uuid\Provider\Time\FixedTimeProvider; +use Ramsey\Uuid\Rfc4122\Version; use Ramsey\Uuid\Type\Hexadecimal; use Ramsey\Uuid\Type\Integer as IntegerObject; use Ramsey\Uuid\Type\Time; @@ -346,7 +347,7 @@ class UuidFactory implements UuidFactoryInterface $bytes = $timeGenerator->generate($nodeHex, $clockSeq); - return $this->uuidFromBytesAndVersion($bytes, 1); + return $this->uuidFromBytesAndVersion($bytes, Version::Time); } /** @@ -356,7 +357,7 @@ class UuidFactory implements UuidFactoryInterface { $bytes = $this->timeGenerator->generate($node, $clockSeq); - return $this->uuidFromBytesAndVersion($bytes, 1); + return $this->uuidFromBytesAndVersion($bytes, Version::Time); } public function uuid2( @@ -372,7 +373,7 @@ class UuidFactory implements UuidFactoryInterface $clockSeq ); - return $this->uuidFromBytesAndVersion($bytes, 2); + return $this->uuidFromBytesAndVersion($bytes, Version::DceSecurity); } /** @@ -381,14 +382,14 @@ class UuidFactory implements UuidFactoryInterface */ public function uuid3(UuidInterface | string $ns, string $name): UuidInterface { - return $this->uuidFromNsAndName($ns, $name, 3, 'md5'); + return $this->uuidFromNsAndName($ns, $name, Version::HashMd5, 'md5'); } public function uuid4(): UuidInterface { $bytes = $this->randomGenerator->generate(16); - return $this->uuidFromBytesAndVersion($bytes, 4); + return $this->uuidFromBytesAndVersion($bytes, Version::Random); } /** @@ -397,7 +398,7 @@ class UuidFactory implements UuidFactoryInterface */ public function uuid5(UuidInterface | string $ns, string $name): UuidInterface { - return $this->uuidFromNsAndName($ns, $name, 5, 'sha1'); + return $this->uuidFromNsAndName($ns, $name, Version::HashSha1, 'sha1'); } public function uuid6(?Hexadecimal $node = null, ?int $clockSeq = null): UuidInterface @@ -417,7 +418,7 @@ class UuidFactory implements UuidFactoryInterface $v6Bytes = hex2bin(substr($v6, 1, 12) . '0' . substr($v6, -3)); $v6Bytes .= substr($bytes, 8); - return $this->uuidFromBytesAndVersion($v6Bytes, 6); + return $this->uuidFromBytesAndVersion($v6Bytes, Version::ReorderedTime); } /** @@ -443,7 +444,7 @@ class UuidFactory implements UuidFactoryInterface * * @param non-empty-string|UuidInterface $ns The namespace (must be a valid UUID) * @param string $name The name to hash together with the namespace - * @param positive-int $version The version of UUID to create (3 or 5) + * @param Version $version The version of UUID to create (3 or 5) * @param non-empty-string $hashAlgorithm The hashing algorithm to use when * hashing together the namespace and name * @@ -455,7 +456,7 @@ class UuidFactory implements UuidFactoryInterface private function uuidFromNsAndName( UuidInterface | string $ns, string $name, - int $version, + Version $version, string $hashAlgorithm, ): UuidInterface { if (!($ns instanceof UuidInterface)) { @@ -474,14 +475,14 @@ class UuidFactory implements UuidFactoryInterface * Returns an RFC 4122 variant Uuid, created from the provided bytes and version * * @param non-empty-string $bytes The byte string to convert to a UUID - * @param positive-int $version The RFC 4122 version to apply to the UUID + * @param Version $version The RFC 4122 version to apply to the UUID * * @return UuidInterface An instance of UuidInterface, created from the * byte string and version * * @psalm-pure */ - private function uuidFromBytesAndVersion(string $bytes, int $version): UuidInterface + private function uuidFromBytesAndVersion(string $bytes, Version $version): UuidInterface { /** @var array $unpackedTime */ $unpackedTime = unpack('n*', substr($bytes, 6, 2)); diff --git a/src/Variant.php b/src/Variant.php index 185cb21..4be8bdf 100644 --- a/src/Variant.php +++ b/src/Variant.php @@ -14,33 +14,47 @@ declare(strict_types=1); namespace Ramsey\Uuid; +/** + * The variant number describes the layout of the UUID + * + * The variant number has the following meaning: + * + * - 0 - Reserved for NCS backward compatibility + * - 2 - The RFC 4122 variant + * - 6 - Reserved, Microsoft Corporation backward compatibility + * - 7 - Reserved for future definition + * + * For RFC 4122 variant UUIDs, this value should always be the integer `2`. + * + * @link https://datatracker.ietf.org/doc/html/rfc4122#section-4.1.1 RFC 4122, § 4.1.1 + */ enum Variant: int { /** * Variant: reserved, NCS backward compatibility * - * @link http://tools.ietf.org/html/rfc4122#section-4.1.1 RFC 4122, § 4.1.1: Variant + * @link https://datatracker.ietf.org/doc/html/rfc4122#section-4.1.1 RFC 4122, § 4.1.1 */ case ReservedNcs = 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 + * @link https://datatracker.ietf.org/doc/html/rfc4122#section-4.1.1 RFC 4122, § 4.1.1 */ case Rfc4122 = 2; /** * Variant: reserved, Microsoft Corporation backward compatibility * - * @link http://tools.ietf.org/html/rfc4122#section-4.1.1 RFC 4122, § 4.1.1: Variant + * @link https://datatracker.ietf.org/doc/html/rfc4122#section-4.1.1 RFC 4122, § 4.1.1 */ case ReservedMicrosoft = 6; /** * Variant: reserved for future definition * - * @link http://tools.ietf.org/html/rfc4122#section-4.1.1 RFC 4122, § 4.1.1: Variant + * @link https://datatracker.ietf.org/doc/html/rfc4122#section-4.1.1 RFC 4122, § 4.1.1 */ case ReservedFuture = 7; } diff --git a/tests/BinaryUtilsTest.php b/tests/BinaryUtilsTest.php index 57ef027..0ff183d 100644 --- a/tests/BinaryUtilsTest.php +++ b/tests/BinaryUtilsTest.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Ramsey\Uuid\Test; use Ramsey\Uuid\BinaryUtils; +use Ramsey\Uuid\Rfc4122\Version; use Ramsey\Uuid\Variant; use function dechex; @@ -14,7 +15,7 @@ class BinaryUtilsTest extends TestCase /** * @dataProvider provideVersionTestValues */ - public function testApplyVersion(int $timeHi, int $version, int $expectedInt, string $expectedHex): void + public function testApplyVersion(int $timeHi, Version $version, int $expectedInt, string $expectedHex): void { $this->assertSame($expectedInt, BinaryUtils::applyVersion($timeHi, $version)); $this->assertSame($expectedHex, dechex(BinaryUtils::applyVersion($timeHi, $version))); @@ -30,98 +31,98 @@ class BinaryUtilsTest extends TestCase } /** - * @return array + * @return array */ public function provideVersionTestValues(): array { return [ [ 'timeHi' => 1001, - 'version' => 1, + 'version' => Version::Time, 'expectedInt' => 5097, 'expectedHex' => '13e9', ], [ 'timeHi' => 1001, - 'version' => 2, + 'version' => Version::DceSecurity, 'expectedInt' => 9193, 'expectedHex' => '23e9', ], [ 'timeHi' => 1001, - 'version' => 3, + 'version' => Version::HashMd5, 'expectedInt' => 13289, 'expectedHex' => '33e9', ], [ 'timeHi' => 1001, - 'version' => 4, + 'version' => Version::Random, 'expectedInt' => 17385, 'expectedHex' => '43e9', ], [ 'timeHi' => 1001, - 'version' => 5, + 'version' => Version::HashSha1, 'expectedInt' => 21481, 'expectedHex' => '53e9', ], [ 'timeHi' => 65535, - 'version' => 1, + 'version' => Version::Time, 'expectedInt' => 8191, 'expectedHex' => '1fff', ], [ 'timeHi' => 65535, - 'version' => 2, + 'version' => Version::DceSecurity, 'expectedInt' => 12287, 'expectedHex' => '2fff', ], [ 'timeHi' => 65535, - 'version' => 3, + 'version' => Version::HashMd5, 'expectedInt' => 16383, 'expectedHex' => '3fff', ], [ 'timeHi' => 65535, - 'version' => 4, + 'version' => Version::Random, 'expectedInt' => 20479, 'expectedHex' => '4fff', ], [ 'timeHi' => 65535, - 'version' => 5, + 'version' => Version::HashSha1, 'expectedInt' => 24575, 'expectedHex' => '5fff', ], [ 'timeHi' => 0, - 'version' => 1, + 'version' => Version::Time, 'expectedInt' => 4096, 'expectedHex' => '1000', ], [ 'timeHi' => 0, - 'version' => 2, + 'version' => Version::DceSecurity, 'expectedInt' => 8192, 'expectedHex' => '2000', ], [ 'timeHi' => 0, - 'version' => 3, + 'version' => Version::HashMd5, 'expectedInt' => 12288, 'expectedHex' => '3000', ], [ 'timeHi' => 0, - 'version' => 4, + 'version' => Version::Random, 'expectedInt' => 16384, 'expectedHex' => '4000', ], [ 'timeHi' => 0, - 'version' => 5, + 'version' => Version::HashSha1, 'expectedInt' => 20480, 'expectedHex' => '5000', ], diff --git a/tests/ExpectedBehaviorTest.php b/tests/ExpectedBehaviorTest.php index 26607fb..30efb99 100644 --- a/tests/ExpectedBehaviorTest.php +++ b/tests/ExpectedBehaviorTest.php @@ -7,6 +7,7 @@ use Ramsey\Uuid\Generator\CombGenerator; use Ramsey\Uuid\Generator\DefaultTimeGenerator; use Ramsey\Uuid\Rfc4122\UuidInterface; use Ramsey\Uuid\Rfc4122\UuidV1; +use Ramsey\Uuid\Rfc4122\Version; use Ramsey\Uuid\Type\Hexadecimal; use Ramsey\Uuid\Type\Time; use Ramsey\Uuid\Uuid; @@ -80,7 +81,7 @@ class ExpectedBehaviorTest extends TestCase ); $this->assertSame(Variant::Rfc4122, $uuid->getFields()->getVariant()); - $this->assertSame((int) substr($method, -1), $uuid->getFields()->getVersion()); + $this->assertSame(Version::tryFrom((int) substr($method, -1)), $uuid->getFields()->getVersion()); $this->assertTrue(ctype_digit((string) $uuid->getInteger())); } @@ -203,7 +204,7 @@ class ExpectedBehaviorTest extends TestCase $this->assertInstanceOf('Ramsey\Uuid\UuidInterface', $uuid); $this->assertSame($string, $uuid->toString()); - $this->assertSame($version, $uuid->getFields()->getVersion()); + $this->assertSame(Version::tryFrom($version), $uuid->getFields()->getVersion()); $this->assertSame(Variant::from($variant), $uuid->getFields()->getVariant()); $components = explode('-', $string); @@ -233,7 +234,7 @@ class ExpectedBehaviorTest extends TestCase $this->assertInstanceOf('Ramsey\Uuid\UuidInterface', $uuid); $this->assertSame($string, $uuid->toString()); - $this->assertSame($version, $uuid->getFields()->getVersion()); + $this->assertSame(Version::tryFrom($version), $uuid->getFields()->getVersion()); $this->assertSame(Variant::from($variant), $uuid->getFields()->getVariant()); $components = explode('-', $string); @@ -263,7 +264,7 @@ class ExpectedBehaviorTest extends TestCase $this->assertInstanceOf('Ramsey\Uuid\UuidInterface', $uuid); $this->assertSame($string, $uuid->toString()); - $this->assertSame($version, $uuid->getFields()->getVersion()); + $this->assertSame(Version::tryFrom($version), $uuid->getFields()->getVersion()); $this->assertSame(Variant::from($variant), $uuid->getFields()->getVariant()); $components = explode('-', $string); @@ -541,7 +542,7 @@ class ExpectedBehaviorTest extends TestCase $this->assertInstanceOf('Ramsey\Uuid\UuidInterface', $uuid); $this->assertSame(Variant::Rfc4122, $uuid->getFields()->getVariant()); - $this->assertSame(4, $uuid->getFields()->getVersion()); + $this->assertSame(Version::Random, $uuid->getFields()->getVersion()); $this->assertSame($expectedBytes, $uuid->getBytes()); $this->assertSame($expectedHex, (string) $uuid->getHex()); } @@ -565,11 +566,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'], - ['UUID_TYPE_TIME', 1], - ['UUID_TYPE_DCE_SECURITY', 2], - ['UUID_TYPE_HASH_MD5', 3], - ['UUID_TYPE_RANDOM', 4], - ['UUID_TYPE_HASH_SHA1', 5], ]; } } diff --git a/tests/FunctionsTest.php b/tests/FunctionsTest.php index 02e926a..7b41b89 100644 --- a/tests/FunctionsTest.php +++ b/tests/FunctionsTest.php @@ -6,6 +6,7 @@ namespace Ramsey\Uuid\Test; use Ramsey\Uuid\Rfc4122\FieldsInterface; use Ramsey\Uuid\Rfc4122\UuidInterface; +use Ramsey\Uuid\Rfc4122\Version; use Ramsey\Uuid\Type\Hexadecimal; use Ramsey\Uuid\Type\Integer as IntegerObject; use Ramsey\Uuid\Uuid; @@ -27,7 +28,7 @@ class FunctionsTest extends TestCase $uuid = Uuid::fromString($v1); $this->assertIsString($v1); - $this->assertSame(Uuid::UUID_TYPE_TIME, $uuid->getFields()->getVersion()); + $this->assertSame(Version::Time, $uuid->getFields()->getVersion()); } public function testV2ReturnsVersion2UuidString(): void @@ -43,7 +44,7 @@ class FunctionsTest extends TestCase $fields = Uuid::fromString($v2)->getFields(); $this->assertIsString($v2); - $this->assertSame(Uuid::UUID_TYPE_DCE_SECURITY, $fields->getVersion()); + $this->assertSame(Version::DceSecurity, $fields->getVersion()); } public function testV3ReturnsVersion3UuidString(): void @@ -55,7 +56,7 @@ class FunctionsTest extends TestCase $uuid = Uuid::fromString($v3); $this->assertIsString($v3); - $this->assertSame(Uuid::UUID_TYPE_HASH_MD5, $uuid->getFields()->getVersion()); + $this->assertSame(Version::HashMd5, $uuid->getFields()->getVersion()); } public function testV4ReturnsVersion4UuidString(): void @@ -66,7 +67,7 @@ class FunctionsTest extends TestCase $uuid = Uuid::fromString($v4); $this->assertIsString($v4); - $this->assertSame(Uuid::UUID_TYPE_RANDOM, $uuid->getFields()->getVersion()); + $this->assertSame(Version::Random, $uuid->getFields()->getVersion()); } public function testV5ReturnsVersion5UuidString(): void @@ -78,7 +79,7 @@ class FunctionsTest extends TestCase $uuid = Uuid::fromString($v5); $this->assertIsString($v5); - $this->assertSame(Uuid::UUID_TYPE_HASH_SHA1, $uuid->getFields()->getVersion()); + $this->assertSame(Version::HashSha1, $uuid->getFields()->getVersion()); } public function testV6ReturnsVersion6UuidString(): void @@ -92,6 +93,6 @@ class FunctionsTest extends TestCase $fields = Uuid::fromString($v6)->getFields(); $this->assertIsString($v6); - $this->assertSame(Uuid::UUID_TYPE_PEABODY, $fields->getVersion()); + $this->assertSame(Version::ReorderedTime, $fields->getVersion()); } } diff --git a/tests/Generator/PeclUuidNameGeneratorTest.php b/tests/Generator/PeclUuidNameGeneratorTest.php index 6908335..a4e18fc 100644 --- a/tests/Generator/PeclUuidNameGeneratorTest.php +++ b/tests/Generator/PeclUuidNameGeneratorTest.php @@ -7,6 +7,7 @@ namespace Ramsey\Uuid\Test\Generator; use Ramsey\Uuid\BinaryUtils; use Ramsey\Uuid\Exception\NameException; use Ramsey\Uuid\Generator\PeclUuidNameGenerator; +use Ramsey\Uuid\Rfc4122\Version; use Ramsey\Uuid\Test\TestCase; use Ramsey\Uuid\Uuid; @@ -28,7 +29,7 @@ class PeclUuidNameGeneratorTest extends TestCase public function testPeclUuidNameGeneratorHashesName(string $ns, string $name, string $algorithm): void { $namespace = Uuid::fromString($ns); - $version = $algorithm === 'md5' ? 3 : 5; + $version = $algorithm === 'md5' ? Version::HashMd5 : Version::HashSha1; $expectedBytes = substr(hash($algorithm, $namespace->getBytes() . $name, true), 0, 16); // Need to add the version and variant, since ext-uuid already includes diff --git a/tests/Generator/PeclUuidRandomGeneratorTest.php b/tests/Generator/PeclUuidRandomGeneratorTest.php index ec778a5..3b27eb9 100644 --- a/tests/Generator/PeclUuidRandomGeneratorTest.php +++ b/tests/Generator/PeclUuidRandomGeneratorTest.php @@ -6,6 +6,7 @@ namespace Ramsey\Uuid\Test\Generator; use Ramsey\Uuid\Generator\PeclUuidRandomGenerator; use Ramsey\Uuid\Rfc4122\Fields; +use Ramsey\Uuid\Rfc4122\Version; use Ramsey\Uuid\Test\TestCase; use Ramsey\Uuid\Uuid; @@ -24,6 +25,6 @@ class PeclUuidRandomGeneratorTest extends TestCase $fields = $uuid->getFields(); $this->assertSame(16, strlen($bytes)); - $this->assertSame(Uuid::UUID_TYPE_RANDOM, $fields->getVersion()); + $this->assertSame(Version::Random, $fields->getVersion()); } } diff --git a/tests/Generator/PeclUuidTimeGeneratorTest.php b/tests/Generator/PeclUuidTimeGeneratorTest.php index acd19bc..ab78660 100644 --- a/tests/Generator/PeclUuidTimeGeneratorTest.php +++ b/tests/Generator/PeclUuidTimeGeneratorTest.php @@ -6,6 +6,7 @@ namespace Ramsey\Uuid\Test\Generator; use Ramsey\Uuid\Generator\PeclUuidTimeGenerator; use Ramsey\Uuid\Rfc4122\Fields; +use Ramsey\Uuid\Rfc4122\Version; use Ramsey\Uuid\Test\TestCase; use Ramsey\Uuid\Uuid; @@ -24,6 +25,6 @@ class PeclUuidTimeGeneratorTest extends TestCase $fields = $uuid->getFields(); $this->assertSame(16, strlen($bytes)); - $this->assertSame(Uuid::UUID_TYPE_TIME, $fields->getVersion()); + $this->assertSame(Version::Time, $fields->getVersion()); } } diff --git a/tests/Guid/FieldsTest.php b/tests/Guid/FieldsTest.php index 9507a75..b62984a 100644 --- a/tests/Guid/FieldsTest.php +++ b/tests/Guid/FieldsTest.php @@ -6,6 +6,7 @@ namespace Ramsey\Uuid\Test\Guid; use Ramsey\Uuid\Exception\InvalidArgumentException; use Ramsey\Uuid\Guid\Fields; +use Ramsey\Uuid\Rfc4122\Version; use Ramsey\Uuid\Test\TestCase; use Ramsey\Uuid\Type\Hexadecimal; use Ramsey\Uuid\Variant; @@ -104,7 +105,7 @@ class FieldsTest extends TestCase public function testFieldGetterMethods( string $bytes, string $methodName, - bool | int | string | Variant | null $expectedValue + bool | string | Variant | Version | null $expectedValue ): void { /** @var non-empty-string $bytes */ $bytes = (string) hex2bin($bytes); @@ -138,7 +139,7 @@ class FieldsTest extends TestCase ['b08c6fff7dc5e111cb210800200c9a66', 'getTimeMid', 'c57d'], ['b08c6fff7dc5e111cb210800200c9a66', 'getTimestamp', '1e1c57dff6f8cb0'], ['b08c6fff7dc5e111cb210800200c9a66', 'getVariant', Variant::ReservedMicrosoft], - ['b08c6fff7dc5e111cb210800200c9a66', 'getVersion', 1], + ['b08c6fff7dc5e111cb210800200c9a66', 'getVersion', Version::Time], ['b08c6fff7dc5e111cb210800200c9a66', 'isNil', false], // For ff6f8cb0-c57d-41e1-db21-0800200c9a66 @@ -151,7 +152,7 @@ class FieldsTest extends TestCase ['b08c6fff7dc5e141db210800200c9a66', 'getTimeMid', 'c57d'], ['b08c6fff7dc5e141db210800200c9a66', 'getTimestamp', '1e1c57dff6f8cb0'], ['b08c6fff7dc5e141db210800200c9a66', 'getVariant', Variant::ReservedMicrosoft], - ['b08c6fff7dc5e141db210800200c9a66', 'getVersion', 4], + ['b08c6fff7dc5e141db210800200c9a66', 'getVersion', Version::Random], ['b08c6fff7dc5e141db210800200c9a66', 'isNil', false], // For ff6f8cb0-c57d-31e1-8b21-0800200c9a66 @@ -164,7 +165,7 @@ class FieldsTest extends TestCase ['b08c6fff7dc5e1318b210800200c9a66', 'getTimeMid', 'c57d'], ['b08c6fff7dc5e1318b210800200c9a66', 'getTimestamp', '1e1c57dff6f8cb0'], ['b08c6fff7dc5e1318b210800200c9a66', 'getVariant', Variant::Rfc4122], - ['b08c6fff7dc5e1318b210800200c9a66', 'getVersion', 3], + ['b08c6fff7dc5e1318b210800200c9a66', 'getVersion', Version::HashMd5], ['b08c6fff7dc5e1318b210800200c9a66', 'isNil', false], // For ff6f8cb0-c57d-51e1-9b21-0800200c9a66 @@ -177,7 +178,7 @@ class FieldsTest extends TestCase ['b08c6fff7dc5e1519b210800200c9a66', 'getTimeMid', 'c57d'], ['b08c6fff7dc5e1519b210800200c9a66', 'getTimestamp', '1e1c57dff6f8cb0'], ['b08c6fff7dc5e1519b210800200c9a66', 'getVariant', Variant::Rfc4122], - ['b08c6fff7dc5e1519b210800200c9a66', 'getVersion', 5], + ['b08c6fff7dc5e1519b210800200c9a66', 'getVersion', Version::HashSha1], ['b08c6fff7dc5e1519b210800200c9a66', 'isNil', false], // For 00000000-0000-0000-0000-000000000000 diff --git a/tests/Nonstandard/UuidV6Test.php b/tests/Nonstandard/UuidV6Test.php index 0df8103..d645abb 100644 --- a/tests/Nonstandard/UuidV6Test.php +++ b/tests/Nonstandard/UuidV6Test.php @@ -14,6 +14,7 @@ use Ramsey\Uuid\Exception\InvalidArgumentException; use Ramsey\Uuid\Lazy\LazyUuidFromString; use Ramsey\Uuid\Nonstandard\UuidV6; use Ramsey\Uuid\Rfc4122\FieldsInterface; +use Ramsey\Uuid\Rfc4122\Version; use Ramsey\Uuid\Test\TestCase; use Ramsey\Uuid\Type\Hexadecimal; use Ramsey\Uuid\Type\Time; @@ -24,7 +25,7 @@ class UuidV6Test extends TestCase /** * @dataProvider provideTestVersions */ - public function testConstructorThrowsExceptionWhenFieldsAreNotValidForType(int $version): void + public function testConstructorThrowsExceptionWhenFieldsAreNotValidForType(Version $version): void { $fields = Mockery::mock(FieldsInterface::class, [ 'getVersion' => $version, @@ -49,15 +50,13 @@ class UuidV6Test extends TestCase public function provideTestVersions(): array { return [ - ['version' => 0], - ['version' => 1], - ['version' => 2], - ['version' => 3], - ['version' => 4], - ['version' => 5], - ['version' => 7], - ['version' => 8], - ['version' => 9], + ['version' => Version::Time], + ['version' => Version::DceSecurity], + ['version' => Version::HashMd5], + ['version' => Version::Random], + ['version' => Version::HashSha1], + ['version' => Version::UnixTime], + ['version' => Version::Custom], ]; } @@ -173,7 +172,7 @@ class UuidV6Test extends TestCase public function testGetDateTimeThrowsException(): void { $fields = Mockery::mock(FieldsInterface::class, [ - 'getVersion' => 6, + 'getVersion' => Version::ReorderedTime, 'getTimestamp' => new Hexadecimal('0'), ]); diff --git a/tests/Rfc4122/FieldsTest.php b/tests/Rfc4122/FieldsTest.php index 2ce62fe..9b7bd10 100644 --- a/tests/Rfc4122/FieldsTest.php +++ b/tests/Rfc4122/FieldsTest.php @@ -6,6 +6,7 @@ namespace Ramsey\Uuid\Test\Rfc4122; use Ramsey\Uuid\Exception\InvalidArgumentException; use Ramsey\Uuid\Rfc4122\Fields; +use Ramsey\Uuid\Rfc4122\Version; use Ramsey\Uuid\Test\TestCase; use Ramsey\Uuid\Type\Hexadecimal; use Ramsey\Uuid\Variant; @@ -94,12 +95,13 @@ class FieldsTest extends TestCase } /** - * @param string|int $expectedValue - * * @dataProvider fieldGetterMethodProvider */ - public function testFieldGetterMethods(string $uuid, string $methodName, $expectedValue): void - { + public function testFieldGetterMethods( + string $uuid, + string $methodName, + bool | string | Variant | Version | null $expectedValue + ): void { /** @var non-empty-string $bytes */ $bytes = (string) hex2bin(str_replace('-', '', $uuid)); $fields = new Fields($bytes); @@ -128,7 +130,7 @@ class FieldsTest extends TestCase ['ff6f8cb0-c57d-11e1-9b21-0800200c9a66', 'getTimeMid', 'c57d'], ['ff6f8cb0-c57d-11e1-9b21-0800200c9a66', 'getTimestamp', '1e1c57dff6f8cb0'], ['ff6f8cb0-c57d-11e1-9b21-0800200c9a66', 'getVariant', Variant::Rfc4122], - ['ff6f8cb0-c57d-11e1-9b21-0800200c9a66', 'getVersion', 1], + ['ff6f8cb0-c57d-11e1-9b21-0800200c9a66', 'getVersion', Version::Time], ['ff6f8cb0-c57d-11e1-9b21-0800200c9a66', 'isNil', false], ['ff6f8cb0-c57d-41e1-ab21-0800200c9a66', 'getClockSeq', '2b21'], @@ -140,7 +142,7 @@ class FieldsTest extends TestCase ['ff6f8cb0-c57d-41e1-ab21-0800200c9a66', 'getTimeMid', 'c57d'], ['ff6f8cb0-c57d-41e1-ab21-0800200c9a66', 'getTimestamp', '1e1c57dff6f8cb0'], ['ff6f8cb0-c57d-41e1-ab21-0800200c9a66', 'getVariant', Variant::Rfc4122], - ['ff6f8cb0-c57d-41e1-ab21-0800200c9a66', 'getVersion', 4], + ['ff6f8cb0-c57d-41e1-ab21-0800200c9a66', 'getVersion', Version::Random], ['ff6f8cb0-c57d-41e1-ab21-0800200c9a66', 'isNil', false], ['ff6f8cb0-c57d-31e1-bb21-0800200c9a66', 'getClockSeq', '3b21'], @@ -152,7 +154,7 @@ class FieldsTest extends TestCase ['ff6f8cb0-c57d-31e1-bb21-0800200c9a66', 'getTimeMid', 'c57d'], ['ff6f8cb0-c57d-31e1-bb21-0800200c9a66', 'getTimestamp', '1e1c57dff6f8cb0'], ['ff6f8cb0-c57d-31e1-bb21-0800200c9a66', 'getVariant', Variant::Rfc4122], - ['ff6f8cb0-c57d-31e1-bb21-0800200c9a66', 'getVersion', 3], + ['ff6f8cb0-c57d-31e1-bb21-0800200c9a66', 'getVersion', Version::HashMd5], ['ff6f8cb0-c57d-31e1-bb21-0800200c9a66', 'isNil', false], ['ff6f8cb0-c57d-51e1-8b21-0800200c9a66', 'getClockSeq', '0b21'], @@ -164,7 +166,7 @@ class FieldsTest extends TestCase ['ff6f8cb0-c57d-51e1-8b21-0800200c9a66', 'getTimeMid', 'c57d'], ['ff6f8cb0-c57d-51e1-8b21-0800200c9a66', 'getTimestamp', '1e1c57dff6f8cb0'], ['ff6f8cb0-c57d-51e1-8b21-0800200c9a66', 'getVariant', Variant::Rfc4122], - ['ff6f8cb0-c57d-51e1-8b21-0800200c9a66', 'getVersion', 5], + ['ff6f8cb0-c57d-51e1-8b21-0800200c9a66', 'getVersion', Version::HashSha1], ['ff6f8cb0-c57d-51e1-8b21-0800200c9a66', 'isNil', false], ['ff6f8cb0-c57d-61e1-8b21-0800200c9a66', 'getClockSeq', '0b21'], @@ -176,7 +178,7 @@ class FieldsTest extends TestCase ['ff6f8cb0-c57d-61e1-8b21-0800200c9a66', 'getTimeMid', 'c57d'], ['ff6f8cb0-c57d-61e1-8b21-0800200c9a66', 'getTimestamp', 'ff6f8cb0c57d1e1'], ['ff6f8cb0-c57d-61e1-8b21-0800200c9a66', 'getVariant', Variant::Rfc4122], - ['ff6f8cb0-c57d-61e1-8b21-0800200c9a66', 'getVersion', 6], + ['ff6f8cb0-c57d-61e1-8b21-0800200c9a66', 'getVersion', Version::ReorderedTime], ['ff6f8cb0-c57d-61e1-8b21-0800200c9a66', 'isNil', false], ['00000000-0000-0000-0000-000000000000', 'getClockSeq', '0000'], @@ -200,7 +202,7 @@ class FieldsTest extends TestCase ['000001f5-5cde-21ea-8400-0242ac130003', 'getTimeMid', '5cde'], ['000001f5-5cde-21ea-8400-0242ac130003', 'getTimestamp', '1ea5cde00000000'], ['000001f5-5cde-21ea-8400-0242ac130003', 'getVariant', Variant::Rfc4122], - ['000001f5-5cde-21ea-8400-0242ac130003', 'getVersion', 2], + ['000001f5-5cde-21ea-8400-0242ac130003', 'getVersion', Version::DceSecurity], ['000001f5-5cde-21ea-8400-0242ac130003', 'isNil', false], ]; } diff --git a/tests/Rfc4122/UuidBuilderTest.php b/tests/Rfc4122/UuidBuilderTest.php index acd18a3..fa743c0 100644 --- a/tests/Rfc4122/UuidBuilderTest.php +++ b/tests/Rfc4122/UuidBuilderTest.php @@ -4,7 +4,6 @@ declare(strict_types=1); namespace Ramsey\Uuid\Test\Rfc4122; -use Mockery; use Ramsey\Uuid\Codec\StringCodec; use Ramsey\Uuid\Converter\Number\GenericNumberConverter; use Ramsey\Uuid\Converter\Time\GenericTimeConverter; @@ -12,13 +11,13 @@ use Ramsey\Uuid\Exception\UnableToBuildUuidException; use Ramsey\Uuid\Math\BrickMathCalculator; use Ramsey\Uuid\Nonstandard\UuidV6; use Ramsey\Uuid\Rfc4122\Fields; -use Ramsey\Uuid\Rfc4122\FieldsInterface; use Ramsey\Uuid\Rfc4122\UuidBuilder; use Ramsey\Uuid\Rfc4122\UuidV1; use Ramsey\Uuid\Rfc4122\UuidV2; use Ramsey\Uuid\Rfc4122\UuidV3; use Ramsey\Uuid\Rfc4122\UuidV4; use Ramsey\Uuid\Rfc4122\UuidV5; +use Ramsey\Uuid\Rfc4122\Version; use Ramsey\Uuid\Test\TestCase; use function hex2bin; @@ -31,7 +30,7 @@ class UuidBuilderTest extends TestCase * * @dataProvider provideBuildTestValues */ - public function testBuild(string $uuid, string $expectedClass, int $expectedVersion): void + public function testBuild(string $uuid, string $expectedClass, Version $expectedVersion): void { /** @var non-empty-string $bytes */ $bytes = (string) hex2bin(str_replace('-', '', $uuid)); @@ -60,32 +59,32 @@ class UuidBuilderTest extends TestCase [ 'uuid' => 'ff6f8cb0-c57d-11e1-9b21-0800200c9a66', 'expectedClass' => UuidV1::class, - 'expectedVersion' => 1, + 'expectedVersion' => Version::Time, ], [ 'uuid' => 'ff6f8cb0-c57d-21e1-9b21-0800200c9a66', 'expectedClass' => UuidV2::class, - 'expectedVersion' => 2, + 'expectedVersion' => Version::DceSecurity, ], [ 'uuid' => 'ff6f8cb0-c57d-31e1-9b21-0800200c9a66', 'expectedClass' => UuidV3::class, - 'expectedVersion' => 3, + 'expectedVersion' => Version::HashMd5, ], [ 'uuid' => 'ff6f8cb0-c57d-41e1-9b21-0800200c9a66', 'expectedClass' => UuidV4::class, - 'expectedVersion' => 4, + 'expectedVersion' => Version::Random, ], [ 'uuid' => 'ff6f8cb0-c57d-51e1-9b21-0800200c9a66', 'expectedClass' => UuidV5::class, - 'expectedVersion' => 5, + 'expectedVersion' => Version::HashSha1, ], [ 'uuid' => 'ff6f8cb0-c57d-61e1-9b21-0800200c9a66', 'expectedClass' => UuidV6::class, - 'expectedVersion' => 6, + 'expectedVersion' => Version::ReorderedTime, ], ]; } @@ -108,26 +107,4 @@ class UuidBuilderTest extends TestCase $builder->build($codec, $bytes); } - - public function testBuildThrowsUnableToBuildExceptionForIncorrectVersionFields(): void - { - $fields = Mockery::mock(FieldsInterface::class, [ - 'isNil' => false, - 'getVersion' => 255, - ]); - - $builder = Mockery::mock(UuidBuilder::class); - $builder->shouldAllowMockingProtectedMethods(); - $builder->shouldReceive('buildFields')->andReturn($fields); - $builder->shouldReceive('build')->passthru(); - - $codec = Mockery::mock(StringCodec::class); - - $this->expectException(UnableToBuildUuidException::class); - $this->expectExceptionMessage( - 'The UUID version in the given fields is not supported by this UUID builder' - ); - - $builder->build($codec, 'foobar'); - } } diff --git a/tests/Rfc4122/UuidV1Test.php b/tests/Rfc4122/UuidV1Test.php index 41f5bb0..bd01c4d 100644 --- a/tests/Rfc4122/UuidV1Test.php +++ b/tests/Rfc4122/UuidV1Test.php @@ -13,6 +13,7 @@ use Ramsey\Uuid\Exception\DateTimeException; use Ramsey\Uuid\Exception\InvalidArgumentException; use Ramsey\Uuid\Rfc4122\FieldsInterface; use Ramsey\Uuid\Rfc4122\UuidV1; +use Ramsey\Uuid\Rfc4122\Version; use Ramsey\Uuid\Test\TestCase; use Ramsey\Uuid\Type\Hexadecimal; use Ramsey\Uuid\Type\Time; @@ -23,7 +24,7 @@ class UuidV1Test extends TestCase /** * @dataProvider provideTestVersions */ - public function testConstructorThrowsExceptionWhenFieldsAreNotValidForType(int $version): void + public function testConstructorThrowsExceptionWhenFieldsAreNotValidForType(Version $version): void { $fields = Mockery::mock(FieldsInterface::class, [ 'getVersion' => $version, @@ -48,15 +49,13 @@ class UuidV1Test extends TestCase public function provideTestVersions(): array { return [ - ['version' => 0], - ['version' => 2], - ['version' => 3], - ['version' => 4], - ['version' => 5], - ['version' => 6], - ['version' => 7], - ['version' => 8], - ['version' => 9], + ['version' => Version::DceSecurity], + ['version' => Version::HashMd5], + ['version' => Version::Random], + ['version' => Version::HashSha1], + ['version' => Version::ReorderedTime], + ['version' => Version::UnixTime], + ['version' => Version::Custom], ]; } @@ -104,7 +103,7 @@ class UuidV1Test extends TestCase public function testGetDateTimeThrowsException(): void { $fields = Mockery::mock(FieldsInterface::class, [ - 'getVersion' => 1, + 'getVersion' => Version::Time, 'getTimestamp' => new Hexadecimal('0'), ]); diff --git a/tests/Rfc4122/UuidV2Test.php b/tests/Rfc4122/UuidV2Test.php index b0226e2..b8e63a8 100644 --- a/tests/Rfc4122/UuidV2Test.php +++ b/tests/Rfc4122/UuidV2Test.php @@ -20,6 +20,7 @@ use Ramsey\Uuid\Provider\Node\StaticNodeProvider; use Ramsey\Uuid\Provider\Time\FixedTimeProvider; use Ramsey\Uuid\Rfc4122\FieldsInterface; use Ramsey\Uuid\Rfc4122\UuidV2; +use Ramsey\Uuid\Rfc4122\Version; use Ramsey\Uuid\Test\TestCase; use Ramsey\Uuid\Type\Hexadecimal; use Ramsey\Uuid\Type\Integer; @@ -34,7 +35,7 @@ class UuidV2Test extends TestCase /** * @dataProvider provideTestVersions */ - public function testConstructorThrowsExceptionWhenFieldsAreNotValidForType(int $version): void + public function testConstructorThrowsExceptionWhenFieldsAreNotValidForType(Version $version): void { $fields = Mockery::mock(FieldsInterface::class, [ 'getVersion' => $version, @@ -59,15 +60,13 @@ class UuidV2Test extends TestCase public function provideTestVersions(): array { return [ - ['version' => 0], - ['version' => 1], - ['version' => 3], - ['version' => 4], - ['version' => 5], - ['version' => 6], - ['version' => 7], - ['version' => 8], - ['version' => 9], + ['version' => Version::Time], + ['version' => Version::HashMd5], + ['version' => Version::Random], + ['version' => Version::HashSha1], + ['version' => Version::ReorderedTime], + ['version' => Version::UnixTime], + ['version' => Version::Custom], ]; } diff --git a/tests/Rfc4122/UuidV3Test.php b/tests/Rfc4122/UuidV3Test.php index b9110ef..9abb5de 100644 --- a/tests/Rfc4122/UuidV3Test.php +++ b/tests/Rfc4122/UuidV3Test.php @@ -11,6 +11,7 @@ use Ramsey\Uuid\Converter\TimeConverterInterface; use Ramsey\Uuid\Exception\InvalidArgumentException; use Ramsey\Uuid\Rfc4122\FieldsInterface; use Ramsey\Uuid\Rfc4122\UuidV3; +use Ramsey\Uuid\Rfc4122\Version; use Ramsey\Uuid\Test\TestCase; class UuidV3Test extends TestCase @@ -18,7 +19,7 @@ class UuidV3Test extends TestCase /** * @dataProvider provideTestVersions */ - public function testConstructorThrowsExceptionWhenFieldsAreNotValidForType(int $version): void + public function testConstructorThrowsExceptionWhenFieldsAreNotValidForType(Version $version): void { $fields = Mockery::mock(FieldsInterface::class, [ 'getVersion' => $version, @@ -43,15 +44,13 @@ class UuidV3Test extends TestCase public function provideTestVersions(): array { return [ - ['version' => 0], - ['version' => 1], - ['version' => 2], - ['version' => 4], - ['version' => 5], - ['version' => 6], - ['version' => 7], - ['version' => 8], - ['version' => 9], + ['version' => Version::Time], + ['version' => Version::DceSecurity], + ['version' => Version::Random], + ['version' => Version::HashSha1], + ['version' => Version::ReorderedTime], + ['version' => Version::UnixTime], + ['version' => Version::Custom], ]; } } diff --git a/tests/Rfc4122/UuidV4Test.php b/tests/Rfc4122/UuidV4Test.php index f595346..bb817d0 100644 --- a/tests/Rfc4122/UuidV4Test.php +++ b/tests/Rfc4122/UuidV4Test.php @@ -11,6 +11,7 @@ use Ramsey\Uuid\Converter\TimeConverterInterface; use Ramsey\Uuid\Exception\InvalidArgumentException; use Ramsey\Uuid\Rfc4122\FieldsInterface; use Ramsey\Uuid\Rfc4122\UuidV4; +use Ramsey\Uuid\Rfc4122\Version; use Ramsey\Uuid\Test\TestCase; class UuidV4Test extends TestCase @@ -18,7 +19,7 @@ class UuidV4Test extends TestCase /** * @dataProvider provideTestVersions */ - public function testConstructorThrowsExceptionWhenFieldsAreNotValidForType(int $version): void + public function testConstructorThrowsExceptionWhenFieldsAreNotValidForType(Version $version): void { $fields = Mockery::mock(FieldsInterface::class, [ 'getVersion' => $version, @@ -43,15 +44,13 @@ class UuidV4Test extends TestCase public function provideTestVersions(): array { return [ - ['version' => 0], - ['version' => 1], - ['version' => 2], - ['version' => 3], - ['version' => 5], - ['version' => 6], - ['version' => 7], - ['version' => 8], - ['version' => 9], + ['version' => Version::Time], + ['version' => Version::DceSecurity], + ['version' => Version::HashMd5], + ['version' => Version::HashSha1], + ['version' => Version::ReorderedTime], + ['version' => Version::UnixTime], + ['version' => Version::Custom], ]; } } diff --git a/tests/Rfc4122/UuidV5Test.php b/tests/Rfc4122/UuidV5Test.php index a1270f2..c38e678 100644 --- a/tests/Rfc4122/UuidV5Test.php +++ b/tests/Rfc4122/UuidV5Test.php @@ -11,6 +11,7 @@ use Ramsey\Uuid\Converter\TimeConverterInterface; use Ramsey\Uuid\Exception\InvalidArgumentException; use Ramsey\Uuid\Rfc4122\FieldsInterface; use Ramsey\Uuid\Rfc4122\UuidV5; +use Ramsey\Uuid\Rfc4122\Version; use Ramsey\Uuid\Test\TestCase; class UuidV5Test extends TestCase @@ -18,7 +19,7 @@ class UuidV5Test extends TestCase /** * @dataProvider provideTestVersions */ - public function testConstructorThrowsExceptionWhenFieldsAreNotValidForType(int $version): void + public function testConstructorThrowsExceptionWhenFieldsAreNotValidForType(Version $version): void { $fields = Mockery::mock(FieldsInterface::class, [ 'getVersion' => $version, @@ -43,15 +44,13 @@ class UuidV5Test extends TestCase public function provideTestVersions(): array { return [ - ['version' => 0], - ['version' => 1], - ['version' => 2], - ['version' => 3], - ['version' => 4], - ['version' => 6], - ['version' => 7], - ['version' => 8], - ['version' => 9], + ['version' => Version::Time], + ['version' => Version::DceSecurity], + ['version' => Version::HashMd5], + ['version' => Version::Random], + ['version' => Version::ReorderedTime], + ['version' => Version::UnixTime], + ['version' => Version::Custom], ]; } } diff --git a/tests/UuidTest.php b/tests/UuidTest.php index 99af9af..3d93305 100644 --- a/tests/UuidTest.php +++ b/tests/UuidTest.php @@ -24,6 +24,7 @@ use Ramsey\Uuid\Provider\Node\RandomNodeProvider; use Ramsey\Uuid\Provider\Time\FixedTimeProvider; use Ramsey\Uuid\Rfc4122\FieldsInterface; use Ramsey\Uuid\Rfc4122\UuidV1; +use Ramsey\Uuid\Rfc4122\Version; use Ramsey\Uuid\Type\Hexadecimal; use Ramsey\Uuid\Type\Time; use Ramsey\Uuid\Uuid; @@ -382,35 +383,35 @@ class UuidTest extends TestCase { /** @var \Ramsey\Uuid\Rfc4122\UuidInterface $uuid */ $uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66'); - $this->assertSame(1, $uuid->getFields()->getVersion()); + $this->assertSame(Version::Time, $uuid->getFields()->getVersion()); } public function testGetVersionForVersion2(): void { /** @var \Ramsey\Uuid\Rfc4122\UuidInterface $uuid */ $uuid = Uuid::fromString('6fa459ea-ee8a-2ca4-894e-db77e160355e'); - $this->assertSame(2, $uuid->getFields()->getVersion()); + $this->assertSame(Version::DceSecurity, $uuid->getFields()->getVersion()); } public function testGetVersionForVersion3(): void { /** @var \Ramsey\Uuid\Rfc4122\UuidInterface $uuid */ $uuid = Uuid::fromString('6fa459ea-ee8a-3ca4-894e-db77e160355e'); - $this->assertSame(3, $uuid->getFields()->getVersion()); + $this->assertSame(Version::HashMd5, $uuid->getFields()->getVersion()); } public function testGetVersionForVersion4(): void { /** @var \Ramsey\Uuid\Rfc4122\UuidInterface $uuid */ $uuid = Uuid::fromString('6fabf0bc-603a-42f2-925b-d9f779bd0032'); - $this->assertSame(4, $uuid->getFields()->getVersion()); + $this->assertSame(Version::Random, $uuid->getFields()->getVersion()); } public function testGetVersionForVersion5(): void { /** @var \Ramsey\Uuid\Rfc4122\UuidInterface $uuid */ $uuid = Uuid::fromString('886313e1-3b8a-5372-9b90-0c9aee199e5d'); - $this->assertSame(5, $uuid->getFields()->getVersion()); + $this->assertSame(Version::HashSha1, $uuid->getFields()->getVersion()); } public function testToString(): void @@ -432,7 +433,7 @@ class UuidTest extends TestCase $uuid = Uuid::uuid1(); $this->assertInstanceOf(DateTimeInterface::class, $uuid->getDateTime()); $this->assertSame(Variant::Rfc4122, $uuid->getFields()->getVariant()); - $this->assertSame(1, $uuid->getFields()->getVersion()); + $this->assertSame(Version::Time, $uuid->getFields()->getVersion()); } public function testUuid1WithNodeAndClockSequence(): void @@ -441,7 +442,7 @@ class UuidTest extends TestCase $uuid = Uuid::uuid1('0800200c9a66', 0x1669); $this->assertInstanceOf(DateTimeInterface::class, $uuid->getDateTime()); $this->assertSame(Variant::Rfc4122, $uuid->getFields()->getVariant()); - $this->assertSame(1, $uuid->getFields()->getVersion()); + $this->assertSame(Version::Time, $uuid->getFields()->getVersion()); $this->assertSame('1669', $uuid->getFields()->getClockSeq()->toString()); $this->assertSame('0800200c9a66', $uuid->getFields()->getNode()->toString()); $this->assertSame('9669-0800200c9a66', substr($uuid->toString(), 19)); @@ -453,7 +454,7 @@ class UuidTest extends TestCase $uuid = Uuid::uuid1(new Hexadecimal('0800200c9a66'), 0x1669); $this->assertInstanceOf(DateTimeInterface::class, $uuid->getDateTime()); $this->assertSame(Variant::Rfc4122, $uuid->getFields()->getVariant()); - $this->assertSame(1, $uuid->getFields()->getVersion()); + $this->assertSame(Version::Time, $uuid->getFields()->getVersion()); $this->assertSame('1669', $uuid->getFields()->getClockSeq()->toString()); $this->assertSame('0800200c9a66', $uuid->getFields()->getNode()->toString()); $this->assertSame('9669-0800200c9a66', substr($uuid->toString(), 19)); @@ -466,7 +467,7 @@ class UuidTest extends TestCase $this->assertInstanceOf(DateTimeInterface::class, $uuid->getDateTime()); $this->assertSame(Variant::Rfc4122, $uuid->getFields()->getVariant()); - $this->assertSame(1, $uuid->getFields()->getVersion()); + $this->assertSame(Version::Time, $uuid->getFields()->getVersion()); $this->assertSame('00007160355e', $uuid->getFields()->getNode()->toString()); } @@ -477,7 +478,7 @@ class UuidTest extends TestCase $this->assertInstanceOf(DateTimeInterface::class, $uuid->getDateTime()); $this->assertSame(Variant::Rfc4122, $uuid->getFields()->getVariant()); - $this->assertSame(1, $uuid->getFields()->getVersion()); + $this->assertSame(Version::Time, $uuid->getFields()->getVersion()); $this->assertSame('00007160355e', $uuid->getFields()->getNode()->toString()); } @@ -488,7 +489,7 @@ class UuidTest extends TestCase $this->assertInstanceOf(DateTimeInterface::class, $uuid->getDateTime()); $this->assertSame(Variant::Rfc4122, $uuid->getFields()->getVariant()); - $this->assertSame(1, $uuid->getFields()->getVersion()); + $this->assertSame(Version::Time, $uuid->getFields()->getVersion()); $this->assertSame('000071b0ad5e', $uuid->getFields()->getNode()->toString()); } @@ -524,7 +525,7 @@ class UuidTest extends TestCase $uuid = Uuid::uuid1(); $this->assertInstanceOf(DateTimeInterface::class, $uuid->getDateTime()); $this->assertSame(Variant::Rfc4122, $uuid->getFields()->getVariant()); - $this->assertSame(1, $uuid->getFields()->getVersion()); + $this->assertSame(Version::Time, $uuid->getFields()->getVersion()); } public function testUuid1WithUserGeneratedRandomNode(): void @@ -533,7 +534,7 @@ class UuidTest extends TestCase $uuid = Uuid::uuid1(new Hexadecimal((string) (new RandomNodeProvider())->getNode())); $this->assertInstanceOf(DateTimeInterface::class, $uuid->getDateTime()); $this->assertSame(Variant::Rfc4122, $uuid->getFields()->getVariant()); - $this->assertSame(1, $uuid->getFields()->getVersion()); + $this->assertSame(Version::Time, $uuid->getFields()->getVersion()); } public function testUuid6(): void @@ -542,7 +543,7 @@ class UuidTest extends TestCase $uuid = Uuid::uuid6(); $this->assertInstanceOf(DateTimeInterface::class, $uuid->getDateTime()); $this->assertSame(Variant::Rfc4122, $uuid->getFields()->getVariant()); - $this->assertSame(6, $uuid->getFields()->getVersion()); + $this->assertSame(Version::ReorderedTime, $uuid->getFields()->getVersion()); } public function testUuid6WithNodeAndClockSequence(): void @@ -551,7 +552,7 @@ class UuidTest extends TestCase $uuid = Uuid::uuid6(new Hexadecimal('0800200c9a66'), 0x1669); $this->assertInstanceOf(DateTimeInterface::class, $uuid->getDateTime()); $this->assertSame(Variant::Rfc4122, $uuid->getFields()->getVariant()); - $this->assertSame(6, $uuid->getFields()->getVersion()); + $this->assertSame(Version::ReorderedTime, $uuid->getFields()->getVersion()); $this->assertSame('1669', $uuid->getFields()->getClockSeq()->toString()); $this->assertSame('0800200c9a66', $uuid->getFields()->getNode()->toString()); $this->assertSame('9669-0800200c9a66', substr($uuid->toString(), 19)); @@ -564,7 +565,7 @@ class UuidTest extends TestCase $this->assertInstanceOf(DateTimeInterface::class, $uuid->getDateTime()); $this->assertSame(Variant::Rfc4122, $uuid->getFields()->getVariant()); - $this->assertSame(6, $uuid->getFields()->getVersion()); + $this->assertSame(Version::ReorderedTime, $uuid->getFields()->getVersion()); $this->assertSame('00007160355e', $uuid->getFields()->getNode()->toString()); } @@ -575,7 +576,7 @@ class UuidTest extends TestCase $this->assertInstanceOf(DateTimeInterface::class, $uuid->getDateTime()); $this->assertSame(Variant::Rfc4122, $uuid->getFields()->getVariant()); - $this->assertSame(6, $uuid->getFields()->getVersion()); + $this->assertSame(Version::ReorderedTime, $uuid->getFields()->getVersion()); $this->assertSame('000071b0ad5e', $uuid->getFields()->getNode()->toString()); } @@ -603,7 +604,7 @@ class UuidTest extends TestCase $uuid = Uuid::uuid6(); $this->assertInstanceOf(DateTimeInterface::class, $uuid->getDateTime()); $this->assertSame(Variant::Rfc4122, $uuid->getFields()->getVariant()); - $this->assertSame(6, $uuid->getFields()->getVersion()); + $this->assertSame(Version::ReorderedTime, $uuid->getFields()->getVersion()); } public function testUuid6WithUserGeneratedRandomNode(): void @@ -612,7 +613,7 @@ class UuidTest extends TestCase $uuid = Uuid::uuid6(new Hexadecimal((string) (new RandomNodeProvider())->getNode())); $this->assertInstanceOf(DateTimeInterface::class, $uuid->getDateTime()); $this->assertSame(Variant::Rfc4122, $uuid->getFields()->getVariant()); - $this->assertSame(6, $uuid->getFields()->getVersion()); + $this->assertSame(Version::ReorderedTime, $uuid->getFields()->getVersion()); } /** @@ -635,7 +636,7 @@ class UuidTest extends TestCase $uobj2 = Uuid::uuid3(Uuid::fromString($ns), $name); $this->assertSame(Variant::Rfc4122, $uobj1->getFields()->getVariant()); - $this->assertSame(3, $uobj1->getFields()->getVersion()); + $this->assertSame(Version::HashMd5, $uobj1->getFields()->getVersion()); $this->assertSame(Uuid::fromString($uuid)->toString(), $uobj1->toString()); $this->assertTrue($uobj1->equals($uobj2)); } @@ -674,7 +675,7 @@ class UuidTest extends TestCase /** @var \Ramsey\Uuid\Rfc4122\UuidInterface $uuid */ $uuid = Uuid::uuid4(); $this->assertSame(Variant::Rfc4122, $uuid->getFields()->getVariant()); - $this->assertSame(4, $uuid->getFields()->getVersion()); + $this->assertSame(Version::Random, $uuid->getFields()->getVersion()); } /** @@ -753,7 +754,7 @@ class UuidTest extends TestCase /** @var \Ramsey\Uuid\Rfc4122\UuidInterface $uuid */ $uuid = $factory->uuid4(); - $this->assertSame(4, $uuid->getFields()->getVersion()); + $this->assertSame(Version::Random, $uuid->getFields()->getVersion()); } /** @@ -776,7 +777,7 @@ class UuidTest extends TestCase $uobj2 = Uuid::uuid5(Uuid::fromString($ns), $name); $this->assertSame(Variant::Rfc4122, $uobj1->getFields()->getVariant()); - $this->assertSame(5, $uobj1->getFields()->getVersion()); + $this->assertSame(Version::HashSha1, $uobj1->getFields()->getVersion()); $this->assertSame(Uuid::fromString($uuid)->toString(), $uobj1->toString()); $this->assertTrue($uobj1->equals($uobj2)); } @@ -1130,8 +1131,8 @@ class UuidTest extends TestCase string $urn, string $time, string $clockSeq, - int $variant, - ?int $version + Variant $variant, + ?Version $version ): void { /** @var non-empty-string $base64DecodedBytes */ $base64DecodedBytes = base64_decode($bytes); @@ -1166,7 +1167,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::from($variant), $uuid->getFields()->getVariant()); + $this->assertSame($variant, $uuid->getFields()->getVariant()); $this->assertSame($version, $uuid->getFields()->getVersion()); } } @@ -1198,7 +1199,7 @@ class UuidTest extends TestCase 'urn' => 'urn:uuid:00000000-0000-0000-0000-000000000000', 'time' => '000000000000000', 'clock_seq' => '0000', - 'variant' => 0, + 'variant' => Variant::ReservedNcs, 'version' => null, ], [ @@ -1218,7 +1219,7 @@ class UuidTest extends TestCase 'urn' => 'urn:uuid:00010203-0405-0607-0809-0a0b0c0d0e0f', 'time' => '607040500010203', 'clock_seq' => '0809', - 'variant' => 0, + 'variant' => Variant::ReservedNcs, 'version' => null, ], [ @@ -1238,8 +1239,8 @@ class UuidTest extends TestCase 'urn' => 'urn:uuid:02d9e6d5-9467-382e-8f9b-9300a64ac3cd', 'time' => '82e946702d9e6d5', 'clock_seq' => '0f9b', - 'variant' => 2, - 'version' => Uuid::UUID_TYPE_HASH_MD5, + 'variant' => Variant::Rfc4122, + 'version' => Version::HashMd5, ], [ 'string' => '12345678-1234-5678-1234-567812345678', @@ -1258,7 +1259,7 @@ class UuidTest extends TestCase 'urn' => 'urn:uuid:12345678-1234-5678-1234-567812345678', 'time' => '678123412345678', 'clock_seq' => '1234', - 'variant' => 0, + 'variant' => Variant::ReservedNcs, 'version' => null, ], [ @@ -1278,8 +1279,8 @@ class UuidTest extends TestCase 'urn' => 'urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8', 'time' => '1d19dad6ba7b810', 'clock_seq' => '00b4', - 'variant' => 2, - 'version' => Uuid::UUID_TYPE_TIME, + 'variant' => Variant::Rfc4122, + 'version' => Version::Time, ], [ 'string' => '6ba7b811-9dad-11d1-80b4-00c04fd430c8', @@ -1298,8 +1299,8 @@ class UuidTest extends TestCase 'urn' => 'urn:uuid:6ba7b811-9dad-11d1-80b4-00c04fd430c8', 'time' => '1d19dad6ba7b811', 'clock_seq' => '00b4', - 'variant' => 2, - 'version' => Uuid::UUID_TYPE_TIME, + 'variant' => Variant::Rfc4122, + 'version' => Version::Time, ], [ 'string' => '6ba7b812-9dad-11d1-80b4-00c04fd430c8', @@ -1318,8 +1319,8 @@ class UuidTest extends TestCase 'urn' => 'urn:uuid:6ba7b812-9dad-11d1-80b4-00c04fd430c8', 'time' => '1d19dad6ba7b812', 'clock_seq' => '00b4', - 'variant' => 2, - 'version' => Uuid::UUID_TYPE_TIME, + 'variant' => Variant::Rfc4122, + 'version' => Version::Time, ], [ 'string' => '6ba7b814-9dad-11d1-80b4-00c04fd430c8', @@ -1338,8 +1339,8 @@ class UuidTest extends TestCase 'urn' => 'urn:uuid:6ba7b814-9dad-11d1-80b4-00c04fd430c8', 'time' => '1d19dad6ba7b814', 'clock_seq' => '00b4', - 'variant' => 2, - 'version' => Uuid::UUID_TYPE_TIME, + 'variant' => Variant::Rfc4122, + 'version' => Version::Time, ], [ 'string' => '7d444840-9dc0-11d1-b245-5ffdce74fad2', @@ -1358,8 +1359,8 @@ class UuidTest extends TestCase 'urn' => 'urn:uuid:7d444840-9dc0-11d1-b245-5ffdce74fad2', 'time' => '1d19dc07d444840', 'clock_seq' => '3245', - 'variant' => 2, - 'version' => Uuid::UUID_TYPE_TIME, + 'variant' => Variant::Rfc4122, + 'version' => Version::Time, ], [ 'string' => 'e902893a-9d22-3c7e-a7b8-d6e313b71d9f', @@ -1378,8 +1379,8 @@ class UuidTest extends TestCase 'urn' => 'urn:uuid:e902893a-9d22-3c7e-a7b8-d6e313b71d9f', 'time' => 'c7e9d22e902893a', 'clock_seq' => '27b8', - 'variant' => 2, - 'version' => Uuid::UUID_TYPE_HASH_MD5, + 'variant' => Variant::Rfc4122, + 'version' => Version::HashMd5, ], [ 'string' => 'eb424026-6f54-4ef8-a4d0-bb658a1fc6cf', @@ -1398,8 +1399,8 @@ class UuidTest extends TestCase 'urn' => 'urn:uuid:eb424026-6f54-4ef8-a4d0-bb658a1fc6cf', 'time' => 'ef86f54eb424026', 'clock_seq' => '24d0', - 'variant' => 2, - 'version' => Uuid::UUID_TYPE_RANDOM, + 'variant' => Variant::Rfc4122, + 'version' => Version::Random, ], [ 'string' => 'f81d4fae-7dec-11d0-a765-00a0c91e6bf6', @@ -1418,8 +1419,8 @@ class UuidTest extends TestCase 'urn' => 'urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6', 'time' => '1d07decf81d4fae', 'clock_seq' => '2765', - 'variant' => 2, - 'version' => Uuid::UUID_TYPE_TIME, + 'variant' => Variant::Rfc4122, + 'version' => Version::Time, ], [ 'string' => 'fffefdfc-fffe-fffe-fffe-fffefdfcfbfa', @@ -1438,7 +1439,7 @@ class UuidTest extends TestCase 'urn' => 'urn:uuid:fffefdfc-fffe-fffe-fffe-fffefdfcfbfa', 'time' => 'ffefffefffefdfc', 'clock_seq' => '3ffe', - 'variant' => 7, + 'variant' => Variant::ReservedFuture, 'version' => null, ], [ @@ -1458,7 +1459,7 @@ class UuidTest extends TestCase 'urn' => 'urn:uuid:ffffffff-ffff-ffff-ffff-ffffffffffff', 'time' => 'fffffffffffffff', 'clock_seq' => '3fff', - 'variant' => 7, + 'variant' => Variant::ReservedFuture, 'version' => null, ], ]; @@ -1538,7 +1539,7 @@ class UuidTest extends TestCase { /** @var \Ramsey\Uuid\Rfc4122\UuidInterface $uuid */ $uuid = Uuid::fromString('ff6f8cb0-c57d-11e1-9b21-0800200c9a66'); - $this->assertSame($uuid->getFields()->getVersion(), Uuid::UUID_TYPE_TIME); + $this->assertSame($uuid->getFields()->getVersion(), Version::Time); } /** @@ -1548,7 +1549,7 @@ class UuidTest extends TestCase { /** @var \Ramsey\Uuid\Rfc4122\UuidInterface $uuid */ $uuid = Uuid::fromString('6fa459ea-ee8a-2ca4-894e-db77e160355e'); - $this->assertSame($uuid->getFields()->getVersion(), Uuid::UUID_TYPE_DCE_SECURITY); + $this->assertSame($uuid->getFields()->getVersion(), Version::DceSecurity); } /** @@ -1558,7 +1559,7 @@ class UuidTest extends TestCase { /** @var \Ramsey\Uuid\Rfc4122\UuidInterface $uuid */ $uuid = Uuid::fromString('6fa459ea-ee8a-3ca4-894e-db77e160355e'); - $this->assertSame($uuid->getFields()->getVersion(), Uuid::UUID_TYPE_HASH_MD5); + $this->assertSame($uuid->getFields()->getVersion(), Version::HashMd5); } /** @@ -1568,7 +1569,7 @@ class UuidTest extends TestCase { /** @var \Ramsey\Uuid\Rfc4122\UuidInterface $uuid */ $uuid = Uuid::fromString('6fabf0bc-603a-42f2-925b-d9f779bd0032'); - $this->assertSame($uuid->getFields()->getVersion(), Uuid::UUID_TYPE_RANDOM); + $this->assertSame($uuid->getFields()->getVersion(), Version::Random); } /** @@ -1578,14 +1579,14 @@ class UuidTest extends TestCase { /** @var \Ramsey\Uuid\Rfc4122\UuidInterface $uuid */ $uuid = Uuid::fromString('886313e1-3b8a-5372-9b90-0c9aee199e5d'); - $this->assertSame($uuid->getFields()->getVersion(), Uuid::UUID_TYPE_HASH_SHA1); + $this->assertSame($uuid->getFields()->getVersion(), Version::HashSha1); } public function testUuidVersionConstantForVersion6(): void { /** @var \Ramsey\Uuid\Rfc4122\UuidInterface $uuid */ $uuid = Uuid::fromString('886313e1-3b8a-6372-9b90-0c9aee199e5d'); - $this->assertSame($uuid->getFields()->getVersion(), Uuid::UUID_TYPE_PEABODY); + $this->assertSame($uuid->getFields()->getVersion(), Version::ReorderedTime); } /**