Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #36066 [Uid] use one class per type of UUID (nicolas-grekas)
This PR was merged into the 5.1-dev branch. Discussion ---------- [Uid] use one class per type of UUID | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - (embeds #36064 for now) Would it make sense to have one class per type of UUID? This aligns the type system and UUID types, so that one could type hint e.g. `UuidV4 $uuid`. This PR does so. `UuidV1`/2/3/4 and `NullUuid` all extend the base `Uuid` class, which provides common methods and the factories needed to create each king of UUID. This means we don't need the `getType()` nor the `isNull()` methods since they can be replaced by instanceof checks. As expected, `getTime()` and `getMac()` then now exist only on the `UuidV1` class - no need for any version check nor any `LogicException` anymore. Each type is guaranteed to contain a UUID that matches its class' type. The base `Uuid` class is used for the "no type" type. Commits ------- 62f6ac4 [Uid] use one class per type of UUID
- Loading branch information
Showing
9 changed files
with
245 additions
and
99 deletions.
There are no files selected for viewing
This file contains 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,27 @@ | ||
<?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 NullUuid extends Uuid | ||
{ | ||
protected const TYPE = UUID_TYPE_NULL; | ||
|
||
public function __construct() | ||
{ | ||
$this->uuid = '00000000-0000-0000-0000-000000000000'; | ||
} | ||
} |
This file contains 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 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 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 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 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,59 @@ | ||
<?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; | ||
|
||
/** | ||
* A v1 UUID contains a 60-bit timestamp and ~60 extra unique bits. | ||
* | ||
* @experimental in 5.1 | ||
* | ||
* @author Grégoire Pineau <lyrixx@lyrixx.info> | ||
*/ | ||
class UuidV1 extends Uuid | ||
{ | ||
protected const TYPE = UUID_TYPE_TIME; | ||
|
||
// https://tools.ietf.org/html/rfc4122#section-4.1.4 | ||
// 0x01b21dd213814000 is the number of 100-ns intervals between the | ||
// UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00. | ||
private const TIME_OFFSET_INT = 0x01b21dd213814000; | ||
private const TIME_OFFSET_COM = "\xfe\x4d\xe2\x2d\xec\x7e\xc0\x00"; | ||
|
||
public function __construct(string $uuid = null) | ||
{ | ||
if (null === $uuid) { | ||
$this->uuid = uuid_create(static::TYPE); | ||
} else { | ||
parent::__construct($uuid); | ||
} | ||
} | ||
|
||
public function getTime(): float | ||
{ | ||
$time = '0'.substr($this->uuid, 15, 3).substr($this->uuid, 9, 4).substr($this->uuid, 0, 8); | ||
|
||
if (\PHP_INT_SIZE >= 8) { | ||
return (hexdec($time) - self::TIME_OFFSET_INT) / 10000000; | ||
} | ||
|
||
$time = str_pad(hex2bin($time), 8, "\0", STR_PAD_LEFT); | ||
$time = BinaryUtil::add($time, self::TIME_OFFSET_COM); | ||
$time[0] = $time[0] & "\x7F"; | ||
|
||
return BinaryUtil::toBase($time, BinaryUtil::BASE10) / 10000000; | ||
} | ||
|
||
public function getNode(): string | ||
{ | ||
return uuid_mac($this->uuid); | ||
} | ||
} |
Oops, something went wrong.