|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the ramsey/uuid library |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copyright (c) Ben Ramsey <[email protected]> |
|
9
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
10
|
|
|
* @link https://benramsey.com/projects/ramsey-uuid/ Documentation |
|
11
|
|
|
* @link https://packagist.org/packages/ramsey/uuid Packagist |
|
12
|
|
|
* @link https://github.com/ramsey/uuid GitHub |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Ramsey\Uuid; |
|
16
|
|
|
|
|
17
|
|
|
use DateTime; |
|
18
|
|
|
use Ramsey\Uuid\Converter\NumberConverterInterface; |
|
19
|
|
|
use Ramsey\Uuid\Codec\CodecInterface; |
|
20
|
|
|
use Ramsey\Uuid\Exception\UnsupportedOperationException; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Represents a universally unique identifier (UUID), according to RFC 4122. |
|
24
|
|
|
* |
|
25
|
|
|
* This class provides immutable UUID objects (the Uuid class) and the static |
|
26
|
|
|
* methods `uuid1()`, `uuid3()`, `uuid4()`, and `uuid5()` for generating version |
|
27
|
|
|
* 1, 3, 4, and 5 UUIDs as specified in RFC 4122. |
|
28
|
|
|
* |
|
29
|
|
|
* If all you want is a unique ID, you should probably call `uuid1()` or `uuid4()`. |
|
30
|
|
|
* Note that `uuid1()` may compromise privacy since it creates a UUID containing |
|
31
|
|
|
* the computer’s network address. `uuid4()` creates a random UUID. |
|
32
|
|
|
* |
|
33
|
|
|
* @link http://tools.ietf.org/html/rfc4122 |
|
34
|
|
|
* @link http://en.wikipedia.org/wiki/Universally_unique_identifier |
|
35
|
|
|
* @link http://docs.python.org/3/library/uuid.html |
|
36
|
|
|
* @link http://docs.oracle.com/javase/6/docs/api/java/util/UUID.html |
|
37
|
|
|
*/ |
|
38
|
|
|
class Uuid implements UuidInterface |
|
39
|
|
|
{ |
|
40
|
|
|
/** |
|
41
|
|
|
* When this namespace is specified, the name string is a fully-qualified domain name. |
|
42
|
|
|
* @link http://tools.ietf.org/html/rfc4122#appendix-C |
|
43
|
|
|
*/ |
|
44
|
|
|
const NAMESPACE_DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8'; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* When this namespace is specified, the name string is a URL. |
|
48
|
|
|
* @link http://tools.ietf.org/html/rfc4122#appendix-C |
|
49
|
|
|
*/ |
|
50
|
|
|
const NAMESPACE_URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8'; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* When this namespace is specified, the name string is an ISO OID. |
|
54
|
|
|
* @link http://tools.ietf.org/html/rfc4122#appendix-C |
|
55
|
|
|
*/ |
|
56
|
|
|
const NAMESPACE_OID = '6ba7b812-9dad-11d1-80b4-00c04fd430c8'; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* When this namespace is specified, the name string is an X.500 DN in DER or a text output format. |
|
60
|
|
|
* @link http://tools.ietf.org/html/rfc4122#appendix-C |
|
61
|
|
|
*/ |
|
62
|
|
|
const NAMESPACE_X500 = '6ba7b814-9dad-11d1-80b4-00c04fd430c8'; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* The nil UUID is special form of UUID that is specified to have all 128 bits set to zero. |
|
66
|
|
|
* @link http://tools.ietf.org/html/rfc4122#section-4.1.7 |
|
67
|
|
|
*/ |
|
68
|
|
|
const NIL = '00000000-0000-0000-0000-000000000000'; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Reserved for NCS compatibility. |
|
72
|
|
|
* @link http://tools.ietf.org/html/rfc4122#section-4.1.1 |
|
73
|
|
|
*/ |
|
74
|
|
|
const RESERVED_NCS = 0; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Specifies the UUID layout given in RFC 4122. |
|
78
|
|
|
* @link http://tools.ietf.org/html/rfc4122#section-4.1.1 |
|
79
|
|
|
*/ |
|
80
|
|
|
const RFC_4122 = 2; |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Reserved for Microsoft compatibility. |
|
84
|
|
|
* @link http://tools.ietf.org/html/rfc4122#section-4.1.1 |
|
85
|
|
|
*/ |
|
86
|
|
|
const RESERVED_MICROSOFT = 6; |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Reserved for future definition. |
|
90
|
|
|
* @link http://tools.ietf.org/html/rfc4122#section-4.1.1 |
|
91
|
|
|
*/ |
|
92
|
|
|
const RESERVED_FUTURE = 7; |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Regular expression pattern for matching a valid UUID of any variant. |
|
96
|
|
|
*/ |
|
97
|
|
|
const VALID_PATTERN = '^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$'; |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Version 1 (time-based) UUID object constant identifier |
|
101
|
|
|
*/ |
|
102
|
|
|
const UUID_TYPE_TIME = 1; |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Version 2 (identifier-based) UUID object constant identifier |
|
106
|
|
|
*/ |
|
107
|
|
|
const UUID_TYPE_IDENTIFIER = 2; |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Version 3 (name-based and hashed with MD5) UUID object constant identifier |
|
111
|
|
|
*/ |
|
112
|
|
|
const UUID_TYPE_HASH_MD5 = 3; |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Version 4 (random) UUID object constant identifier |
|
116
|
|
|
*/ |
|
117
|
|
|
const UUID_TYPE_RANDOM = 4; |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Version 5 (name-based and hashed with SHA1) UUID object constant identifier |
|
121
|
|
|
*/ |
|
122
|
|
|
const UUID_TYPE_HASH_SHA1 = 5; |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* The factory to use when creating UUIDs. |
|
126
|
|
|
* @var UuidFactoryInterface |
|
127
|
|
|
*/ |
|
128
|
|
|
private static $factory = null; |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* The codec to use when encoding or decoding UUID strings. |
|
132
|
|
|
* @var CodecInterface |
|
133
|
|
|
*/ |
|
134
|
|
|
protected $codec; |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* The fields that make up this UUID. |
|
138
|
|
|
* |
|
139
|
|
|
* This is initialized to the nil value. |
|
140
|
|
|
* |
|
141
|
|
|
* @var array |
|
142
|
|
|
* @see UuidInterface::getFieldsHex() |
|
143
|
|
|
*/ |
|
144
|
|
|
protected $fields = array( |
|
145
|
|
|
'time_low' => '00000000', |
|
146
|
|
|
'time_mid' => '0000', |
|
147
|
|
|
'time_hi_and_version' => '0000', |
|
148
|
|
|
'clock_seq_hi_and_reserved' => '00', |
|
149
|
|
|
'clock_seq_low' => '00', |
|
150
|
|
|
'node' => '000000000000', |
|
151
|
|
|
); |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* The number converter to use for converting hex values to/from integers. |
|
155
|
|
|
* @var NumberConverterInterface |
|
156
|
|
|
*/ |
|
157
|
|
|
protected $converter; |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Creates a universally unique identifier (UUID) from an array of fields. |
|
161
|
|
|
* |
|
162
|
|
|
* Unless you're making advanced use of this library to generate identifiers |
|
163
|
|
|
* that deviate from RFC 4122, you probably do not want to instantiate a |
|
164
|
|
|
* UUID directly. Use the static methods, instead: |
|
165
|
|
|
* |
|
166
|
|
|
* ``` |
|
167
|
|
|
* use Ramsey\Uuid\Uuid; |
|
168
|
|
|
* |
|
169
|
|
|
* $timeBasedUuid = Uuid::uuid1(); |
|
170
|
|
|
* $namespaceMd5Uuid = Uuid::uuid3(Uuid::NAMESPACE_URL, 'http://php.net/'); |
|
171
|
|
|
* $randomUuid = Uuid::uuid4(); |
|
172
|
|
|
* $namespaceSha1Uuid = Uuid::uuid5(Uuid::NAMESPACE_URL, 'http://php.net/'); |
|
173
|
|
|
* ``` |
|
174
|
|
|
* |
|
175
|
|
|
* @param array $fields An array of fields from which to construct a UUID; |
|
176
|
|
|
* see {@see \Ramsey\Uuid\UuidInterface::getFieldsHex()} for array structure. |
|
177
|
|
|
* @param NumberConverterInterface $converter The number converter to use |
|
178
|
|
|
* for converting hex values to/from integers. |
|
179
|
|
|
* @param CodecInterface $codec The codec to use when encoding or decoding |
|
180
|
|
|
* UUID strings. |
|
181
|
|
|
*/ |
|
182
|
|
|
public function __construct( |
|
183
|
|
|
array $fields, |
|
184
|
|
|
NumberConverterInterface $converter, |
|
185
|
|
|
CodecInterface $codec |
|
186
|
|
|
) { |
|
187
|
|
|
$this->fields = $fields; |
|
188
|
|
|
$this->codec = $codec; |
|
189
|
|
|
$this->converter = $converter; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Converts this UUID object to a string when the object is used in any |
|
194
|
|
|
* string context. |
|
195
|
|
|
* |
|
196
|
|
|
* @return string |
|
197
|
|
|
* @link http://www.php.net/manual/en/language.oop5.magic.php#object.tostring |
|
198
|
|
|
*/ |
|
199
|
|
|
public function __toString() |
|
200
|
|
|
{ |
|
201
|
|
|
return $this->toString(); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Converts this UUID object to a string when the object is serialized |
|
206
|
|
|
* with `json_encode()` |
|
207
|
|
|
* |
|
208
|
|
|
* @return string |
|
209
|
|
|
* @link http://php.net/manual/en/class.jsonserializable.php |
|
210
|
|
|
*/ |
|
211
|
|
|
public function jsonSerialize() |
|
212
|
|
|
{ |
|
213
|
|
|
return $this->toString(); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Converts this UUID object to a string when the object is serialized |
|
218
|
|
|
* with `serialize()` |
|
219
|
|
|
* |
|
220
|
|
|
* @return string |
|
221
|
|
|
* @link http://php.net/manual/en/class.serializable.php |
|
222
|
|
|
*/ |
|
223
|
|
|
public function serialize() |
|
224
|
|
|
{ |
|
225
|
|
|
return $this->toString(); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
/** |
|
229
|
|
|
* Re-constructs the object from its serialized form. |
|
230
|
|
|
* |
|
231
|
|
|
* @param string $serialized |
|
232
|
|
|
* @link http://php.net/manual/en/class.serializable.php |
|
233
|
|
|
* @throws \Ramsey\Uuid\Exception\InvalidUuidStringException |
|
234
|
|
|
*/ |
|
235
|
|
|
public function unserialize($serialized) |
|
236
|
|
|
{ |
|
237
|
|
|
$uuid = self::fromString($serialized); |
|
238
|
|
|
$this->codec = $uuid->codec; |
|
|
|
|
|
|
239
|
|
|
$this->converter = $uuid->converter; |
|
|
|
|
|
|
240
|
|
|
$this->fields = $uuid->fields; |
|
|
|
|
|
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
public function compareTo(UuidInterface $other) |
|
244
|
|
|
{ |
|
245
|
|
|
$comparison = 0; |
|
246
|
|
|
|
|
247
|
|
|
if ($this->getMostSignificantBitsHex() < $other->getMostSignificantBitsHex()) { |
|
248
|
|
|
$comparison = -1; |
|
249
|
|
|
} elseif ($this->getMostSignificantBitsHex() > $other->getMostSignificantBitsHex()) { |
|
250
|
|
|
$comparison = 1; |
|
251
|
|
|
} elseif ($this->getLeastSignificantBitsHex() < $other->getLeastSignificantBitsHex()) { |
|
252
|
|
|
$comparison = -1; |
|
253
|
|
|
} elseif ($this->getLeastSignificantBitsHex() > $other->getLeastSignificantBitsHex()) { |
|
254
|
|
|
$comparison = 1; |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
return $comparison; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
public function equals($other) |
|
261
|
|
|
{ |
|
262
|
|
|
if (!($other instanceof UuidInterface)) { |
|
263
|
|
|
return false; |
|
264
|
|
|
} |
|
265
|
|
|
|
|
266
|
|
|
return ($this->compareTo($other) == 0); |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
public function getBytes() |
|
270
|
|
|
{ |
|
271
|
|
|
return $this->codec->encodeBinary($this); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* Returns the high field of the clock sequence multiplexed with the variant |
|
276
|
|
|
* (bits 65-72 of the UUID). |
|
277
|
|
|
* |
|
278
|
|
|
* @return int Unsigned 8-bit integer value of clock_seq_hi_and_reserved |
|
279
|
|
|
*/ |
|
280
|
|
|
public function getClockSeqHiAndReserved() |
|
281
|
|
|
{ |
|
282
|
|
|
return hexdec($this->getClockSeqHiAndReservedHex()); |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
public function getClockSeqHiAndReservedHex() |
|
286
|
|
|
{ |
|
287
|
|
|
return $this->fields['clock_seq_hi_and_reserved']; |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
/** |
|
291
|
|
|
* Returns the low field of the clock sequence (bits 73-80 of the UUID). |
|
292
|
|
|
* |
|
293
|
|
|
* @return int Unsigned 8-bit integer value of clock_seq_low |
|
294
|
|
|
*/ |
|
295
|
|
|
public function getClockSeqLow() |
|
296
|
|
|
{ |
|
297
|
|
|
return hexdec($this->getClockSeqLowHex()); |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
public function getClockSeqLowHex() |
|
301
|
|
|
{ |
|
302
|
|
|
return $this->fields['clock_seq_low']; |
|
303
|
|
|
} |
|
304
|
|
|
|
|
305
|
|
|
/** |
|
306
|
|
|
* Returns the clock sequence value associated with this UUID. |
|
307
|
|
|
* |
|
308
|
|
|
* For UUID version 1, the clock sequence is used to help avoid |
|
309
|
|
|
* duplicates that could arise when the clock is set backwards in time |
|
310
|
|
|
* or if the node ID changes. |
|
311
|
|
|
* |
|
312
|
|
|
* For UUID version 3 or 5, the clock sequence is a 14-bit value |
|
313
|
|
|
* constructed from a name as described in RFC 4122, Section 4.3. |
|
314
|
|
|
* |
|
315
|
|
|
* For UUID version 4, clock sequence is a randomly or pseudo-randomly |
|
316
|
|
|
* generated 14-bit value as described in RFC 4122, Section 4.4. |
|
317
|
|
|
* |
|
318
|
|
|
* @return int Unsigned 14-bit integer value of clock sequence |
|
319
|
|
|
* @link http://tools.ietf.org/html/rfc4122#section-4.1.5 |
|
320
|
|
|
*/ |
|
321
|
|
|
public function getClockSequence() |
|
322
|
|
|
{ |
|
323
|
|
|
return (($this->getClockSeqHiAndReserved() & 0x3f) << 8) |
|
324
|
|
|
| $this->getClockSeqLow(); |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
public function getClockSequenceHex() |
|
328
|
|
|
{ |
|
329
|
|
|
return sprintf('%04x', $this->getClockSequence()); |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
public function getNumberConverter() |
|
333
|
|
|
{ |
|
334
|
|
|
return $this->converter; |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
/** |
|
338
|
|
|
* @inheritdoc |
|
339
|
|
|
*/ |
|
340
|
|
|
public function getDateTime() |
|
341
|
|
|
{ |
|
342
|
|
|
if ($this->getVersion() != 1) { |
|
343
|
|
|
throw new UnsupportedOperationException('Not a time-based UUID'); |
|
344
|
|
|
} |
|
345
|
|
|
|
|
346
|
|
|
$unixTime = ($this->getTimestamp() - 0x01b21dd213814000) / 1e7; |
|
347
|
|
|
$unixTime = number_format($unixTime, 0, '', ''); |
|
348
|
|
|
|
|
349
|
|
|
return new DateTime("@{$unixTime}"); |
|
350
|
|
|
} |
|
351
|
|
|
|
|
352
|
|
|
/** |
|
353
|
|
|
* Returns an array of the fields of this UUID, with keys named according |
|
354
|
|
|
* to the RFC 4122 names for the fields. |
|
355
|
|
|
* |
|
356
|
|
|
* * **time_low**: The low field of the timestamp, an unsigned 32-bit integer |
|
357
|
|
|
* * **time_mid**: The middle field of the timestamp, an unsigned 16-bit integer |
|
358
|
|
|
* * **time_hi_and_version**: The high field of the timestamp multiplexed with |
|
359
|
|
|
* the version number, an unsigned 16-bit integer |
|
360
|
|
|
* * **clock_seq_hi_and_reserved**: The high field of the clock sequence |
|
361
|
|
|
* multiplexed with the variant, an unsigned 8-bit integer |
|
362
|
|
|
* * **clock_seq_low**: The low field of the clock sequence, an unsigned |
|
363
|
|
|
* 8-bit integer |
|
364
|
|
|
* * **node**: The spatially unique node identifier, an unsigned 48-bit |
|
365
|
|
|
* integer |
|
366
|
|
|
* |
|
367
|
|
|
* @return array The UUID fields represented as integer values |
|
368
|
|
|
* @link http://tools.ietf.org/html/rfc4122#section-4.1.2 |
|
369
|
|
|
*/ |
|
370
|
|
|
public function getFields() |
|
371
|
|
|
{ |
|
372
|
|
|
return array( |
|
373
|
|
|
'time_low' => $this->getTimeLow(), |
|
374
|
|
|
'time_mid' => $this->getTimeMid(), |
|
375
|
|
|
'time_hi_and_version' => $this->getTimeHiAndVersion(), |
|
376
|
|
|
'clock_seq_hi_and_reserved' => $this->getClockSeqHiAndReserved(), |
|
377
|
|
|
'clock_seq_low' => $this->getClockSeqLow(), |
|
378
|
|
|
'node' => $this->getNode(), |
|
379
|
|
|
); |
|
380
|
|
|
} |
|
381
|
|
|
|
|
382
|
|
|
public function getFieldsHex() |
|
383
|
|
|
{ |
|
384
|
|
|
return $this->fields; |
|
385
|
|
|
} |
|
386
|
|
|
|
|
387
|
|
|
public function getHex() |
|
388
|
|
|
{ |
|
389
|
|
|
return str_replace('-', '', $this->toString()); |
|
390
|
|
|
} |
|
391
|
|
|
|
|
392
|
|
|
/** |
|
393
|
|
|
* @inheritdoc |
|
394
|
|
|
*/ |
|
395
|
|
|
public function getInteger() |
|
396
|
|
|
{ |
|
397
|
|
|
return $this->converter->fromHex($this->getHex()); |
|
398
|
|
|
} |
|
399
|
|
|
|
|
400
|
|
|
/** |
|
401
|
|
|
* Returns the least significant 64 bits of this UUID's 128 bit value. |
|
402
|
|
|
* |
|
403
|
|
|
* @return mixed Converted representation of the unsigned 64-bit integer value |
|
404
|
|
|
* @throws \Ramsey\Uuid\Exception\UnsatisfiedDependencyException if `Moontoast\Math\BigNumber` is not present |
|
405
|
|
|
*/ |
|
406
|
|
|
public function getLeastSignificantBits() |
|
407
|
|
|
{ |
|
408
|
|
|
return $this->converter->fromHex($this->getLeastSignificantBitsHex()); |
|
409
|
|
|
} |
|
410
|
|
|
|
|
411
|
|
|
public function getLeastSignificantBitsHex() |
|
412
|
|
|
{ |
|
413
|
|
|
return sprintf( |
|
414
|
|
|
'%02s%02s%012s', |
|
415
|
|
|
$this->fields['clock_seq_hi_and_reserved'], |
|
416
|
|
|
$this->fields['clock_seq_low'], |
|
417
|
|
|
$this->fields['node'] |
|
418
|
|
|
); |
|
419
|
|
|
} |
|
420
|
|
|
|
|
421
|
|
|
/** |
|
422
|
|
|
* Returns the most significant 64 bits of this UUID's 128 bit value. |
|
423
|
|
|
* |
|
424
|
|
|
* @return mixed Converted representation of the unsigned 64-bit integer value |
|
425
|
|
|
* @throws \Ramsey\Uuid\Exception\UnsatisfiedDependencyException if `Moontoast\Math\BigNumber` is not present |
|
426
|
|
|
*/ |
|
427
|
|
|
public function getMostSignificantBits() |
|
428
|
|
|
{ |
|
429
|
|
|
return $this->converter->fromHex($this->getMostSignificantBitsHex()); |
|
430
|
|
|
} |
|
431
|
|
|
|
|
432
|
|
|
public function getMostSignificantBitsHex() |
|
433
|
|
|
{ |
|
434
|
|
|
return sprintf( |
|
435
|
|
|
'%08s%04s%04s', |
|
436
|
|
|
$this->fields['time_low'], |
|
437
|
|
|
$this->fields['time_mid'], |
|
438
|
|
|
$this->fields['time_hi_and_version'] |
|
439
|
|
|
); |
|
440
|
|
|
} |
|
441
|
|
|
|
|
442
|
|
|
/** |
|
443
|
|
|
* Returns the node value associated with this UUID |
|
444
|
|
|
* |
|
445
|
|
|
* For UUID version 1, the node field consists of an IEEE 802 MAC |
|
446
|
|
|
* address, usually the host address. For systems with multiple IEEE |
|
447
|
|
|
* 802 addresses, any available one can be used. The lowest addressed |
|
448
|
|
|
* octet (octet number 10) contains the global/local bit and the |
|
449
|
|
|
* unicast/multicast bit, and is the first octet of the address |
|
450
|
|
|
* transmitted on an 802.3 LAN. |
|
451
|
|
|
* |
|
452
|
|
|
* For systems with no IEEE address, a randomly or pseudo-randomly |
|
453
|
|
|
* generated value may be used; see RFC 4122, Section 4.5. The |
|
454
|
|
|
* multicast bit must be set in such addresses, in order that they |
|
455
|
|
|
* will never conflict with addresses obtained from network cards. |
|
456
|
|
|
* |
|
457
|
|
|
* For UUID version 3 or 5, the node field is a 48-bit value constructed |
|
458
|
|
|
* from a name as described in RFC 4122, Section 4.3. |
|
459
|
|
|
* |
|
460
|
|
|
* For UUID version 4, the node field is a randomly or pseudo-randomly |
|
461
|
|
|
* generated 48-bit value as described in RFC 4122, Section 4.4. |
|
462
|
|
|
* |
|
463
|
|
|
* @return int Unsigned 48-bit integer value of node |
|
464
|
|
|
* @link http://tools.ietf.org/html/rfc4122#section-4.1.6 |
|
465
|
|
|
*/ |
|
466
|
|
|
public function getNode() |
|
467
|
|
|
{ |
|
468
|
|
|
return hexdec($this->getNodeHex()); |
|
469
|
|
|
} |
|
470
|
|
|
|
|
471
|
|
|
public function getNodeHex() |
|
472
|
|
|
{ |
|
473
|
|
|
return $this->fields['node']; |
|
474
|
|
|
} |
|
475
|
|
|
|
|
476
|
|
|
/** |
|
477
|
|
|
* Returns the high field of the timestamp multiplexed with the version |
|
478
|
|
|
* number (bits 49-64 of the UUID). |
|
479
|
|
|
* |
|
480
|
|
|
* @return int Unsigned 16-bit integer value of time_hi_and_version |
|
481
|
|
|
*/ |
|
482
|
|
|
public function getTimeHiAndVersion() |
|
483
|
|
|
{ |
|
484
|
|
|
return hexdec($this->getTimeHiAndVersionHex()); |
|
485
|
|
|
} |
|
486
|
|
|
|
|
487
|
|
|
public function getTimeHiAndVersionHex() |
|
488
|
|
|
{ |
|
489
|
|
|
return $this->fields['time_hi_and_version']; |
|
490
|
|
|
} |
|
491
|
|
|
|
|
492
|
|
|
/** |
|
493
|
|
|
* Returns the low field of the timestamp (the first 32 bits of the UUID). |
|
494
|
|
|
* |
|
495
|
|
|
* @return int Unsigned 32-bit integer value of time_low |
|
496
|
|
|
*/ |
|
497
|
|
|
public function getTimeLow() |
|
498
|
|
|
{ |
|
499
|
|
|
return hexdec($this->getTimeLowHex()); |
|
500
|
|
|
} |
|
501
|
|
|
|
|
502
|
|
|
public function getTimeLowHex() |
|
503
|
|
|
{ |
|
504
|
|
|
return $this->fields['time_low']; |
|
505
|
|
|
} |
|
506
|
|
|
|
|
507
|
|
|
/** |
|
508
|
|
|
* Returns the middle field of the timestamp (bits 33-48 of the UUID). |
|
509
|
|
|
* |
|
510
|
|
|
* @return int Unsigned 16-bit integer value of time_mid |
|
511
|
|
|
*/ |
|
512
|
|
|
public function getTimeMid() |
|
513
|
|
|
{ |
|
514
|
|
|
return hexdec($this->getTimeMidHex()); |
|
515
|
|
|
} |
|
516
|
|
|
|
|
517
|
|
|
public function getTimeMidHex() |
|
518
|
|
|
{ |
|
519
|
|
|
return $this->fields['time_mid']; |
|
520
|
|
|
} |
|
521
|
|
|
|
|
522
|
|
|
/** |
|
523
|
|
|
* Returns the timestamp value associated with this UUID. |
|
524
|
|
|
* |
|
525
|
|
|
* The 60 bit timestamp value is constructed from the time_low, |
|
526
|
|
|
* time_mid, and time_hi fields of this UUID. The resulting |
|
527
|
|
|
* timestamp is measured in 100-nanosecond units since midnight, |
|
528
|
|
|
* October 15, 1582 UTC. |
|
529
|
|
|
* |
|
530
|
|
|
* The timestamp value is only meaningful in a time-based UUID, which |
|
531
|
|
|
* has version type 1. If this UUID is not a time-based UUID then |
|
532
|
|
|
* this method throws UnsupportedOperationException. |
|
533
|
|
|
* |
|
534
|
|
|
* @return int Unsigned 60-bit integer value of the timestamp |
|
535
|
|
|
* @throws UnsupportedOperationException If this UUID is not a version 1 UUID |
|
536
|
|
|
* @link http://tools.ietf.org/html/rfc4122#section-4.1.4 |
|
537
|
|
|
*/ |
|
538
|
|
|
public function getTimestamp() |
|
539
|
|
|
{ |
|
540
|
|
|
if ($this->getVersion() != 1) { |
|
541
|
|
|
throw new UnsupportedOperationException('Not a time-based UUID'); |
|
542
|
|
|
} |
|
543
|
|
|
|
|
544
|
|
|
return hexdec($this->getTimestampHex()); |
|
545
|
|
|
} |
|
546
|
|
|
|
|
547
|
|
|
/** |
|
548
|
|
|
* @inheritdoc |
|
549
|
|
|
*/ |
|
550
|
|
|
public function getTimestampHex() |
|
551
|
|
|
{ |
|
552
|
|
|
if ($this->getVersion() != 1) { |
|
553
|
|
|
throw new UnsupportedOperationException('Not a time-based UUID'); |
|
554
|
|
|
} |
|
555
|
|
|
|
|
556
|
|
|
return sprintf( |
|
557
|
|
|
'%03x%04s%08s', |
|
558
|
|
|
($this->getTimeHiAndVersion() & 0x0fff), |
|
559
|
|
|
$this->fields['time_mid'], |
|
560
|
|
|
$this->fields['time_low'] |
|
561
|
|
|
); |
|
562
|
|
|
} |
|
563
|
|
|
|
|
564
|
|
|
public function getUrn() |
|
565
|
|
|
{ |
|
566
|
|
|
return 'urn:uuid:' . $this->toString(); |
|
567
|
|
|
} |
|
568
|
|
|
|
|
569
|
|
|
public function getVariant() |
|
570
|
|
|
{ |
|
571
|
|
|
$clockSeq = $this->getClockSeqHiAndReserved(); |
|
572
|
|
|
if (0 === ($clockSeq & 0x80)) { |
|
573
|
|
|
$variant = self::RESERVED_NCS; |
|
574
|
|
|
} elseif (0 === ($clockSeq & 0x40)) { |
|
575
|
|
|
$variant = self::RFC_4122; |
|
576
|
|
|
} elseif (0 === ($clockSeq & 0x20)) { |
|
577
|
|
|
$variant = self::RESERVED_MICROSOFT; |
|
578
|
|
|
} else { |
|
579
|
|
|
$variant = self::RESERVED_FUTURE; |
|
580
|
|
|
} |
|
581
|
|
|
|
|
582
|
|
|
return $variant; |
|
583
|
|
|
} |
|
584
|
|
|
|
|
585
|
|
|
public function getVersion() |
|
586
|
|
|
{ |
|
587
|
|
|
if ($this->getVariant() == self::RFC_4122) { |
|
588
|
|
|
return (int) (($this->getTimeHiAndVersion() >> 12) & 0x0f); |
|
589
|
|
|
} |
|
590
|
|
|
|
|
591
|
|
|
return null; |
|
592
|
|
|
} |
|
593
|
|
|
|
|
594
|
|
|
public function toString() |
|
595
|
|
|
{ |
|
596
|
|
|
return $this->codec->encode($this); |
|
597
|
|
|
} |
|
598
|
|
|
|
|
599
|
|
|
/** |
|
600
|
|
|
* Returns the currently set factory used to create UUIDs. |
|
601
|
|
|
* |
|
602
|
|
|
* @return UuidFactoryInterface |
|
603
|
|
|
*/ |
|
604
|
|
|
public static function getFactory() |
|
605
|
|
|
{ |
|
606
|
|
|
if (!self::$factory) { |
|
607
|
|
|
self::$factory = new UuidFactory(); |
|
608
|
|
|
} |
|
609
|
|
|
|
|
610
|
|
|
return self::$factory; |
|
611
|
|
|
} |
|
612
|
|
|
|
|
613
|
|
|
/** |
|
614
|
|
|
* Sets the factory used to create UUIDs. |
|
615
|
|
|
* |
|
616
|
|
|
* @param UuidFactoryInterface $factory |
|
617
|
|
|
*/ |
|
618
|
|
|
public static function setFactory(UuidFactoryInterface $factory) |
|
619
|
|
|
{ |
|
620
|
|
|
self::$factory = $factory; |
|
621
|
|
|
} |
|
622
|
|
|
|
|
623
|
|
|
/** |
|
624
|
|
|
* Creates a UUID from a byte string. |
|
625
|
|
|
* |
|
626
|
|
|
* @param string $bytes |
|
627
|
|
|
* @return UuidInterface |
|
628
|
|
|
* @throws \Ramsey\Uuid\Exception\InvalidUuidStringException |
|
629
|
|
|
* @throws \InvalidArgumentException |
|
630
|
|
|
*/ |
|
631
|
|
|
public static function fromBytes($bytes) |
|
632
|
|
|
{ |
|
633
|
|
|
return self::getFactory()->fromBytes($bytes); |
|
634
|
|
|
} |
|
635
|
|
|
|
|
636
|
|
|
/** |
|
637
|
|
|
* Creates a UUID from the string standard representation. |
|
638
|
|
|
* |
|
639
|
|
|
* @param string $name A string that specifies a UUID |
|
640
|
|
|
* @return UuidInterface |
|
641
|
|
|
* @throws \Ramsey\Uuid\Exception\InvalidUuidStringException |
|
642
|
|
|
*/ |
|
643
|
|
|
public static function fromString($name) |
|
644
|
|
|
{ |
|
645
|
|
|
return self::getFactory()->fromString($name); |
|
646
|
|
|
} |
|
647
|
|
|
|
|
648
|
|
|
/** |
|
649
|
|
|
* Creates a UUID from a 128-bit integer string. |
|
650
|
|
|
* |
|
651
|
|
|
* @param string $integer String representation of 128-bit integer |
|
652
|
|
|
* @return UuidInterface |
|
653
|
|
|
* @throws \Ramsey\Uuid\Exception\UnsatisfiedDependencyException if `Moontoast\Math\BigNumber` is not present |
|
654
|
|
|
* @throws \Ramsey\Uuid\Exception\InvalidUuidStringException |
|
655
|
|
|
*/ |
|
656
|
|
|
public static function fromInteger($integer) |
|
657
|
|
|
{ |
|
658
|
|
|
return self::getFactory()->fromInteger($integer); |
|
659
|
|
|
} |
|
660
|
|
|
|
|
661
|
|
|
/** |
|
662
|
|
|
* Check if a string is a valid UUID. |
|
663
|
|
|
* |
|
664
|
|
|
* @param string $uuid The string UUID to test |
|
665
|
|
|
* @return boolean |
|
666
|
|
|
*/ |
|
667
|
|
|
public static function isValid($uuid) |
|
668
|
|
|
{ |
|
669
|
|
|
$uuid = str_replace(array('urn:', 'uuid:', '{', '}'), '', $uuid); |
|
670
|
|
|
|
|
671
|
|
|
if ($uuid == self::NIL) { |
|
672
|
|
|
return true; |
|
673
|
|
|
} |
|
674
|
|
|
|
|
675
|
|
|
if (!preg_match('/' . self::VALID_PATTERN . '/D', $uuid)) { |
|
676
|
|
|
return false; |
|
677
|
|
|
} |
|
678
|
|
|
|
|
679
|
|
|
return true; |
|
680
|
|
|
} |
|
681
|
|
|
|
|
682
|
|
|
/** |
|
683
|
|
|
* Generate a version 1 UUID from a host ID, sequence number, and the current time. |
|
684
|
|
|
* |
|
685
|
|
|
* @param int|string $node A 48-bit number representing the hardware address |
|
686
|
|
|
* This number may be represented as an integer or a hexadecimal string. |
|
687
|
|
|
* @param int $clockSeq A 14-bit number used to help avoid duplicates that |
|
688
|
|
|
* could arise when the clock is set backwards in time or if the node ID |
|
689
|
|
|
* changes. |
|
690
|
|
|
* @return UuidInterface |
|
691
|
|
|
* @throws \Ramsey\Uuid\Exception\UnsatisfiedDependencyException if called on a 32-bit system and |
|
692
|
|
|
* `Moontoast\Math\BigNumber` is not present |
|
693
|
|
|
* @throws \InvalidArgumentException |
|
694
|
|
|
* @throws \Exception if it was not possible to gather sufficient entropy |
|
695
|
|
|
*/ |
|
696
|
|
|
public static function uuid1($node = null, $clockSeq = null) |
|
697
|
|
|
{ |
|
698
|
|
|
return self::getFactory()->uuid1($node, $clockSeq); |
|
699
|
|
|
} |
|
700
|
|
|
|
|
701
|
|
|
/** |
|
702
|
|
|
* Generate a version 3 UUID based on the MD5 hash of a namespace identifier |
|
703
|
|
|
* (which is a UUID) and a name (which is a string). |
|
704
|
|
|
* |
|
705
|
|
|
* @param string $ns The UUID namespace in which to create the named UUID |
|
706
|
|
|
* @param string $name The name to create a UUID for |
|
707
|
|
|
* @return UuidInterface |
|
708
|
|
|
* @throws \Ramsey\Uuid\Exception\InvalidUuidStringException |
|
709
|
|
|
*/ |
|
710
|
|
|
public static function uuid3($ns, $name) |
|
711
|
|
|
{ |
|
712
|
|
|
return self::getFactory()->uuid3($ns, $name); |
|
713
|
|
|
} |
|
714
|
|
|
|
|
715
|
|
|
/** |
|
716
|
|
|
* Generate a version 4 (random) UUID. |
|
717
|
|
|
* |
|
718
|
|
|
* @return UuidInterface |
|
719
|
|
|
* @throws \Ramsey\Uuid\Exception\UnsatisfiedDependencyException if `Moontoast\Math\BigNumber` is not present |
|
720
|
|
|
* @throws \InvalidArgumentException |
|
721
|
|
|
* @throws \Exception |
|
722
|
|
|
*/ |
|
723
|
|
|
public static function uuid4() |
|
724
|
|
|
{ |
|
725
|
|
|
return self::getFactory()->uuid4(); |
|
726
|
|
|
} |
|
727
|
|
|
|
|
728
|
|
|
/** |
|
729
|
|
|
* Generate a version 5 UUID based on the SHA-1 hash of a namespace |
|
730
|
|
|
* identifier (which is a UUID) and a name (which is a string). |
|
731
|
|
|
* |
|
732
|
|
|
* @param string $ns The UUID namespace in which to create the named UUID |
|
733
|
|
|
* @param string $name The name to create a UUID for |
|
734
|
|
|
* @return UuidInterface |
|
735
|
|
|
* @throws \Ramsey\Uuid\Exception\InvalidUuidStringException |
|
736
|
|
|
*/ |
|
737
|
|
|
public static function uuid5($ns, $name) |
|
738
|
|
|
{ |
|
739
|
|
|
return self::getFactory()->uuid5($ns, $name); |
|
740
|
|
|
} |
|
741
|
|
|
} |
|
742
|
|
|
|