1 | <?php |
||
10 | class Uuid |
||
11 | { |
||
12 | /** |
||
13 | * The 'Nil' UUID described in section 4.1.7 of RFC 4122. |
||
14 | */ |
||
15 | const NIL = '00000000-0000-0000-0000-000000000000'; |
||
16 | |||
17 | /** |
||
18 | * The regular expression of what the value object considers to be a valid UUID in textual form. |
||
19 | * |
||
20 | * It does not try to enforce any particular version. |
||
21 | */ |
||
22 | const TEXTUAL_FORMAT = '/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i'; |
||
23 | |||
24 | /** |
||
25 | * Ordered sequence of 16 bytes. |
||
26 | * |
||
27 | * @example 0x96aaab697b764461b008cbb9cfcb6fdf |
||
28 | * |
||
29 | * @var string |
||
30 | */ |
||
31 | private $raw; |
||
32 | |||
33 | /** |
||
34 | * Textual UUID representation of the $raw byte sequence. |
||
35 | * |
||
36 | * @example '96aaab69-7b76-4461-b008-cbb9cfcb6fdf' |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | private $textual; |
||
41 | |||
42 | /** |
||
43 | * Direct usage of the constructor is only allowed inside the class. |
||
44 | */ |
||
45 | 9 | private function __construct(string $raw, string $textual) |
|
50 | |||
51 | /** |
||
52 | * Alias of asString(). |
||
53 | */ |
||
54 | 1 | public function __toString(): string |
|
58 | |||
59 | /** |
||
60 | * Returns the raw 16-byte sequence of the UUID. |
||
61 | */ |
||
62 | 3 | public function asBytes(): string |
|
66 | |||
67 | /** |
||
68 | * Returns the textual representation of the UUID. |
||
69 | */ |
||
70 | 7 | public function asString(): string |
|
74 | |||
75 | /** |
||
76 | * Factory to create a new Uuid instance from a raw byte sequence. |
||
77 | * |
||
78 | * Most of the time this method should be used by UuidGenerator |
||
79 | * implementations, not the end user of the library. |
||
80 | * |
||
81 | * @throws \InvalidArgumentException If $bytes is not exactly 16 bytes long. |
||
82 | */ |
||
83 | 7 | public static function fromBytes(string $bytes): Uuid |
|
91 | |||
92 | /** |
||
93 | * Factory to create a new Uuid instance from a valid Uuid in string form. |
||
94 | * |
||
95 | * As opposed to the fromBytes() method, this one is meant to |
||
96 | * be used by the end user of the library. |
||
97 | * |
||
98 | * @throws \InvalidArgumentException If $text is not actually a valid Uuid. |
||
99 | */ |
||
100 | 6 | public static function fromString(string $text): Uuid |
|
108 | |||
109 | /** |
||
110 | * Factory to create a new 'Nil' Uuid instance. |
||
111 | */ |
||
112 | 1 | public static function nil(): Uuid |
|
116 | |||
117 | /** |
||
118 | * Helper method to validate if a given string can be considered a valid UUID. |
||
119 | */ |
||
120 | 9 | public static function isUuid(string $candidate): bool |
|
124 | |||
125 | /** |
||
126 | * Turns the textual form of an UUID to its equivalent raw bytes. |
||
127 | * |
||
128 | * Precondition: $uuid is a valid Uuid. |
||
129 | * |
||
130 | * @example '96aaab69-7b76-4461-b008-cbb9cfcb6fdf' => 0x96aaab697b764461b008cbb9cfcb6fdf |
||
131 | */ |
||
132 | 5 | private static function str2bin(string $uuid): string |
|
136 | |||
137 | /** |
||
138 | * Turns the 16 raw bytes of an UUID to its textual form. |
||
139 | * |
||
140 | * Precondition: $bytes is exactly 16 bytes long. |
||
141 | * |
||
142 | * @example 0x96aaab697b764461b008cbb9cfcb6fdf => '96aaab69-7b76-4461-b008-cbb9cfcb6fdf' |
||
143 | */ |
||
144 | 6 | private static function bin2str(string $bytes): string |
|
148 | } |
||
149 |