Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c5bf578

Browse filesBrowse files
[Uid] use one class per type of UUID
1 parent 49efe9a commit c5bf578
Copy full SHA for c5bf578

7 files changed

+249-95Lines changed: 249 additions & 95 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file
+27Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Uid;
13+
14+
/**
15+
* @experimental in 5.1
16+
*
17+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
18+
*/
19+
class NullUuid extends Uuid
20+
{
21+
protected const TYPE = UUID_TYPE_NULL;
22+
23+
public function __construct()
24+
{
25+
$this->uuid = '00000000-0000-0000-0000-000000000000';
26+
}
27+
}
Collapse file

‎src/Symfony/Component/Uid/Tests/UuidTest.php‎

Copy file name to clipboardExpand all lines: src/Symfony/Component/Uid/Tests/UuidTest.php
+31-27Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@
1212
namespace Symfony\Tests\Component\Uid;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Uid\NullUuid;
1516
use Symfony\Component\Uid\Uuid;
17+
use Symfony\Component\Uid\UuidV1;
18+
use Symfony\Component\Uid\UuidV3;
19+
use Symfony\Component\Uid\UuidV4;
20+
use Symfony\Component\Uid\UuidV5;
1621

1722
class UuidTest extends TestCase
1823
{
@@ -24,12 +29,12 @@ public function testConstructorWithInvalidUuid()
2429
$this->expectException(\InvalidArgumentException::class);
2530
$this->expectExceptionMessage('Invalid UUID: "this is not a uuid".');
2631

27-
new Uuid('this is not a uuid');
32+
Uuid::fromString('this is not a uuid');
2833
}
2934

3035
public function testConstructorWithValidUuid()
3136
{
32-
$uuid = new Uuid(self::A_UUID_V4);
37+
$uuid = Uuid::v4(self::A_UUID_V4);
3338

3439
$this->assertSame(self::A_UUID_V4, (string) $uuid);
3540
$this->assertSame('"'.self::A_UUID_V4.'"', json_encode($uuid));
@@ -39,56 +44,56 @@ public function testV1()
3944
{
4045
$uuid = Uuid::v1();
4146

42-
$this->assertSame(Uuid::TYPE_1, $uuid->getType());
47+
$this->assertInstanceOf(UuidV1::class, $uuid);
48+
49+
$uuid = new UuidV1(self::A_UUID_V1);
50+
51+
$this->assertSame(1583245966.746458, $uuid->getTime());
52+
$this->assertSame('3499710062d0', $uuid->getMac());
4353
}
4454

4555
public function testV3()
4656
{
47-
$uuid = Uuid::v3(new Uuid(self::A_UUID_V4), 'the name');
57+
$uuid = Uuid::v3(new UuidV4(self::A_UUID_V4), 'the name');
4858

49-
$this->assertSame(Uuid::TYPE_3, $uuid->getType());
59+
$this->assertInstanceOf(UuidV3::class, $uuid);
5060
}
5161

5262
public function testV4()
5363
{
5464
$uuid = Uuid::v4();
5565

56-
$this->assertSame(Uuid::TYPE_4, $uuid->getType());
66+
$this->assertInstanceOf(UuidV4::class, $uuid);
5767
}
5868

5969
public function testV5()
6070
{
61-
$uuid = Uuid::v5(new Uuid(self::A_UUID_V4), 'the name');
71+
$uuid = Uuid::v5(new UuidV4(self::A_UUID_V4), 'the name');
6272

63-
$this->assertSame(Uuid::TYPE_5, $uuid->getType());
73+
$this->assertInstanceOf(UuidV5::class, $uuid);
6474
}
6575

6676
public function testBinary()
6777
{
68-
$uuid = new Uuid(self::A_UUID_V4);
78+
$uuid = Uuid::v4(self::A_UUID_V4);
79+
$uuid = Uuid::fromBinary($uuid->toBinary());
6980

70-
$this->assertSame(self::A_UUID_V4, (string) Uuid::fromBinary($uuid->toBinary()));
81+
$this->assertInstanceOf(UuidV4::class, $uuid);
82+
$this->assertSame(self::A_UUID_V4, (string) $uuid);
7183
}
7284

7385
public function testIsValid()
7486
{
7587
$this->assertFalse(Uuid::isValid('not a uuid'));
7688
$this->assertTrue(Uuid::isValid(self::A_UUID_V4));
77-
}
78-
79-
public function testIsNull()
80-
{
81-
$uuid = new Uuid(self::A_UUID_V1);
82-
$this->assertFalse($uuid->isNull());
83-
84-
$uuid = new Uuid('00000000-0000-0000-0000-000000000000');
85-
$this->assertTrue($uuid->isNull());
89+
$this->assertFalse(UuidV4::isValid(self::A_UUID_V1));
90+
$this->assertTrue(UuidV4::isValid(self::A_UUID_V4));
8691
}
8792

8893
public function testEquals()
8994
{
90-
$uuid1 = new Uuid(self::A_UUID_V1);
91-
$uuid2 = new Uuid(self::A_UUID_V4);
95+
$uuid1 = new UuidV1(self::A_UUID_V1);
96+
$uuid2 = new UuidV4(self::A_UUID_V4);
9297

9398
$this->assertTrue($uuid1->equals($uuid1));
9499
$this->assertFalse($uuid1->equals($uuid2));
@@ -99,7 +104,7 @@ public function testEquals()
99104
*/
100105
public function testEqualsAgainstOtherType($other)
101106
{
102-
$this->assertFalse((new Uuid(self::A_UUID_V4))->equals($other));
107+
$this->assertFalse((new UuidV4(self::A_UUID_V4))->equals($other));
103108
}
104109

105110
public function provideInvalidEqualType()
@@ -128,12 +133,11 @@ public function testCompare()
128133
$this->assertSame([$a, $b, $c, $d], $uuids);
129134
}
130135

131-
public function testExtraMethods()
136+
public function testNullUuid()
132137
{
133-
$uuid = new Uuid(self::A_UUID_V1);
138+
$uuid = Uuid::fromString('00000000-0000-0000-0000-000000000000');
134139

135-
$this->assertSame(1583245966.746458, $uuid->getTime());
136-
$this->assertSame('3499710062d0', $uuid->getMac());
137-
$this->assertSame(self::A_UUID_V1, (string) $uuid);
140+
$this->assertInstanceOf(NullUuid::class, $uuid);
141+
$this->assertSame('00000000-0000-0000-0000-000000000000', (string) $uuid);
138142
}
139143
}
Collapse file

‎src/Symfony/Component/Uid/Uuid.php‎

Copy file name to clipboardExpand all lines: src/Symfony/Component/Uid/Uuid.php
+47-68Lines changed: 47 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -18,74 +18,86 @@
1818
*/
1919
class Uuid implements \JsonSerializable
2020
{
21-
public const TYPE_1 = UUID_TYPE_TIME;
22-
public const TYPE_3 = UUID_TYPE_MD5;
23-
public const TYPE_4 = UUID_TYPE_RANDOM;
24-
public const TYPE_5 = UUID_TYPE_SHA1;
21+
protected const TYPE = UUID_TYPE_DEFAULT;
2522

26-
// https://tools.ietf.org/html/rfc4122#section-4.1.4
27-
// 0x01b21dd213814000 is the number of 100-ns intervals between the
28-
// UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00.
29-
private const TIME_OFFSET_INT = 0x01b21dd213814000;
30-
private const TIME_OFFSET_COM = "\xfe\x4d\xe2\x2d\xec\x7e\xc0\x00";
23+
protected $uuid;
3124

32-
private $uuid;
33-
34-
public function __construct(string $uuid = null)
25+
public function __construct(string $uuid)
3526
{
36-
if (null === $uuid) {
37-
$this->uuid = uuid_create(self::TYPE_4);
38-
39-
return;
27+
if (static::TYPE !== uuid_type($uuid)) {
28+
throw new \InvalidArgumentException(sprintf('Invalid UUID%s: "%s".', static::TYPE ? 'v'.static::TYPE : '', $uuid));
4029
}
4130

42-
if (!uuid_is_valid($uuid)) {
43-
throw new \InvalidArgumentException(sprintf('Invalid UUID: "%s".', $uuid));
31+
$this->uuid = strtr($uuid, 'ABCDEF', 'abcdef');
32+
}
33+
34+
/**
35+
* @return static
36+
*/
37+
public static function fromBinary(string $bytes): self
38+
{
39+
if (!$uuid = uuid_unparse($bytes)) {
40+
throw new \InvalidArgumentException('Invalid binary UUID provided.');
4441
}
4542

46-
$this->uuid = strtr($uuid, 'ABCDEF', 'abcdef');
43+
return static::fromString($uuid);
4744
}
4845

49-
public static function v1(): self
46+
/**
47+
* @return static
48+
*/
49+
public static function fromString(string $uuid): self
5050
{
51-
return new self(uuid_create(self::TYPE_1));
51+
if (__CLASS__ !== static::class) {
52+
return new static($uuid);
53+
}
54+
55+
switch (uuid_type($uuid)) {
56+
case UuidV1::TYPE: return new UuidV1($uuid);
57+
case UuidV3::TYPE: return new UuidV3($uuid);
58+
case UuidV4::TYPE: return new UuidV4($uuid);
59+
case UuidV5::TYPE: return new UuidV5($uuid);
60+
case NullUuid::TYPE: return new NullUuid();
61+
case self::TYPE: return new self($uuid);
62+
}
63+
64+
throw new \InvalidArgumentException(sprintf('Invalid UUID: "%s".', $uuid));
5265
}
5366

54-
public static function v3(self $uuidNamespace, string $name): self
67+
public static function v1(string $uuid = null): UuidV1
5568
{
56-
return new self(uuid_generate_md5($uuidNamespace->uuid, $name));
69+
return new UuidV1($uuid);
5770
}
5871

59-
public static function v4(): self
72+
public static function v3(self $namespace, string $name): UuidV3
6073
{
61-
return new self(uuid_create(self::TYPE_4));
74+
return new UuidV3(uuid_generate_md5($namespace->uuid, $name));
6275
}
6376

64-
public static function v5(self $uuidNamespace, string $name): self
77+
public static function v4(string $uuid = null): UuidV4
6578
{
66-
return new self(uuid_generate_sha1($uuidNamespace->uuid, $name));
79+
return new UuidV4($uuid);
6780
}
6881

69-
public static function fromBinary(string $uuidAsBinary): self
82+
public static function v5(self $namespace, string $name): UuidV5
7083
{
71-
return new self(uuid_unparse($uuidAsBinary));
84+
return new UuidV5(uuid_generate_sha1($namespace->uuid, $name));
7285
}
7386

7487
public static function isValid(string $uuid): bool
7588
{
76-
return uuid_is_valid($uuid);
89+
if (__CLASS__ === static::class) {
90+
return uuid_is_valid($uuid);
91+
}
92+
93+
return static::TYPE === uuid_type($uuid);
7794
}
7895

7996
public function toBinary(): string
8097
{
8198
return uuid_parse($this->uuid);
8299
}
83100

84-
public function isNull(): bool
85-
{
86-
return uuid_is_null($this->uuid);
87-
}
88-
89101
/**
90102
* Returns whether the argument is of class Uuid and contains the same value as the current instance.
91103
*/
@@ -103,39 +115,6 @@ public function compare(self $other): int
103115
return uuid_compare($this->uuid, $other->uuid);
104116
}
105117

106-
public function getType(): int
107-
{
108-
return uuid_type($this->uuid);
109-
}
110-
111-
public function getTime(): float
112-
{
113-
if (self::TYPE_1 !== $t = uuid_type($this->uuid)) {
114-
throw new \LogicException("UUID of type $t doesn't contain a time.");
115-
}
116-
117-
$time = '0'.substr($this->uuid, 15, 3).substr($this->uuid, 9, 4).substr($this->uuid, 0, 8);
118-
119-
if (\PHP_INT_SIZE >= 8) {
120-
return (hexdec($time) - self::TIME_OFFSET_INT) / 10000000;
121-
}
122-
123-
$time = str_pad(hex2bin($time), 8, "\0", STR_PAD_LEFT);
124-
$time = InternalUtil::binaryAdd($time, self::TIME_OFFSET_COM);
125-
$time[0] = $time[0] & "\x7F";
126-
127-
return InternalUtil::toDecimal($time) / 10000000;
128-
}
129-
130-
public function getMac(): string
131-
{
132-
if (self::TYPE_1 !== $t = uuid_type($this->uuid)) {
133-
throw new \LogicException("UUID of type $t doesn't contain a MAC.");
134-
}
135-
136-
return uuid_mac($this->uuid);
137-
}
138-
139118
public function __toString(): string
140119
{
141120
return $this->uuid;
Collapse file
+59Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Uid;
13+
14+
/**
15+
* A v1 UUID contains a 60-bit timestamp and ~60 extra unique bits.
16+
*
17+
* @experimental in 5.1
18+
*
19+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
20+
*/
21+
class UuidV1 extends Uuid
22+
{
23+
protected const TYPE = UUID_TYPE_TIME;
24+
25+
// https://tools.ietf.org/html/rfc4122#section-4.1.4
26+
// 0x01b21dd213814000 is the number of 100-ns intervals between the
27+
// UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00.
28+
private const TIME_OFFSET_INT = 0x01b21dd213814000;
29+
private const TIME_OFFSET_COM = "\xfe\x4d\xe2\x2d\xec\x7e\xc0\x00";
30+
31+
public function __construct(string $uuid = null)
32+
{
33+
if (null === $uuid) {
34+
$this->uuid = uuid_create(static::TYPE);
35+
} else {
36+
parent::__construct($uuid);
37+
}
38+
}
39+
40+
public function getTime(): float
41+
{
42+
$time = '0'.substr($this->uuid, 15, 3).substr($this->uuid, 9, 4).substr($this->uuid, 0, 8);
43+
44+
if (\PHP_INT_SIZE >= 8) {
45+
return (hexdec($time) - self::TIME_OFFSET_INT) / 10000000;
46+
}
47+
48+
$time = str_pad(hex2bin($time), 8, "\0", STR_PAD_LEFT);
49+
$time = InternalUtil::binaryAdd($time, self::TIME_OFFSET_COM);
50+
$time[0] = $time[0] & "\x7F";
51+
52+
return InternalUtil::toDecimal($time) / 10000000;
53+
}
54+
55+
public function getMac(): string
56+
{
57+
return uuid_mac($this->uuid);
58+
}
59+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.