| 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 |
declare (strict_types=1); |
| 13 |
namespace Dudlewebs\WPMCS\Ramsey\Uuid\Lazy; |
| 14 |
|
| 15 |
use DateTimeInterface; |
| 16 |
use Dudlewebs\WPMCS\Ramsey\Uuid\Converter\NumberConverterInterface; |
| 17 |
use Dudlewebs\WPMCS\Ramsey\Uuid\Exception\UnsupportedOperationException; |
| 18 |
use Dudlewebs\WPMCS\Ramsey\Uuid\Fields\FieldsInterface; |
| 19 |
use Dudlewebs\WPMCS\Ramsey\Uuid\Nonstandard\UuidV6; |
| 20 |
use Dudlewebs\WPMCS\Ramsey\Uuid\Rfc4122\UuidV1; |
| 21 |
use Dudlewebs\WPMCS\Ramsey\Uuid\Type\Hexadecimal; |
| 22 |
use Dudlewebs\WPMCS\Ramsey\Uuid\Type\Integer as IntegerObject; |
| 23 |
use Dudlewebs\WPMCS\Ramsey\Uuid\UuidFactory; |
| 24 |
use Dudlewebs\WPMCS\Ramsey\Uuid\UuidInterface; |
| 25 |
use ValueError; |
| 26 |
use function assert; |
| 27 |
use function bin2hex; |
| 28 |
use function hex2bin; |
| 29 |
use function sprintf; |
| 30 |
use function str_replace; |
| 31 |
use function substr; |
| 32 |
/** |
| 33 |
* Lazy version of a UUID: its format has not been determined yet, so it is mostly only usable for string/bytes |
| 34 |
* conversion. This object optimizes instantiation, serialization and string conversion time, at the cost of |
| 35 |
* increased overhead for more advanced UUID operations. |
| 36 |
* |
| 37 |
* @internal this type is used internally for performance reasons, and is not supposed to be directly referenced |
| 38 |
* in consumer libraries. |
| 39 |
* |
| 40 |
* @psalm-immutable |
| 41 |
* |
| 42 |
* Note: the {@see FieldsInterface} does not declare methods that deprecated API |
| 43 |
* relies upon: the API has been ported from the {@see \Ramsey\Uuid\Uuid} definition, |
| 44 |
* and is deprecated anyway. |
| 45 |
* Note: the deprecated API from {@see \Ramsey\Uuid\Uuid} is in use here (on purpose): it will be removed |
| 46 |
* once the deprecated API is gone from this class too. |
| 47 |
* |
| 48 |
* @psalm-suppress UndefinedInterfaceMethod |
| 49 |
* @psalm-suppress DeprecatedMethod |
| 50 |
*/ |
| 51 |
final class LazyUuidFromString implements UuidInterface |
| 52 |
{ |
| 53 |
public const VALID_REGEX = '/\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/ms'; |
| 54 |
/** |
| 55 |
* @var string |
| 56 |
* @psalm-var non-empty-string |
| 57 |
*/ |
| 58 |
private $uuid; |
| 59 |
/** @var UuidInterface|null */ |
| 60 |
private $unwrapped; |
| 61 |
/** @psalm-param non-empty-string $uuid */ |
| 62 |
public function __construct(string $uuid) |
| 63 |
{ |
| 64 |
$this->uuid = $uuid; |
| 65 |
} |
| 66 |
/** @psalm-pure */ |
| 67 |
public static function fromBytes(string $bytes): self |
| 68 |
{ |
| 69 |
$base16Uuid = bin2hex($bytes); |
| 70 |
return new self(substr($base16Uuid, 0, 8) . '-' . substr($base16Uuid, 8, 4) . '-' . substr($base16Uuid, 12, 4) . '-' . substr($base16Uuid, 16, 4) . '-' . substr($base16Uuid, 20, 12)); |
| 71 |
} |
| 72 |
public function serialize(): string |
| 73 |
{ |
| 74 |
return $this->uuid; |
| 75 |
} |
| 76 |
/** |
| 77 |
* @return array{string: string} |
| 78 |
* |
| 79 |
* @psalm-return array{string: non-empty-string} |
| 80 |
*/ |
| 81 |
public function __serialize(): array |
| 82 |
{ |
| 83 |
return ['string' => $this->uuid]; |
| 84 |
} |
| 85 |
/** |
| 86 |
* {@inheritDoc} |
| 87 |
* |
| 88 |
* @param string $serialized |
| 89 |
* |
| 90 |
* @psalm-param non-empty-string $serialized |
| 91 |
*/ |
| 92 |
public function unserialize($serialized): void |
| 93 |
{ |
| 94 |
$this->uuid = $serialized; |
| 95 |
} |
| 96 |
/** |
| 97 |
* @param array{string: string} $data |
| 98 |
* |
| 99 |
* @psalm-param array{string: non-empty-string} $data |
| 100 |
*/ |
| 101 |
public function __unserialize(array $data): void |
| 102 |
{ |
| 103 |
// @codeCoverageIgnoreStart |
| 104 |
if (!isset($data['string'])) { |
| 105 |
throw new ValueError(sprintf('%s(): Argument #1 ($data) is invalid', __METHOD__)); |
| 106 |
} |
| 107 |
// @codeCoverageIgnoreEnd |
| 108 |
$this->unserialize($data['string']); |
| 109 |
} |
| 110 |
/** @psalm-suppress DeprecatedMethod */ |
| 111 |
public function getNumberConverter(): NumberConverterInterface |
| 112 |
{ |
| 113 |
return ($this->unwrapped ?? $this->unwrap())->getNumberConverter(); |
| 114 |
} |
| 115 |
/** |
| 116 |
* {@inheritDoc} |
| 117 |
* |
| 118 |
* @psalm-suppress DeprecatedMethod |
| 119 |
*/ |
| 120 |
public function getFieldsHex(): array |
| 121 |
{ |
| 122 |
return ($this->unwrapped ?? $this->unwrap())->getFieldsHex(); |
| 123 |
} |
| 124 |
/** @psalm-suppress DeprecatedMethod */ |
| 125 |
public function getClockSeqHiAndReservedHex(): string |
| 126 |
{ |
| 127 |
return ($this->unwrapped ?? $this->unwrap())->getClockSeqHiAndReservedHex(); |
| 128 |
} |
| 129 |
/** @psalm-suppress DeprecatedMethod */ |
| 130 |
public function getClockSeqLowHex(): string |
| 131 |
{ |
| 132 |
return ($this->unwrapped ?? $this->unwrap())->getClockSeqLowHex(); |
| 133 |
} |
| 134 |
/** @psalm-suppress DeprecatedMethod */ |
| 135 |
public function getClockSequenceHex(): string |
| 136 |
{ |
| 137 |
return ($this->unwrapped ?? $this->unwrap())->getClockSequenceHex(); |
| 138 |
} |
| 139 |
/** @psalm-suppress DeprecatedMethod */ |
| 140 |
public function getDateTime(): DateTimeInterface |
| 141 |
{ |
| 142 |
return ($this->unwrapped ?? $this->unwrap())->getDateTime(); |
| 143 |
} |
| 144 |
/** @psalm-suppress DeprecatedMethod */ |
| 145 |
public function getLeastSignificantBitsHex(): string |
| 146 |
{ |
| 147 |
return ($this->unwrapped ?? $this->unwrap())->getLeastSignificantBitsHex(); |
| 148 |
} |
| 149 |
/** @psalm-suppress DeprecatedMethod */ |
| 150 |
public function getMostSignificantBitsHex(): string |
| 151 |
{ |
| 152 |
return ($this->unwrapped ?? $this->unwrap())->getMostSignificantBitsHex(); |
| 153 |
} |
| 154 |
/** @psalm-suppress DeprecatedMethod */ |
| 155 |
public function getNodeHex(): string |
| 156 |
{ |
| 157 |
return ($this->unwrapped ?? $this->unwrap())->getNodeHex(); |
| 158 |
} |
| 159 |
/** @psalm-suppress DeprecatedMethod */ |
| 160 |
public function getTimeHiAndVersionHex(): string |
| 161 |
{ |
| 162 |
return ($this->unwrapped ?? $this->unwrap())->getTimeHiAndVersionHex(); |
| 163 |
} |
| 164 |
/** @psalm-suppress DeprecatedMethod */ |
| 165 |
public function getTimeLowHex(): string |
| 166 |
{ |
| 167 |
return ($this->unwrapped ?? $this->unwrap())->getTimeLowHex(); |
| 168 |
} |
| 169 |
/** @psalm-suppress DeprecatedMethod */ |
| 170 |
public function getTimeMidHex(): string |
| 171 |
{ |
| 172 |
return ($this->unwrapped ?? $this->unwrap())->getTimeMidHex(); |
| 173 |
} |
| 174 |
/** @psalm-suppress DeprecatedMethod */ |
| 175 |
public function getTimestampHex(): string |
| 176 |
{ |
| 177 |
return ($this->unwrapped ?? $this->unwrap())->getTimestampHex(); |
| 178 |
} |
| 179 |
/** @psalm-suppress DeprecatedMethod */ |
| 180 |
public function getUrn(): string |
| 181 |
{ |
| 182 |
return ($this->unwrapped ?? $this->unwrap())->getUrn(); |
| 183 |
} |
| 184 |
/** @psalm-suppress DeprecatedMethod */ |
| 185 |
public function getVariant(): ?int |
| 186 |
{ |
| 187 |
return ($this->unwrapped ?? $this->unwrap())->getVariant(); |
| 188 |
} |
| 189 |
/** @psalm-suppress DeprecatedMethod */ |
| 190 |
public function getVersion(): ?int |
| 191 |
{ |
| 192 |
return ($this->unwrapped ?? $this->unwrap())->getVersion(); |
| 193 |
} |
| 194 |
public function compareTo(UuidInterface $other): int |
| 195 |
{ |
| 196 |
return ($this->unwrapped ?? $this->unwrap())->compareTo($other); |
| 197 |
} |
| 198 |
public function equals(?object $other): bool |
| 199 |
{ |
| 200 |
if (!$other instanceof UuidInterface) { |
| 201 |
return \false; |
| 202 |
} |
| 203 |
return $this->uuid === $other->toString(); |
| 204 |
} |
| 205 |
/** |
| 206 |
* {@inheritDoc} |
| 207 |
* |
| 208 |
* @psalm-suppress MoreSpecificReturnType |
| 209 |
* @psalm-suppress LessSpecificReturnStatement we know that {@see self::$uuid} is a non-empty string, so |
| 210 |
* we know that {@see hex2bin} will retrieve a non-empty string too. |
| 211 |
*/ |
| 212 |
public function getBytes(): string |
| 213 |
{ |
| 214 |
/** @phpstan-ignore-next-line PHPStan complains that this is not a non-empty-string. */ |
| 215 |
return (string) hex2bin(str_replace('-', '', $this->uuid)); |
| 216 |
} |
| 217 |
public function getFields(): FieldsInterface |
| 218 |
{ |
| 219 |
return ($this->unwrapped ?? $this->unwrap())->getFields(); |
| 220 |
} |
| 221 |
public function getHex(): Hexadecimal |
| 222 |
{ |
| 223 |
return ($this->unwrapped ?? $this->unwrap())->getHex(); |
| 224 |
} |
| 225 |
public function getInteger(): IntegerObject |
| 226 |
{ |
| 227 |
return ($this->unwrapped ?? $this->unwrap())->getInteger(); |
| 228 |
} |
| 229 |
public function toString(): string |
| 230 |
{ |
| 231 |
return $this->uuid; |
| 232 |
} |
| 233 |
public function __toString(): string |
| 234 |
{ |
| 235 |
return $this->uuid; |
| 236 |
} |
| 237 |
public function jsonSerialize(): string |
| 238 |
{ |
| 239 |
return $this->uuid; |
| 240 |
} |
| 241 |
/** |
| 242 |
* @deprecated Use {@see UuidInterface::getFields()} to get a |
| 243 |
* {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} |
| 244 |
* instance, you may call {@see Rfc4122FieldsInterface::getClockSeqHiAndReserved()} |
| 245 |
* and use the arbitrary-precision math library of your choice to |
| 246 |
* convert it to a string integer. |
| 247 |
* |
| 248 |
* @psalm-suppress UndefinedInterfaceMethod |
| 249 |
* @psalm-suppress DeprecatedMethod |
| 250 |
* @psalm-suppress MixedArgument |
| 251 |
* @psalm-suppress MixedMethodCall |
| 252 |
*/ |
| 253 |
public function getClockSeqHiAndReserved(): string |
| 254 |
{ |
| 255 |
$instance = $this->unwrapped ?? $this->unwrap(); |
| 256 |
return $instance->getNumberConverter()->fromHex($instance->getFields()->getClockSeqHiAndReserved()->toString()); |
| 257 |
} |
| 258 |
/** |
| 259 |
* @deprecated Use {@see UuidInterface::getFields()} to get a |
| 260 |
* {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} |
| 261 |
* instance, you may call {@see Rfc4122FieldsInterface::getClockSeqLow()} |
| 262 |
* and use the arbitrary-precision math library of your choice to |
| 263 |
* convert it to a string integer. |
| 264 |
* |
| 265 |
* @psalm-suppress UndefinedInterfaceMethod |
| 266 |
* @psalm-suppress DeprecatedMethod |
| 267 |
* @psalm-suppress MixedArgument |
| 268 |
* @psalm-suppress MixedMethodCall |
| 269 |
*/ |
| 270 |
public function getClockSeqLow(): string |
| 271 |
{ |
| 272 |
$instance = $this->unwrapped ?? $this->unwrap(); |
| 273 |
return $instance->getNumberConverter()->fromHex($instance->getFields()->getClockSeqLow()->toString()); |
| 274 |
} |
| 275 |
/** |
| 276 |
* @deprecated Use {@see UuidInterface::getFields()} to get a |
| 277 |
* {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} |
| 278 |
* instance, you may call {@see Rfc4122FieldsInterface::getClockSeq()} |
| 279 |
* and use the arbitrary-precision math library of your choice to |
| 280 |
* convert it to a string integer. |
| 281 |
* |
| 282 |
* @psalm-suppress UndefinedInterfaceMethod |
| 283 |
* @psalm-suppress DeprecatedMethod |
| 284 |
* @psalm-suppress MixedArgument |
| 285 |
* @psalm-suppress MixedMethodCall |
| 286 |
*/ |
| 287 |
public function getClockSequence(): string |
| 288 |
{ |
| 289 |
$instance = $this->unwrapped ?? $this->unwrap(); |
| 290 |
return $instance->getNumberConverter()->fromHex($instance->getFields()->getClockSeq()->toString()); |
| 291 |
} |
| 292 |
/** |
| 293 |
* @deprecated This method will be removed in 5.0.0. There is no direct |
| 294 |
* alternative, but the same information may be obtained by splitting |
| 295 |
* in half the value returned by {@see UuidInterface::getHex()}. |
| 296 |
* |
| 297 |
* @psalm-suppress UndefinedInterfaceMethod |
| 298 |
* @psalm-suppress DeprecatedMethod |
| 299 |
* @psalm-suppress MixedArgument |
| 300 |
* @psalm-suppress MixedMethodCall |
| 301 |
*/ |
| 302 |
public function getLeastSignificantBits(): string |
| 303 |
{ |
| 304 |
$instance = $this->unwrapped ?? $this->unwrap(); |
| 305 |
return $instance->getNumberConverter()->fromHex(substr($instance->getHex()->toString(), 16)); |
| 306 |
} |
| 307 |
/** |
| 308 |
* @deprecated This method will be removed in 5.0.0. There is no direct |
| 309 |
* alternative, but the same information may be obtained by splitting |
| 310 |
* in half the value returned by {@see UuidInterface::getHex()}. |
| 311 |
* |
| 312 |
* @psalm-suppress UndefinedInterfaceMethod |
| 313 |
* @psalm-suppress DeprecatedMethod |
| 314 |
* @psalm-suppress MixedArgument |
| 315 |
* @psalm-suppress MixedMethodCall |
| 316 |
*/ |
| 317 |
public function getMostSignificantBits(): string |
| 318 |
{ |
| 319 |
$instance = $this->unwrapped ?? $this->unwrap(); |
| 320 |
return $instance->getNumberConverter()->fromHex(substr($instance->getHex()->toString(), 0, 16)); |
| 321 |
} |
| 322 |
/** |
| 323 |
* @deprecated Use {@see UuidInterface::getFields()} to get a |
| 324 |
* {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} |
| 325 |
* instance, you may call {@see Rfc4122FieldsInterface::getNode()} |
| 326 |
* and use the arbitrary-precision math library of your choice to |
| 327 |
* convert it to a string integer. |
| 328 |
* |
| 329 |
* @psalm-suppress UndefinedInterfaceMethod |
| 330 |
* @psalm-suppress DeprecatedMethod |
| 331 |
* @psalm-suppress MixedArgument |
| 332 |
* @psalm-suppress MixedMethodCall |
| 333 |
*/ |
| 334 |
public function getNode(): string |
| 335 |
{ |
| 336 |
$instance = $this->unwrapped ?? $this->unwrap(); |
| 337 |
return $instance->getNumberConverter()->fromHex($instance->getFields()->getNode()->toString()); |
| 338 |
} |
| 339 |
/** |
| 340 |
* @deprecated Use {@see UuidInterface::getFields()} to get a |
| 341 |
* {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} |
| 342 |
* instance, you may call {@see Rfc4122FieldsInterface::getTimeHiAndVersion()} |
| 343 |
* and use the arbitrary-precision math library of your choice to |
| 344 |
* convert it to a string integer. |
| 345 |
* |
| 346 |
* @psalm-suppress UndefinedInterfaceMethod |
| 347 |
* @psalm-suppress DeprecatedMethod |
| 348 |
* @psalm-suppress MixedArgument |
| 349 |
* @psalm-suppress MixedMethodCall |
| 350 |
*/ |
| 351 |
public function getTimeHiAndVersion(): string |
| 352 |
{ |
| 353 |
$instance = $this->unwrapped ?? $this->unwrap(); |
| 354 |
return $instance->getNumberConverter()->fromHex($instance->getFields()->getTimeHiAndVersion()->toString()); |
| 355 |
} |
| 356 |
/** |
| 357 |
* @deprecated Use {@see UuidInterface::getFields()} to get a |
| 358 |
* {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} |
| 359 |
* instance, you may call {@see Rfc4122FieldsInterface::getTimeLow()} |
| 360 |
* and use the arbitrary-precision math library of your choice to |
| 361 |
* convert it to a string integer. |
| 362 |
* |
| 363 |
* @psalm-suppress UndefinedInterfaceMethod |
| 364 |
* @psalm-suppress DeprecatedMethod |
| 365 |
* @psalm-suppress MixedArgument |
| 366 |
* @psalm-suppress MixedMethodCall |
| 367 |
*/ |
| 368 |
public function getTimeLow(): string |
| 369 |
{ |
| 370 |
$instance = $this->unwrapped ?? $this->unwrap(); |
| 371 |
return $instance->getNumberConverter()->fromHex($instance->getFields()->getTimeLow()->toString()); |
| 372 |
} |
| 373 |
/** |
| 374 |
* @deprecated Use {@see UuidInterface::getFields()} to get a |
| 375 |
* {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} |
| 376 |
* instance, you may call {@see Rfc4122FieldsInterface::getTimeMid()} |
| 377 |
* and use the arbitrary-precision math library of your choice to |
| 378 |
* convert it to a string integer. |
| 379 |
* |
| 380 |
* @psalm-suppress UndefinedInterfaceMethod |
| 381 |
* @psalm-suppress DeprecatedMethod |
| 382 |
* @psalm-suppress MixedArgument |
| 383 |
* @psalm-suppress MixedMethodCall |
| 384 |
*/ |
| 385 |
public function getTimeMid(): string |
| 386 |
{ |
| 387 |
$instance = $this->unwrapped ?? $this->unwrap(); |
| 388 |
return $instance->getNumberConverter()->fromHex($instance->getFields()->getTimeMid()->toString()); |
| 389 |
} |
| 390 |
/** |
| 391 |
* @deprecated Use {@see UuidInterface::getFields()} to get a |
| 392 |
* {@see FieldsInterface} instance. If it is a {@see Rfc4122FieldsInterface} |
| 393 |
* instance, you may call {@see Rfc4122FieldsInterface::getTimestamp()} |
| 394 |
* and use the arbitrary-precision math library of your choice to |
| 395 |
* convert it to a string integer. |
| 396 |
* |
| 397 |
* @psalm-suppress UndefinedInterfaceMethod |
| 398 |
* @psalm-suppress DeprecatedMethod |
| 399 |
* @psalm-suppress MixedArgument |
| 400 |
* @psalm-suppress MixedMethodCall |
| 401 |
*/ |
| 402 |
public function getTimestamp(): string |
| 403 |
{ |
| 404 |
$instance = $this->unwrapped ?? $this->unwrap(); |
| 405 |
$fields = $instance->getFields(); |
| 406 |
if ($fields->getVersion() !== 1) { |
| 407 |
throw new UnsupportedOperationException('Not a time-based UUID'); |
| 408 |
} |
| 409 |
return $instance->getNumberConverter()->fromHex($fields->getTimestamp()->toString()); |
| 410 |
} |
| 411 |
public function toUuidV1(): UuidV1 |
| 412 |
{ |
| 413 |
$instance = $this->unwrapped ?? $this->unwrap(); |
| 414 |
if ($instance instanceof UuidV1) { |
| 415 |
return $instance; |
| 416 |
} |
| 417 |
assert($instance instanceof UuidV6); |
| 418 |
return $instance->toUuidV1(); |
| 419 |
} |
| 420 |
public function toUuidV6(): UuidV6 |
| 421 |
{ |
| 422 |
$instance = $this->unwrapped ?? $this->unwrap(); |
| 423 |
assert($instance instanceof UuidV6); |
| 424 |
return $instance; |
| 425 |
} |
| 426 |
/** |
| 427 |
* @psalm-suppress ImpureMethodCall the retrieval of the factory is a clear violation of purity here: this is a |
| 428 |
* known pitfall of the design of this library, where a value object contains |
| 429 |
* a mutable reference to a factory. We use a fixed factory here, so the violation |
| 430 |
* will not have real-world effects, as this object is only instantiated with the |
| 431 |
* default factory settings/features. |
| 432 |
* @psalm-suppress InaccessibleProperty property {@see $unwrapped} is used as a cache: we don't expose it to the |
| 433 |
* outside world, so we should be fine here. |
| 434 |
*/ |
| 435 |
private function unwrap(): UuidInterface |
| 436 |
{ |
| 437 |
return $this->unwrapped = (new UuidFactory())->fromString($this->uuid); |
| 438 |
} |
| 439 |
} |
| 440 |
|