diff --git a/src/functions.php b/src/functions.php index 735eb0b..3c78651 100644 --- a/src/functions.php +++ b/src/functions.php @@ -15,6 +15,9 @@ declare(strict_types=1); namespace Ramsey\Uuid; +use Ramsey\Uuid\Type\Hexadecimal; +use Ramsey\Uuid\Type\IntegerValue; + /** * Returns a version 1 (time-based) UUID from a host ID, sequence number, * and the current time @@ -30,6 +33,31 @@ function v1($node = null, ?int $clockSeq = null): string return Uuid::uuid1($node, $clockSeq)->toString(); } +/** + * 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 + */ +function v2( + int $localDomain, + ?IntegerValue $localIdentifier = null, + ?Hexadecimal $node = null, + ?int $clockSeq = null +): string { + return Uuid::uuid2($localDomain, $localIdentifier, $node, $clockSeq)->toString(); +} + /** * Returns a version 3 (name-based) UUID based on the MD5 hash of a * namespace ID and a name diff --git a/tests/FunctionsTest.php b/tests/FunctionsTest.php index 2764dbb..4babdde 100644 --- a/tests/FunctionsTest.php +++ b/tests/FunctionsTest.php @@ -4,9 +4,13 @@ declare(strict_types=1); namespace Ramsey\Uuid\Test; +use Ramsey\Uuid\Rfc4122\FieldsInterface; +use Ramsey\Uuid\Type\Hexadecimal; +use Ramsey\Uuid\Type\IntegerValue; use Ramsey\Uuid\Uuid; use function Ramsey\Uuid\v1; +use function Ramsey\Uuid\v2; use function Ramsey\Uuid\v3; use function Ramsey\Uuid\v4; use function Ramsey\Uuid\v5; @@ -21,6 +25,22 @@ class FunctionsTest extends TestCase $this->assertSame(Uuid::UUID_TYPE_TIME, Uuid::fromString($v1)->getVersion()); } + public function testV2ReturnsVersion2UuidString(): void + { + $v2 = v2( + Uuid::DCE_DOMAIN_PERSON, + new IntegerValue('1004'), + new Hexadecimal('aabbccdd0011'), + 1234 + ); + + /** @var FieldsInterface $fields */ + $fields = Uuid::fromString($v2)->getFields(); + + $this->assertIsString($v2); + $this->assertSame(Uuid::UUID_TYPE_IDENTIFIER, $fields->getVersion()); + } + public function testV3ReturnsVersion3UuidString(): void { $ns = Uuid::fromString(Uuid::NAMESPACE_URL);