/
ULID.php
285 lines (266 loc) · 8.46 KB
/
ULID.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php declare(strict_types=1);
/*
* This file is part of the Koded package.
*
* (c) Mihail Binev <mihail@kodeart.com>
*
* Please view the LICENSE distributed with this source code
* for the full copyright and license information.
*/
namespace Koded\Stdlib;
use ArgumentCountError;
use Countable;
use DateTime;
use DateTimeZone;
use InvalidArgumentException;
use Throwable;
use function array_key_first;
use function count;
use function current;
use function dechex;
use function intval;
use function microtime;
use function mt_rand;
use function preg_match;
use function sprintf;
use function str_contains;
use function str_pad;
use function strlen;
use function strpos;
use function substr;
/**
* Class ULID generates Universally Unique Lexicographically Sortable Identifiers
* that are sortable, has monotonic sort order (correctly detects and handles the
* same millisecond), uses Crockford's base32 for better readability
* and will work until 10.889AD among other things.
*
* ULID:
*
* 01GXDATSFG 43B0Y7R64G172FVH
* |--------| |--------------|
* Timestamp Randomness
* 48bits 80bits
*
* ULID as UUID:
*
* 01875aad-65f0-a911-bc1d-77989d5c99bb
* |-----------| |--------------------|
* Timestamp Randomness
*
*/
class ULID implements Countable
{
public const REGEX = '[a-f0-9]{8}\-[a-f0-9]{4}\-[a-f0-9]{4}\-[a-f0-9]{4}\-[a-f0-9]{12}';
private const ENCODING = '0123456789ABCDEFGHJKMNPQRSTVWXYZ'; // Crockford's base32
protected array $timestamps = [];
private array $randomness = [];
private function __construct(int $timestamps, int $count)
{
if ($count < 1) {
throw new ArgumentCountError('count must be greater then 0', 400);
}
$this->randomize(false);
for ($i = 0; $i < $count; ++$i) {
$this->timestamps[$i] = $timestamps;
}
}
/**
* Creates an instance of ULID with number
* of timestamps defined by the count argument.
* @param int $count
* @return static
*/
public static function generate(int $count = 1): self
{
return new static((int)(microtime(true) * 1000), $count);
}
/**
* Decode the ULID string into an instance of ULID.
* @param string $ulid
* @return static
*/
public static function fromULID(string $ulid): self
{
if (26 !== strlen($ulid)) {
throw new InvalidArgumentException('Invalid ULID, wrong length', 400);
}
if (!preg_match('/^[' . static::ENCODING . ']{26}$/', $ulid)) {
throw new InvalidArgumentException('Invalid ULID, non supported characters', 400);
}
$timestamp = 0;
$chars = substr($ulid, 0, 10);
for ($i = 0; $i < 10; ++$i) {
$timestamp = $timestamp * 32 + strpos(static::ENCODING, $chars[$i]);
}
return new static ($timestamp, 1);
}
/**
* Decode the ULID string in UUID format into an instance of ULID.
* @param string $ulid UUID representation of ULID value
* @return static
*/
public static function fromUUID(string $ulid): self
{
if (false === static::valid($ulid)) {
throw new InvalidArgumentException('Invalid ULID', 400);
}
$timestamp = hexdec(substr($ulid, 0, 8) . substr($ulid, 9, 4));
return new static($timestamp, 1);
}
/**
* Decode the date time string into an instance of ULID.
* @param float $timestamp UNIX timestamp with or without the milliseconds part
* @return static
*/
public static function fromTimestamp(float $timestamp): self
{
if ($timestamp <= 0 || $timestamp >= PHP_INT_MAX) {
throw new InvalidArgumentException("Invalid timestamp ($timestamp)", 400);
}
$timestamp = (string)$timestamp;
if (str_contains($timestamp, '.')) {
$timestamp = (string)($timestamp * 1000);
}
if (strlen($timestamp) >= 13) {
$timestamp = substr($timestamp, 0, 13);
}
return new static(intval($timestamp), 1);
}
/**
* Decode the date time string into an instance of ULID.
* @param string $datetime in format: Y-m-d H:i:s with optional 'v' (milliseconds)
* @return static
*/
public static function fromDateTime(string $datetime): self
{
try {
$dt = (str_contains($datetime, '.'))
? DateTime::createFromFormat('Y-m-d H:i:s.v', $datetime, new DateTimeZone('UTC'))
: DateTime::createFromFormat('Y-m-d H:i:s', $datetime, new DateTimeZone('UTC'));
return new static(intval($dt->getTimestamp() . $dt->format('v')), 1);
} catch (Throwable) {
throw new InvalidArgumentException("Invalid datetime ($datetime)", 400);
}
}
public static function valid(string $uuid): bool
{
return (bool)preg_match('/^' . static::REGEX . '$/i', $uuid);
}
/**
* Creates a single, or a list, of UUID values.
* @return array|string
*/
public function toUUID(): array|string
{
$list = [];
foreach ($this->timestamps as $ts) {
$timestamp = $this->generateUuidParts($ts);
$hex = substr(str_pad(dechex(intval($timestamp)), 12, '0', STR_PAD_LEFT), -12);
$list[sprintf('%08s-%04s-%04x-%04x-%012x',
substr($hex, 0, 8),
substr($hex, 8, 4),
...$this->randomness
)] = $ts;
}
return (1 === $this->count())
? array_key_first($list)
: $list;
}
/**
* Creates a single, or a list of ULID values.
* @return array|string
*/
public function toULID(): array|string
{
$list = [];
foreach ($this->timestamps as $ts) {
[$timestamp, $randomness] = $this->generateUlidParts(intval($ts));
$list[$timestamp . $randomness] = $ts;
}
return (1 === $this->count())
? array_key_first($list)
: $list;
}
/**
* Returns a single, or a list, of DateTime instances.
* @return array|DateTime
*/
public function toDateTime(): array|DateTime
{
$list = [];
foreach ($this->timestamps as $timestamp) {
$timestamp = (string)$timestamp;
$datetime = new DateTime('@' . substr($timestamp, 0, 10), new DateTimeZone('UTC'));
if (strlen($timestamp) >= 13) {
$ms = substr($timestamp, 10, 3);
$datetime->modify("+{$ms} milliseconds");
}
$list[] = $datetime;
}
return (1 === $this->count())
? current($list)
: $list;
}
/**
* The number of generated timestamps in the ULID instance.
* @return int
*/
public function count(): int
{
return count($this->timestamps);
}
private function generateUuidParts(int $milliseconds): int
{
static $lastTime = 0;
$sameTimestamp = $lastTime === $milliseconds;
$lastTime = $milliseconds;
if ($sameTimestamp) {
++$this->randomness[2];
} else {
$this->randomize(false);
}
return $lastTime;
}
private function generateUlidParts(int $milliseconds): array
{
static $lastTime = 0;
$sameTimestamp = $lastTime === $milliseconds;
$lastTime = $milliseconds;
$timestamp = $randomness = '';
// Timestamp
for ($i = 10; $i > 0; $i--) {
$mod = $milliseconds % 32;
$timestamp = static::ENCODING[$mod] . $timestamp;
$milliseconds = ($milliseconds - $mod) / 32;
}
// Randomness
if (count($this->randomness) < 16) {
$this->randomize(true);
}
if ($sameTimestamp) {
for ($i = 15; $i >= 0 && (31 === $this->randomness[$i]); $i--) {
$this->randomness[$i] = 0;
}
++$this->randomness[$i];
}
for ($i = 0; $i < 16; ++$i) {
$randomness .= static::ENCODING[$this->randomness[$i]];
}
return [$timestamp, $randomness];
}
private function randomize(bool $list): void
{
if ($list) {
$this->randomness = [];
for ($i = 0; $i < 16; ++$i) {
$this->randomness[] = mt_rand(0, 31);
}
} else {
$this->randomness = [
mt_rand(0, 0xffff),
mt_rand(0, 0xffff),
mt_rand(0, 1 << 48)
];
}
}
}