Fields.php
3 years ago
FieldsInterface.php
3 years ago
NilTrait.php
3 years ago
NilUuid.php
3 years ago
UuidBuilder.php
3 years ago
UuidInterface.php
3 years ago
UuidV1.php
3 years ago
UuidV2.php
3 years ago
UuidV3.php
3 years ago
UuidV4.php
3 years ago
UuidV5.php
3 years ago
Validator.php
3 years ago
VariantTrait.php
3 years ago
VersionTrait.php
3 years ago
Fields.php
195 lines
| 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\Rfc4122; |
| 16 | |
| 17 | use Ramsey\Uuid\Exception\InvalidArgumentException; |
| 18 | use Ramsey\Uuid\Fields\SerializableFieldsTrait; |
| 19 | use Ramsey\Uuid\Type\Hexadecimal; |
| 20 | use Ramsey\Uuid\Uuid; |
| 21 | |
| 22 | use function bin2hex; |
| 23 | use function dechex; |
| 24 | use function hexdec; |
| 25 | use function sprintf; |
| 26 | use function str_pad; |
| 27 | use function strlen; |
| 28 | use function substr; |
| 29 | use function unpack; |
| 30 | |
| 31 | use const STR_PAD_LEFT; |
| 32 | |
| 33 | /** |
| 34 | * RFC 4122 variant UUIDs are comprised of a set of named fields |
| 35 | * |
| 36 | * Internally, this class represents the fields together as a 16-byte binary |
| 37 | * string. |
| 38 | * |
| 39 | * @psalm-immutable |
| 40 | */ |
| 41 | final class Fields implements FieldsInterface |
| 42 | { |
| 43 | use NilTrait; |
| 44 | use SerializableFieldsTrait; |
| 45 | use VariantTrait; |
| 46 | use VersionTrait; |
| 47 | |
| 48 | /** |
| 49 | * @var string |
| 50 | */ |
| 51 | private $bytes; |
| 52 | |
| 53 | /** |
| 54 | * @param string $bytes A 16-byte binary string representation of a UUID |
| 55 | * |
| 56 | * @throws InvalidArgumentException if the byte string is not exactly 16 bytes |
| 57 | * @throws InvalidArgumentException if the byte string does not represent an RFC 4122 UUID |
| 58 | * @throws InvalidArgumentException if the byte string does not contain a valid version |
| 59 | */ |
| 60 | public function __construct(string $bytes) |
| 61 | { |
| 62 | if (strlen($bytes) !== 16) { |
| 63 | throw new InvalidArgumentException( |
| 64 | 'The byte string must be 16 bytes long; ' |
| 65 | . 'received ' . strlen($bytes) . ' bytes' |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | $this->bytes = $bytes; |
| 70 | |
| 71 | if (!$this->isCorrectVariant()) { |
| 72 | throw new InvalidArgumentException( |
| 73 | 'The byte string received does not conform to the RFC 4122 variant' |
| 74 | ); |
| 75 | } |
| 76 | |
| 77 | if (!$this->isCorrectVersion()) { |
| 78 | throw new InvalidArgumentException( |
| 79 | 'The byte string received does not contain a valid RFC 4122 version' |
| 80 | ); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | public function getBytes(): string |
| 85 | { |
| 86 | return $this->bytes; |
| 87 | } |
| 88 | |
| 89 | public function getClockSeq(): Hexadecimal |
| 90 | { |
| 91 | $clockSeq = hexdec(bin2hex(substr($this->bytes, 8, 2))) & 0x3fff; |
| 92 | |
| 93 | return new Hexadecimal(str_pad(dechex($clockSeq), 4, '0', STR_PAD_LEFT)); |
| 94 | } |
| 95 | |
| 96 | public function getClockSeqHiAndReserved(): Hexadecimal |
| 97 | { |
| 98 | return new Hexadecimal(bin2hex(substr($this->bytes, 8, 1))); |
| 99 | } |
| 100 | |
| 101 | public function getClockSeqLow(): Hexadecimal |
| 102 | { |
| 103 | return new Hexadecimal(bin2hex(substr($this->bytes, 9, 1))); |
| 104 | } |
| 105 | |
| 106 | public function getNode(): Hexadecimal |
| 107 | { |
| 108 | return new Hexadecimal(bin2hex(substr($this->bytes, 10))); |
| 109 | } |
| 110 | |
| 111 | public function getTimeHiAndVersion(): Hexadecimal |
| 112 | { |
| 113 | return new Hexadecimal(bin2hex(substr($this->bytes, 6, 2))); |
| 114 | } |
| 115 | |
| 116 | public function getTimeLow(): Hexadecimal |
| 117 | { |
| 118 | return new Hexadecimal(bin2hex(substr($this->bytes, 0, 4))); |
| 119 | } |
| 120 | |
| 121 | public function getTimeMid(): Hexadecimal |
| 122 | { |
| 123 | return new Hexadecimal(bin2hex(substr($this->bytes, 4, 2))); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Returns the full 60-bit timestamp, without the version |
| 128 | * |
| 129 | * For version 2 UUIDs, the time_low field is the local identifier and |
| 130 | * should not be returned as part of the time. For this reason, we set the |
| 131 | * bottom 32 bits of the timestamp to 0's. As a result, there is some loss |
| 132 | * of fidelity of the timestamp, for version 2 UUIDs. The timestamp can be |
| 133 | * off by a range of 0 to 429.4967295 seconds (or 7 minutes, 9 seconds, and |
| 134 | * 496730 microseconds). |
| 135 | * |
| 136 | * For version 6 UUIDs, the timestamp order is reversed from the typical RFC |
| 137 | * 4122 order (the time bits are in the correct bit order, so that it is |
| 138 | * monotonically increasing). In returning the timestamp value, we put the |
| 139 | * bits in the order: time_low + time_mid + time_hi. |
| 140 | */ |
| 141 | public function getTimestamp(): Hexadecimal |
| 142 | { |
| 143 | switch ($this->getVersion()) { |
| 144 | case Uuid::UUID_TYPE_DCE_SECURITY: |
| 145 | $timestamp = sprintf( |
| 146 | '%03x%04s%08s', |
| 147 | hexdec($this->getTimeHiAndVersion()->toString()) & 0x0fff, |
| 148 | $this->getTimeMid()->toString(), |
| 149 | '' |
| 150 | ); |
| 151 | |
| 152 | break; |
| 153 | case Uuid::UUID_TYPE_PEABODY: |
| 154 | $timestamp = sprintf( |
| 155 | '%08s%04s%03x', |
| 156 | $this->getTimeLow()->toString(), |
| 157 | $this->getTimeMid()->toString(), |
| 158 | hexdec($this->getTimeHiAndVersion()->toString()) & 0x0fff |
| 159 | ); |
| 160 | |
| 161 | break; |
| 162 | default: |
| 163 | $timestamp = sprintf( |
| 164 | '%03x%04s%08s', |
| 165 | hexdec($this->getTimeHiAndVersion()->toString()) & 0x0fff, |
| 166 | $this->getTimeMid()->toString(), |
| 167 | $this->getTimeLow()->toString() |
| 168 | ); |
| 169 | } |
| 170 | |
| 171 | return new Hexadecimal($timestamp); |
| 172 | } |
| 173 | |
| 174 | public function getVersion(): ?int |
| 175 | { |
| 176 | if ($this->isNil()) { |
| 177 | return null; |
| 178 | } |
| 179 | |
| 180 | /** @var array $parts */ |
| 181 | $parts = unpack('n*', $this->bytes); |
| 182 | |
| 183 | return (int) $parts[4] >> 12; |
| 184 | } |
| 185 | |
| 186 | private function isCorrectVariant(): bool |
| 187 | { |
| 188 | if ($this->isNil()) { |
| 189 | return true; |
| 190 | } |
| 191 | |
| 192 | return $this->getVariant() === Uuid::RFC_4122; |
| 193 | } |
| 194 | } |
| 195 |