| 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\Exception\InvalidBytesException; |
| 16 |
use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Uuid; |
| 17 |
use function decbin; |
| 18 |
use function str_pad; |
| 19 |
use function str_starts_with; |
| 20 |
use function strlen; |
| 21 |
use function substr; |
| 22 |
use function unpack; |
| 23 |
use const STR_PAD_LEFT; |
| 24 |
/** |
| 25 |
* Provides common functionality for handling the variant, as defined by RFC 9562 (formerly RFC 4122) |
| 26 |
* |
| 27 |
* @immutable |
| 28 |
*/ |
| 29 |
trait VariantTrait |
| 30 |
{ |
| 31 |
/** |
| 32 |
* Returns the bytes that comprise the fields |
| 33 |
*/ |
| 34 |
public abstract function getBytes() : string; |
| 35 |
/** |
| 36 |
* Returns the variant |
| 37 |
* |
| 38 |
* The variant number describes the layout of the UUID. The variant number has the following meaning: |
| 39 |
* |
| 40 |
* - 0 - Reserved for NCS backward compatibility |
| 41 |
* - 2 - The RFC 9562 (formerly RFC 4122) variant |
| 42 |
* - 6 - Reserved, Microsoft Corporation backward compatibility |
| 43 |
* - 7 - Reserved for future definition |
| 44 |
* |
| 45 |
* For RFC 9562 (formerly RFC 4122) variant UUIDs, this value should always be the integer `2`. |
| 46 |
* |
| 47 |
* @link https://www.rfc-editor.org/rfc/rfc9562#section-4.1 RFC 9562, 4.1. Variant Field |
| 48 |
*/ |
| 49 |
public function getVariant() : int |
| 50 |
{ |
| 51 |
if (strlen($this->getBytes()) !== 16) { |
| 52 |
throw new InvalidBytesException('Invalid number of bytes'); |
| 53 |
} |
| 54 |
// According to RFC 9562, sections {@link https://www.rfc-editor.org/rfc/rfc9562#section-4.1 4.1} and |
| 55 |
// {@link https://www.rfc-editor.org/rfc/rfc9562#section-5.10 5.10}, the Max UUID falls within the range |
| 56 |
// of the future variant. |
| 57 |
if ($this->isMax()) { |
| 58 |
return Uuid::RESERVED_FUTURE; |
| 59 |
} |
| 60 |
// According to RFC 9562, sections {@link https://www.rfc-editor.org/rfc/rfc9562#section-4.1 4.1} and |
| 61 |
// {@link https://www.rfc-editor.org/rfc/rfc9562#section-5.9 5.9}, the Nil UUID falls within the range |
| 62 |
// of the Apollo NCS variant. |
| 63 |
if ($this->isNil()) { |
| 64 |
return Uuid::RESERVED_NCS; |
| 65 |
} |
| 66 |
/** @var int[] $parts */ |
| 67 |
$parts = unpack('n*', $this->getBytes()); |
| 68 |
// $parts[5] is a 16-bit, unsigned integer containing the variant bits of the UUID. We convert this integer into |
| 69 |
// a string containing a binary representation, padded to 16 characters. We analyze the first three characters |
| 70 |
// (three most-significant bits) to determine the variant. |
| 71 |
$msb = substr(str_pad(decbin($parts[5]), 16, '0', STR_PAD_LEFT), 0, 3); |
| 72 |
if ($msb === '111') { |
| 73 |
return Uuid::RESERVED_FUTURE; |
| 74 |
} elseif ($msb === '110') { |
| 75 |
return Uuid::RESERVED_MICROSOFT; |
| 76 |
} elseif (str_starts_with($msb, '10')) { |
| 77 |
return Uuid::RFC_4122; |
| 78 |
} |
| 79 |
return Uuid::RESERVED_NCS; |
| 80 |
} |
| 81 |
} |
| 82 |
|