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