From 73eca65e69cc2f21fed510ef5ca85f722eabcd28 Mon Sep 17 00:00:00 2001 From: Ilya Plotnikov Date: Thu, 4 May 2023 20:06:23 +0600 Subject: [PATCH 1/2] Added methods for creating 7 and 8 versions of UUID to the UuidFactoryInterface --- src/UuidFactoryInterface.php | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/UuidFactoryInterface.php b/src/UuidFactoryInterface.php index d99fc9d5..cf7e1cc5 100644 --- a/src/UuidFactoryInterface.php +++ b/src/UuidFactoryInterface.php @@ -179,4 +179,34 @@ public function uuid5($ns, string $name): UuidInterface; * version 6 UUID */ public function uuid6(?Hexadecimal $node = null, ?int $clockSeq = null): UuidInterface; + + /** + * Returns a version 7 (Unix Epoch time) UUID + * + * @param DateTimeInterface|null $dateTime An optional date/time from which + * to create the version 7 UUID. If not provided, the UUID is generated + * using the current date/time. + * + * @return UuidInterface A UuidInterface instance that represents a + * version 7 UUID + */ + public function uuid7(?DateTimeInterface $dateTime = null): UuidInterface; + + /** + * Returns a version 8 (Custom) UUID + * + * The bytes provided may contain any value according to your application's + * needs. Be aware, however, that other applications may not understand the + * semantics of the value. + * + * @param string $bytes A 16-byte octet string. This is an open blob + * of data that you may fill with 128 bits of information. Be aware, + * however, bits 48 through 51 will be replaced with the UUID version + * field, and bits 64 and 65 will be replaced with the UUID variant. You + * MUST NOT rely on these bits for your application needs. + * + * @return UuidInterface A UuidInterface instance that represents a + * version 8 UUID + */ + public function uuid8(string $bytes): UuidInterface; } From 0f15cb8e41043c377dfd21cbc5f36fdfedb0eb20 Mon Sep 17 00:00:00 2001 From: Ilya Plotnikov Date: Thu, 4 May 2023 23:06:41 +0600 Subject: [PATCH 2/2] Modify tests --- tests/ExpectedBehaviorTest.php | 6 ++++++ tests/UuidTest.php | 27 --------------------------- 2 files changed, 6 insertions(+), 27 deletions(-) diff --git a/tests/ExpectedBehaviorTest.php b/tests/ExpectedBehaviorTest.php index 804239c2..11f78229 100644 --- a/tests/ExpectedBehaviorTest.php +++ b/tests/ExpectedBehaviorTest.php @@ -444,6 +444,9 @@ public function testFactoryProvidesFunctionality() 'uuid3' => $uuid, 'uuid4' => $uuid, 'uuid5' => $uuid, + 'uuid6' => $uuid, + 'uuid7' => $uuid, + 'uuid8' => $uuid, 'fromBytes' => $uuid, 'fromString' => $uuid, 'fromInteger' => $uuid, @@ -455,6 +458,9 @@ public function testFactoryProvidesFunctionality() $this->assertSame($uuid, Uuid::uuid3(Uuid::NAMESPACE_URL, 'https://example.com/foo')); $this->assertSame($uuid, Uuid::uuid4()); $this->assertSame($uuid, Uuid::uuid5(Uuid::NAMESPACE_URL, 'https://example.com/foo')); + $this->assertSame($uuid, Uuid::uuid6()); + $this->assertSame($uuid, Uuid::uuid7()); + $this->assertSame($uuid, Uuid::uuid8(hex2bin('ffffffffffffffffffffffffffffffff'))); $this->assertSame($uuid, Uuid::fromBytes(hex2bin('ffffffffffffffffffffffffffffffff'))); $this->assertSame($uuid, Uuid::fromString('ffffffff-ffff-ffff-ffff-ffffffffffff')); $this->assertSame($uuid, Uuid::fromInteger('340282366920938463463374607431768211455')); diff --git a/tests/UuidTest.php b/tests/UuidTest.php index feea5a04..28688739 100644 --- a/tests/UuidTest.php +++ b/tests/UuidTest.php @@ -10,7 +10,6 @@ use DateTimeImmutable; use DateTimeInterface; use Mockery; -use Mockery\MockInterface; use PHPUnit\Framework\MockObject\MockObject; use Ramsey\Uuid\Builder\DefaultUuidBuilder; use Ramsey\Uuid\Codec\StringCodec; @@ -782,19 +781,6 @@ public function testUuid7(): void $this->assertSame(7, $uuid->getVersion()); } - public function testUuid7ThrowsExceptionForUnsupportedFactory(): void - { - /** @var UuidFactoryInterface&MockInterface $factory */ - $factory = Mockery::mock(UuidFactoryInterface::class); - - Uuid::setFactory($factory); - - $this->expectException(UnsupportedOperationException::class); - $this->expectExceptionMessage('The provided factory does not support the uuid7() method'); - - Uuid::uuid7(); - } - public function testUuid7WithDateTime(): void { $dateTime = new DateTimeImmutable('@281474976710.655'); @@ -875,19 +861,6 @@ public function testUuid8(): void $this->assertSame(8, $uuid->getVersion()); } - public function testUuid8ThrowsExceptionForUnsupportedFactory(): void - { - /** @var UuidFactoryInterface&MockInterface $factory */ - $factory = Mockery::mock(UuidFactoryInterface::class); - - Uuid::setFactory($factory); - - $this->expectException(UnsupportedOperationException::class); - $this->expectExceptionMessage('The provided factory does not support the uuid8() method'); - - Uuid::uuid8("\x00\x11\x22\x33\x44\x55\x66\x77\x88\x99\xaa\xbb\xcc\xdd\xee\xff"); - } - /** * Tests known version-3 UUIDs *