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 029531a

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

7 files changed

+244-91Lines changed: 244 additions & 91 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
+49-64Lines changed: 49 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -18,72 +18,90 @@
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
{
3627
if (null === $uuid) {
37-
$this->uuid = uuid_create(self::TYPE_4);
28+
$this->uuid = uuid_create(static::TYPE);
3829

3930
return;
4031
}
4132

42-
if (!uuid_is_valid($uuid)) {
43-
throw new \InvalidArgumentException(sprintf('Invalid UUID: "%s".', $uuid));
33+
if (static::TYPE !== uuid_type($uuid)) {
34+
throw new \InvalidArgumentException(sprintf('Invalid UUID%s: "%s".', static::TYPE ? 'v'.static::TYPE : '', $uuid));
4435
}
4536

4637
$this->uuid = strtr($uuid, 'ABCDEF', 'abcdef');
4738
}
4839

49-
public static function v1(): self
40+
/**
41+
* @return static
42+
*/
43+
public static function fromBinary(string $bytes): self
5044
{
51-
return new self(uuid_create(self::TYPE_1));
45+
if (!$uuid = uuid_unparse($bytes)) {
46+
throw new \InvalidArgumentException('Invalid binary UUID provided.');
47+
}
48+
49+
return static::fromString($uuid);
5250
}
5351

54-
public static function v3(self $uuidNamespace, string $name): self
52+
/**
53+
* @return static
54+
*/
55+
public static function fromString(string $uuid): self
5556
{
56-
return new self(uuid_generate_md5($uuidNamespace->uuid, $name));
57+
if (__CLASS__ !== static::class) {
58+
return new static($uuid);
59+
}
60+
61+
switch (uuid_type($uuid)) {
62+
case UuidV1::TYPE: return new UuidV1($uuid);
63+
case UuidV3::TYPE: return new UuidV3($uuid);
64+
case UuidV4::TYPE: return new UuidV4($uuid);
65+
case UuidV5::TYPE: return new UuidV5($uuid);
66+
case NullUuid::TYPE: return new NullUuid();
67+
case self::TYPE: return new self($uuid);
68+
}
69+
70+
throw new \InvalidArgumentException(sprintf('Invalid UUID: "%s".', $uuid));
5771
}
5872

59-
public static function v4(): self
73+
public static function v1(string $uuid = null): UuidV1
6074
{
61-
return new self(uuid_create(self::TYPE_4));
75+
return new UuidV1($uuid);
6276
}
6377

64-
public static function v5(self $uuidNamespace, string $name): self
78+
public static function v3(self $namespace, string $name): UuidV3
6579
{
66-
return new self(uuid_generate_sha1($uuidNamespace->uuid, $name));
80+
return new UuidV3(uuid_generate_md5($namespace->uuid, $name));
6781
}
6882

69-
public static function fromBinary(string $uuidAsBinary): self
83+
public static function v4(string $uuid = null): UuidV4
7084
{
71-
return new self(uuid_unparse($uuidAsBinary));
85+
return new UuidV4($uuid);
7286
}
7387

74-
public static function isValid(string $uuid): bool
88+
public static function v5(self $namespace, string $name): UuidV5
7589
{
76-
return uuid_is_valid($uuid);
90+
return new UuidV5(uuid_generate_sha1($namespace->uuid, $name));
7791
}
7892

79-
public function toBinary(): string
93+
public static function isValid(string $uuid): bool
8094
{
81-
return uuid_parse($this->uuid);
95+
if (__CLASS__ === static::class) {
96+
return uuid_is_valid($uuid);
97+
}
98+
99+
return static::TYPE === uuid_type($uuid);
82100
}
83101

84-
public function isNull(): bool
102+
public function toBinary(): string
85103
{
86-
return uuid_is_null($this->uuid);
104+
return uuid_parse($this->uuid);
87105
}
88106

89107
/**
@@ -103,39 +121,6 @@ public function compare(self $other): int
103121
return uuid_compare($this->uuid, $other->uuid);
104122
}
105123

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-
139124
public function __toString(): string
140125
{
141126
return $this->uuid;
Collapse file
+62Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 UuidV1 extends Uuid
20+
{
21+
protected const TYPE = UUID_TYPE_TIME;
22+
23+
// https://tools.ietf.org/html/rfc4122#section-4.1.4
24+
// 0x01b21dd213814000 is the number of 100-ns intervals between the
25+
// UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00.
26+
private const TIME_OFFSET_INT = 0x01b21dd213814000;
27+
private const TIME_OFFSET_COM = "\xfe\x4d\xe2\x2d\xec\x7e\xc0\x00";
28+
29+
public function __construct(string $uuid = null)
30+
{
31+
if (null === $uuid) {
32+
$this->uuid = uuid_create(static::TYPE);
33+
} else {
34+
parent::__construct($uuid);
35+
}
36+
}
37+
38+
public static function fromBinary(string $uuidAsBinary): self
39+
{
40+
return new static(uuid_unparse($uuidAsBinary));
41+
}
42+
43+
public function getTime(): float
44+
{
45+
$time = '0'.substr($this->uuid, 15, 3).substr($this->uuid, 9, 4).substr($this->uuid, 0, 8);
46+
47+
if (\PHP_INT_SIZE >= 8) {
48+
return (hexdec($time) - self::TIME_OFFSET_INT) / 10000000;
49+
}
50+
51+
$time = str_pad(hex2bin($time), 8, "\0", STR_PAD_LEFT);
52+
$time = InternalUtil::binaryAdd($time, self::TIME_OFFSET_COM);
53+
$time[0] = $time[0] & "\x7F";
54+
55+
return InternalUtil::toDecimal($time) / 10000000;
56+
}
57+
58+
public function getMac(): string
59+
{
60+
return uuid_mac($this->uuid);
61+
}
62+
}

0 commit comments

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