| 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\GCP\Ramsey\Uuid\Lazy; |
| 14 |
|
| 15 |
use DateTimeInterface; |
| 16 |
use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Converter\NumberConverterInterface; |
| 17 |
use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Exception\UnsupportedOperationException; |
| 18 |
use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Fields\FieldsInterface; |
| 19 |
use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Rfc4122\UuidV1; |
| 20 |
use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Rfc4122\UuidV6; |
| 21 |
use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Type\Hexadecimal; |
| 22 |
use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Type\Integer as IntegerObject; |
| 23 |
use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\UuidFactory; |
| 24 |
use Dudlewebs\WPMCS\GCP\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 increased |
| 35 |
* overhead for more advanced UUID operations. |
| 36 |
* |
| 37 |
* > [!NOTE] |
| 38 |
* > The {@see FieldsInterface} does not declare methods that deprecated API relies upon: the API has been ported from |
| 39 |
* > the {@see \Ramsey\Uuid\Uuid} definition, and is deprecated anyway. |
| 40 |
* |
| 41 |
* > [!NOTE] |
| 42 |
* > The deprecated API from {@see \Ramsey\Uuid\Uuid} is in use here (on purpose): it will be removed once the |
| 43 |
* > deprecated API is gone from this class too. |
| 44 |
* |
| 45 |
* @internal this type is used internally for performance reasons and is not supposed to be directly referenced in consumer libraries. |
| 46 |
*/ |
| 47 |
final class LazyUuidFromString implements UuidInterface |
| 48 |
{ |
| 49 |
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'; |
| 50 |
private ?UuidInterface $unwrapped = null; |
| 51 |
/** |
| 52 |
* @param non-empty-string $uuid |
| 53 |
*/ |
| 54 |
public function __construct(private string $uuid) |
| 55 |
{ |
| 56 |
} |
| 57 |
public static function fromBytes(string $bytes) : self |
| 58 |
{ |
| 59 |
$base16Uuid = bin2hex($bytes); |
| 60 |
return new self(substr($base16Uuid, 0, 8) . '-' . substr($base16Uuid, 8, 4) . '-' . substr($base16Uuid, 12, 4) . '-' . substr($base16Uuid, 16, 4) . '-' . substr($base16Uuid, 20, 12)); |
| 61 |
} |
| 62 |
public function serialize() : string |
| 63 |
{ |
| 64 |
return $this->uuid; |
| 65 |
} |
| 66 |
/** |
| 67 |
* @return array{string: non-empty-string} |
| 68 |
*/ |
| 69 |
public function __serialize() : array |
| 70 |
{ |
| 71 |
return ['string' => $this->uuid]; |
| 72 |
} |
| 73 |
/** |
| 74 |
* {@inheritDoc} |
| 75 |
* |
| 76 |
* @param non-empty-string $data |
| 77 |
*/ |
| 78 |
public function unserialize(string $data) : void |
| 79 |
{ |
| 80 |
$this->uuid = $data; |
| 81 |
} |
| 82 |
/** |
| 83 |
* @param array{string?: non-empty-string} $data |
| 84 |
*/ |
| 85 |
public function __unserialize(array $data) : void |
| 86 |
{ |
| 87 |
// @codeCoverageIgnoreStart |
| 88 |
if (!isset($data['string'])) { |
| 89 |
throw new ValueError(sprintf('%s(): Argument #1 ($data) is invalid', __METHOD__)); |
| 90 |
} |
| 91 |
// @codeCoverageIgnoreEnd |
| 92 |
$this->unserialize($data['string']); |
| 93 |
} |
| 94 |
public function getNumberConverter() : NumberConverterInterface |
| 95 |
{ |
| 96 |
return ($this->unwrapped ?? $this->unwrap())->getNumberConverter(); |
| 97 |
} |
| 98 |
/** |
| 99 |
* @inheritDoc |
| 100 |
*/ |
| 101 |
public function getFieldsHex() : array |
| 102 |
{ |
| 103 |
return ($this->unwrapped ?? $this->unwrap())->getFieldsHex(); |
| 104 |
} |
| 105 |
public function getClockSeqHiAndReservedHex() : string |
| 106 |
{ |
| 107 |
return ($this->unwrapped ?? $this->unwrap())->getClockSeqHiAndReservedHex(); |
| 108 |
} |
| 109 |
public function getClockSeqLowHex() : string |
| 110 |
{ |
| 111 |
return ($this->unwrapped ?? $this->unwrap())->getClockSeqLowHex(); |
| 112 |
} |
| 113 |
public function getClockSequenceHex() : string |
| 114 |
{ |
| 115 |
return ($this->unwrapped ?? $this->unwrap())->getClockSequenceHex(); |
| 116 |
} |
| 117 |
public function getDateTime() : DateTimeInterface |
| 118 |
{ |
| 119 |
return ($this->unwrapped ?? $this->unwrap())->getDateTime(); |
| 120 |
} |
| 121 |
public function getLeastSignificantBitsHex() : string |
| 122 |
{ |
| 123 |
return ($this->unwrapped ?? $this->unwrap())->getLeastSignificantBitsHex(); |
| 124 |
} |
| 125 |
public function getMostSignificantBitsHex() : string |
| 126 |
{ |
| 127 |
return ($this->unwrapped ?? $this->unwrap())->getMostSignificantBitsHex(); |
| 128 |
} |
| 129 |
public function getNodeHex() : string |
| 130 |
{ |
| 131 |
return ($this->unwrapped ?? $this->unwrap())->getNodeHex(); |
| 132 |
} |
| 133 |
public function getTimeHiAndVersionHex() : string |
| 134 |
{ |
| 135 |
return ($this->unwrapped ?? $this->unwrap())->getTimeHiAndVersionHex(); |
| 136 |
} |
| 137 |
public function getTimeLowHex() : string |
| 138 |
{ |
| 139 |
return ($this->unwrapped ?? $this->unwrap())->getTimeLowHex(); |
| 140 |
} |
| 141 |
public function getTimeMidHex() : string |
| 142 |
{ |
| 143 |
return ($this->unwrapped ?? $this->unwrap())->getTimeMidHex(); |
| 144 |
} |
| 145 |
public function getTimestampHex() : string |
| 146 |
{ |
| 147 |
return ($this->unwrapped ?? $this->unwrap())->getTimestampHex(); |
| 148 |
} |
| 149 |
public function getUrn() : string |
| 150 |
{ |
| 151 |
return ($this->unwrapped ?? $this->unwrap())->getUrn(); |
| 152 |
} |
| 153 |
public function getVariant() : ?int |
| 154 |
{ |
| 155 |
return ($this->unwrapped ?? $this->unwrap())->getVariant(); |
| 156 |
} |
| 157 |
public function getVersion() : ?int |
| 158 |
{ |
| 159 |
return ($this->unwrapped ?? $this->unwrap())->getVersion(); |
| 160 |
} |
| 161 |
public function compareTo(UuidInterface $other) : int |
| 162 |
{ |
| 163 |
return ($this->unwrapped ?? $this->unwrap())->compareTo($other); |
| 164 |
} |
| 165 |
public function equals(?object $other) : bool |
| 166 |
{ |
| 167 |
if (!$other instanceof UuidInterface) { |
| 168 |
return \false; |
| 169 |
} |
| 170 |
return $this->uuid === $other->toString(); |
| 171 |
} |
| 172 |
public function getBytes() : string |
| 173 |
{ |
| 174 |
/** |
| 175 |
* @var non-empty-string |
| 176 |
* @phpstan-ignore possiblyImpure.functionCall, possiblyImpure.functionCall |
| 177 |
*/ |
| 178 |
return (string) hex2bin(str_replace('-', '', $this->uuid)); |
| 179 |
} |
| 180 |
public function getFields() : FieldsInterface |
| 181 |
{ |
| 182 |
return ($this->unwrapped ?? $this->unwrap())->getFields(); |
| 183 |
} |
| 184 |
public function getHex() : Hexadecimal |
| 185 |
{ |
| 186 |
return ($this->unwrapped ?? $this->unwrap())->getHex(); |
| 187 |
} |
| 188 |
public function getInteger() : IntegerObject |
| 189 |
{ |
| 190 |
return ($this->unwrapped ?? $this->unwrap())->getInteger(); |
| 191 |
} |
| 192 |
public function toString() : string |
| 193 |
{ |
| 194 |
return $this->uuid; |
| 195 |
} |
| 196 |
public function __toString() : string |
| 197 |
{ |
| 198 |
return $this->uuid; |
| 199 |
} |
| 200 |
public function jsonSerialize() : string |
| 201 |
{ |
| 202 |
return $this->uuid; |
| 203 |
} |
| 204 |
/** |
| 205 |
* @deprecated Use {@see UuidInterface::getFields()} to get a {@see FieldsInterface} instance. If it is a |
| 206 |
* {@see Rfc4122FieldsInterface} instance, you may call {@see Rfc4122FieldsInterface::getClockSeqHiAndReserved()} |
| 207 |
* and use the arbitrary-precision math library of your choice to convert it to a string integer. |
| 208 |
*/ |
| 209 |
public function getClockSeqHiAndReserved() : string |
| 210 |
{ |
| 211 |
$instance = $this->unwrapped ?? $this->unwrap(); |
| 212 |
$fields = $instance->getFields(); |
| 213 |
assert($fields instanceof \Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Rfc4122\FieldsInterface); |
| 214 |
return $instance->getNumberConverter()->fromHex($fields->getClockSeqHiAndReserved()->toString()); |
| 215 |
} |
| 216 |
/** |
| 217 |
* @deprecated Use {@see UuidInterface::getFields()} to get a {@see FieldsInterface} instance. If it is a |
| 218 |
* {@see Rfc4122FieldsInterface} instance, you may call {@see Rfc4122FieldsInterface::getClockSeqLow()} and use |
| 219 |
* the arbitrary-precision math library of your choice to convert it to a string integer. |
| 220 |
*/ |
| 221 |
public function getClockSeqLow() : string |
| 222 |
{ |
| 223 |
$instance = $this->unwrapped ?? $this->unwrap(); |
| 224 |
$fields = $instance->getFields(); |
| 225 |
assert($fields instanceof \Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Rfc4122\FieldsInterface); |
| 226 |
return $instance->getNumberConverter()->fromHex($fields->getClockSeqLow()->toString()); |
| 227 |
} |
| 228 |
/** |
| 229 |
* @deprecated Use {@see UuidInterface::getFields()} to get a {@see FieldsInterface} instance. If it is a |
| 230 |
* {@see Rfc4122FieldsInterface} instance, you may call {@see Rfc4122FieldsInterface::getClockSeq()} and use the |
| 231 |
* arbitrary-precision math library of your choice to convert it to a string integer. |
| 232 |
*/ |
| 233 |
public function getClockSequence() : string |
| 234 |
{ |
| 235 |
$instance = $this->unwrapped ?? $this->unwrap(); |
| 236 |
$fields = $instance->getFields(); |
| 237 |
assert($fields instanceof \Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Rfc4122\FieldsInterface); |
| 238 |
return $instance->getNumberConverter()->fromHex($fields->getClockSeq()->toString()); |
| 239 |
} |
| 240 |
/** |
| 241 |
* @deprecated This method will be removed in 5.0.0. There is no direct alternative, but the same information may be |
| 242 |
* obtained by splitting in half the value returned by {@see UuidInterface::getHex()}. |
| 243 |
*/ |
| 244 |
public function getLeastSignificantBits() : string |
| 245 |
{ |
| 246 |
$instance = $this->unwrapped ?? $this->unwrap(); |
| 247 |
return $instance->getNumberConverter()->fromHex(substr($instance->getHex()->toString(), 16)); |
| 248 |
} |
| 249 |
/** |
| 250 |
* @deprecated This method will be removed in 5.0.0. There is no direct alternative, but the same information may be |
| 251 |
* obtained by splitting in half the value returned by {@see UuidInterface::getHex()}. |
| 252 |
*/ |
| 253 |
public function getMostSignificantBits() : string |
| 254 |
{ |
| 255 |
$instance = $this->unwrapped ?? $this->unwrap(); |
| 256 |
return $instance->getNumberConverter()->fromHex(substr($instance->getHex()->toString(), 0, 16)); |
| 257 |
} |
| 258 |
/** |
| 259 |
* @deprecated Use {@see UuidInterface::getFields()} to get a {@see FieldsInterface} instance. If it is a |
| 260 |
* {@see Rfc4122FieldsInterface} instance, you may call {@see Rfc4122FieldsInterface::getNode()} and use the |
| 261 |
* arbitrary-precision math library of your choice to convert it to a string integer. |
| 262 |
*/ |
| 263 |
public function getNode() : string |
| 264 |
{ |
| 265 |
$instance = $this->unwrapped ?? $this->unwrap(); |
| 266 |
$fields = $instance->getFields(); |
| 267 |
assert($fields instanceof \Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Rfc4122\FieldsInterface); |
| 268 |
return $instance->getNumberConverter()->fromHex($fields->getNode()->toString()); |
| 269 |
} |
| 270 |
/** |
| 271 |
* @deprecated Use {@see UuidInterface::getFields()} to get a {@see FieldsInterface} instance. If it is a |
| 272 |
* {@see Rfc4122FieldsInterface} instance, you may call {@see Rfc4122FieldsInterface::getTimeHiAndVersion()} and |
| 273 |
* use the arbitrary-precision math library of your choice to convert it to a string integer. |
| 274 |
*/ |
| 275 |
public function getTimeHiAndVersion() : string |
| 276 |
{ |
| 277 |
$instance = $this->unwrapped ?? $this->unwrap(); |
| 278 |
$fields = $instance->getFields(); |
| 279 |
assert($fields instanceof \Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Rfc4122\FieldsInterface); |
| 280 |
return $instance->getNumberConverter()->fromHex($fields->getTimeHiAndVersion()->toString()); |
| 281 |
} |
| 282 |
/** |
| 283 |
* @deprecated Use {@see UuidInterface::getFields()} to get a {@see FieldsInterface} instance. If it is a |
| 284 |
* {@see Rfc4122FieldsInterface} instance, you may call {@see Rfc4122FieldsInterface::getTimeLow()} and use the |
| 285 |
* arbitrary-precision math library of your choice to convert it to a string integer. |
| 286 |
*/ |
| 287 |
public function getTimeLow() : string |
| 288 |
{ |
| 289 |
$instance = $this->unwrapped ?? $this->unwrap(); |
| 290 |
$fields = $instance->getFields(); |
| 291 |
assert($fields instanceof \Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Rfc4122\FieldsInterface); |
| 292 |
return $instance->getNumberConverter()->fromHex($fields->getTimeLow()->toString()); |
| 293 |
} |
| 294 |
/** |
| 295 |
* @deprecated Use {@see UuidInterface::getFields()} to get a {@see FieldsInterface} instance. If it is a |
| 296 |
* {@see Rfc4122FieldsInterface} instance, you may call {@see Rfc4122FieldsInterface::getTimeMid()} and use the |
| 297 |
* arbitrary-precision math library of your choice to convert it to a string integer. |
| 298 |
*/ |
| 299 |
public function getTimeMid() : string |
| 300 |
{ |
| 301 |
$instance = $this->unwrapped ?? $this->unwrap(); |
| 302 |
$fields = $instance->getFields(); |
| 303 |
assert($fields instanceof \Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Rfc4122\FieldsInterface); |
| 304 |
return $instance->getNumberConverter()->fromHex($fields->getTimeMid()->toString()); |
| 305 |
} |
| 306 |
/** |
| 307 |
* @deprecated Use {@see UuidInterface::getFields()} to get a {@see FieldsInterface} instance. If it is a |
| 308 |
* {@see Rfc4122FieldsInterface} instance, you may call {@see Rfc4122FieldsInterface::getTimestamp()} and use |
| 309 |
* the arbitrary-precision math library of your choice to convert it to a string integer. |
| 310 |
*/ |
| 311 |
public function getTimestamp() : string |
| 312 |
{ |
| 313 |
$instance = $this->unwrapped ?? $this->unwrap(); |
| 314 |
$fields = $instance->getFields(); |
| 315 |
assert($fields instanceof \Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Rfc4122\FieldsInterface); |
| 316 |
if ($fields->getVersion() !== 1) { |
| 317 |
throw new UnsupportedOperationException('Not a time-based UUID'); |
| 318 |
} |
| 319 |
return $instance->getNumberConverter()->fromHex($fields->getTimestamp()->toString()); |
| 320 |
} |
| 321 |
public function toUuidV1() : UuidV1 |
| 322 |
{ |
| 323 |
$instance = $this->unwrapped ?? $this->unwrap(); |
| 324 |
if ($instance instanceof UuidV1) { |
| 325 |
return $instance; |
| 326 |
} |
| 327 |
assert($instance instanceof UuidV6); |
| 328 |
return $instance->toUuidV1(); |
| 329 |
} |
| 330 |
public function toUuidV6() : UuidV6 |
| 331 |
{ |
| 332 |
$instance = $this->unwrapped ?? $this->unwrap(); |
| 333 |
assert($instance instanceof UuidV6); |
| 334 |
return $instance; |
| 335 |
} |
| 336 |
private function unwrap() : UuidInterface |
| 337 |
{ |
| 338 |
return $this->unwrapped = (new UuidFactory())->fromString($this->uuid); |
| 339 |
} |
| 340 |
} |
| 341 |
|