| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* This file is part of the ramsey/uuid library
|
|
|
5 |
*
|
|
|
6 |
* For the full copyright and license information, please view the LICENSE
|
|
|
7 |
* file that was distributed with this source code.
|
|
|
8 |
*
|
|
|
9 |
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
|
|
|
10 |
* @license http://opensource.org/licenses/MIT MIT
|
|
|
11 |
*/
|
|
|
12 |
|
|
|
13 |
declare(strict_types=1);
|
|
|
14 |
|
|
|
15 |
namespace Ramsey\Uuid;
|
|
|
16 |
|
|
|
17 |
use DateTimeImmutable;
|
|
|
18 |
use DateTimeInterface;
|
|
|
19 |
use Ramsey\Uuid\Converter\NumberConverterInterface;
|
|
|
20 |
use Ramsey\Uuid\Exception\DateTimeException;
|
|
|
21 |
use Ramsey\Uuid\Exception\UnsupportedOperationException;
|
|
|
22 |
use Throwable;
|
|
|
23 |
|
|
|
24 |
use function str_pad;
|
|
|
25 |
use function substr;
|
|
|
26 |
|
|
|
27 |
use const STR_PAD_LEFT;
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* This trait encapsulates deprecated methods for ramsey/uuid; this trait and
|
|
|
31 |
* its methods will be removed in ramsey/uuid 5.0.0.
|
|
|
32 |
*
|
|
|
33 |
* @deprecated This trait and its methods will be removed in ramsey/uuid 5.0.0.
|
|
|
34 |
*
|
|
|
35 |
* @psalm-immutable
|
|
|
36 |
*/
|
|
|
37 |
trait DeprecatedUuidMethodsTrait
|
|
|
38 |
{
|
|
|
39 |
/**
|
|
|
40 |
* @deprecated Use {@see UuidInterface::getFields()} to get a
|
|
|
41 |
* {@see \Ramsey\Uuid\Fields\FieldsInterface} instance. If it is a
|
|
|
42 |
* {@see \Ramsey\Uuid\Rfc4122\FieldsInterface} instance, you may call
|
|
|
43 |
* {@see \Ramsey\Uuid\Rfc4122\FieldsInterface::getClockSeqHiAndReserved()}
|
|
|
44 |
* and use the arbitrary-precision math library of your choice to
|
|
|
45 |
* convert it to a string integer.
|
|
|
46 |
*/
|
|
|
47 |
public function getClockSeqHiAndReserved(): string
|
|
|
48 |
{
|
|
|
49 |
return $this->numberConverter->fromHex($this->fields->getClockSeqHiAndReserved()->toString());
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
/**
|
|
|
53 |
* @deprecated Use {@see UuidInterface::getFields()} to get a
|
|
|
54 |
* {@see \Ramsey\Uuid\Fields\FieldsInterface} instance. If it is a
|
|
|
55 |
* {@see \Ramsey\Uuid\Rfc4122\FieldsInterface} instance, you may call
|
|
|
56 |
* {@see \Ramsey\Uuid\Rfc4122\FieldsInterface::getClockSeqHiAndReserved()}.
|
|
|
57 |
*/
|
|
|
58 |
public function getClockSeqHiAndReservedHex(): string
|
|
|
59 |
{
|
|
|
60 |
return $this->fields->getClockSeqHiAndReserved()->toString();
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
* @deprecated Use {@see UuidInterface::getFields()} to get a
|
|
|
65 |
* {@see \Ramsey\Uuid\Fields\FieldsInterface} instance. If it is a
|
|
|
66 |
* {@see \Ramsey\Uuid\Rfc4122\FieldsInterface} instance, you may call
|
|
|
67 |
* {@see \Ramsey\Uuid\Rfc4122\FieldsInterface::getClockSeqLow()}
|
|
|
68 |
* and use the arbitrary-precision math library of your choice to
|
|
|
69 |
* convert it to a string integer.
|
|
|
70 |
*/
|
|
|
71 |
public function getClockSeqLow(): string
|
|
|
72 |
{
|
|
|
73 |
return $this->numberConverter->fromHex($this->fields->getClockSeqLow()->toString());
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
/**
|
|
|
77 |
* @deprecated Use {@see UuidInterface::getFields()} to get a
|
|
|
78 |
* {@see \Ramsey\Uuid\Fields\FieldsInterface} instance. If it is a
|
|
|
79 |
* {@see \Ramsey\Uuid\Rfc4122\FieldsInterface} instance, you may call
|
|
|
80 |
* {@see \Ramsey\Uuid\Rfc4122\FieldsInterface::getClockSeqLow()}.
|
|
|
81 |
*/
|
|
|
82 |
public function getClockSeqLowHex(): string
|
|
|
83 |
{
|
|
|
84 |
return $this->fields->getClockSeqLow()->toString();
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
/**
|
|
|
88 |
* @deprecated Use {@see UuidInterface::getFields()} to get a
|
|
|
89 |
* {@see \Ramsey\Uuid\Fields\FieldsInterface} instance. If it is a
|
|
|
90 |
* {@see \Ramsey\Uuid\Rfc4122\FieldsInterface} instance, you may call
|
|
|
91 |
* {@see \Ramsey\Uuid\Rfc4122\FieldsInterface::getClockSeq()}
|
|
|
92 |
* and use the arbitrary-precision math library of your choice to
|
|
|
93 |
* convert it to a string integer.
|
|
|
94 |
*/
|
|
|
95 |
public function getClockSequence(): string
|
|
|
96 |
{
|
|
|
97 |
return $this->numberConverter->fromHex($this->fields->getClockSeq()->toString());
|
|
|
98 |
}
|
|
|
99 |
|
|
|
100 |
/**
|
|
|
101 |
* @deprecated Use {@see UuidInterface::getFields()} to get a
|
|
|
102 |
* {@see \Ramsey\Uuid\Fields\FieldsInterface} instance. If it is a
|
|
|
103 |
* {@see \Ramsey\Uuid\Rfc4122\FieldsInterface} instance, you may call
|
|
|
104 |
* {@see \Ramsey\Uuid\Rfc4122\FieldsInterface::getClockSeq()}.
|
|
|
105 |
*/
|
|
|
106 |
public function getClockSequenceHex(): string
|
|
|
107 |
{
|
|
|
108 |
return $this->fields->getClockSeq()->toString();
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
/**
|
|
|
112 |
* @deprecated This method will be removed in 5.0.0. There is no alternative
|
|
|
113 |
* recommendation, so plan accordingly.
|
|
|
114 |
*/
|
|
|
115 |
public function getNumberConverter(): NumberConverterInterface
|
|
|
116 |
{
|
|
|
117 |
return $this->numberConverter;
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
/**
|
|
|
121 |
* @deprecated In ramsey/uuid version 5.0.0, this will be removed.
|
|
|
122 |
* It is available at {@see UuidV1::getDateTime()}.
|
|
|
123 |
*
|
|
|
124 |
* @return DateTimeImmutable An immutable instance of DateTimeInterface
|
|
|
125 |
*
|
|
|
126 |
* @throws UnsupportedOperationException if UUID is not time-based
|
|
|
127 |
* @throws DateTimeException if DateTime throws an exception/error
|
|
|
128 |
*/
|
|
|
129 |
public function getDateTime(): DateTimeInterface
|
|
|
130 |
{
|
|
|
131 |
if ($this->fields->getVersion() !== 1) {
|
|
|
132 |
throw new UnsupportedOperationException('Not a time-based UUID');
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
$time = $this->timeConverter->convertTime($this->fields->getTimestamp());
|
|
|
136 |
|
|
|
137 |
try {
|
|
|
138 |
return new DateTimeImmutable(
|
|
|
139 |
'@'
|
|
|
140 |
. $time->getSeconds()->toString()
|
|
|
141 |
. '.'
|
|
|
142 |
. str_pad($time->getMicroseconds()->toString(), 6, '0', STR_PAD_LEFT)
|
|
|
143 |
);
|
|
|
144 |
} catch (Throwable $e) {
|
|
|
145 |
throw new DateTimeException($e->getMessage(), (int) $e->getCode(), $e);
|
|
|
146 |
}
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
/**
|
|
|
150 |
* @deprecated Use {@see UuidInterface::getFields()} to get a
|
|
|
151 |
* {@see \Ramsey\Uuid\Fields\FieldsInterface} instance.
|
|
|
152 |
*
|
|
|
153 |
* @return string[]
|
|
|
154 |
*/
|
|
|
155 |
public function getFieldsHex(): array
|
|
|
156 |
{
|
|
|
157 |
return [
|
|
|
158 |
'time_low' => $this->fields->getTimeLow()->toString(),
|
|
|
159 |
'time_mid' => $this->fields->getTimeMid()->toString(),
|
|
|
160 |
'time_hi_and_version' => $this->fields->getTimeHiAndVersion()->toString(),
|
|
|
161 |
'clock_seq_hi_and_reserved' => $this->fields->getClockSeqHiAndReserved()->toString(),
|
|
|
162 |
'clock_seq_low' => $this->fields->getClockSeqLow()->toString(),
|
|
|
163 |
'node' => $this->fields->getNode()->toString(),
|
|
|
164 |
];
|
|
|
165 |
}
|
|
|
166 |
|
|
|
167 |
/**
|
|
|
168 |
* @deprecated This method will be removed in 5.0.0. There is no direct
|
|
|
169 |
* alternative, but the same information may be obtained by splitting
|
|
|
170 |
* in half the value returned by {@see UuidInterface::getHex()}.
|
|
|
171 |
*/
|
|
|
172 |
public function getLeastSignificantBits(): string
|
|
|
173 |
{
|
|
|
174 |
$leastSignificantHex = substr($this->getHex()->toString(), 16);
|
|
|
175 |
|
|
|
176 |
return $this->numberConverter->fromHex($leastSignificantHex);
|
|
|
177 |
}
|
|
|
178 |
|
|
|
179 |
/**
|
|
|
180 |
* @deprecated This method will be removed in 5.0.0. There is no direct
|
|
|
181 |
* alternative, but the same information may be obtained by splitting
|
|
|
182 |
* in half the value returned by {@see UuidInterface::getHex()}.
|
|
|
183 |
*/
|
|
|
184 |
public function getLeastSignificantBitsHex(): string
|
|
|
185 |
{
|
|
|
186 |
return substr($this->getHex()->toString(), 16);
|
|
|
187 |
}
|
|
|
188 |
|
|
|
189 |
/**
|
|
|
190 |
* @deprecated This method will be removed in 5.0.0. There is no direct
|
|
|
191 |
* alternative, but the same information may be obtained by splitting
|
|
|
192 |
* in half the value returned by {@see UuidInterface::getHex()}.
|
|
|
193 |
*/
|
|
|
194 |
public function getMostSignificantBits(): string
|
|
|
195 |
{
|
|
|
196 |
$mostSignificantHex = substr($this->getHex()->toString(), 0, 16);
|
|
|
197 |
|
|
|
198 |
return $this->numberConverter->fromHex($mostSignificantHex);
|
|
|
199 |
}
|
|
|
200 |
|
|
|
201 |
/**
|
|
|
202 |
* @deprecated This method will be removed in 5.0.0. There is no direct
|
|
|
203 |
* alternative, but the same information may be obtained by splitting
|
|
|
204 |
* in half the value returned by {@see UuidInterface::getHex()}.
|
|
|
205 |
*/
|
|
|
206 |
public function getMostSignificantBitsHex(): string
|
|
|
207 |
{
|
|
|
208 |
return substr($this->getHex()->toString(), 0, 16);
|
|
|
209 |
}
|
|
|
210 |
|
|
|
211 |
/**
|
|
|
212 |
* @deprecated Use {@see UuidInterface::getFields()} to get a
|
|
|
213 |
* {@see \Ramsey\Uuid\Fields\FieldsInterface} instance. If it is a
|
|
|
214 |
* {@see \Ramsey\Uuid\Rfc4122\FieldsInterface} instance, you may call
|
|
|
215 |
* {@see \Ramsey\Uuid\Rfc4122\FieldsInterface::getNode()} and use the
|
|
|
216 |
* arbitrary-precision math library of your choice to convert it to a
|
|
|
217 |
* string integer.
|
|
|
218 |
*/
|
|
|
219 |
public function getNode(): string
|
|
|
220 |
{
|
|
|
221 |
return $this->numberConverter->fromHex($this->fields->getNode()->toString());
|
|
|
222 |
}
|
|
|
223 |
|
|
|
224 |
/**
|
|
|
225 |
* @deprecated Use {@see UuidInterface::getFields()} to get a
|
|
|
226 |
* {@see \Ramsey\Uuid\Fields\FieldsInterface} instance. If it is a
|
|
|
227 |
* {@see \Ramsey\Uuid\Rfc4122\FieldsInterface} instance, you may call
|
|
|
228 |
* {@see \Ramsey\Uuid\Rfc4122\FieldsInterface::getNode()}.
|
|
|
229 |
*/
|
|
|
230 |
public function getNodeHex(): string
|
|
|
231 |
{
|
|
|
232 |
return $this->fields->getNode()->toString();
|
|
|
233 |
}
|
|
|
234 |
|
|
|
235 |
/**
|
|
|
236 |
* @deprecated Use {@see UuidInterface::getFields()} to get a
|
|
|
237 |
* {@see \Ramsey\Uuid\Fields\FieldsInterface} instance. If it is a
|
|
|
238 |
* {@see \Ramsey\Uuid\Rfc4122\FieldsInterface} instance, you may call
|
|
|
239 |
* {@see \Ramsey\Uuid\Rfc4122\FieldsInterface::getTimeHiAndVersion()}
|
|
|
240 |
* and use the arbitrary-precision math library of your choice to
|
|
|
241 |
* convert it to a string integer.
|
|
|
242 |
*/
|
|
|
243 |
public function getTimeHiAndVersion(): string
|
|
|
244 |
{
|
|
|
245 |
return $this->numberConverter->fromHex($this->fields->getTimeHiAndVersion()->toString());
|
|
|
246 |
}
|
|
|
247 |
|
|
|
248 |
/**
|
|
|
249 |
* @deprecated Use {@see UuidInterface::getFields()} to get a
|
|
|
250 |
* {@see \Ramsey\Uuid\Fields\FieldsInterface} instance. If it is a
|
|
|
251 |
* {@see \Ramsey\Uuid\Rfc4122\FieldsInterface} instance, you may call
|
|
|
252 |
* {@see \Ramsey\Uuid\Rfc4122\FieldsInterface::getTimeHiAndVersion()}.
|
|
|
253 |
*/
|
|
|
254 |
public function getTimeHiAndVersionHex(): string
|
|
|
255 |
{
|
|
|
256 |
return $this->fields->getTimeHiAndVersion()->toString();
|
|
|
257 |
}
|
|
|
258 |
|
|
|
259 |
/**
|
|
|
260 |
* @deprecated Use {@see UuidInterface::getFields()} to get a
|
|
|
261 |
* {@see \Ramsey\Uuid\Fields\FieldsInterface} instance. If it is a
|
|
|
262 |
* {@see \Ramsey\Uuid\Rfc4122\FieldsInterface} instance, you may call
|
|
|
263 |
* {@see \Ramsey\Uuid\Rfc4122\FieldsInterface::getTimeLow()} and use the
|
|
|
264 |
* arbitrary-precision math library of your choice to convert it to a
|
|
|
265 |
* string integer.
|
|
|
266 |
*/
|
|
|
267 |
public function getTimeLow(): string
|
|
|
268 |
{
|
|
|
269 |
return $this->numberConverter->fromHex($this->fields->getTimeLow()->toString());
|
|
|
270 |
}
|
|
|
271 |
|
|
|
272 |
/**
|
|
|
273 |
* @deprecated Use {@see UuidInterface::getFields()} to get a
|
|
|
274 |
* {@see \Ramsey\Uuid\Fields\FieldsInterface} instance. If it is a
|
|
|
275 |
* {@see \Ramsey\Uuid\Rfc4122\FieldsInterface} instance, you may call
|
|
|
276 |
* {@see \Ramsey\Uuid\Rfc4122\FieldsInterface::getTimeLow()}.
|
|
|
277 |
*/
|
|
|
278 |
public function getTimeLowHex(): string
|
|
|
279 |
{
|
|
|
280 |
return $this->fields->getTimeLow()->toString();
|
|
|
281 |
}
|
|
|
282 |
|
|
|
283 |
/**
|
|
|
284 |
* @deprecated Use {@see UuidInterface::getFields()} to get a
|
|
|
285 |
* {@see \Ramsey\Uuid\Fields\FieldsInterface} instance. If it is a
|
|
|
286 |
* {@see \Ramsey\Uuid\Rfc4122\FieldsInterface} instance, you may call
|
|
|
287 |
* {@see \Ramsey\Uuid\Rfc4122\FieldsInterface::getTimeMid()} and use the
|
|
|
288 |
* arbitrary-precision math library of your choice to convert it to a
|
|
|
289 |
* string integer.
|
|
|
290 |
*/
|
|
|
291 |
public function getTimeMid(): string
|
|
|
292 |
{
|
|
|
293 |
return $this->numberConverter->fromHex($this->fields->getTimeMid()->toString());
|
|
|
294 |
}
|
|
|
295 |
|
|
|
296 |
/**
|
|
|
297 |
* @deprecated Use {@see UuidInterface::getFields()} to get a
|
|
|
298 |
* {@see \Ramsey\Uuid\Fields\FieldsInterface} instance. If it is a
|
|
|
299 |
* {@see \Ramsey\Uuid\Rfc4122\FieldsInterface} instance, you may call
|
|
|
300 |
* {@see \Ramsey\Uuid\Rfc4122\FieldsInterface::getTimeMid()}.
|
|
|
301 |
*/
|
|
|
302 |
public function getTimeMidHex(): string
|
|
|
303 |
{
|
|
|
304 |
return $this->fields->getTimeMid()->toString();
|
|
|
305 |
}
|
|
|
306 |
|
|
|
307 |
/**
|
|
|
308 |
* @deprecated Use {@see UuidInterface::getFields()} to get a
|
|
|
309 |
* {@see \Ramsey\Uuid\Fields\FieldsInterface} instance. If it is a
|
|
|
310 |
* {@see \Ramsey\Uuid\Rfc4122\FieldsInterface} instance, you may call
|
|
|
311 |
* {@see \Ramsey\Uuid\Rfc4122\FieldsInterface::getTimestamp()} and use
|
|
|
312 |
* the arbitrary-precision math library of your choice to convert it to
|
|
|
313 |
* a string integer.
|
|
|
314 |
*/
|
|
|
315 |
public function getTimestamp(): string
|
|
|
316 |
{
|
|
|
317 |
if ($this->fields->getVersion() !== 1) {
|
|
|
318 |
throw new UnsupportedOperationException('Not a time-based UUID');
|
|
|
319 |
}
|
|
|
320 |
|
|
|
321 |
return $this->numberConverter->fromHex($this->fields->getTimestamp()->toString());
|
|
|
322 |
}
|
|
|
323 |
|
|
|
324 |
/**
|
|
|
325 |
* @deprecated Use {@see UuidInterface::getFields()} to get a
|
|
|
326 |
* {@see \Ramsey\Uuid\Fields\FieldsInterface} instance. If it is a
|
|
|
327 |
* {@see \Ramsey\Uuid\Rfc4122\FieldsInterface} instance, you may call
|
|
|
328 |
* {@see \Ramsey\Uuid\Rfc4122\FieldsInterface::getTimestamp()}.
|
|
|
329 |
*/
|
|
|
330 |
public function getTimestampHex(): string
|
|
|
331 |
{
|
|
|
332 |
if ($this->fields->getVersion() !== 1) {
|
|
|
333 |
throw new UnsupportedOperationException('Not a time-based UUID');
|
|
|
334 |
}
|
|
|
335 |
|
|
|
336 |
return $this->fields->getTimestamp()->toString();
|
|
|
337 |
}
|
|
|
338 |
|
|
|
339 |
/**
|
|
|
340 |
* @deprecated Use {@see UuidInterface::getFields()} to get a
|
|
|
341 |
* {@see \Ramsey\Uuid\Fields\FieldsInterface} instance. If it is a
|
|
|
342 |
* {@see \Ramsey\Uuid\Rfc4122\FieldsInterface} instance, you may call
|
|
|
343 |
* {@see \Ramsey\Uuid\Rfc4122\FieldsInterface::getVariant()}.
|
|
|
344 |
*/
|
|
|
345 |
public function getVariant(): ?int
|
|
|
346 |
{
|
|
|
347 |
return $this->fields->getVariant();
|
|
|
348 |
}
|
|
|
349 |
|
|
|
350 |
/**
|
|
|
351 |
* @deprecated Use {@see UuidInterface::getFields()} to get a
|
|
|
352 |
* {@see \Ramsey\Uuid\Fields\FieldsInterface} instance. If it is a
|
|
|
353 |
* {@see \Ramsey\Uuid\Rfc4122\FieldsInterface} instance, you may call
|
|
|
354 |
* {@see \Ramsey\Uuid\Rfc4122\FieldsInterface::getVersion()}.
|
|
|
355 |
*/
|
|
|
356 |
public function getVersion(): ?int
|
|
|
357 |
{
|
|
|
358 |
return $this->fields->getVersion();
|
|
|
359 |
}
|
|
|
360 |
}
|