| 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\Rfc4122; |
| 14 |
|
| 15 |
use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Uuid; |
| 16 |
/** |
| 17 |
* Provides common functionality for handling the version, as defined by RFC 9562 (formerly RFC 4122) |
| 18 |
* |
| 19 |
* @immutable |
| 20 |
*/ |
| 21 |
trait VersionTrait |
| 22 |
{ |
| 23 |
/** |
| 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 |
| 43 |
*/ |
| 44 |
public abstract function getVersion() : ?int; |
| 45 |
/** |
| 46 |
* Returns true if these fields represent a max UUID |
| 47 |
*/ |
| 48 |
public abstract function isMax() : bool; |
| 49 |
/** |
| 50 |
* Returns true if these fields represent a nil UUID |
| 51 |
*/ |
| 52 |
public abstract function isNil() : bool; |
| 53 |
/** |
| 54 |
* Returns true if the version matches one of those defined by RFC 9562 (formerly RFC 4122) |
| 55 |
* |
| 56 |
* @return bool True if the UUID version is valid, false otherwise |
| 57 |
*/ |
| 58 |
private function isCorrectVersion() : bool |
| 59 |
{ |
| 60 |
if ($this->isNil() || $this->isMax()) { |
| 61 |
return \true; |
| 62 |
} |
| 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 |
}; |
| 67 |
} |
| 68 |
} |
| 69 |
|