minor #36267 [Uid] Improve the code (Nilmar Sanchez Muguercia)

This PR was squashed before being merged into the 5.1-dev branch.

Discussion
----------

[Uid] Improve the code

Improve the reuse of code, programing for extension and not for modification, create a Trait with similar behavior for getTime method in uuid types. Eliminate duplicate code in Uuid

| Q             | A
| ------------- | ---
| Branch?       | 5.1
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #...
| License       | MIT
| Doc PR        | symfony/symfony-docs#...

Keep the same functionality, just improve a little the code, eliminating duplications and reusing some lines.

Commits
-------

106c733bce [Uid] Improve the code
This commit is contained in:
Nicolas Grekas 2020-03-31 20:10:39 +02:00
commit bb9d522346
4 changed files with 33 additions and 40 deletions

View File

@ -36,6 +36,12 @@ class BinaryUtil
'u' => 52, 'v' => 53, 'w' => 54, 'x' => 55, 'y' => 56, 'z' => 57,
];
// 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 static function toBase(string $bytes, array $map): string
{
$base = \strlen($alphabet = $map['']);
@ -107,4 +113,17 @@ class BinaryUtil
return $a;
}
public static function timeToFloat(string $time): float
{
if (\PHP_INT_SIZE >= 8) {
return (hexdec($time) - self::TIME_OFFSET_INT) / 10000000;
}
$time = str_pad(hex2bin($time), 8, "\0", STR_PAD_LEFT);
$time = self::add($time, self::TIME_OFFSET_COM);
$time[0] = $time[0] & "\x7F";
return self::toBase($time, self::BASE10) / 10000000;
}
}

View File

@ -76,12 +76,8 @@ class Uuid extends AbstractUid
{
// don't use uuid_generate_md5(), some versions are buggy
$uuid = md5(hex2bin(str_replace('-', '', $namespace->uid)).$name, true);
$uuid[8] = $uuid[8] & "\x3F" | "\x80";
$uuid = substr_replace(bin2hex($uuid), '-', 8, 0);
$uuid = substr_replace($uuid, '-3', 13, 1);
$uuid = substr_replace($uuid, '-', 18, 0);
return new UuidV3(substr_replace($uuid, '-', 23, 0));
return new UuidV3(self::format($uuid, '-3'));
}
final public static function v4(): UuidV4
@ -93,12 +89,8 @@ class Uuid extends AbstractUid
{
// don't use uuid_generate_sha1(), some versions are buggy
$uuid = substr(sha1(hex2bin(str_replace('-', '', $namespace->uid)).$name, true), 0, 16);
$uuid[8] = $uuid[8] & "\x3F" | "\x80";
$uuid = substr_replace(bin2hex($uuid), '-', 8, 0);
$uuid = substr_replace($uuid, '-5', 13, 1);
$uuid = substr_replace($uuid, '-', 18, 0);
return new UuidV5(substr_replace($uuid, '-', 23, 0));
return new UuidV5(self::format($uuid, '-5'));
}
final public static function v6(): UuidV6
@ -133,4 +125,14 @@ class Uuid extends AbstractUid
return parent::compare($other);
}
private static function format(string $uuid, string $version): string
{
$uuid[8] = $uuid[8] & "\x3F" | "\x80";
$uuid = substr_replace(bin2hex($uuid), '-', 8, 0);
$uuid = substr_replace($uuid, $version, 13, 1);
$uuid = substr_replace($uuid, '-', 18, 0);
return substr_replace($uuid, '-', 23, 0);
}
}

View File

@ -22,12 +22,6 @@ 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) {
@ -41,15 +35,7 @@ class UuidV1 extends Uuid
{
$time = '0'.substr($this->uid, 15, 3).substr($this->uid, 9, 4).substr($this->uid, 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;
return BinaryUtil::timeToFloat($time);
}
public function getNode(): string

View File

@ -22,12 +22,6 @@ class UuidV6 extends Uuid
{
protected const TYPE = 6;
// 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) {
@ -42,15 +36,7 @@ class UuidV6 extends Uuid
{
$time = '0'.substr($this->uid, 0, 8).substr($this->uid, 9, 4).substr($this->uid, 15, 3);
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;
return BinaryUtil::timeToFloat($time);
}
public function getNode(): string