8000 feature #35940 [UID] Added the component + Added support for UUID (ly… · symfony/symfony@d108f7b · GitHub
[go: up one dir, main page]

Skip to content

Commit d108f7b

Browse files
committed
feature #35940 [UID] Added the component + Added support for UUID (lyrixx)
This PR was merged into the 5.1-dev branch. Discussion ---------- [UID] Added the component + Added support for UUID | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | | License | MIT | Doc PR | --- ```php <?php require __DIR__.'/vendor/autoload.php'; use Symfony\Component\Uid\Uuid; $u = Uuid::v1(); dump($u); dump($u->isNull()); dump($u->getType()); dump($u->getVaria 8000 nt()); dump($u->getTime()); dump($u->getMac()); dump($u->isEqualsTo($u)); dump($u->compare($u)); dump(Uuid::fromBinary($u->toBinary())); ``` Commits ------- c3f14dd [UID] Added the component + Added support for UUID
2 parents 49e5d57 + c3f14dd commit d108f7b

File tree

11 files changed

+378
-2
lines changed

11 files changed

+378
-2
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ before_install:
5757
sudo wget -O - http://packages.couchbase.com/ubuntu/couchbase.key | sudo apt-key add -
5858
echo "deb http://packages.couchbase.com/ubuntu xenial xenial/main" | sudo tee /etc/apt/sources.list.d/couchbase.list
5959
sudo apt update
60-
sudo apt install -y librabbitmq-dev libsodium-dev libcouchbase-dev zlib1g-dev
60+
sudo apt install -y libcouchbase-dev librabbitmq-dev libsodium-dev php-uuid zlib1g-dev
6161
6262
- |
6363
# Start Couchbase

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
"symfony/polyfill-intl-idn": "^1.10",
3434
"symfony/polyfill-intl-normalizer": "~1.0",
3535
"symfony/polyfill-mbstring": "~1.0",
36-
"symfony/polyfill-php73": "^1.11"
36+
"symfony/polyfill-php73": "^1.11",
37+
"symfony/polyfill-uuid": "^1.15"
3738
},
3839
"replace": {
3940
"symfony/asset": "self.version",
@@ -90,6 +91,7 @@
9091
"symfony/translation": "self.version",
9192
"symfony/twig-bridge": "self.version",
9293
"symfony/twig-bundle": "self.version",
94+
"symfony/uid": "self.version",
9395
"symfony/validator": "self.version",
9496
"symfony/var-dumper": "self.version",
9597
"symfony/var-exporter": "self.version",
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/Tests export-ignore
2+
/phpunit.xml.dist export-ignore
3+
/.gitignore export-ignore

src/Symfony/Component/Uid/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
composer.lock
2+
phpunit.xml
3+
vendor/
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CHANGELOG
2+
=========
3+
4+
5.1.0
5+
-----
6+
7+
* added support for UUID
8+
* added the component

src/Symfony/Component/Uid/LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2020 Fabien Potencier
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS B F438 E LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

src/Symfony/Component/Uid/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Uid Component
2+
=============
3+
4+
The UID component provides an object-oriented API to generate and represent UIDs.
5+
6+
**This Component is experimental**.
7+
[Experimental features](https://symfony.com/doc/current/contributing/code/experimental.html)
8+
are not covered by Symfony's
9+
[Backward Compatibility Promise](https://symfony.com/doc/current/contributing/code/bc.html).
10+
11+
Resources
12+
---------
13+
14+
* [Documentation](https://symfony.com/doc/current/components/uid.html)
15+
* [Contributing](https://symfony.com/doc/current/contributing/index.html)
16+
* [Report issues](https://github.com/symfony/symfony/issues) and
17+
[send Pull Requests](https://github.com/symfony/symfony/pulls)
18+
in the [main Symfony repository](https://github.com/symfony/symfony)
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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\Tests\Component\Uid;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Uid\Uuid;
16+
17+
class UuidTest extends TestCase
18+
{
19+
private const A_UUID_V1 = 'd9e7a184-5d5b-11ea-a62a-3499710062d0';
20+
private const A_UUID_V4 = 'd6b3345b-2905-4048-a83c-b5988e765d98';
21+
22+
public function testConstructorWithInvalidUuid()
23+
{
24+
$this->expectException(\InvalidArgumentException::class);
25+
$this->expectExceptionMessage('Invalid UUID: "this is not a uuid".');
26+
27+
new Uuid('this is not a uuid');
28+
}
29+
30+
public function testConstructorWithValidUuid()
31+
{
32+
$uuid = new Uuid(self::A_UUID_V4);
33+
34+
$this->assertSame(self::A_UUID_V4, (string) $uuid);
35+
$this->assertSame('"'.self::A_UUID_V4.'"', json_encode($uuid));
36+
}
37+
38+
public function testV1()
39+
{
40+
$uuid = Uuid::v1();
41+
42+
$this->assertSame(Uuid::TYPE_1, $uuid->getType());
43+
}
44+
45+
public function testV3()
46+
{
47+
$uuid = Uuid::v3(new Uuid(self::A_UUID_V4), 'the name');
48+
49+
$this->assertSame(Uuid::TYPE_3, $uuid->getType());
50+
}
51+
52+
public function testV4()
53+
{
54+
$uuid = Uuid::v4();
55+
56+
$this->assertSame(Uuid::TYPE_4, $uuid->getType());
57+
}
58+
59+
public function testV5()
60+
{
61+
$uuid = Uuid::v5(new Uuid(self::A_UUID_V4), 'the name');
62+
63+
$this->assertSame(Uuid::TYPE_5, $uuid->getType());
64+
}
65+
66+
public function testBinary()
67+
{
68+
$uuid = new Uuid(self::A_UUID_V4);
69+
70+
$this->assertSame(self::A_UUID_V4, (string) Uuid::fromBinary($uuid->toBinary()));
71+
}
72+
73+
public function testIsValid()
74+
{
75+
$this->assertFalse(Uuid::isValid('not a uuid'));
76+
$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());
86+
}
87+
88+
public function testEquals()
89+
{
90+
$uuid1 = new Uuid(self::A_UUID_V1);
91+
$uuid2 = new Uuid(self::A_UUID_V4);
92+
93+
$this->assertTrue($uuid1->equals($uuid1));
94+
$this->assertFalse($uuid1->equals($uuid2));
95+
}
96+
97+
public function testCompare()
98+
{
99+
$uuids = [];
100+
101+
$uuids[] = $b = new Uuid('00000000-0000-0000-0000-00000000000b');
102+
$uuids[] = $a = new Uuid('00000000-0000-0000-0000-00000000000a');
103+
$uuids[] = $d = new Uuid('00000000-0000-0000-0000-00000000000d');
104+
$uuids[] = $c = new Uuid('00000000-0000-0000-0000-00000000000c');
105+
106+
$this->assertNotSame([$a, $b, $c, $d], $uuids);
107+
108+
usort($uuids, static function (Uuid $a, Uuid $b): int {
109+
return $a->compare($b);
110+
});
111+
112+
$this->assertSame([$a, $b, $c, $d], $uuids);
113+
}
114+
115+
public function testExtraMethods()
116+
{
117+
$uuid = new Uuid(self::A_UUID_V1);
118+
119+
$this->assertSame(Uuid::VARIANT_DCE, $uuid->getVariant());
120+
$this->assertSame(1583245966, $uuid->getTime());
121+
$this->assertSame('3499710062d0', $uuid->getMac());
122+
$this->assertSame(self::A_UUID_V1, (string) $uuid);
123+
}
124+
}

src/Symfony/Component/Uid/Uuid.php

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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 Uuid implements \JsonSerializable
20+
{
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;
25+
26+
public const VARIANT_NCS = UUID_VARIANT_NCS;
27+
public const VARIANT_DCE = UUID_VARIANT_DCE;
28+
public const VARIANT_MICROSOFT = UUID_VARIANT_MICROSOFT;
29+
public const VARIANT_OTHER = UUID_VARIANT_OTHER;
30+
31+
private $uuid;
32+
33+
public function __construct(string $uuid = null)
34+
{
35+
if (null === $uuid) {
36+
$this->uuid = uuid_create(self::TYPE_4);
37+
38+
return;
39+
}
40+
41+
if (!uuid_is_valid($uuid)) {
42+
throw new \InvalidArgumentException(sprintf('Invalid UUID: "%s".', $uuid));
43+
}
44+
45+
$this->uuid = $uuid;
46+
}
47+
48+
public static function v1(): self
49+
{
50+
return new self(uuid_create(self::TYPE_1));
51+
}
52+
53+
public static function v3(self $uuidNamespace, string $name): self
54+
{
55+
return new self(uuid_generate_md5($uuidNamespace->uuid, $name));
56+
}
57+
58+
public static function v4(): self
59+
{
60+
return new self(uuid_create(self::TYPE_4));
61+
}
62+
63+
public static function v5(self $uuidNamespace, string $name): self
64+
{
65+
return new self(uuid_generate_sha1($uuidNamespace->uuid, $name));
66+
}
67+
68+
public static function fromBinary(string $uuidAsBinary): self
69+
{
70+
return new self(uuid_unparse($uuidAsBinary));
71+
}
72+
73+
public static function isValid(string $uuid): bool
74+
{
75+
return uuid_is_valid($uuid);
76+
}
77+
78+
public function toBinary(): string
79+
{
80+
return uuid_parse($this->uuid);
81+
}
82+
83+
public function isNull(): bool
84+
{
85+
return uuid_is_null($this->uuid);
86+
}
87+
88+
public function equals(self $other): bool
89+
{
90+
return 0 === uuid_compare($this->uuid, $other->uuid);
91+
}
92+
93+
public function compare(self $other): int
94+
{
95+
return uuid_compare($this->uuid, $other->uuid);
96+
}
97+
98+
public function getType(): int
99+
{
100+
return uuid_type($this->uuid);
101+
}
102+
103+
public function getVariant(): int
104+
{
105+
return uuid_variant($this->uuid);
106+
}
107+
108+
public function getTime(): int
109+
{
110+
if (self::TYPE_1 !== $t = uuid_type($this->uuid)) {
111+
throw new \LogicException("UUID of type $t doesn't contain a time.");
112+
}
113+
114+
return uuid_time($this->uuid);
115+
}
116+
117+
public function getMac(): string
118+
{
119+
if (self::TYPE_1 !== $t = uuid_type($this->uuid)) {
120+
throw new \LogicException("UUID of type $t doesn't contain a MAC.");
121+
}
122+
123+
return uuid_mac($this->uuid);
124+
}
125+
126+
public function __toString(): string
127+
{
128+
return $this->uuid;
129+
}
130+
131+
public function jsonSerialize(): string
132+
{
133+
return $this->uuid;
134+
}
135+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "symfony/uid",
3+
"type": "library",
4+
"description": "Symfony Uid component",
5+
"keywords": ["uid", "uuid"],
6+
"homepage": "https://symfony.com",
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Grégoire Pineau",
11+
"email": "lyrixx@lyrixx.info"
12+
},
13+
{
14+
"name": "Symfony Community",
15+
"homepage": "https://symfony.com/contributors"
16+
}
17+
],
18+
"require": {
19+
"php": "^7.2.5",
20+
"symfony/polyfill-uuid": "^1.15"
21+
},
22+
"autoload": {
23+
"psr-4": { "Symfony\\Component\\Uid\\": "" },
24+
"exclude-from-classmap": [
25+
"/Tests/"
26+
]
27+
},
28+
"minimum-stability": "dev",
29+
"extra": {
30+
"branch-alias": {
31+
"dev-master": "5.1-dev"
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)
0