PluginProbe
Media Cloud Sync / 1.4.2
Media Cloud Sync v1.4.2
1.4.2 1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 All 36 releases
media-cloud-sync / includes / sdk / google / ramsey / uuid / src / Guid / Fields.php

Fields.php in Media Cloud Sync 1.4.2, at includes/sdk/google/ramsey/uuid/src/Guid/Fields.php

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