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\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 |