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