Accept Type\Hexadecimal for the first parameter to uuid1()

This commit is contained in:
Ben Ramsey
2020-02-24 14:30:48 -06:00
parent ab3c89fb8e
commit fec93008bc
7 changed files with 73 additions and 14 deletions
+4
View File
@@ -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
+5
View File
@@ -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) {
+4 -2
View File
@@ -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
+3 -2
View File
@@ -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
+9 -8
View File
@@ -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
+3 -2
View File
@@ -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
+45
View File
@@ -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
*