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