-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[UID] Added the component + Added support for UUID #35940
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/Tests export-ignore | ||
/phpunit.xml.dist export-ignore | ||
/.gitignore export-ignore |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
composer.lock | ||
phpunit.xml | ||
vendor/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
CHANGELOG | ||
========= | ||
|
||
5.1.0 | ||
----- | ||
|
||
* added support for UUID | ||
* added the component |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2020 Fabien Potencier | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is furnished | ||
to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
Uid Component | ||
============= | ||
|
||
The UID component provides an object-oriented API to generate and represent UIDs. | ||
|
||
**This Component is experimental**. | ||
[Experimental features](https://symfony.com/doc/current/contributing/code/experimental.html) | ||
are not covered by Symfony's | ||
[Backward Compatibility Promise](https://symfony.com/doc/current/contributing/code/bc.html). | ||
|
||
Resources | ||
--------- | ||
|
||
* [Documentation](https://symfony.com/doc/current/components/uid.html) | ||
* [Contributing](https://symfony.com/doc/current/contributing/index.html) | ||
* [Report issues](https://github.com/symfony/symfony/issues) and | ||
[send Pull Requests](https://github.com/symfony/symfony/pulls) | ||
in the [main Symfony repository](https://github.com/symfony/symfony) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Tests\Component\Uid; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Uid\Uuid; | ||
|
||
class UuidTest extends TestCase | ||
{ | ||
private const A_UUID_V1 = 'd9e7a184-5d5b-11ea-a62a-3499710062d0'; | ||
private const A_UUID_V4 = 'd6b3345b-2905-4048-a83c-b5988e765d98'; | ||
|
||
public function testConstructorWithInvalidUuid() | ||
{ | ||
$this->expectException(\InvalidArgumentException::class); | ||
$this->expectExceptionMessage('Invalid UUID: "this is not a uuid".'); | ||
|
||
new Uuid('this is not a uuid'); | ||
} | ||
|
||
public function testConstructorWithValidUuid() | ||
{ | ||
$uuid = new Uuid(self::A_UUID_V4); | ||
|
||
$this->assertSame(self::A_UUID_V4, (string) $uuid); | ||
$this->assertSame('"'.self::A_UUID_V4.'"', json_encode($uuid)); | ||
} | ||
|
||
public function testV1() | ||
{ | ||
$uuid = Uuid::v1(); | ||
|
||
$this->assertSame(Uuid::TYPE_1, $uuid->getType()); | ||
} | ||
|
||
public function testV3() | ||
{ | ||
$uuid = Uuid::v3(new Uuid(self::A_UUID_V4), 'the name'); | ||
|
||
$this->assertSame(Uuid::TYPE_3, $uuid->getType()); | ||
} | ||
|
||
public function testV4() | ||
{ | ||
$uuid = Uuid::v4(); | ||
|
||
$this->assertSame(Uuid::TYPE_4, $uuid->getType()); | ||
} | ||
|
||
public function testV5() | ||
{ | ||
$uuid = Uuid::v5(new Uuid(self::A_UUID_V4), 'the name'); | ||
|
||
$this->assertSame(Uuid::TYPE_5, $uuid->getType()); | ||
} | ||
|
||
public function testBinary() | ||
{ | ||
$uuid = new Uuid(self::A_UUID_V4); | ||
|
||
$this->assertSame(self::A_UUID_V4, (string) Uuid::fromBinary($uuid->toBinary())); | ||
} | ||
|
||
public function testIsValid() | ||
{ | ||
$this->assertFalse(Uuid::isValid('not a uuid')); | ||
$this->assertTrue(Uuid::isValid(self::A_UUID_V4)); | ||
} | ||
|
||
public function testIsNull() | ||
{ | ||
$uuid = new Uuid(self::A_UUID_V1); | ||
$this->assertFalse($uuid->isNull()); | ||
|
||
$uuid = new Uuid('00000000-0000-0000-0000-000000000000'); | ||
$this->assertTrue($uuid->isNull()); | ||
} | ||
|
||
public function testEquals() | ||
{ | ||
$uuid1 = new Uuid(self::A_UUID_V1); | ||
$uuid2 = new Uuid(self::A_UUID_V4); | ||
|
||
$this->assertTrue($uuid1->equals($uuid1)); | ||
$this->assertFalse($uuid1->equals($uuid2)); | ||
} | ||
|
||
public function testCompare() | ||
{ | ||
$uuids = []; | ||
|
||
$uuids[] = $b = new Uuid('00000000-0000-0000-0000-00000000000b'); | ||
$uuids[] = $a = new Uuid('00000000-0000-0000-0000-00000000000a'); | ||
$uuids[] = $d = new Uuid('00000000-0000-0000-0000-00000000000d'); | ||
$uuids[] = $c = new Uuid('00000000-0000-0000-0000-00000000000c'); | ||
|
||
$this->assertNotSame([$a, $b, $c, $d], $uuids); | ||
|
||
usort($uuids, static function (Uuid $a, Uuid $b): int { | ||
return $a->compare($b); | ||
}); | ||
|
||
$this->assertSame([$a, $b, $c, $d], $uuids); | ||
} | ||
|
||
public function testExtraMethods() | ||
{ | ||
$uuid = new Uuid(self::A_UUID_V1); | ||
|
||
$this->assertSame(Uuid::VARIANT_DCE, $uuid->getVariant()); | ||
$this->assertSame(1583245966, $uuid->getTime()); | ||
$this->assertSame('3499710062d0', $uuid->getMac()); | ||
$this->assertSame(self::A_UUID_V1, (string) $uuid); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Uid; | ||
|
||
/** | ||
* @experimental in 5.1 | ||
* | ||
* @author Grégoire Pineau <lyrixx@lyrixx.info> | ||
*/ | ||
class Uuid implements \JsonSerializable | ||
{ | ||
public const TYPE_1 = UUID_TYPE_TIME; | ||
public const TYPE_3 = UUID_TYPE_MD5; | ||
public const TYPE_4 = UUID_TYPE_RANDOM; | ||
public const TYPE_5 = UUID_TYPE_SHA1; | ||
|
||
public const VARIANT_NCS = UUID_VARIANT_NCS; | ||
public const VARIANT_DCE = UUID_VARIANT_DCE; | ||
public const VARIANT_MICROSOFT = UUID_VARIANT_MICROSOFT; | ||
public const VARIANT_OTHER = UUID_VARIANT_OTHER; | ||
|
||
private $uuid; | ||
|
||
public function __construct(string $uuid = null) | ||
{ | ||
if (null === $uuid) { | ||
$this->uuid = uuid_create(self::TYPE_4); | ||
|
||
return; | ||
} | ||
|
||
if (!uuid_is_valid($uuid)) { | ||
throw new \InvalidArgumentException(sprintf('Invalid UUID: "%s".', $uuid)); | ||
} | ||
|
||
$this->uuid = $uuid; | ||
} | ||
|
||
public static function v1(): self | ||
{ | ||
return new self(uuid_create(self::TYPE_1)); | ||
} | ||
|
||
public static function v3(self $uuidNamespace, string $name): self | ||
{ | ||
return new self(uuid_generate_md5($uuidNamespace->uuid, $name)); | ||
} | ||
|
||
public static function v4(): self | ||
{ | ||
return new self(uuid_create(self::TYPE_4)); | ||
} | ||
|
||
public static function v5(self $uuidNamespace, string $name): self | ||
{ | ||
return new self(uuid_generate_sha1($uuidNamespace->uuid, $name)); | ||
} | ||
|
||
public static function fromBinary(string $uuidAsBinary): self | ||
{ | ||
return new self(uuid_unparse($uuidAsBinary)); | ||
} | ||
|
||
public static function isValid(string $uuid): bool | ||
{ | ||
return uuid_is_valid($uuid); | ||
} | ||
|
||
public function toBinary(): string | ||
{ | ||
return uuid_parse($this->uuid); | ||
} | ||
|
||
public function isNull(): bool | ||
{ | ||
return uuid_is_null($this->uuid); | ||
} | ||
|
||
public function equals(self $other): bool | ||
{ | ||
return 0 === uuid_compare($this->uuid, $other->uuid); | ||
} | ||
|
||
public function compare(self $other): int | ||
{ | ||
return uuid_compare($this->uuid, $other->uuid); | ||
} | ||
|
||
public function getType(): int | ||
{ | ||
return uuid_type($this->uuid); | ||
} | ||
|
||
public function getVariant(): int | ||
{ | ||
return uuid_variant($this->uuid); | ||
} | ||
|
||
public function getTime(): int | ||
{ | ||
if (self::TYPE_1 !== $t = uuid_type($this->uuid)) { | ||
throw new \LogicException("UUID of type $t doesn't contain a time."); | ||
} | ||
|
||
return uuid_time($this->uuid); | ||
} | ||
|
||
public function getMac(): string | ||
{ | ||
if (self::TYPE_1 !== $t = uuid_type($this->uuid)) { | ||
throw new \LogicException("UUID of type $t doesn't contain a MAC."); | ||
} | ||
|
||
return uuid_mac($this->uuid); | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return $this->uuid; | ||
} | ||
|
||
public function jsonSerialize(): string | ||
{ | ||
return $this->uuid; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"name": "symfony/uid", | ||
"type": "library", | ||
"description": "Symfony Uid component", | ||
"keywords": ["uid", "uuid"], | ||
"homepage": "https://symfony.com", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Grégoire Pineau", | ||
"email": "lyrixx@lyrixx.info" | ||
}, | ||
{ | ||
"name": "Symfony Community", | ||
"homepage": "https://symfony.com/contributors" | ||
} | ||
], | ||
"require": { | ||
"php": "^7.2.5", | ||
"symfony/polyfill-uuid": "^1.15" | ||
}, | ||
"autoload": { | ||
"psr-4": { "Symfony\\Component\\Uid\\": "" }, | ||
"exclude-from-classmap": [ | ||
"/Tests/" | ||
] | ||
}, | ||
"minimum-stability": "dev", | ||
"extra": { | ||
"branch-alias": { | ||
"dev-master": "5.1-dev" | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.