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