PluginProbe
WCPOS – Point of Sale (POS) plugin for WooCommerce / 1.10.21
WCPOS – Point of Sale (POS) plugin for WooCommerce v1.10.21
1.10.25 1.10.24 1.10.23 1.10.22 1.10.21 1.10.20 1.10.19 1.10.18 1.10.17 1.10.16 1.10.15 1.10.13 1.10.14 1.10.12 1.10.11 1.10.10 1.10.9 1.10.8 untagged-3d9b7ccddc54df87c672 1.10.7 1.10.6 1.10.5 1.10.3 1.10.4 1.10.2 All 169 releases
woocommerce-pos / vendor / ramsey / uuid / src / Guid / Fields.php

Fields.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.21, at vendor/ramsey/uuid/src/Guid/Fields.php

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