Fields.php
3 years ago
FieldsInterface.php
3 years ago
NilTrait.php
3 years ago
NilUuid.php
3 years ago
UuidBuilder.php
3 years ago
UuidInterface.php
3 years ago
UuidV1.php
3 years ago
UuidV2.php
3 years ago
UuidV3.php
3 years ago
UuidV4.php
3 years ago
UuidV5.php
3 years ago
Validator.php
3 years ago
VariantTrait.php
3 years ago
VersionTrait.php
3 years ago
VariantTrait.php
91 lines
| 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 <ben@benramsey.com> |
| 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\Exception\InvalidBytesException; |
| 18 | use Ramsey\Uuid\Uuid; |
| 19 | |
| 20 | use function decbin; |
| 21 | use function str_pad; |
| 22 | use function strlen; |
| 23 | use function strpos; |
| 24 | use function substr; |
| 25 | use function unpack; |
| 26 | |
| 27 | use const STR_PAD_LEFT; |
| 28 | |
| 29 | /** |
| 30 | * Provides common functionality for handling the variant, as defined by RFC 4122 |
| 31 | * |
| 32 | * @psalm-immutable |
| 33 | */ |
| 34 | trait VariantTrait |
| 35 | { |
| 36 | /** |
| 37 | * Returns the bytes that comprise the fields |
| 38 | */ |
| 39 | abstract public function getBytes(): string; |
| 40 | |
| 41 | /** |
| 42 | * Returns the variant identifier, according to RFC 4122, for the given bytes |
| 43 | * |
| 44 | * The following values may be returned: |
| 45 | * |
| 46 | * - `0` -- Reserved, NCS backward compatibility. |
| 47 | * - `2` -- The variant specified in RFC 4122. |
| 48 | * - `6` -- Reserved, Microsoft Corporation backward compatibility. |
| 49 | * - `7` -- Reserved for future definition. |
| 50 | * |
| 51 | * @link https://tools.ietf.org/html/rfc4122#section-4.1.1 RFC 4122, § 4.1.1: Variant |
| 52 | * |
| 53 | * @return int The variant identifier, according to RFC 4122 |
| 54 | */ |
| 55 | public function getVariant(): int |
| 56 | { |
| 57 | if (strlen($this->getBytes()) !== 16) { |
| 58 | throw new InvalidBytesException('Invalid number of bytes'); |
| 59 | } |
| 60 | |
| 61 | /** @var array $parts */ |
| 62 | $parts = unpack('n*', $this->getBytes()); |
| 63 | |
| 64 | // $parts[5] is a 16-bit, unsigned integer containing the variant bits |
| 65 | // of the UUID. We convert this integer into a string containing a |
| 66 | // binary representation, padded to 16 characters. We analyze the first |
| 67 | // three characters (three most-significant bits) to determine the |
| 68 | // variant. |
| 69 | $binary = str_pad( |
| 70 | decbin((int) $parts[5]), |
| 71 | 16, |
| 72 | '0', |
| 73 | STR_PAD_LEFT |
| 74 | ); |
| 75 | |
| 76 | $msb = substr($binary, 0, 3); |
| 77 | |
| 78 | if ($msb === '111') { |
| 79 | $variant = Uuid::RESERVED_FUTURE; |
| 80 | } elseif ($msb === '110') { |
| 81 | $variant = Uuid::RESERVED_MICROSOFT; |
| 82 | } elseif (strpos($msb, '10') === 0) { |
| 83 | $variant = Uuid::RFC_4122; |
| 84 | } else { |
| 85 | $variant = Uuid::RESERVED_NCS; |
| 86 | } |
| 87 | |
| 88 | return $variant; |
| 89 | } |
| 90 | } |
| 91 |