| 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; |
| 14 |
|
| 15 |
use BadMethodCallException; |
| 16 |
use DateTimeInterface; |
| 17 |
use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Codec\CodecInterface; |
| 18 |
use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Converter\NumberConverterInterface; |
| 19 |
use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Converter\TimeConverterInterface; |
| 20 |
use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Exception\InvalidArgumentException; |
| 21 |
use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Exception\UnsupportedOperationException; |
| 22 |
use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Fields\FieldsInterface; |
| 23 |
use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Lazy\LazyUuidFromString; |
| 24 |
use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Rfc4122\FieldsInterface as Rfc4122FieldsInterface; |
| 25 |
use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Type\Hexadecimal; |
| 26 |
use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Type\Integer as IntegerObject; |
| 27 |
use ValueError; |
| 28 |
use function assert; |
| 29 |
use function bin2hex; |
| 30 |
use function method_exists; |
| 31 |
use function preg_match; |
| 32 |
use function sprintf; |
| 33 |
use function str_replace; |
| 34 |
use function strcmp; |
| 35 |
use function strlen; |
| 36 |
use function strtolower; |
| 37 |
use function substr; |
| 38 |
/** |
| 39 |
* Uuid provides constants and static methods for working with and generating UUIDs |
| 40 |
* |
| 41 |
* @immutable |
| 42 |
*/ |
| 43 |
class Uuid implements UuidInterface |
| 44 |
{ |
| 45 |
use DeprecatedUuidMethodsTrait; |
| 46 |
/** |
| 47 |
* When this namespace is specified, the name string is a fully qualified domain name |
| 48 |
* |
| 49 |
* @link https://www.rfc-editor.org/rfc/rfc9562#section-6.6 RFC 9562, 6.6. Namespace ID Usage and Allocation |
| 50 |
*/ |
| 51 |
public const NAMESPACE_DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8'; |
| 52 |
/** |
| 53 |
* When this namespace is specified, the name string is a URL |
| 54 |
* |
| 55 |
* @link https://www.rfc-editor.org/rfc/rfc9562#section-6.6 RFC 9562, 6.6. Namespace ID Usage and Allocation |
| 56 |
*/ |
| 57 |
public const NAMESPACE_URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8'; |
| 58 |
/** |
| 59 |
* When this namespace is specified, the name string is an ISO OID |
| 60 |
* |
| 61 |
* @link https://www.rfc-editor.org/rfc/rfc9562#section-6.6 RFC 9562, 6.6. Namespace ID Usage and Allocation |
| 62 |
*/ |
| 63 |
public const NAMESPACE_OID = '6ba7b812-9dad-11d1-80b4-00c04fd430c8'; |
| 64 |
/** |
| 65 |
* When this namespace is specified, the name string is an X.500 DN (in DER or a text output format) |
| 66 |
* |
| 67 |
* @link https://www.rfc-editor.org/rfc/rfc9562#section-6.6 RFC 9562, 6.6. Namespace ID Usage and Allocation |
| 68 |
*/ |
| 69 |
public const NAMESPACE_X500 = '6ba7b814-9dad-11d1-80b4-00c04fd430c8'; |
| 70 |
/** |
| 71 |
* The Nil UUID is a special form of UUID that is specified to have all 128 bits set to zero |
| 72 |
* |
| 73 |
* @link https://www.rfc-editor.org/rfc/rfc9562#section-5.9 RFC 9562, 5.9. Nil UUID |
| 74 |
*/ |
| 75 |
public const NIL = '00000000-0000-0000-0000-000000000000'; |
| 76 |
/** |
| 77 |
* The Max UUID is a special form of UUID that is specified to have all 128 bits set to one |
| 78 |
* |
| 79 |
* @link https://www.rfc-editor.org/rfc/rfc9562#section-5.10 RFC 9562, 5.10. Max UUID |
| 80 |
*/ |
| 81 |
public const MAX = 'ffffffff-ffff-ffff-ffff-ffffffffffff'; |
| 82 |
/** |
| 83 |
* Variant: reserved, NCS backward compatibility |
| 84 |
* |
| 85 |
* @link https://www.rfc-editor.org/rfc/rfc9562#section-4.1 RFC 9562, 4.1. Variant Field |
| 86 |
*/ |
| 87 |
public const RESERVED_NCS = 0; |
| 88 |
/** |
| 89 |
* Variant: the UUID layout specified in RFC 9562 (formerly RFC 4122) |
| 90 |
* |
| 91 |
* @link https://www.rfc-editor.org/rfc/rfc9562#section-4.1 RFC 9562, 4.1. Variant Field |
| 92 |
* @see Uuid::RFC_9562 |
| 93 |
*/ |
| 94 |
public const RFC_4122 = 2; |
| 95 |
/** |
| 96 |
* Variant: the UUID layout specified in RFC 9562 (formerly RFC 4122) |
| 97 |
* |
| 98 |
* @link https://www.rfc-editor.org/rfc/rfc9562#section-4.1 RFC 9562, 4.1. Variant Field |
| 99 |
*/ |
| 100 |
public const RFC_9562 = 2; |
| 101 |
/** |
| 102 |
* Variant: reserved, Microsoft Corporation backward compatibility |
| 103 |
* |
| 104 |
* @link https://www.rfc-editor.org/rfc/rfc9562#section-4.1 RFC 9562, 4.1. Variant Field |
| 105 |
*/ |
| 106 |
public const RESERVED_MICROSOFT = 6; |
| 107 |
/** |
| 108 |
* Variant: reserved for future definition |
| 109 |
* |
| 110 |
* @link https://www.rfc-editor.org/rfc/rfc9562#section-4.1 RFC 9562, 4.1. Variant Field |
| 111 |
*/ |
| 112 |
public const RESERVED_FUTURE = 7; |
| 113 |
/** |
| 114 |
* @deprecated Use {@see ValidatorInterface::getPattern()} instead. |
| 115 |
*/ |
| 116 |
public const VALID_PATTERN = '^[0-9A-Fa-f]{8}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{4}-[0-9A-Fa-f]{12}$'; |
| 117 |
/** |
| 118 |
* Version 1 (Gregorian time) UUID |
| 119 |
* |
| 120 |
* @link https://www.rfc-editor.org/rfc/rfc9562#section-4.2 RFC 9562, 4.2. Version Field |
| 121 |
*/ |
| 122 |
public const UUID_TYPE_TIME = 1; |
| 123 |
/** |
| 124 |
* Version 2 (DCE Security) UUID |
| 125 |
* |
| 126 |
* @link https://www.rfc-editor.org/rfc/rfc9562#section-4.2 RFC 9562, 4.2. Version Field |
| 127 |
*/ |
| 128 |
public const UUID_TYPE_DCE_SECURITY = 2; |
| 129 |
/** |
| 130 |
* @deprecated Use {@see Uuid::UUID_TYPE_DCE_SECURITY} instead. |
| 131 |
*/ |
| 132 |
public const UUID_TYPE_IDENTIFIER = 2; |
| 133 |
/** |
| 134 |
* Version 3 (name-based and hashed with MD5) UUID |
| 135 |
* |
| 136 |
* @link https://www.rfc-editor.org/rfc/rfc9562#section-4.2 RFC 9562, 4.2. Version Field |
| 137 |
*/ |
| 138 |
public const UUID_TYPE_HASH_MD5 = 3; |
| 139 |
/** |
| 140 |
* Version 4 (random) UUID |
| 141 |
* |
| 142 |
* @link https://www.rfc-editor.org/rfc/rfc9562#section-4.2 RFC 9562, 4.2. Version Field |
| 143 |
*/ |
| 144 |
public const UUID_TYPE_RANDOM = 4; |
| 145 |
/** |
| 146 |
* Version 5 (name-based and hashed with SHA1) UUID |
| 147 |
* |
| 148 |
* @link https://www.rfc-editor.org/rfc/rfc9562#section-4.2 RFC 9562, 4.2. Version Field |
| 149 |
*/ |
| 150 |
public const UUID_TYPE_HASH_SHA1 = 5; |
| 151 |
/** |
| 152 |
* @deprecated Use {@see Uuid::UUID_TYPE_REORDERED_TIME} instead. |
| 153 |
*/ |
| 154 |
public const UUID_TYPE_PEABODY = 6; |
| 155 |
/** |
| 156 |
* Version 6 (reordered Gregorian time) UUID |
| 157 |
* |
| 158 |
* @link https://www.rfc-editor.org/rfc/rfc9562#section-4.2 RFC 9562, 4.2. Version Field |
| 159 |
*/ |
| 160 |
public const UUID_TYPE_REORDERED_TIME = 6; |
| 161 |
/** |
| 162 |
* Version 7 (Unix Epoch time) UUID |
| 163 |
* |
| 164 |
* @link https://www.rfc-editor.org/rfc/rfc9562#section-4.2 RFC 9562, 4.2. Version Field |
| 165 |
*/ |
| 166 |
public const UUID_TYPE_UNIX_TIME = 7; |
| 167 |
/** |
| 168 |
* Version 8 (custom format) UUID |
| 169 |
* |
| 170 |
* @link https://www.rfc-editor.org/rfc/rfc9562#section-4.2 RFC 9562, 4.2. Version Field |
| 171 |
*/ |
| 172 |
public const UUID_TYPE_CUSTOM = 8; |
| 173 |
/** |
| 174 |
* DCE Security principal domain |
| 175 |
* |
| 176 |
* @link https://pubs.opengroup.org/onlinepubs/9696989899/chap11.htm#tagcjh_14_05_01_01 DCE 1.1, §11.5.1.1 |
| 177 |
*/ |
| 178 |
public const DCE_DOMAIN_PERSON = 0; |
| 179 |
/** |
| 180 |
* DCE Security group domain |
| 181 |
* |
| 182 |
* @link https://pubs.opengroup.org/onlinepubs/9696989899/chap11.htm#tagcjh_14_05_01_01 DCE 1.1, §11.5.1.1 |
| 183 |
*/ |
| 184 |
public const DCE_DOMAIN_GROUP = 1; |
| 185 |
/** |
| 186 |
* DCE Security organization domain |
| 187 |
* |
| 188 |
* @link https://pubs.opengroup.org/onlinepubs/9696989899/chap11.htm#tagcjh_14_05_01_01 DCE 1.1, §11.5.1.1 |
| 189 |
*/ |
| 190 |
public const DCE_DOMAIN_ORG = 2; |
| 191 |
/** |
| 192 |
* DCE Security domain string names |
| 193 |
* |
| 194 |
* @link https://pubs.opengroup.org/onlinepubs/9696989899/chap11.htm#tagcjh_14_05_01_01 DCE 1.1, §11.5.1.1 |
| 195 |
*/ |
| 196 |
public const DCE_DOMAIN_NAMES = [self::DCE_DOMAIN_PERSON => 'person', self::DCE_DOMAIN_GROUP => 'group', self::DCE_DOMAIN_ORG => 'org']; |
| 197 |
/** |
| 198 |
* @phpstan-ignore property.readOnlyByPhpDocDefaultValue |
| 199 |
*/ |
| 200 |
private static ?UuidFactoryInterface $factory = null; |
| 201 |
/** |
| 202 |
* @var bool flag to detect if the UUID factory was replaced internally, which disables all optimizations for the |
| 203 |
* default/happy path internal scenarios |
| 204 |
* @phpstan-ignore property.readOnlyByPhpDocDefaultValue |
| 205 |
*/ |
| 206 |
private static bool $factoryReplaced = \false; |
| 207 |
protected CodecInterface $codec; |
| 208 |
protected NumberConverterInterface $numberConverter; |
| 209 |
protected Rfc4122FieldsInterface $fields; |
| 210 |
protected TimeConverterInterface $timeConverter; |
| 211 |
/** |
| 212 |
* Creates a universally unique identifier (UUID) from an array of fields |
| 213 |
* |
| 214 |
* Unless you're making advanced use of this library to generate identifiers that deviate from RFC 9562 (formerly |
| 215 |
* RFC 4122), you probably do not want to instantiate a UUID directly. Use the static methods, instead: |
| 216 |
* |
| 217 |
* ``` |
| 218 |
* use Ramsey\Uuid\Uuid; |
| 219 |
* |
| 220 |
* $timeBasedUuid = Uuid::uuid1(); |
| 221 |
* $namespaceMd5Uuid = Uuid::uuid3(Uuid::NAMESPACE_URL, 'http://php.net/'); |
| 222 |
* $randomUuid = Uuid::uuid4(); |
| 223 |
* $namespaceSha1Uuid = Uuid::uuid5(Uuid::NAMESPACE_URL, 'http://php.net/'); |
| 224 |
* ``` |
| 225 |
* |
| 226 |
* @param Rfc4122FieldsInterface $fields The fields from which to construct a UUID |
| 227 |
* @param NumberConverterInterface $numberConverter The number converter to use for converting hex values to/from integers |
| 228 |
* @param CodecInterface $codec The codec to use when encoding or decoding UUID strings |
| 229 |
* @param TimeConverterInterface $timeConverter The time converter to use for converting timestamps extracted from a |
| 230 |
* UUID to unix timestamps |
| 231 |
*/ |
| 232 |
public function __construct(Rfc4122FieldsInterface $fields, NumberConverterInterface $numberConverter, CodecInterface $codec, TimeConverterInterface $timeConverter) |
| 233 |
{ |
| 234 |
$this->fields = $fields; |
| 235 |
$this->codec = $codec; |
| 236 |
$this->numberConverter = $numberConverter; |
| 237 |
$this->timeConverter = $timeConverter; |
| 238 |
} |
| 239 |
/** |
| 240 |
* @return non-empty-string |
| 241 |
*/ |
| 242 |
public function __toString() : string |
| 243 |
{ |
| 244 |
return $this->toString(); |
| 245 |
} |
| 246 |
/** |
| 247 |
* Converts the UUID to a string for JSON serialization |
| 248 |
*/ |
| 249 |
public function jsonSerialize() : string |
| 250 |
{ |
| 251 |
return $this->toString(); |
| 252 |
} |
| 253 |
/** |
| 254 |
* Converts the UUID to a string for PHP serialization |
| 255 |
*/ |
| 256 |
public function serialize() : string |
| 257 |
{ |
| 258 |
return $this->codec->encode($this); |
| 259 |
} |
| 260 |
/** |
| 261 |
* @return array{bytes: string} |
| 262 |
*/ |
| 263 |
public function __serialize() : array |
| 264 |
{ |
| 265 |
return ['bytes' => $this->serialize()]; |
| 266 |
} |
| 267 |
/** |
| 268 |
* Re-constructs the object from its serialized form |
| 269 |
* |
| 270 |
* @param string $data The serialized PHP string to unserialize into a UuidInterface instance |
| 271 |
*/ |
| 272 |
public function unserialize(string $data) : void |
| 273 |
{ |
| 274 |
if (strlen($data) === 16) { |
| 275 |
/** @var Uuid $uuid */ |
| 276 |
$uuid = self::getFactory()->fromBytes($data); |
| 277 |
} else { |
| 278 |
/** @var Uuid $uuid */ |
| 279 |
$uuid = self::getFactory()->fromString($data); |
| 280 |
} |
| 281 |
/** @phpstan-ignore property.readOnlyByPhpDocAssignNotInConstructor */ |
| 282 |
$this->codec = $uuid->codec; |
| 283 |
/** @phpstan-ignore property.readOnlyByPhpDocAssignNotInConstructor */ |
| 284 |
$this->numberConverter = $uuid->numberConverter; |
| 285 |
/** @phpstan-ignore property.readOnlyByPhpDocAssignNotInConstructor */ |
| 286 |
$this->fields = $uuid->fields; |
| 287 |
/** @phpstan-ignore property.readOnlyByPhpDocAssignNotInConstructor */ |
| 288 |
$this->timeConverter = $uuid->timeConverter; |
| 289 |
} |
| 290 |
/** |
| 291 |
* @param array{bytes?: string} $data |
| 292 |
*/ |
| 293 |
public function __unserialize(array $data) : void |
| 294 |
{ |
| 295 |
// @codeCoverageIgnoreStart |
| 296 |
if (!isset($data['bytes'])) { |
| 297 |
throw new ValueError(sprintf('%s(): Argument #1 ($data) is invalid', __METHOD__)); |
| 298 |
} |
| 299 |
// @codeCoverageIgnoreEnd |
| 300 |
$this->unserialize($data['bytes']); |
| 301 |
} |
| 302 |
public function compareTo(UuidInterface $other) : int |
| 303 |
{ |
| 304 |
$compare = strcmp($this->toString(), $other->toString()); |
| 305 |
if ($compare < 0) { |
| 306 |
return -1; |
| 307 |
} |
| 308 |
if ($compare > 0) { |
| 309 |
return 1; |
| 310 |
} |
| 311 |
return 0; |
| 312 |
} |
| 313 |
public function equals(?object $other) : bool |
| 314 |
{ |
| 315 |
if (!$other instanceof UuidInterface) { |
| 316 |
return \false; |
| 317 |
} |
| 318 |
return $this->compareTo($other) === 0; |
| 319 |
} |
| 320 |
/** |
| 321 |
* @return non-empty-string |
| 322 |
*/ |
| 323 |
public function getBytes() : string |
| 324 |
{ |
| 325 |
return $this->codec->encodeBinary($this); |
| 326 |
} |
| 327 |
public function getFields() : FieldsInterface |
| 328 |
{ |
| 329 |
return $this->fields; |
| 330 |
} |
| 331 |
public function getHex() : Hexadecimal |
| 332 |
{ |
| 333 |
return new Hexadecimal(str_replace('-', '', $this->toString())); |
| 334 |
} |
| 335 |
public function getInteger() : IntegerObject |
| 336 |
{ |
| 337 |
return new IntegerObject($this->numberConverter->fromHex($this->getHex()->toString())); |
| 338 |
} |
| 339 |
public function getUrn() : string |
| 340 |
{ |
| 341 |
return 'urn:uuid:' . $this->toString(); |
| 342 |
} |
| 343 |
/** |
| 344 |
* @return non-empty-string |
| 345 |
*/ |
| 346 |
public function toString() : string |
| 347 |
{ |
| 348 |
return $this->codec->encode($this); |
| 349 |
} |
| 350 |
/** |
| 351 |
* Returns the factory used to create UUIDs |
| 352 |
*/ |
| 353 |
public static function getFactory() : UuidFactoryInterface |
| 354 |
{ |
| 355 |
if (self::$factory === null) { |
| 356 |
self::$factory = new UuidFactory(); |
| 357 |
} |
| 358 |
return self::$factory; |
| 359 |
} |
| 360 |
/** |
| 361 |
* Sets the factory used to create UUIDs |
| 362 |
* |
| 363 |
* @param UuidFactoryInterface $factory A factory that will be used by this class to create UUIDs |
| 364 |
*/ |
| 365 |
public static function setFactory(UuidFactoryInterface $factory) : void |
| 366 |
{ |
| 367 |
// Note: non-strict equality is intentional here. If the factory is configured differently, every assumption |
| 368 |
// around purity is broken, and we have to internally decide everything differently. |
| 369 |
// phpcs:ignore SlevomatCodingStandard.Operators.DisallowEqualOperators.DisallowedNotEqualOperator |
| 370 |
self::$factoryReplaced = $factory != new UuidFactory(); |
| 371 |
self::$factory = $factory; |
| 372 |
} |
| 373 |
/** |
| 374 |
* Creates a UUID from a byte string |
| 375 |
* |
| 376 |
* @param string $bytes A binary string |
| 377 |
* |
| 378 |
* @return UuidInterface A UuidInterface instance created from a binary string representation |
| 379 |
* |
| 380 |
* @throws InvalidArgumentException |
| 381 |
* |
| 382 |
* @pure |
| 383 |
*/ |
| 384 |
public static function fromBytes(string $bytes) : UuidInterface |
| 385 |
{ |
| 386 |
/** @phpstan-ignore impure.staticPropertyAccess */ |
| 387 |
if (!self::$factoryReplaced && strlen($bytes) === 16) { |
| 388 |
$base16Uuid = bin2hex($bytes); |
| 389 |
// Note: we are calling `fromString` internally because we don't know if the given `$bytes` is a valid UUID |
| 390 |
return self::fromString(substr($base16Uuid, 0, 8) . '-' . substr($base16Uuid, 8, 4) . '-' . substr($base16Uuid, 12, 4) . '-' . substr($base16Uuid, 16, 4) . '-' . substr($base16Uuid, 20, 12)); |
| 391 |
} |
| 392 |
/** @phpstan-ignore possiblyImpure.methodCall */ |
| 393 |
return self::getFactory()->fromBytes($bytes); |
| 394 |
} |
| 395 |
/** |
| 396 |
* Creates a UUID from the string standard representation |
| 397 |
* |
| 398 |
* @param string $uuid A hexadecimal string |
| 399 |
* |
| 400 |
* @return UuidInterface A UuidInterface instance created from a hexadecimal string representation |
| 401 |
* |
| 402 |
* @throws InvalidArgumentException |
| 403 |
* |
| 404 |
* @pure |
| 405 |
*/ |
| 406 |
public static function fromString(string $uuid) : UuidInterface |
| 407 |
{ |
| 408 |
$uuid = strtolower($uuid); |
| 409 |
/** @phpstan-ignore impure.staticPropertyAccess, possiblyImpure.functionCall */ |
| 410 |
if (!self::$factoryReplaced && preg_match(LazyUuidFromString::VALID_REGEX, $uuid) === 1) { |
| 411 |
/** @phpstan-ignore possiblyImpure.functionCall */ |
| 412 |
assert($uuid !== ''); |
| 413 |
/** @phpstan-ignore possiblyImpure.new */ |
| 414 |
return new LazyUuidFromString($uuid); |
| 415 |
} |
| 416 |
/** @phpstan-ignore possiblyImpure.methodCall */ |
| 417 |
return self::getFactory()->fromString($uuid); |
| 418 |
} |
| 419 |
/** |
| 420 |
* Creates a UUID from a DateTimeInterface instance |
| 421 |
* |
| 422 |
* @param DateTimeInterface $dateTime The date and time |
| 423 |
* @param Hexadecimal | null $node A 48-bit number representing the hardware address |
| 424 |
* @param int | null $clockSeq A 14-bit number used to help avoid duplicates that could arise when the clock is set |
| 425 |
* backwards in time or if the node ID changes |
| 426 |
* |
| 427 |
* @return UuidInterface A UuidInterface instance that represents a version 1 UUID created from a DateTimeInterface instance |
| 428 |
*/ |
| 429 |
public static function fromDateTime(DateTimeInterface $dateTime, ?Hexadecimal $node = null, ?int $clockSeq = null) : UuidInterface |
| 430 |
{ |
| 431 |
return self::getFactory()->fromDateTime($dateTime, $node, $clockSeq); |
| 432 |
} |
| 433 |
/** |
| 434 |
* Creates a UUID from the Hexadecimal object |
| 435 |
* |
| 436 |
* @param Hexadecimal $hex Hexadecimal object representing a hexadecimal number |
| 437 |
* |
| 438 |
* @return UuidInterface A UuidInterface instance created from the Hexadecimal object representing a hexadecimal number |
| 439 |
* |
| 440 |
* @throws InvalidArgumentException |
| 441 |
* |
| 442 |
* @pure |
| 443 |
*/ |
| 444 |
public static function fromHexadecimal(Hexadecimal $hex) : UuidInterface |
| 445 |
{ |
| 446 |
/** @phpstan-ignore possiblyImpure.methodCall */ |
| 447 |
$factory = self::getFactory(); |
| 448 |
if (method_exists($factory, 'fromHexadecimal')) { |
| 449 |
/** @phpstan-ignore possiblyImpure.methodCall */ |
| 450 |
$uuid = $factory->fromHexadecimal($hex); |
| 451 |
/** @phpstan-ignore possiblyImpure.functionCall */ |
| 452 |
assert($uuid instanceof UuidInterface); |
| 453 |
return $uuid; |
| 454 |
} |
| 455 |
throw new BadMethodCallException('The method fromHexadecimal() does not exist on the provided factory'); |
| 456 |
} |
| 457 |
/** |
| 458 |
* Creates a UUID from a 128-bit integer string |
| 459 |
* |
| 460 |
* @param string $integer String representation of 128-bit integer |
| 461 |
* |
| 462 |
* @return UuidInterface A UuidInterface instance created from the string representation of a 128-bit integer |
| 463 |
* |
| 464 |
* @throws InvalidArgumentException |
| 465 |
* |
| 466 |
* @pure |
| 467 |
*/ |
| 468 |
public static function fromInteger(string $integer) : UuidInterface |
| 469 |
{ |
| 470 |
/** @phpstan-ignore possiblyImpure.methodCall */ |
| 471 |
return self::getFactory()->fromInteger($integer); |
| 472 |
} |
| 473 |
/** |
| 474 |
* Returns true if the provided string is a valid UUID |
| 475 |
* |
| 476 |
* @param string $uuid A string to validate as a UUID |
| 477 |
* |
| 478 |
* @return bool True if the string is a valid UUID, false otherwise |
| 479 |
* |
| 480 |
* @phpstan-assert-if-true =non-empty-string $uuid |
| 481 |
* |
| 482 |
* @pure |
| 483 |
*/ |
| 484 |
public static function isValid(string $uuid) : bool |
| 485 |
{ |
| 486 |
/** @phpstan-ignore possiblyImpure.methodCall, possiblyImpure.methodCall */ |
| 487 |
return self::getFactory()->getValidator()->validate($uuid); |
| 488 |
} |
| 489 |
/** |
| 490 |
* Returns a version 1 (Gregorian time) UUID from a host ID, sequence number, and the current time |
| 491 |
* |
| 492 |
* @param Hexadecimal | int | string | null $node A 48-bit number representing the hardware address; this number may |
| 493 |
* be represented as an integer or a hexadecimal string |
| 494 |
* @param int | null $clockSeq A 14-bit number used to help avoid duplicates that could arise when the clock is set |
| 495 |
* backwards in time or if the node ID changes |
| 496 |
* |
| 497 |
* @return UuidInterface A UuidInterface instance that represents a version 1 UUID |
| 498 |
*/ |
| 499 |
public static function uuid1($node = null, ?int $clockSeq = null) : UuidInterface |
| 500 |
{ |
| 501 |
return self::getFactory()->uuid1($node, $clockSeq); |
| 502 |
} |
| 503 |
/** |
| 504 |
* Returns a version 2 (DCE Security) UUID from a local domain, local identifier, host ID, clock sequence, and the current time |
| 505 |
* |
| 506 |
* @param int $localDomain The local domain to use when generating bytes, according to DCE Security |
| 507 |
* @param IntegerObject | null $localIdentifier The local identifier for the given domain; this may be a UID or GID |
| 508 |
* on POSIX systems, if the local domain is "person" or "group," or it may be a site-defined identifier if the |
| 509 |
* local domain is "org" |
| 510 |
* @param Hexadecimal | null $node A 48-bit number representing the hardware address |
| 511 |
* @param int | null $clockSeq A 14-bit number used to help avoid duplicates that could arise when the clock is set |
| 512 |
* backwards in time or if the node ID changes (in a version 2 UUID, the lower 8 bits of this number are |
| 513 |
* replaced with the domain). |
| 514 |
* |
| 515 |
* @return UuidInterface A UuidInterface instance that represents a version 2 UUID |
| 516 |
*/ |
| 517 |
public static function uuid2(int $localDomain, ?IntegerObject $localIdentifier = null, ?Hexadecimal $node = null, ?int $clockSeq = null) : UuidInterface |
| 518 |
{ |
| 519 |
return self::getFactory()->uuid2($localDomain, $localIdentifier, $node, $clockSeq); |
| 520 |
} |
| 521 |
/** |
| 522 |
* Returns a version 3 (name-based) UUID based on the MD5 hash of a namespace ID and a name |
| 523 |
* |
| 524 |
* @param UuidInterface | string $ns The namespace (must be a valid UUID) |
| 525 |
* @param string $name The name to use for creating a UUID |
| 526 |
* |
| 527 |
* @return UuidInterface A UuidInterface instance that represents a version 3 UUID |
| 528 |
* |
| 529 |
* @pure |
| 530 |
*/ |
| 531 |
public static function uuid3($ns, string $name) : UuidInterface |
| 532 |
{ |
| 533 |
/** @phpstan-ignore possiblyImpure.methodCall */ |
| 534 |
return self::getFactory()->uuid3($ns, $name); |
| 535 |
} |
| 536 |
/** |
| 537 |
* Returns a version 4 (random) UUID |
| 538 |
* |
| 539 |
* @return UuidInterface A UuidInterface instance that represents a version 4 UUID |
| 540 |
*/ |
| 541 |
public static function uuid4() : UuidInterface |
| 542 |
{ |
| 543 |
return self::getFactory()->uuid4(); |
| 544 |
} |
| 545 |
/** |
| 546 |
* Returns a version 5 (name-based) UUID based on the SHA-1 hash of a namespace ID and a name |
| 547 |
* |
| 548 |
* @param UuidInterface | string $ns The namespace (must be a valid UUID) |
| 549 |
* @param string $name The name to use for creating a UUID |
| 550 |
* |
| 551 |
* @return UuidInterface A UuidInterface instance that represents a version 5 UUID |
| 552 |
* |
| 553 |
* @pure |
| 554 |
*/ |
| 555 |
public static function uuid5($ns, string $name) : UuidInterface |
| 556 |
{ |
| 557 |
/** @phpstan-ignore possiblyImpure.methodCall */ |
| 558 |
return self::getFactory()->uuid5($ns, $name); |
| 559 |
} |
| 560 |
/** |
| 561 |
* Returns a version 6 (reordered Gregorian time) UUID from a host ID, sequence number, and the current time |
| 562 |
* |
| 563 |
* @param Hexadecimal | null $node A 48-bit number representing the hardware address |
| 564 |
* @param int | null $clockSeq A 14-bit number used to help avoid duplicates that could arise when the clock is set |
| 565 |
* backwards in time or if the node ID changes |
| 566 |
* |
| 567 |
* @return UuidInterface A UuidInterface instance that represents a version 6 UUID |
| 568 |
*/ |
| 569 |
public static function uuid6(?Hexadecimal $node = null, ?int $clockSeq = null) : UuidInterface |
| 570 |
{ |
| 571 |
return self::getFactory()->uuid6($node, $clockSeq); |
| 572 |
} |
| 573 |
/** |
| 574 |
* Returns a version 7 (Unix Epoch time) UUID |
| 575 |
* |
| 576 |
* @param DateTimeInterface | null $dateTime An optional date/time from which to create the version 7 UUID. If not |
| 577 |
* provided, the UUID is generated using the current date/time. |
| 578 |
* |
| 579 |
* @return UuidInterface A UuidInterface instance that represents a version 7 UUID |
| 580 |
*/ |
| 581 |
public static function uuid7(?DateTimeInterface $dateTime = null) : UuidInterface |
| 582 |
{ |
| 583 |
$factory = self::getFactory(); |
| 584 |
if (method_exists($factory, 'uuid7')) { |
| 585 |
/** @var UuidInterface */ |
| 586 |
return $factory->uuid7($dateTime); |
| 587 |
} |
| 588 |
throw new UnsupportedOperationException('The provided factory does not support the uuid7() method'); |
| 589 |
} |
| 590 |
/** |
| 591 |
* Returns a version 8 (custom format) UUID |
| 592 |
* |
| 593 |
* The bytes provided may contain any value according to your application's needs. Be aware, however, that other |
| 594 |
* applications may not understand the semantics of the value. |
| 595 |
* |
| 596 |
* @param string $bytes A 16-byte octet string. This is an open blob of data that you may fill with 128 bits of |
| 597 |
* information. Be aware, however, bits 48 through 51 will be replaced with the UUID version field, and bits 64 |
| 598 |
* and 65 will be replaced with the UUID variant. You MUST NOT rely on these bits for your application needs. |
| 599 |
* |
| 600 |
* @return UuidInterface A UuidInterface instance that represents a version 8 UUID |
| 601 |
* |
| 602 |
* @pure |
| 603 |
*/ |
| 604 |
public static function uuid8(string $bytes) : UuidInterface |
| 605 |
{ |
| 606 |
/** @phpstan-ignore possiblyImpure.methodCall */ |
| 607 |
$factory = self::getFactory(); |
| 608 |
if (method_exists($factory, 'uuid8')) { |
| 609 |
/** |
| 610 |
* @var UuidInterface |
| 611 |
* @phpstan-ignore possiblyImpure.methodCall |
| 612 |
*/ |
| 613 |
return $factory->uuid8($bytes); |
| 614 |
} |
| 615 |
throw new UnsupportedOperationException('The provided factory does not support the uuid8() method'); |
| 616 |
} |
| 617 |
} |
| 618 |
|