Debian Codegraph @

vendor/ramsey/uuid/src/Guid/Fields.php

raw
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
13 declare(strict_types=1);
14
15 namespace Ramsey\Uuid\Guid;
16
17 use Ramsey\Uuid\Exception\InvalidArgumentException;
18 use Ramsey\Uuid\Fields\SerializableFieldsTrait;
19 use Ramsey\Uuid\Rfc4122\FieldsInterface;
20 use Ramsey\Uuid\Rfc4122\MaxTrait;
21 use Ramsey\Uuid\Rfc4122\NilTrait;
22 use Ramsey\Uuid\Rfc4122\VariantTrait;
23 use Ramsey\Uuid\Rfc4122\VersionTrait;
24 use Ramsey\Uuid\Type\Hexadecimal;
25 use Ramsey\Uuid\Uuid;
26
27 use function bin2hex;
28 use function dechex;
29 use function hexdec;
30 use function pack;
31 use function sprintf;
32 use function str_pad;
33 use function strlen;
34 use function substr;
35 use function unpack;
36
37 use const STR_PAD_LEFT;
38
39 /**
40 * GUIDs consist of a set of named fields, according to RFC 9562 (formerly RFC 4122)
41 *
42 * @see Guid
43 *
44 * @immutable
45 */
46 final class Fields implements FieldsInterface
47 {
48 use MaxTrait;
49 use NilTrait;
50 use SerializableFieldsTrait;
51 use VariantTrait;
52 use VersionTrait;
53
54 /**
55 * @param string $bytes A 16-byte binary string representation of a UUID
56 *
57 * @throws InvalidArgumentException if the byte string is not exactly 16 bytes
58 * @throws InvalidArgumentException if the byte string does not represent a GUID
59 * @throws InvalidArgumentException if the byte string does not contain a valid version
60 */
61 public function __construct(private string $bytes)
62 {
63 if (strlen($this->bytes) !== 16) {
64 throw new InvalidArgumentException(
65 'The byte string must be 16 bytes long; received ' . strlen($this->bytes) . ' bytes',
66 );
67 }
68
69 if (!$this->isCorrectVariant()) {
70 throw new InvalidArgumentException(
71 'The byte string received does not conform to the RFC 9562 (formerly RFC 4122) '
72 . 'or Microsoft Corporation variants',
73 );
74 }
75
76 if (!$this->isCorrectVersion()) {
77 throw new InvalidArgumentException('The byte string received does not contain a valid version');
78 }
79 }
80
81 public function getBytes(): string
82 {
83 return $this->bytes;
84 }
85
86 public function getTimeLow(): Hexadecimal
87 {
88 // Swap the bytes from little endian to network byte order.
89 /** @var string[] $hex */
90 $hex = unpack(
91 'H*',
92 pack(
93 'v*',
94 hexdec(bin2hex(substr($this->bytes, 2, 2))),
95 hexdec(bin2hex(substr($this->bytes, 0, 2))),
96 ),
97 );
98
99 return new Hexadecimal($hex[1] ?? '');
100 }
101
102 public function getTimeMid(): Hexadecimal
103 {
104 // Swap the bytes from little endian to network byte order.
105 /** @var string[] $hex */
106 $hex = unpack('H*', pack('v', hexdec(bin2hex(substr($this->bytes, 4, 2)))));
107
108 return new Hexadecimal($hex[1] ?? '');
109 }
110
111 public function getTimeHiAndVersion(): Hexadecimal
112 {
113 // Swap the bytes from little endian to network byte order.
114 /** @var string[] $hex */
115 $hex = unpack('H*', pack('v', hexdec(bin2hex(substr($this->bytes, 6, 2)))));
116
117 return new Hexadecimal($hex[1] ?? '');
118 }
119
120 public function getTimestamp(): Hexadecimal
121 {
122 return new Hexadecimal(sprintf(
123 '%03x%04s%08s',
124 hexdec($this->getTimeHiAndVersion()->toString()) & 0x0fff,
125 $this->getTimeMid()->toString(),
126 $this->getTimeLow()->toString()
127 ));
128 }
129
130 public function getClockSeq(): Hexadecimal
131 {
132 if ($this->isMax()) {
133 $clockSeq = 0xffff;
134 } elseif ($this->isNil()) {
135 $clockSeq = 0x0000;
136 } else {
137 $clockSeq = hexdec(bin2hex(substr($this->bytes, 8, 2))) & 0x3fff;
138 }
139
140 return new Hexadecimal(str_pad(dechex($clockSeq), 4, '0', STR_PAD_LEFT));
141 }
142
143 public function getClockSeqHiAndReserved(): Hexadecimal
144 {
145 return new Hexadecimal(bin2hex(substr($this->bytes, 8, 1)));
146 }
147
148 public function getClockSeqLow(): Hexadecimal
149 {
150 return new Hexadecimal(bin2hex(substr($this->bytes, 9, 1)));
151 }
152
153 public function getNode(): Hexadecimal
154 {
155 return new Hexadecimal(bin2hex(substr($this->bytes, 10)));
156 }
157
158 public function getVersion(): ?int
159 {
160 if ($this->isNil() || $this->isMax()) {
161 return null;
162 }
163
164 /** @var int[] $parts */
165 $parts = unpack('n*', $this->bytes);
166
167 return ($parts[4] >> 4) & 0x00f;
168 }
169
170 private function isCorrectVariant(): bool
171 {
172 if ($this->isNil() || $this->isMax()) {
173 return true;
174 }
175
176 $variant = $this->getVariant();
177
178 return $variant === Uuid::RFC_4122 || $variant === Uuid::RESERVED_MICROSOFT;
179 }
180 }