From fec93008bc7fa8b56e10654918d2c85b420093bb Mon Sep 17 00:00:00 2001 From: Ben Ramsey Date: Mon, 24 Feb 2020 14:30:48 -0600 Subject: [PATCH] Accept Type\Hexadecimal for the first parameter to uuid1() --- CHANGELOG.md | 4 +++ src/Generator/DefaultTimeGenerator.php | 5 +++ src/Generator/TimeGeneratorInterface.php | 6 ++-- src/Uuid.php | 5 +-- src/UuidFactoryInterface.php | 17 ++++----- src/functions.php | 5 +-- tests/UuidTest.php | 45 ++++++++++++++++++++++++ 7 files changed, 73 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 79fdcbf..855d3a1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. method instead. * Change the `$node` parameter for `UuidFactoryInterface::uuid6()` to accept `null` or `Type\Hexadecimal`. +* Accept `Type\Hexadecimal` for the `$node` parameter for + `UuidFactoryInterface::uuid1()`. This is in addition to the `int|string` types + already accepted, so there are no BC breaks. `Type\Hexadecimal` is now the + recommended type to pass for `$node`. ### Deprecated diff --git a/src/Generator/DefaultTimeGenerator.php b/src/Generator/DefaultTimeGenerator.php index 3feea5c..79989f0 100644 --- a/src/Generator/DefaultTimeGenerator.php +++ b/src/Generator/DefaultTimeGenerator.php @@ -20,6 +20,7 @@ use Ramsey\Uuid\Exception\RandomSourceException; use Ramsey\Uuid\Exception\TimeSourceException; use Ramsey\Uuid\Provider\NodeProviderInterface; use Ramsey\Uuid\Provider\TimeProviderInterface; +use Ramsey\Uuid\Type\Hexadecimal; use Throwable; use function ctype_xdigit; @@ -72,6 +73,10 @@ class DefaultTimeGenerator implements TimeGeneratorInterface */ public function generate($node = null, ?int $clockSeq = null): string { + if ($node instanceof Hexadecimal) { + $node = $node->toString(); + } + $node = $this->getValidNode($node); if ($clockSeq === null) { diff --git a/src/Generator/TimeGeneratorInterface.php b/src/Generator/TimeGeneratorInterface.php index 7abd8d8..18f21c4 100644 --- a/src/Generator/TimeGeneratorInterface.php +++ b/src/Generator/TimeGeneratorInterface.php @@ -14,6 +14,8 @@ declare(strict_types=1); namespace Ramsey\Uuid\Generator; +use Ramsey\Uuid\Type\Hexadecimal; + /** * A time generator generates strings of binary data based on a node ID, * clock sequence, and the current time @@ -23,8 +25,8 @@ interface TimeGeneratorInterface /** * Generate a binary string from a node ID, clock sequence, and current time * - * @param int|string|null $node A 48-bit number representing the hardware - * address; this number may be represented as an integer or a + * @param Hexadecimal|int|string|null $node A 48-bit number representing the + * hardware address; this number may be represented as an integer or a * hexadecimal string * @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 diff --git a/src/Uuid.php b/src/Uuid.php index a0d1dd1..68b4ff6 100644 --- a/src/Uuid.php +++ b/src/Uuid.php @@ -458,8 +458,9 @@ class Uuid implements UuidInterface * Returns a version 1 (time-based) UUID from a host ID, sequence number, * and the current time * - * @param int|string $node A 48-bit number representing the hardware address; - * this number may be represented as an integer or a hexadecimal string + * @param Hexadecimal|int|string|null $node A 48-bit number representing the + * hardware address; this number may be represented as an integer or a + * hexadecimal string * @param int $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 diff --git a/src/UuidFactoryInterface.php b/src/UuidFactoryInterface.php index ec2ddb8..d0b1045 100644 --- a/src/UuidFactoryInterface.php +++ b/src/UuidFactoryInterface.php @@ -36,11 +36,12 @@ interface UuidFactoryInterface * Returns a version 1 (time-based) UUID from a host ID, sequence number, * and the current time * - * @param int|string $node A 48-bit number representing the hardware address; - * this number may be represented as an integer or a hexadecimal string - * @param int $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 + * @param Hexadecimal|int|string|null $node A 48-bit number representing the + * hardware address; this number may be represented as an integer or a + * hexadecimal string + * @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 1 UUID @@ -111,9 +112,9 @@ interface UuidFactoryInterface * * @param Hexadecimal|null $node A 48-bit number representing the hardware * address - * @param int $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 + * @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 6 UUID diff --git a/src/functions.php b/src/functions.php index b148447..7b29ec4 100644 --- a/src/functions.php +++ b/src/functions.php @@ -22,8 +22,9 @@ use Ramsey\Uuid\Type\Integer as IntegerObject; * Returns a version 1 (time-based) UUID from a host ID, sequence number, * and the current time * - * @param int|string $node A 48-bit number representing the hardware address; - * this number may be represented as an integer or a hexadecimal string + * @param Hexadecimal|int|string|null $node A 48-bit number representing the + * hardware address; this number may be represented as an integer or a + * hexadecimal string * @param int $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 diff --git a/tests/UuidTest.php b/tests/UuidTest.php index 2a6d3b2..67b367d 100644 --- a/tests/UuidTest.php +++ b/tests/UuidTest.php @@ -26,6 +26,7 @@ use Ramsey\Uuid\Generator\RandomGeneratorInterface; use Ramsey\Uuid\Guid\Guid; use Ramsey\Uuid\Nonstandard\Uuid as NonstandardUuid; use Ramsey\Uuid\Nonstandard\UuidV6; +use Ramsey\Uuid\Provider\Node\RandomNodeProvider; use Ramsey\Uuid\Provider\Time\FixedTimeProvider; use Ramsey\Uuid\Rfc4122\FieldsInterface; use Ramsey\Uuid\Rfc4122\NilUuid; @@ -481,6 +482,19 @@ class UuidTest extends TestCase $this->assertEquals('9669-0800200c9a66', substr($uuid->toString(), 19)); } + public function testUuid1WithHexadecimalObjectNodeAndClockSequence(): void + { + /** @var Uuid $uuid */ + $uuid = Uuid::uuid1(new Hexadecimal('0800200c9a66'), 0x1669); + $this->assertInstanceOf(Uuid::class, $uuid); + $this->assertInstanceOf(DateTimeInterface::class, $uuid->getDateTime()); + $this->assertEquals(2, $uuid->getVariant()); + $this->assertEquals(1, $uuid->getVersion()); + $this->assertEquals(5737, $uuid->getClockSequence()); + $this->assertSame('8796630719078', $uuid->getNode()); + $this->assertEquals('9669-0800200c9a66', substr($uuid->toString(), 19)); + } + public function testUuid1WithHexadecimalNode(): void { /** @var Uuid $uuid */ @@ -494,6 +508,19 @@ class UuidTest extends TestCase $this->assertSame('1902130526', $uuid->getNode()); } + public function testUuid1WithHexadecimalObjectNode(): void + { + /** @var Uuid $uuid */ + $uuid = Uuid::uuid1(new Hexadecimal('7160355e')); + + $this->assertInstanceOf(Uuid::class, $uuid); + $this->assertInstanceOf(DateTimeInterface::class, $uuid->getDateTime()); + $this->assertEquals(2, $uuid->getVariant()); + $this->assertEquals(1, $uuid->getVersion()); + $this->assertEquals('00007160355e', $uuid->getNodeHex()); + $this->assertSame('1902130526', $uuid->getNode()); + } + public function testUuid1WithMixedCaseHexadecimalNode(): void { /** @var Uuid $uuid */ @@ -542,6 +569,15 @@ class UuidTest extends TestCase $this->assertEquals(1, $uuid->getVersion()); } + public function testUuid1WithUserGeneratedRandomNode(): void + { + $uuid = Uuid::uuid1(new Hexadecimal((string) (new RandomNodeProvider())->getNode())); + $this->assertInstanceOf(Uuid::class, $uuid); + $this->assertInstanceOf(DateTimeInterface::class, $uuid->getDateTime()); + $this->assertEquals(2, $uuid->getVariant()); + $this->assertEquals(1, $uuid->getVersion()); + } + public function testUuid6(): void { $uuid = Uuid::uuid6(); @@ -612,6 +648,15 @@ class UuidTest extends TestCase $this->assertEquals(6, $uuid->getVersion()); } + public function testUuid6WithUserGeneratedRandomNode(): void + { + $uuid = Uuid::uuid6(new Hexadecimal((string) (new RandomNodeProvider())->getNode())); + $this->assertInstanceOf(UuidV6::class, $uuid); + $this->assertInstanceOf(DateTimeInterface::class, $uuid->getDateTime()); + $this->assertEquals(2, $uuid->getVariant()); + $this->assertEquals(6, $uuid->getVersion()); + } + /** * Tests known version-3 UUIDs *