1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the xAPI package. |
5
|
|
|
* |
6
|
|
|
* (c) Christian Flothmann <[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 Xabbuh\XApi\Model; |
13
|
|
|
|
14
|
|
|
use Ramsey\Uuid\Uuid as RamseyUuid; |
15
|
|
|
use Rhumsaa\Uuid\Uuid as RhumsaaUuid; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Jérôme Parmentier <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
final class Uuid |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var RamseyUuid|RhumsaaUuid; |
24
|
|
|
*/ |
25
|
|
|
private $uuid; |
26
|
|
|
|
27
|
|
|
private function __construct($uuid) |
28
|
|
|
{ |
29
|
|
|
$this->uuid = $uuid; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param string $uuid |
34
|
|
|
* |
35
|
|
|
* @return Uuid |
36
|
|
|
*/ |
37
|
|
|
public static function fromString($uuid) |
38
|
|
|
{ |
39
|
|
|
if (class_exists('Rhumsaa\Uuid\Uuid')) { |
40
|
|
|
return new self(RhumsaaUuid::fromString($uuid)); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return new self(RamseyUuid::fromString($uuid)); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Generate a version 1 UUID from a host ID, sequence number, and the current time. |
48
|
|
|
* |
49
|
|
|
* @param int|string $node a 48-bit number representing the hardware address |
50
|
|
|
* This number may be represented as an integer or a hexadecimal string |
51
|
|
|
* @param int $clockSeq a 14-bit number used to help avoid duplicates that |
52
|
|
|
* could arise when the clock is set backwards in time or if the node ID |
53
|
|
|
* changes |
54
|
|
|
* |
55
|
|
|
* @return self |
56
|
|
|
*/ |
57
|
|
|
public static function uuid1($node = null, $clockSeq = null) |
58
|
|
|
{ |
59
|
|
|
if (class_exists('Rhumsaa\Uuid\Uuid')) { |
60
|
|
|
return new self(RhumsaaUuid::uuid1($node, $clockSeq)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return new self(RamseyUuid::uuid1($node, $clockSeq)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Generate a version 3 UUID based on the MD5 hash of a namespace identifier |
68
|
|
|
* (which is a UUID) and a name (which is a string). |
69
|
|
|
* |
70
|
|
|
* @param string $ns The UUID namespace in which to create the named UUID |
71
|
|
|
* @param string $name The name to create a UUID for |
72
|
|
|
* |
73
|
|
|
* @return self |
74
|
|
|
*/ |
75
|
|
|
public static function uuid3($ns, $name) |
76
|
|
|
{ |
77
|
|
|
if (class_exists('Rhumsaa\Uuid\Uuid')) { |
78
|
|
|
return new self(RhumsaaUuid::uuid3($ns, $name)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return new self(RamseyUuid::uuid3($ns, $name)); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Generate a version 4 (random) UUID. |
86
|
|
|
* |
87
|
|
|
* @return self |
88
|
|
|
*/ |
89
|
|
|
public static function uuid4() |
90
|
|
|
{ |
91
|
|
|
if (class_exists('Rhumsaa\Uuid\Uuid')) { |
92
|
|
|
return new self(RhumsaaUuid::uuid4()); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return new self(RamseyUuid::uuid4()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Generate a version 5 UUID based on the SHA-1 hash of a namespace |
100
|
|
|
* identifier (which is a UUID) and a name (which is a string). |
101
|
|
|
* |
102
|
|
|
* @param string $ns The UUID namespace in which to create the named UUID |
103
|
|
|
* @param string $name The name to create a UUID for |
104
|
|
|
* |
105
|
|
|
* @return self |
106
|
|
|
*/ |
107
|
|
|
public static function uuid5($ns, $name) |
108
|
|
|
{ |
109
|
|
|
if (class_exists('Rhumsaa\Uuid\Uuid')) { |
110
|
|
|
return new self(RhumsaaUuid::uuid5($ns, $name)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return new self(RamseyUuid::uuid5($ns, $name)); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function __toString() |
117
|
|
|
{ |
118
|
|
|
return $this->uuid->toString(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function equals(Uuid $uuid) |
122
|
|
|
{ |
123
|
|
|
return $this->uuid->equals($uuid->uuid); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|