| 148 |
lars |
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 str_starts_with;
|
|
|
23 |
use function strlen;
|
|
|
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 |
if ($this->isMax() || $this->isNil()) {
|
|
|
62 |
// RFC 4122 defines these special types of UUID, so we will consider
|
|
|
63 |
// them as belonging to the RFC 4122 variant.
|
|
|
64 |
return Uuid::RFC_4122;
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
/** @var int[] $parts */
|
|
|
68 |
$parts = unpack('n*', $this->getBytes());
|
|
|
69 |
|
|
|
70 |
// $parts[5] is a 16-bit, unsigned integer containing the variant bits
|
|
|
71 |
// of the UUID. We convert this integer into a string containing a
|
|
|
72 |
// binary representation, padded to 16 characters. We analyze the first
|
|
|
73 |
// three characters (three most-significant bits) to determine the
|
|
|
74 |
// variant.
|
|
|
75 |
$binary = str_pad(
|
|
|
76 |
decbin($parts[5]),
|
|
|
77 |
16,
|
|
|
78 |
'0',
|
|
|
79 |
STR_PAD_LEFT
|
|
|
80 |
);
|
|
|
81 |
|
|
|
82 |
$msb = substr($binary, 0, 3);
|
|
|
83 |
|
|
|
84 |
if ($msb === '111') {
|
|
|
85 |
return Uuid::RESERVED_FUTURE;
|
|
|
86 |
} elseif ($msb === '110') {
|
|
|
87 |
return Uuid::RESERVED_MICROSOFT;
|
|
|
88 |
} elseif (str_starts_with($msb, '10')) {
|
|
|
89 |
return Uuid::RFC_4122;
|
|
|
90 |
}
|
|
|
91 |
|
|
|
92 |
return Uuid::RESERVED_NCS;
|
|
|
93 |
}
|
|
|
94 |
}
|