forked from ramsey/uuid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUuid.php
846 lines (769 loc) · 27.1 KB
/
Uuid.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
<?php
/**
* This file is part of the Ramsey\Uuid library
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @copyright Copyright (c) 2012-2014 Ben Ramsey <http://benramsey.com>
* @license http://opensource.org/licenses/MIT MIT
*/
namespace Ramsey\Uuid;
use InvalidArgumentException;
use Ramsey\Uuid\Converter\NumberConverterInterface;
/**
* Represents a universally unique identifier (UUID), according to RFC 4122
*
* This class provides immutable UUID objects (the Uuid class) and the static
* methods `uuid1()`, `uuid3()`, `uuid4()`, and `uuid5()` for generating version
* 1, 3, 4, and 5 UUIDs as specified in RFC 4122.
*
* If all you want is a unique ID, you should probably call `uuid1()` or `uuid4()`.
* Note that `uuid1()` may compromise privacy since it creates a UUID containing
* the computer’s network address. `uuid4()` creates a random UUID.
*
* @link http://tools.ietf.org/html/rfc4122
* @link http://en.wikipedia.org/wiki/Universally_unique_identifier
* @link http://docs.python.org/3/library/uuid.html
* @link http://docs.oracle.com/javase/6/docs/api/java/util/UUID.html
*/
class Uuid implements UuidInterface, \JsonSerializable
{
/**
* When this namespace is specified, the name string is a fully-qualified domain name.
* @link http://tools.ietf.org/html/rfc4122#appendix-C
*/
const NAMESPACE_DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
/**
* When this namespace is specified, the name string is a URL.
* @link http://tools.ietf.org/html/rfc4122#appendix-C
*/
const NAMESPACE_URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8';
/**
* When this namespace is specified, the name string is an ISO OID.
* @link http://tools.ietf.org/html/rfc4122#appendix-C
*/
const NAMESPACE_OID = '6ba7b812-9dad-11d1-80b4-00c04fd430c8';
/**
* When this namespace is specified, the name string is an X.500 DN in DER or a text output format.
* @link http://tools.ietf.org/html/rfc4122#appendix-C
*/
const NAMESPACE_X500 = '6ba7b814-9dad-11d1-80b4-00c04fd430c8';
/**
* The nil UUID is special form of UUID that is specified to have all 128 bits set to zero.
* @link http://tools.ietf.org/html/rfc4122#section-4.1.7
*/
const NIL = '00000000-0000-0000-0000-000000000000';
/**
* Reserved for NCS compatibility.
* @link http://tools.ietf.org/html/rfc4122#section-4.1.1
*/
const RESERVED_NCS = 0;
/**
* Specifies the UUID layout given in RFC 4122.
* @link http://tools.ietf.org/html/rfc4122#section-4.1.1
*/
const RFC_4122 = 2;
/**
* Reserved for Microsoft compatibility.
* @link http://tools.ietf.org/html/rfc4122#section-4.1.1
*/
const RESERVED_MICROSOFT = 6;
/**
* Reserved for future definition.
* @link http://tools.ietf.org/html/rfc4122#section-4.1.1
*/
const RESERVED_FUTURE = 7;
/**
* Regular expression pattern for matching a valid UUID of any variant.
*/
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}$';
/**
* Version of the Ramsey\Uuid package
*/
const VERSION = '3.0.x-dev';
/**
*
* @var UuidFactoryInterface
*/
private static $factory = null;
/**
* String codec
* @var CodecInterface
*/
protected $codec;
/**
* The fields that make up this UUID
*
* This is initialized to the nil value.
*
* @var array
* @link Ramsey.Uuid.Uuid.html#method_getFields
*/
protected $fields = array(
'time_low' => '00000000',
'time_mid' => '0000',
'time_hi_and_version' => '0000',
'clock_seq_hi_and_reserved' => '00',
'clock_seq_low' => '00',
'node' => '000000000000',
);
protected $converter;
/**
* Creates a universally unique identifier (UUID) from an array of fields.
*
* Protected to prevent direct instantiation. Use static methods to create
* UUIDs.
*
* @param array $fields
* @param CodecInterface $codec String codec
* @link Ramsey.Uuid.Uuid.html#method_getFields
*/
public function __construct(array $fields, NumberConverterInterface $converter, CodecInterface $codec)
{
$this->fields = $fields;
$this->codec = $codec;
$this->converter = $converter;
}
/**
* Converts this UUID object to a string when the object is used in any
* string context
*
* @return string
* @link http://www.php.net/manual/en/language.oop5.magic.php#object.tostring
*/
public function __toString()
{
return $this->toString();
}
/**
* Converts this UUID object to a string when the object is serialized
* with json_encode()
*
* @link http://php.net/manual/en/class.jsonserializable.php
*
* @return string
*/
public function jsonSerialize()
{
return $this->toString();
}
/**
* Compares this UUID with the specified UUID.
*
* The first of two UUIDs is greater than the second if the most
* significant field in which the UUIDs differ is greater for the first
* UUID.
*
* Q. What's the value of being able to sort UUIDs?<br>
* A. Use them as keys in a B-Tree or similar mapping.
*
* @param Uuid $uuid UUID to which this UUID is to be compared
* @return int -1, 0 or 1 as this UUID is less than, equal to, or greater than $uuid
*/
public function compareTo(UuidInterface $uuid)
{
$comparison = 0;
if ($this->getMostSignificantBitsHex() < $uuid->getMostSignificantBitsHex()) {
$comparison = -1;
} elseif ($this->getMostSignificantBitsHex() > $uuid->getMostSignificantBitsHex()) {
$comparison = 1;
} elseif ($this->getLeastSignificantBitsHex() < $uuid->getLeastSignificantBitsHex()) {
$comparison = -1;
} elseif ($this->getLeastSignificantBitsHex() > $uuid->getLeastSignificantBitsHex()) {
$comparison = 1;
}
return $comparison;
}
/**
* Compares this object to the specified object.
*
* The result is true if and only if the argument is not null, is a UUID
* object, has the same variant, and contains the same value, bit for bit,
* as this UUID.
*
* @param object $obj
* @return bool True if $obj is equal to this UUID
*/
public function equals($obj)
{
if (!($obj instanceof Uuid)) {
return false;
}
return ($this->compareTo($obj) == 0);
}
/**
* Returns the UUID as a 16-byte string (containing the six integer fields
* in big-endian byte order)
*
* @return string
*/
public function getBytes()
{
return $this->codec->encodeBinary($this);
}
/**
* Returns the high field of the clock sequence multiplexed with the variant
* (bits 65-72 of the UUID).
*
* @return int Unsigned 8-bit integer value of clock_seq_hi_and_reserved
*/
public function getClockSeqHiAndReserved()
{
return hexdec($this->getClockSeqHiAndReservedHex());
}
/**
* Returns the high field of the clock sequence multiplexed with the variant
* (bits 65-72 of the UUID).
*
* @return string Hexadecimal value of clock_seq_hi_and_reserved
*/
public function getClockSeqHiAndReservedHex()
{
return $this->fields['clock_seq_hi_and_reserved'];
}
/**
* Returns the low field of the clock sequence (bits 73-80 of the UUID).
*
* @return int Unsigned 8-bit integer value of clock_seq_low
*/
public function getClockSeqLow()
{
return hexdec($this->getClockSeqLowHex());
}
/**
* Returns the low field of the clock sequence (bits 73-80 of the UUID).
*
* @return string Hexadecimal value of clock_seq_low
*/
public function getClockSeqLowHex()
{
return $this->fields['clock_seq_low'];
}
/**
* Returns the clock sequence value associated with this UUID.
*
* For UUID version 1, the clock sequence is used to help avoid
* duplicates that could arise when the clock is set backwards in time
* or if the node ID changes.
*
* For UUID version 3 or 5, the clock sequence is a 14-bit value
* constructed from a name as described in RFC 4122, Section 4.3.
*
* For UUID version 4, clock sequence is a randomly or pseudo-randomly
* generated 14-bit value as described in RFC 4122, Section 4.4.
*
* @return int Unsigned 14-bit integer value of clock sequence
* @link http://tools.ietf.org/html/rfc4122#section-4.1.5
*/
public function getClockSequence()
{
return (($this->getClockSeqHiAndReserved() & 0x3f) << 8)
| $this->getClockSeqLow();
}
/**
* Returns the clock sequence value associated with this UUID.
*
* @return string Hexadecimal value of clock sequence
*/
public function getClockSequenceHex()
{
return sprintf('%04x', $this->getClockSequence());
}
public function getNumberConverter()
{
return $this->converter;
}
/**
* Returns a PHP DateTime object representing the timestamp associated
* with this UUID.
*
* The timestamp value is only meaningful in a time-based UUID, which
* has version type 1. If this UUID is not a time-based UUID then
* this method throws UnsupportedOperationException.
*
* @return \DateTime A PHP DateTime representation of the date
* @throws Exception\UnsupportedOperationException If this UUID is not a version 1 UUID
* @throws Exception\UnsatisfiedDependencyException if called on a 32-bit system and
* Moontoast\Math\BigNumber is not present
*/
public function getDateTime()
{
if ($this->getVersion() != 1) {
throw new Exception\UnsupportedOperationException('Not a time-based UUID');
}
$unixTime = ($this->getTimestamp() - 0x01b21dd213814000) / 1e7;
$unixTime = number_format($unixTime, 0, '', '');
return new \DateTime("@{$unixTime}");
}
/**
* Returns an array of the fields of this UUID, with keys named according
* to the RFC 4122 names for the fields.
*
* * **time_low**: The low field of the timestamp, an unsigned 32-bit integer
* * **time_mid**: The middle field of the timestamp, an unsigned 16-bit integer
* * **time_hi_and_version**: The high field of the timestamp multiplexed with
* the version number, an unsigned 16-bit integer
* * **clock_seq_hi_and_reserved**: The high field of the clock sequence
* multiplexed with the variant, an unsigned 8-bit integer
* * **clock_seq_low**: The low field of the clock sequence, an unsigned
* 8-bit integer
* * **node**: The spatially unique node identifier, an unsigned 48-bit
* integer
*
* @return array The UUID fields represented as integer values
* @throws Exception\UnsatisfiedDependencyException if called on a 32-bit system
* @link http://tools.ietf.org/html/rfc4122#section-4.1.2
*/
public function getFields()
{
return array(
'time_low' => $this->getTimeLow(),
'time_mid' => $this->getTimeMid(),
'time_hi_and_version' => $this->getTimeHiAndVersion(),
'clock_seq_hi_and_reserved' => $this->getClockSeqHiAndReserved(),
'clock_seq_low' => $this->getClockSeqLow(),
'node' => $this->getNode(),
);
}
/**
* Returns an array of the fields of this UUID, with keys named according
* to the RFC 4122 names for the fields.
*
* * **time_low**: The low field of the timestamp, an unsigned 32-bit integer
* * **time_mid**: The middle field of the timestamp, an unsigned 16-bit integer
* * **time_hi_and_version**: The high field of the timestamp multiplexed with
* the version number, an unsigned 16-bit integer
* * **clock_seq_hi_and_reserved**: The high field of the clock sequence
* multiplexed with the variant, an unsigned 8-bit integer
* * **clock_seq_low**: The low field of the clock sequence, an unsigned
* 8-bit integer
* * **node**: The spatially unique node identifier, an unsigned 48-bit
* integer
*
* @return array The UUID fields represented as hexadecimal values
*/
public function getFieldsHex()
{
return $this->fields;
}
/**
* Returns the hexadecimal value of the UUID
*
* @return string
*/
public function getHex()
{
return str_replace('-', '', $this->toString());
}
/**
* Returns the integer value of the UUID, represented as a BigNumber
*
* @return \Moontoast\Math\BigNumber BigNumber representation of the unsigned 128-bit integer value
* @throws Exception\UnsatisfiedDependencyException if Moontoast\Math\BigNumber is not present
*/
public function getInteger()
{
return $this->converter->fromHex($this->getHex());
}
/**
* Returns the least significant 64 bits of this UUID's 128 bit value
*
* @return \Moontoast\Math\BigNumber BigNumber representation of the unsigned 64-bit integer value
* @throws Exception\UnsatisfiedDependencyException if Moontoast\Math\BigNumber is not present
*/
public function getLeastSignificantBits()
{
return $this->converter->fromHex($this->getLeastSignificantBitsHex());
}
/**
* Returns the least significant 64 bits of this UUID's 128 bit value
*
* @return string Hexadecimal value of least significant bits
*/
public function getLeastSignificantBitsHex()
{
return sprintf(
'%02s%02s%012s',
$this->fields['clock_seq_hi_and_reserved'],
$this->fields['clock_seq_low'],
$this->fields['node']
);
}
/**
* Returns the most significant 64 bits of this UUID's 128 bit value
*
* @return \Moontoast\Math\BigNumber BigNumber representation of the unsigned 64-bit integer value
* @throws Exception\UnsatisfiedDependencyException if Moontoast\Math\BigNumber is not present
*/
public function getMostSignificantBits()
{
return $this->converter->fromHex($this->getMostSignificantBitsHex());
}
/**
* Returns the most significant 64 bits of this UUID's 128 bit value
*
* @return string Hexadecimal value of most significant bits
*/
public function getMostSignificantBitsHex()
{
return sprintf(
'%08s%04s%04s',
$this->fields['time_low'],
$this->fields['time_mid'],
$this->fields['time_hi_and_version']
);
}
/**
* Returns the node value associated with this UUID
*
* For UUID version 1, the node field consists of an IEEE 802 MAC
* address, usually the host address. For systems with multiple IEEE
* 802 addresses, any available one can be used. The lowest addressed
* octet (octet number 10) contains the global/local bit and the
* unicast/multicast bit, and is the first octet of the address
* transmitted on an 802.3 LAN.
*
* For systems with no IEEE address, a randomly or pseudo-randomly
* generated value may be used; see RFC 4122, Section 4.5. The
* multicast bit must be set in such addresses, in order that they
* will never conflict with addresses obtained from network cards.
*
* For UUID version 3 or 5, the node field is a 48-bit value constructed
* from a name as described in RFC 4122, Section 4.3.
*
* For UUID version 4, the node field is a randomly or pseudo-randomly
* generated 48-bit value as described in RFC 4122, Section 4.4.
*
* @return int Unsigned 48-bit integer value of node
* @link http://tools.ietf.org/html/rfc4122#section-4.1.6
* @throws Exception\UnsatisfiedDependencyException if called on a 32-bit system
*/
public function getNode()
{
return hexdec($this->getNodeHex());
}
/**
* Returns the node value associated with this UUID
*
* For UUID version 1, the node field consists of an IEEE 802 MAC
* address, usually the host address. For systems with multiple IEEE
* 802 addresses, any available one can be used. The lowest addressed
* octet (octet number 10) contains the global/local bit and the
* unicast/multicast bit, and is the first octet of the address
* transmitted on an 802.3 LAN.
*
* For systems with no IEEE address, a randomly or pseudo-randomly
* generated value may be used; see RFC 4122, Section 4.5. The
* multicast bit must be set in such addresses, in order that they
* will never conflict with addresses obtained from network cards.
*
* For UUID version 3 or 5, the node field is a 48-bit value constructed
* from a name as described in RFC 4122, Section 4.3.
*
* For UUID version 4, the node field is a randomly or pseudo-randomly
* generated 48-bit value as described in RFC 4122, Section 4.4.
*
* @return string Hexadecimal value of node
* @link http://tools.ietf.org/html/rfc4122#section-4.1.6
*/
public function getNodeHex()
{
return $this->fields['node'];
}
/**
* Returns the high field of the timestamp multiplexed with the version
* number (bits 49-64 of the UUID).
*
* @return int Unsigned 16-bit integer value of time_hi_and_version
*/
public function getTimeHiAndVersion()
{
return hexdec($this->getTimeHiAndVersionHex());
}
/**
* Returns the high field of the timestamp multiplexed with the version
* number (bits 49-64 of the UUID).
*
* @return string Hexadecimal value of time_hi_and_version
*/
public function getTimeHiAndVersionHex()
{
return $this->fields['time_hi_and_version'];
}
/**
* Returns the low field of the timestamp (the first 32 bits of the UUID).
*
* @return int Unsigned 32-bit integer value of time_low
* @throws Exception\UnsatisfiedDependencyException if called on a 32-bit system
*/
public function getTimeLow()
{
return hexdec($this->getTimeLowHex());
}
/**
* Returns the low field of the timestamp (the first 32 bits of the UUID).
*
* @return string Hexadecimal value of time_low
*/
public function getTimeLowHex()
{
return $this->fields['time_low'];
}
/**
* Returns the middle field of the timestamp (bits 33-48 of the UUID).
*
* @return int Unsigned 16-bit integer value of time_mid
*/
public function getTimeMid()
{
return hexdec($this->getTimeMidHex());
}
/**
* Returns the middle field of the timestamp (bits 33-48 of the UUID).
*
* @return string Hexadecimal value of time_mid
*/
public function getTimeMidHex()
{
return $this->fields['time_mid'];
}
/**
* The timestamp value associated with this UUID
*
* The 60 bit timestamp value is constructed from the time_low,
* time_mid, and time_hi fields of this UUID. The resulting
* timestamp is measured in 100-nanosecond units since midnight,
* October 15, 1582 UTC.
*
* The timestamp value is only meaningful in a time-based UUID, which
* has version type 1. If this UUID is not a time-based UUID then
* this method throws UnsupportedOperationException.
*
* @return int Unsigned 60-bit integer value of the timestamp
* @throws Exception\UnsupportedOperationException If this UUID is not a version 1 UUID
* @throws Exception\UnsatisfiedDependencyException if called on a 32-bit system
* @link http://tools.ietf.org/html/rfc4122#section-4.1.4
*/
public function getTimestamp()
{
if ($this->getVersion() != 1) {
throw new Exception\UnsupportedOperationException('Not a time-based UUID');
}
return hexdec($this->getTimestampHex());
}
/**
* The timestamp value associated with this UUID
*
* The 60 bit timestamp value is constructed from the time_low,
* time_mid, and time_hi fields of this UUID. The resulting
* timestamp is measured in 100-nanosecond units since midnight,
* October 15, 1582 UTC.
*
* The timestamp value is only meaningful in a time-based UUID, which
* has version type 1. If this UUID is not a time-based UUID then
* this method throws UnsupportedOperationException.
*
* @return string Hexadecimal value of the timestamp
* @throws Exception\UnsupportedOperationException If this UUID is not a version 1 UUID
* @link http://tools.ietf.org/html/rfc4122#section-4.1.4
*/
public function getTimestampHex()
{
if ($this->getVersion() != 1) {
throw new Exception\UnsupportedOperationException('Not a time-based UUID');
}
return sprintf(
'%03x%04s%08s',
($this->getTimeHiAndVersion() & 0x0fff),
$this->fields['time_mid'],
$this->fields['time_low']
);
}
/**
* Returns the string representation of the UUID as a URN.
*
* @return string
* @link http://en.wikipedia.org/wiki/Uniform_Resource_Name
*/
public function getUrn()
{
return 'urn:uuid:' . $this->toString();
}
/**
* Returns the variant number associated with this UUID.
*
* The variant number describes the layout of the UUID. The variant
* number has the following meaning:
*
* * 0 - Reserved for NCS backward compatibility
* * 2 - The RFC 4122 variant (used by this class)
* * 6 - Reserved, Microsoft Corporation backward compatibility
* * 7 - Reserved for future definition
*
* @return int
* @link http://tools.ietf.org/html/rfc4122#section-4.1.1
*/
public function getVariant()
{
$clockSeq = $this->getClockSeqHiAndReserved();
if (0 === ($clockSeq & 0x80)) {
$variant = self::RESERVED_NCS;
} elseif (0 === ($clockSeq & 0x40)) {
$variant = self::RFC_4122;
} elseif (0 === ($clockSeq & 0x20)) {
$variant = self::RESERVED_MICROSOFT;
} else {
$variant = self::RESERVED_FUTURE;
}
return $variant;
}
/**
* The version number associated with this UUID. The version
* number describes how this UUID was generated.
*
* The version number has the following meaning:
*
* * 1 - Time-based UUID
* * 2 - DCE security UUID
* * 3 - Name-based UUID hashed with MD5
* * 4 - Randomly generated UUID
* * 5 - Name-based UUID hashed with SHA-1
*
* Returns null if this UUID is not an RFC 4122 variant, since version
* is only meaningful for this variant.
*
* @return int|null
* @link http://tools.ietf.org/html/rfc4122#section-4.1.3
*/
public function getVersion()
{
if ($this->getVariant() == self::RFC_4122) {
return (int) (($this->getTimeHiAndVersion() >> 12) & 0x0f);
}
return null;
}
/**
* Converts this UUID into a string representation
*
* @return string
*/
public function toString()
{
return $this->codec->encode($this);
}
public static function getFactory()
{
if (! self::$factory) {
self::$factory = new PeclUuidFactory(new UuidFactory());
}
return self::$factory;
}
public static function setFactory(UuidFactoryInterface $factory)
{
self::$factory = $factory;
}
/**
* Creates a UUID from a byte string.
*
* @param string $bytes
* @return Uuid
* @throws InvalidArgumentException If the $bytes string does not contain 16 characters
*/
public static function fromBytes($bytes)
{
return self::getFactory()->fromBytes($bytes);
}
/**
* Creates a UUID from the string standard representation as described
* in the toString() method.
*
* @param string $name A string that specifies a UUID
* @return Uuid
* @throws InvalidArgumentException If the $name isn't a valid UUID
*/
public static function fromString($name)
{
return self::getFactory()->fromString($name);
}
/**
* Creates a UUID from either the UUID as a 128-bit integer string or a Moontoast\Math\BigNumber object.
*
* @param string|\Moontoast\Math\BigNumber $integer String/BigNumber representation of UUID integer
* @throws Exception\UnsatisfiedDependencyException If Moontoast\Math\BigNumber is not present
* @return \Ramsey\Uuid\Uuid
*/
public static function fromInteger($integer)
{
return self::getFactory()->fromInteger($integer);
}
/**
* Check if a string is a valid uuid
*
* @param string $uuid The uuid to test
* @return boolean
*/
public static function isValid($uuid)
{
$uuid = str_replace(array('urn:', 'uuid:', '{', '}'), '', $uuid);
if ($uuid == self::NIL) {
return true;
}
if (!preg_match('/' . self::VALID_PATTERN . '/', $uuid)) {
return false;
}
return true;
}
/**
* Generate a version 1 UUID from a host ID, sequence number, and the current time.
* If $node is not given, we will attempt to obtain the local hardware
* address. If $clockSeq is given, it is used as the sequence number;
* otherwise a random 14-bit sequence number is chosen.
*
* @param int|string $node A 48-bit number representing the hardware
* address. This number may be represented as
* an integer or a hexadecimal string.
* @param int $clockSeq A 14-bit number used to help avoid duplicates that
* could arise when the clock is set backwards in time
* or if the node ID changes.
* @return Uuid
* @throws InvalidArgumentException if the $node is invalid
*/
public static function uuid1($node = null, $clockSeq = null)
{
return self::getFactory()->uuid1($node, $clockSeq);
}
/**
* Generate a version 3 UUID based on the MD5 hash of a namespace identifier (which
* is a UUID) and a name (which is a string).
*
* @param string $ns The UUID namespace in which to create the named UUID
* @param string $name The name to create a UUID for
* @return Uuid
*/
public static function uuid3($ns, $name)
{
return self::getFactory()->uuid3($ns, $name);
}
/**
* Generate a version 4 (random) UUID.
*
* @return Uuid
*/
public static function uuid4()
{
return self::getFactory()->uuid4();
}
/**
* Generate a version 5 UUID based on the SHA-1 hash of a namespace identifier (which
* is a UUID) and a name (which is a string).
*
* @param string $ns The UUID namespace in which to create the named UUID
* @param string $name The name to create a UUID for
* @return Uuid
*/
public static function uuid5($ns, $name)
{
return self::getFactory()->uuid5($ns, $name);
}
}