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

Appearance settings

Commit bb9d522

Browse filesBrowse files
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 ------- 106c733 [Uid] Improve the code
2 parents 3987914 + 106c733 commit bb9d522
Copy full SHA for bb9d522

File tree

4 files changed

+33
-40
lines changed
Filter options

4 files changed

+33
-40
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Uid/BinaryUtil.php
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ class BinaryUtil
3636
'u' => 52, 'v' => 53, 'w' => 54, 'x' => 55, 'y' => 56, 'z' => 57,
3737
];
3838

39+
// https://tools.ietf.org/html/rfc4122#section-4.1.4
40+
// 0x01b21dd213814000 is the number of 100-ns intervals between the
41+
// UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00.
42+
private const TIME_OFFSET_INT = 0x01b21dd213814000;
43+
private const TIME_OFFSET_COM = "\xfe\x4d\xe2\x2d\xec\x7e\xc0\x00";
44+
3945
public static function toBase(string $bytes, array $map): string
4046
{
4147
$base = \strlen($alphabet = $map['']);
@@ -107,4 +113,17 @@ public static function add(string $a, string $b): string
107113

108114
return $a;
109115
}
116+
117+
public static function timeToFloat(string $time): float
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 = self::add($time, self::TIME_OFFSET_COM);
125+
$time[0] = $time[0] & "\x7F";
126+
127+
return self::toBase($time, self::BASE10) / 10000000;
128+
}
110129
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Uid/Uuid.php
+12-10Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,8 @@ final public static function v3(self $namespace, string $name): UuidV3
7676
{
7777
// don't use uuid_generate_md5(), some versions are buggy
7878
$uuid = md5(hex2bin(str_replace('-', '', $namespace->uid)).$name, true);
79-
$uuid[8] = $uuid[8] & "\x3F" | "\x80";
80-
$uuid = substr_replace(bin2hex($uuid), '-', 8, 0);
81-
$uuid = substr_replace($uuid, '-3', 13, 1);
82-
$uuid = substr_replace($uuid, '-', 18, 0);
8379

84-
return new UuidV3(substr_replace($uuid, '-', 23, 0));
80+
return new UuidV3(self::format($uuid, '-3'));
8581
}
8682

8783
final public static function v4(): UuidV4
@@ -93,12 +89,8 @@ final public static function v5(self $namespace, string $name): UuidV5
9389
{
9490
// don't use uuid_generate_sha1(), some versions are buggy
9591
$uuid = substr(sha1(hex2bin(str_replace('-', '', $namespace->uid)).$name, true), 0, 16);
96-
$uuid[8] = $uuid[8] & "\x3F" | "\x80";
97-
$uuid = substr_replace(bin2hex($uuid), '-', 8, 0);
98-
$uuid = substr_replace($uuid, '-5', 13, 1);
99-
$uuid = substr_replace($uuid, '-', 18, 0);
10092

101-
return new UuidV5(substr_replace($uuid, '-', 23, 0));
93+
return new UuidV5(self::format($uuid, '-5'));
10294
}
10395

10496
final public static function v6(): UuidV6
@@ -133,4 +125,14 @@ public function compare(parent $other): int
133125

134126
return parent::compare($other);
135127
}
128+
129+
private static function format(string $uuid, string $version): string
130+
{
131+
$uuid[8] = $uuid[8] & "\x3F" | "\x80";
132+
$uuid = substr_replace(bin2hex($uuid), '-', 8, 0);
133+
$uuid = substr_replace($uuid, $version, 13, 1);
134+
$uuid = substr_replace($uuid, '-', 18, 0);
135+
136+
return substr_replace($uuid, '-', 23, 0);
137+
}
136138
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Uid/UuidV1.php
+1-15Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@ class UuidV1 extends Uuid
2222
{
2323
protected const TYPE = UUID_TYPE_TIME;
2424

25-
// https://tools.ietf.org/html/rfc4122#section-4.1.4
26-
// 0x01b21dd213814000 is the number of 100-ns intervals between the
27-
// UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00.
28-
private const TIME_OFFSET_INT = 0x01b21dd213814000;
29-
private const TIME_OFFSET_COM = "\xfe\x4d\xe2\x2d\xec\x7e\xc0\x00";
30-
3125
public function __construct(string $uuid = null)
3226
{
3327
if (null === $uuid) {
@@ -41,15 +35,7 @@ public function getTime(): float
4135
{
4236
$time = '0'.substr($this->uid, 15, 3).substr($this->uid, 9, 4).substr($this->uid, 0, 8);
4337

44-
if (\PHP_INT_SIZE >= 8) {
45-
return (hexdec($time) - self::TIME_OFFSET_INT) / 10000000;
46-
}
47-
48-
$time = str_pad(hex2bin($time), 8, "\0", STR_PAD_LEFT);
49-
$time = BinaryUtil::add($time, self::TIME_OFFSET_COM);
50-
$time[0] = $time[0] & "\x7F";
51-
52-
return BinaryUtil::toBase($time, BinaryUtil::BASE10) / 10000000;
38+
return BinaryUtil::timeToFloat($time);
5339
}
5440

5541
public function getNode(): string

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Uid/UuidV6.php
+1-15Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@ class UuidV6 extends Uuid
2222
{
2323
protected const TYPE = 6;
2424

25-
// https://tools.ietf.org/html/rfc4122#section-4.1.4
26-
// 0x01b21dd213814000 is the number of 100-ns intervals between the
27-
// UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00.
28-
private const TIME_OFFSET_INT = 0x01b21dd213814000;
29-
private const TIME_OFFSET_COM = "\xfe\x4d\xe2\x2d\xec\x7e\xc0\x00";
30-
3125
public function __construct(string $uuid = null)
3226
{
3327
if (null === $uuid) {
@@ -42,15 +36,7 @@ public function getTime(): float
4236
{
4337
$time = '0'.substr($this->uid, 0, 8).substr($this->uid, 9, 4).substr($this->uid, 15, 3);
4438

45-
if (\PHP_INT_SIZE >= 8) {
46-
return (hexdec($time) - self::TIME_OFFSET_INT) / 10000000;
47-
}
48-
49-
$time = str_pad(hex2bin($time), 8, "\0", STR_PAD_LEFT);
50-
$time = BinaryUtil::add($time, self::TIME_OFFSET_COM);
51-
$time[0] = $time[0] & "\x7F";
52-
53-
return BinaryUtil::toBase($time, BinaryUtil::BASE10) / 10000000;
39+
return BinaryUtil::timeToFloat($time);
5440
}
5541

5642
public function getNode(): string

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.