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