Debian Codegraph @

lib/3rdParty/composer/symfony/uid/Ulid.php

raw symbols SCIP
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
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 use Symfony\Component\Uid\Exception\InvalidArgumentException;
15
16 /**
17 * A ULID is lexicographically sortable and contains a 48-bit timestamp and 80-bit of crypto-random entropy.
18 *
19 * @see https://github.com/ulid/spec
20 *
21 * @author Nicolas Grekas <[email protected]>
22 */
23 class Ulid extends AbstractUid implements TimeBasedUidInterface
24 {
25 protected const NIL = '00000000000000000000000000';
26 protected const MAX = '7ZZZZZZZZZZZZZZZZZZZZZZZZZ';
27
28 private static string $time = '';
29 private static array $rand = [];
30
31 public function __construct(?string $ulid = null)
32 {
33 if (null === $ulid) {
34 $this->uid = static::generate();
35 } elseif (self::NIL === $ulid) {
36 $this->uid = self::NIL;
37 } else {
38 $this->uid = strtoupper($ulid);
39
40 if (self::MAX === $this->uid) {
41 $this->uid = self::MAX;
42 } elseif (!self::isValid($ulid)) {
43 throw new InvalidArgumentException('Invalid ULID.');
44 }
45 }
46 }
47
48 public static function isValid(string $ulid): bool
49 {
50 if (26 !== \strlen($ulid)) {
51 return false;
52 }
53
54 if (26 !== strspn($ulid, '0123456789ABCDEFGHJKMNPQRSTVWXYZabcdefghjkmnpqrstvwxyz')) {
55 return false;
56 }
57
58 return $ulid[0] <= '7';
59 }
60
61 public static function fromString(string $ulid): static
62 {
63 if (36 === \strlen($ulid) && preg_match('{^[0-9a-f]{8}(?:-[0-9a-f]{4}){3}-[0-9a-f]{12}$}Di', $ulid)) {
64 $ulid = hex2bin(str_replace('-', '', $ulid));
65 } elseif (22 === \strlen($ulid) && 22 === strspn($ulid, BinaryUtil::BASE58[''])) {
66 $ulid = str_pad(BinaryUtil::fromBase($ulid, BinaryUtil::BASE58), 16, "\0", \STR_PAD_LEFT);
67 }
68
69 if (16 !== \strlen($ulid)) {
70 return match (strtr($ulid, 'z', 'Z')) {
71 self::NIL => new NilUlid(),
72 self::MAX => new MaxUlid(),
73 default => new static($ulid),
74 };
75 }
76
77 $ulid = bin2hex($ulid);
78 $ulid = \sprintf('%02s%04s%04s%04s%04s%04s%04s',
79 base_convert(substr($ulid, 0, 2), 16, 32),
80 base_convert(substr($ulid, 2, 5), 16, 32),
81 base_convert(substr($ulid, 7, 5), 16, 32),
82 base_convert(substr($ulid, 12, 5), 16, 32),
83 base_convert(substr($ulid, 17, 5), 16, 32),
84 base_convert(substr($ulid, 22, 5), 16, 32),
85 base_convert(substr($ulid, 27, 5), 16, 32)
86 );
87
88 if (self::NIL === $ulid) {
89 return new NilUlid();
90 }
91
92 if (self::MAX === $ulid = strtr($ulid, 'abcdefghijklmnopqrstuv', 'ABCDEFGHJKMNPQRSTVWXYZ')) {
93 return new MaxUlid();
94 }
95
96 $u = new static(self::NIL);
97 $u->uid = $ulid;
98
99 return $u;
100 }
101
102 public function toBinary(): string
103 {
104 $ulid = strtr($this->uid, 'ABCDEFGHJKMNPQRSTVWXYZ', 'abcdefghijklmnopqrstuv');
105
106 $ulid = \sprintf('%02s%05s%05s%05s%05s%05s%05s',
107 base_convert(substr($ulid, 0, 2), 32, 16),
108 base_convert(substr($ulid, 2, 4), 32, 16),
109 base_convert(substr($ulid, 6, 4), 32, 16),
110 base_convert(substr($ulid, 10, 4), 32, 16),
111 base_convert(substr($ulid, 14, 4), 32, 16),
112 base_convert(substr($ulid, 18, 4), 32, 16),
113 base_convert(substr($ulid, 22, 4), 32, 16)
114 );
115
116 return hex2bin($ulid);
117 }
118
119 /**
120 * Returns the identifier as a base32 case insensitive string.
121 *
122 * @see https://tools.ietf.org/html/rfc4648#section-6
123 *
124 * @example 09EJ0S614A9FXVG9C5537Q9ZE1 (len=26)
125 */
126 public function toBase32(): string
127 {
128 return $this->uid;
129 }
130
131 public function getDateTime(): \DateTimeImmutable
132 {
133 $time = strtr(substr($this->uid, 0, 10), 'ABCDEFGHJKMNPQRSTVWXYZ', 'abcdefghijklmnopqrstuv');
134
135 if (\PHP_INT_SIZE >= 8) {
136 $time = (string) hexdec(base_convert($time, 32, 16));
137 } else {
138 $time = \sprintf('%02s%05s%05s',
139 base_convert(substr($time, 0, 2), 32, 16),
140 base_convert(substr($time, 2, 4), 32, 16),
141 base_convert(substr($time, 6, 4), 32, 16)
142 );
143 $time = BinaryUtil::toBase(hex2bin($time), BinaryUtil::BASE10);
144 }
145
146 if (4 > \strlen($time)) {
147 $time = '000'.$time;
148 }
149
150 return \DateTimeImmutable::createFromFormat('U.u', substr_replace($time, '.', -3, 0));
151 }
152
153 public static function generate(?\DateTimeInterface $time = null): string
154 {
155 if (null === $mtime = $time) {
156 $time = microtime(false);
157 $time = substr($time, 11).substr($time, 2, 3);
158 } elseif (0 > $time = $time->format('Uv')) {
159 throw new InvalidArgumentException('The timestamp must be positive.');
160 }
161
162 if ($time > self::$time || (null !== $mtime && $time !== self::$time)) {
163 randomize:
164 $r = unpack('n*', random_bytes(10));
165 $r[1] |= ($r[5] <<= 4) & 0xF0000;
166 $r[2] |= ($r[5] <<= 4) & 0xF0000;
167 $r[3] |= ($r[5] <<= 4) & 0xF0000;
168 $r[4] |= ($r[5] <<= 4) & 0xF0000;
169 unset($r[5]);
170 self::$rand = $r;
171 self::$time = $time;
172 } elseif ([1 => 0xFFFFF, 0xFFFFF, 0xFFFFF, 0xFFFFF] === self::$rand) {
173 if (\PHP_INT_SIZE >= 8 || 10 > \strlen($time = self::$time)) {
174 $time = (string) (1 + $time);
175 } elseif ('999999999' === $mtime = substr($time, -9)) {
176 $time = (1 + substr($time, 0, -9)).'000000000';
177 } else {
178 $time = substr_replace($time, str_pad(++$mtime, 9, '0', \STR_PAD_LEFT), -9);
179 }
180
181 goto randomize;
182 } else {
183 for ($i = 4; $i > 0 && 0xFFFFF === self::$rand[$i]; --$i) {
184 self::$rand[$i] = 0;
185 }
186
187 ++self::$rand[$i];
188 $time = self::$time;
189 }
190
191 if (\PHP_INT_SIZE >= 8) {
192 $time = base_convert($time, 10, 32);
193 } else {
194 $time = str_pad(bin2hex(BinaryUtil::fromBase($time, BinaryUtil::BASE10)), 12, '0', \STR_PAD_LEFT);
195 $time = \sprintf('%s%04s%04s',
196 base_convert(substr($time, 0, 2), 16, 32),
197 base_convert(substr($time, 2, 5), 16, 32),
198 base_convert(substr($time, 7, 5), 16, 32)
199 );
200 }
201
202 return strtr(\sprintf('%010s%04s%04s%04s%04s',
203 $time,
204 base_convert(self::$rand[1], 10, 32),
205 base_convert(self::$rand[2], 10, 32),
206 base_convert(self::$rand[3], 10, 32),
207 base_convert(self::$rand[4], 10, 32)
208 ), 'abcdefghijklmnopqrstuv', 'ABCDEFGHJKMNPQRSTVWXYZ');
209 }
210 }