Debian Codegraph @

vendor/ramsey/uuid/src/Rfc4122/VersionTrait.php

raw
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\Rfc4122;
16
17 use Ramsey\Uuid\Uuid;
18
19 /**
20 * Provides common functionality for handling the version, as defined by RFC 9562 (formerly RFC 4122)
21 *
22 * @immutable
23 */
24 trait VersionTrait
25 {
26 /**
27 * Returns the UUID version
28 *
29 * The version number describes how the UUID was generated and has the following meaning:
30 *
31 * 1. Gregorian time UUID
32 * 2. DCE security UUID
33 * 3. Name-based UUID hashed with MD5
34 * 4. Randomly generated UUID
35 * 5. Name-based UUID hashed with SHA-1
36 * 6. Reordered Gregorian time UUID
37 * 7. Unix Epoch time UUID
38 * 8. Custom format UUID
39 *
40 * This returns `null` if the UUID is not an RFC 9562 (formerly RFC 4122) variant, since the version is only
41 * meaningful for this variant.
42 *
43 * @link https://www.rfc-editor.org/rfc/rfc9562#section-4.2 RFC 9562, 4.2. Version Field
44 *
45 * @pure
46 */
47 abstract public function getVersion(): ?int;
48
49 /**
50 * Returns true if these fields represent a max UUID
51 */
52 abstract public function isMax(): bool;
53
54 /**
55 * Returns true if these fields represent a nil UUID
56 */
57 abstract public function isNil(): bool;
58
59 /**
60 * Returns true if the version matches one of those defined by RFC 9562 (formerly RFC 4122)
61 *
62 * @return bool True if the UUID version is valid, false otherwise
63 */
64 private function isCorrectVersion(): bool
65 {
66 if ($this->isNil() || $this->isMax()) {
67 return true;
68 }
69
70 return match ($this->getVersion()) {
71 Uuid::UUID_TYPE_TIME, Uuid::UUID_TYPE_DCE_SECURITY,
72 Uuid::UUID_TYPE_HASH_MD5, Uuid::UUID_TYPE_RANDOM,
73 Uuid::UUID_TYPE_HASH_SHA1, Uuid::UUID_TYPE_REORDERED_TIME,
74 Uuid::UUID_TYPE_UNIX_TIME, Uuid::UUID_TYPE_CUSTOM => true,
75 default => false,
76 };
77 }
78 }