Debian Codegraph @

vendor/ramsey/uuid/src/Nonstandard/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\Nonstandard;
16
17 use Ramsey\Uuid\Exception\InvalidArgumentException;
18 use Ramsey\Uuid\Fields\SerializableFieldsTrait;
19 use Ramsey\Uuid\Rfc4122\FieldsInterface;
20 use Ramsey\Uuid\Rfc4122\VariantTrait;
21 use Ramsey\Uuid\Type\Hexadecimal;
22
23 use function bin2hex;
24 use function dechex;
25 use function hexdec;
26 use function sprintf;
27 use function str_pad;
28 use function strlen;
29 use function substr;
30
31 use const STR_PAD_LEFT;
32
33 /**
34 * Nonstandard UUID fields do not conform to the RFC 9562 (formerly RFC 4122) standard
35 *
36 * Since some systems may create nonstandard UUIDs, this implements the {@see FieldsInterface}, so that functionality of
37 * a nonstandard UUID is not degraded, in the event these UUIDs are expected to contain RFC 9562 (formerly RFC 4122) fields.
38 *
39 * Internally, this class represents the fields together as a 16-byte binary string.
40 *
41 * @immutable
42 */
43 final class Fields implements FieldsInterface
44 {
45 use SerializableFieldsTrait;
46 use VariantTrait;
47
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 */
53 public function __construct(private string $bytes)
54 {
55 if (strlen($this->bytes) !== 16) {
56 throw new InvalidArgumentException(
57 'The byte string must be 16 bytes long; received ' . strlen($this->bytes) . ' bytes',
58 );
59 }
60 }
61
62 public function getBytes(): string
63 {
64 return $this->bytes;
65 }
66
67 public function getClockSeq(): Hexadecimal
68 {
69 $clockSeq = hexdec(bin2hex(substr($this->bytes, 8, 2))) & 0x3fff;
70
71 return new Hexadecimal(str_pad(dechex($clockSeq), 4, '0', STR_PAD_LEFT));
72 }
73
74 public function getClockSeqHiAndReserved(): Hexadecimal
75 {
76 return new Hexadecimal(bin2hex(substr($this->bytes, 8, 1)));
77 }
78
79 public function getClockSeqLow(): Hexadecimal
80 {
81 return new Hexadecimal(bin2hex(substr($this->bytes, 9, 1)));
82 }
83
84 public function getNode(): Hexadecimal
85 {
86 return new Hexadecimal(bin2hex(substr($this->bytes, 10)));
87 }
88
89 public function getTimeHiAndVersion(): Hexadecimal
90 {
91 return new Hexadecimal(bin2hex(substr($this->bytes, 6, 2)));
92 }
93
94 public function getTimeLow(): Hexadecimal
95 {
96 return new Hexadecimal(bin2hex(substr($this->bytes, 0, 4)));
97 }
98
99 public function getTimeMid(): Hexadecimal
100 {
101 return new Hexadecimal(bin2hex(substr($this->bytes, 4, 2)));
102 }
103
104 public function getTimestamp(): Hexadecimal
105 {
106 return new Hexadecimal(sprintf(
107 '%03x%04s%08s',
108 hexdec($this->getTimeHiAndVersion()->toString()) & 0x0fff,
109 $this->getTimeMid()->toString(),
110 $this->getTimeLow()->toString()
111 ));
112 }
113
114 public function getVersion(): ?int
115 {
116 return null;
117 }
118
119 public function isNil(): bool
120 {
121 return false;
122 }
123
124 public function isMax(): bool
125 {
126 return false;
127 }
128 }