PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.1.0
Defender Security – Malware Scanner, Login Security & Firewall v6.1.0
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / vendor / ramsey / uuid / src / Uuid.php

Uuid.php in Defender Security – Malware Scanner, Login Security & Firewall 6.1.0, at vendor/ramsey/uuid/src/Uuid.php

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