Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added methods for creating 7 and 8 versions of UUID to the UuidFactor… #505

Open
wants to merge 2 commits into
base: 4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/UuidFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
6 changes: 6 additions & 0 deletions tests/ExpectedBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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'));
Expand Down
27 changes: 0 additions & 27 deletions tests/UuidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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
*
Expand Down