Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commit5f6a725

Browse files
[Uid] use one class per type of UUID
1 parent49efe9a commit5f6a725

File tree

7 files changed

+239
-91
lines changed

7 files changed

+239
-91
lines changed
Lines 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+
namespaceSymfony\Component\Uid;
13+
14+
/**
15+
* @experimental in 5.1
16+
*
17+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
18+
*/
19+
class NullUuidextends Uuid
20+
{
21+
protectedconstTYPE =UUID_TYPE_NULL;
22+
23+
publicfunction__construct()
24+
{
25+
$this->uuid ='00000000-0000-0000-0000-000000000000';
26+
}
27+
}

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

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@
1212
namespaceSymfony\Tests\Component\Uid;
1313

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

1722
class UuidTestextends 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-
newUuid('this is not a uuid');
32+
Uuid::fromString('this is not a uuid');
2833
}
2934

3035
publicfunctiontestConstructorWithValidUuid()
3136
{
32-
$uuid =newUuid(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 =newUuidV1(self::A_UUID_V1);
50+
51+
$this->assertSame(1583245966.746458,$uuid->getTime());
52+
$this->assertSame('3499710062d0',$uuid->getMac());
4353
}
4454

4555
publicfunctiontestV3()
4656
{
47-
$uuid = Uuid::v3(newUuid(self::A_UUID_V4),'the name');
57+
$uuid = Uuid::v3(newUuidV4(self::A_UUID_V4),'the name');
4858

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

5262
publicfunctiontestV4()
5363
{
5464
$uuid = Uuid::v4();
5565

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

5969
publicfunctiontestV5()
6070
{
61-
$uuid = Uuid::v5(newUuid(self::A_UUID_V4),'the name');
71+
$uuid = Uuid::v5(newUuidV4(self::A_UUID_V4),'the name');
6272

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

6676
publicfunctiontestBinary()
6777
{
68-
$uuid =newUuid(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
publicfunctiontestIsValid()
7486
{
7587
$this->assertFalse(Uuid::isValid('not a uuid'));
7688
$this->assertTrue(Uuid::isValid(self::A_UUID_V4));
77-
}
78-
79-
publicfunctiontestIsNull()
80-
{
81-
$uuid =newUuid(self::A_UUID_V1);
82-
$this->assertFalse($uuid->isNull());
83-
84-
$uuid =newUuid('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
publicfunctiontestEquals()
8994
{
90-
$uuid1 =newUuid(self::A_UUID_V1);
91-
$uuid2 =newUuid(self::A_UUID_V4);
95+
$uuid1 =newUuidV1(self::A_UUID_V1);
96+
$uuid2 =newUuidV4(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
publicfunctiontestEqualsAgainstOtherType($other)
101106
{
102-
$this->assertFalse((newUuid(self::A_UUID_V4))->equals($other));
107+
$this->assertFalse((newUuidV4(self::A_UUID_V4))->equals($other));
103108
}
104109

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

131-
publicfunctiontestExtraMethods()
136+
publicfunctiontestNullUuid()
132137
{
133-
$uuid =newUuid(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
}

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

Lines changed: 49 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -18,72 +18,90 @@
1818
*/
1919
class Uuidimplements \JsonSerializable
2020
{
21-
publicconstTYPE_1 =UUID_TYPE_TIME;
22-
publicconstTYPE_3 =UUID_TYPE_MD5;
23-
publicconstTYPE_4 =UUID_TYPE_RANDOM;
24-
publicconstTYPE_5 =UUID_TYPE_SHA1;
21+
protectedconstTYPE =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-
privateconstTIME_OFFSET_INT =0x01b21dd213814000;
30-
privateconstTIME_OFFSET_COM ="\xfe\x4d\xe2\x2d\xec\x7e\xc0\x00";
23+
protected$uuid;
3124

32-
private$uuid;
33-
34-
publicfunction__construct(string$uuid =null)
25+
publicfunction__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-
thrownew \InvalidArgumentException(sprintf('Invalid UUID: "%s".',$uuid));
33+
if (static::TYPE !==uuid_type($uuid)) {
34+
thrownew \InvalidArgumentException(sprintf('Invalid UUID%s: "%s".',static::TYPE ?'v'.static::TYPE :'',$uuid));
4435
}
4536

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

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

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

59-
publicstaticfunctionv4():self
73+
publicstaticfunctionv1(string$uuid =null):UuidV1
6074
{
61-
returnnewself(uuid_create(self::TYPE_4));
75+
returnnewUuidV1($uuid);
6276
}
6377

64-
publicstaticfunctionv5(self$uuidNamespace,string$name):self
78+
publicstaticfunctionv3(self$namespace,string$name):UuidV3
6579
{
66-
returnnewself(uuid_generate_sha1($uuidNamespace->uuid,$name));
80+
returnnewUuidV3(uuid_generate_md5($namespace->uuid,$name));
6781
}
6882

69-
publicstaticfunctionfromBinary(string$uuidAsBinary):self
83+
publicstaticfunctionv4(string$uuid =null):UuidV4
7084
{
71-
returnnewself(uuid_unparse($uuidAsBinary));
85+
returnnewUuidV4($uuid);
7286
}
7387

74-
publicstaticfunctionisValid(string$uuid):bool
88+
publicstaticfunctionv5(self$namespace,string$name):UuidV5
7589
{
76-
returnuuid_is_valid($uuid);
90+
returnnewUuidV5(uuid_generate_sha1($namespace->uuid,$name));
7791
}
7892

79-
publicfunctiontoBinary():string
93+
publicstaticfunctionisValid(string$uuid):bool
8094
{
81-
returnuuid_parse($this->uuid);
95+
if (__CLASS__ ===static::class) {
96+
returnuuid_is_valid($uuid);
97+
}
98+
99+
returnstatic::TYPE ===uuid_type($uuid);
82100
}
83101

84-
publicfunctionisNull():bool
102+
publicfunctiontoBinary():string
85103
{
86-
returnuuid_is_null($this->uuid);
104+
returnuuid_parse($this->uuid);
87105
}
88106

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

106-
publicfunctiongetType():int
107-
{
108-
returnuuid_type($this->uuid);
109-
}
110-
111-
publicfunctiongetTime():float
112-
{
113-
if (self::TYPE_1 !==$t =uuid_type($this->uuid)) {
114-
thrownew \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-
publicfunctiongetMac():string
131-
{
132-
if (self::TYPE_1 !==$t =uuid_type($this->uuid)) {
133-
thrownew \LogicException("UUID of type$t doesn't contain a MAC.");
134-
}
135-
136-
returnuuid_mac($this->uuid);
137-
}
138-
139124
publicfunction__toString():string
140125
{
141126
return$this->uuid;

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
namespaceSymfony\Component\Uid;
13+
14+
/**
15+
* @experimental in 5.1
16+
*
17+
* @author Grégoire Pineau <lyrixx@lyrixx.info>
18+
*/
19+
class UuidV1extends Uuid
20+
{
21+
protectedconstTYPE =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+
privateconstTIME_OFFSET_INT =0x01b21dd213814000;
27+
privateconstTIME_OFFSET_COM ="\xfe\x4d\xe2\x2d\xec\x7e\xc0\x00";
28+
29+
publicfunction__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+
publicfunctiongetTime():float
39+
{
40+
$time ='0'.substr($this->uuid,15,3).substr($this->uuid,9,4).substr($this->uuid,0,8);
41+
42+
if (\PHP_INT_SIZE >=8) {
43+
return (hexdec($time) -self::TIME_OFFSET_INT) /10000000;
44+
}
45+
46+
$time =str_pad(hex2bin($time),8,"\0",STR_PAD_LEFT);
47+
$time = InternalUtil::binaryAdd($time,self::TIME_OFFSET_COM);
48+
$time[0] =$time[0] &"\x7F";
49+
50+
return InternalUtil::toDecimal($time) /10000000;
51+
}
52+
53+
publicfunctiongetMac():string
54+
{
55+
returnuuid_mac($this->uuid);
56+
}
57+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp