diff --git a/CHANGELOG.md b/CHANGELOG.md index faa182f..e7a512c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,7 +43,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. * `Rfc4122\NilUuid` * Add `Rfc4122\UuidBuilder` to build RFC 4122 variant UUIDs. This replaces the existing `Builder\DefaultUuidBuilder`, which is now deprecated. -* Add ability to generate version 2 (DCE Security) UUIDs. +* Add ability to generate version 2 (DCE Security) UUIDs, including the static + method `Uuid::uuid2()`, which returns an `Rfc4122\UuidV2` instance. * Add classes to represent GUIDs and nonstandard (non-RFC 4122 variant) UUIDs: * `Guid\Guid` * `Nonstandard\Uuid`. diff --git a/src/Uuid.php b/src/Uuid.php index 13f19b4..a729da2 100644 --- a/src/Uuid.php +++ b/src/Uuid.php @@ -23,6 +23,8 @@ use Ramsey\Uuid\Exception\DateTimeException; use Ramsey\Uuid\Exception\UnsupportedOperationException; use Ramsey\Uuid\Fields\FieldsInterface; use Ramsey\Uuid\Rfc4122\FieldsInterface as Rfc4122FieldsInterface; +use Ramsey\Uuid\Type\Hexadecimal; +use Ramsey\Uuid\Type\IntegerValue; use Throwable; /** @@ -731,6 +733,34 @@ class Uuid implements UuidInterface return self::getFactory()->uuid1($node, $clockSeq); } + /** + * Returns a version 2 (DCE Security) UUID from a local domain, local + * identifier, host ID, clock sequence, and the current time + * + * @param int $localDomain The local domain to use when generating bytes, + * according to DCE Security + * @param IntegerValue|null $localIdentifier The local identifier for the + * given domain; this may be a UID or GID on POSIX systems, if the local + * domain is person or group, or it may be a site-defined identifier + * if the local domain is org + * @param Hexadecimal|null $node A 48-bit number representing the hardware + * address + * @param int|null $clockSeq A 14-bit number used to help avoid duplicates + * that could arise when the clock is set backwards in time or if the + * node ID changes + * + * @return UuidInterface A UuidInterface instance that represents a + * version 2 UUID + */ + public static function uuid2( + int $localDomain, + ?IntegerValue $localIdentifier = null, + ?Hexadecimal $node = null, + ?int $clockSeq = null + ): UuidInterface { + return self::getFactory()->uuid2($localDomain, $localIdentifier, $node, $clockSeq); + } + /** * Returns a version 3 (name-based) UUID based on the MD5 hash of a * namespace ID and a name diff --git a/src/UuidFactory.php b/src/UuidFactory.php index e5e4164..b5a8f43 100644 --- a/src/UuidFactory.php +++ b/src/UuidFactory.php @@ -247,7 +247,7 @@ class UuidFactory implements UuidFactoryInterface public function uuid2( int $localDomain, - ?IntegerValue $localIdentifier, + ?IntegerValue $localIdentifier = null, ?Hexadecimal $node = null, ?int $clockSeq = null ): UuidInterface { diff --git a/src/UuidFactoryInterface.php b/src/UuidFactoryInterface.php index 88a308c..88339f9 100644 --- a/src/UuidFactoryInterface.php +++ b/src/UuidFactoryInterface.php @@ -61,10 +61,13 @@ interface UuidFactoryInterface * @param int|null $clockSeq A 14-bit number used to help avoid duplicates * that could arise when the clock is set backwards in time or if the * node ID changes + * + * @return UuidInterface A UuidInterface instance that represents a + * version 2 UUID */ public function uuid2( int $localDomain, - ?IntegerValue $localIdentifier, + ?IntegerValue $localIdentifier = null, ?Hexadecimal $node = null, ?int $clockSeq = null ): UuidInterface; diff --git a/tests/UuidTest.php b/tests/UuidTest.php index 8cf3b57..c0278bd 100644 --- a/tests/UuidTest.php +++ b/tests/UuidTest.php @@ -29,6 +29,7 @@ use Ramsey\Uuid\Provider\Time\FixedTimeProvider; use Ramsey\Uuid\Rfc4122\FieldsInterface; use Ramsey\Uuid\Rfc4122\NilUuid; 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; @@ -1474,6 +1475,7 @@ class UuidTest extends TestCase { return [ [UuidV1::class, 'uuid1'], + [UuidV2::class, 'uuid2', [Uuid::DCE_DOMAIN_PERSON]], [UuidV3::class, 'uuid3', [Uuid::NIL, 'foobar']], [UuidV4::class, 'uuid4'], [UuidV5::class, 'uuid5', [Uuid::NIL, 'foobar']],