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
|
|
|
public static function fromString(string $uuid): self |
33
|
|
|
{ |
34
|
|
|
if (class_exists(RhumsaaUuid::class)) { |
35
|
|
|
return new self(RhumsaaUuid::fromString($uuid)); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return new self(RamseyUuid::fromString($uuid)); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Generate a version 1 UUID from a host ID, sequence number, and the current time. |
43
|
|
|
* |
44
|
|
|
* @param int|string $node a 48-bit number representing the hardware address |
45
|
|
|
* This number may be represented as an integer or a hexadecimal string |
46
|
|
|
* @param int $clockSeq a 14-bit number used to help avoid duplicates that |
47
|
|
|
* could arise when the clock is set backwards in time or if the node ID |
48
|
|
|
* changes |
49
|
|
|
*/ |
50
|
|
|
public static function uuid1($node = null, int $clockSeq = null): self |
51
|
|
|
{ |
52
|
|
|
if (class_exists(RhumsaaUuid::class)) { |
53
|
|
|
return new self(RhumsaaUuid::uuid1($node, $clockSeq)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return new self(RamseyUuid::uuid1($node, $clockSeq)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Generate a version 3 UUID based on the MD5 hash of a namespace identifier |
61
|
|
|
* (which is a UUID) and a name (which is a string). |
62
|
|
|
* |
63
|
|
|
* @param string $ns The UUID namespace in which to create the named UUID |
64
|
|
|
* @param string $name The name to create a UUID for |
65
|
|
|
*/ |
66
|
|
|
public static function uuid3(string $ns, string $name): self |
67
|
|
|
{ |
68
|
|
|
if (class_exists(RhumsaaUuid::class)) { |
69
|
|
|
return new self(RhumsaaUuid::uuid3($ns, $name)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return new self(RamseyUuid::uuid3($ns, $name)); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Generate a version 4 (random) UUID. |
77
|
|
|
*/ |
78
|
|
|
public static function uuid4(): self |
79
|
|
|
{ |
80
|
|
|
if (class_exists(RhumsaaUuid::class)) { |
81
|
|
|
return new self(RhumsaaUuid::uuid4()); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return new self(RamseyUuid::uuid4()); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Generate a version 5 UUID based on the SHA-1 hash of a namespace |
89
|
|
|
* identifier (which is a UUID) and a name (which is a string). |
90
|
|
|
* |
91
|
|
|
* @param string $ns The UUID namespace in which to create the named UUID |
92
|
|
|
* @param string $name The name to create a UUID for |
93
|
|
|
*/ |
94
|
|
|
public static function uuid5(string $ns, string $name): self |
95
|
|
|
{ |
96
|
|
|
if (class_exists(RhumsaaUuid::class)) { |
97
|
|
|
return new self(RhumsaaUuid::uuid5($ns, $name)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return new self(RamseyUuid::uuid5($ns, $name)); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function __toString(): string |
104
|
|
|
{ |
105
|
|
|
return $this->uuid->toString(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function equals(Uuid $uuid): bool |
109
|
|
|
{ |
110
|
|
|
return $this->uuid->equals($uuid->uuid); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|