| 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 |
declare (strict_types=1); |
| 13 |
namespace Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Rfc4122; |
| 14 |
|
| 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\Type\Hexadecimal; |
| 18 |
use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Uuid; |
| 19 |
use function bin2hex; |
| 20 |
use function dechex; |
| 21 |
use function hexdec; |
| 22 |
use function sprintf; |
| 23 |
use function str_pad; |
| 24 |
use function strlen; |
| 25 |
use function substr; |
| 26 |
use function unpack; |
| 27 |
use const STR_PAD_LEFT; |
| 28 |
/** |
| 29 |
* RFC 9562 (formerly RFC 4122) variant UUIDs consist of a set of named fields |
| 30 |
* |
| 31 |
* Internally, this class represents the fields together as a 16-byte binary string. |
| 32 |
* |
| 33 |
* @immutable |
| 34 |
*/ |
| 35 |
final class Fields implements FieldsInterface |
| 36 |
{ |
| 37 |
use MaxTrait; |
| 38 |
use NilTrait; |
| 39 |
use SerializableFieldsTrait; |
| 40 |
use VariantTrait; |
| 41 |
use VersionTrait; |
| 42 |
/** |
| 43 |
* @param string $bytes A 16-byte binary string representation of a UUID |
| 44 |
* |
| 45 |
* @throws InvalidArgumentException if the byte string is not exactly 16 bytes |
| 46 |
* @throws InvalidArgumentException if the byte string does not represent an RFC 9562 (formerly RFC 4122) UUID |
| 47 |
* @throws InvalidArgumentException if the byte string does not contain a valid version |
| 48 |
*/ |
| 49 |
public function __construct(private string $bytes) |
| 50 |
{ |
| 51 |
if (strlen($this->bytes) !== 16) { |
| 52 |
throw new InvalidArgumentException('The byte string must be 16 bytes long; ' . 'received ' . strlen($this->bytes) . ' bytes'); |
| 53 |
} |
| 54 |
if (!$this->isCorrectVariant()) { |
| 55 |
throw new InvalidArgumentException('The byte string received does not conform to the RFC 9562 (formerly RFC 4122) variant'); |
| 56 |
} |
| 57 |
if (!$this->isCorrectVersion()) { |
| 58 |
throw new InvalidArgumentException('The byte string received does not contain a valid RFC 9562 (formerly RFC 4122) version'); |
| 59 |
} |
| 60 |
} |
| 61 |
/** |
| 62 |
* @pure |
| 63 |
*/ |
| 64 |
public function getBytes() : string |
| 65 |
{ |
| 66 |
return $this->bytes; |
| 67 |
} |
| 68 |
public function getClockSeq() : Hexadecimal |
| 69 |
{ |
| 70 |
if ($this->isMax()) { |
| 71 |
$clockSeq = 0xffff; |
| 72 |
} elseif ($this->isNil()) { |
| 73 |
$clockSeq = 0x0; |
| 74 |
} else { |
| 75 |
$clockSeq = hexdec(bin2hex(substr($this->bytes, 8, 2))) & 0x3fff; |
| 76 |
} |
| 77 |
return new Hexadecimal(str_pad(dechex($clockSeq), 4, '0', STR_PAD_LEFT)); |
| 78 |
} |
| 79 |
public function getClockSeqHiAndReserved() : Hexadecimal |
| 80 |
{ |
| 81 |
return new Hexadecimal(bin2hex(substr($this->bytes, 8, 1))); |
| 82 |
} |
| 83 |
public function getClockSeqLow() : Hexadecimal |
| 84 |
{ |
| 85 |
return new Hexadecimal(bin2hex(substr($this->bytes, 9, 1))); |
| 86 |
} |
| 87 |
public function getNode() : Hexadecimal |
| 88 |
{ |
| 89 |
return new Hexadecimal(bin2hex(substr($this->bytes, 10))); |
| 90 |
} |
| 91 |
public function getTimeHiAndVersion() : Hexadecimal |
| 92 |
{ |
| 93 |
return new Hexadecimal(bin2hex(substr($this->bytes, 6, 2))); |
| 94 |
} |
| 95 |
public function getTimeLow() : Hexadecimal |
| 96 |
{ |
| 97 |
return new Hexadecimal(bin2hex(substr($this->bytes, 0, 4))); |
| 98 |
} |
| 99 |
public function getTimeMid() : Hexadecimal |
| 100 |
{ |
| 101 |
return new Hexadecimal(bin2hex(substr($this->bytes, 4, 2))); |
| 102 |
} |
| 103 |
/** |
| 104 |
* Returns the full 60-bit timestamp, without the version |
| 105 |
* |
| 106 |
* For version 2 UUIDs, the time_low field is the local identifier and should not be returned as part of the time. |
| 107 |
* For this reason, we set the bottom 32 bits of the timestamp to 0's. As a result, there is some loss of timestamp |
| 108 |
* fidelity, for version 2 UUIDs. The timestamp can be off by a range of 0 to 429.4967295 seconds (or 7 minutes, 9 |
| 109 |
* seconds, and 496,730 microseconds). |
| 110 |
* |
| 111 |
* For version 6 UUIDs, the timestamp order is reversed from the typical RFC 9562 (formerly RFC 4122) order (the |
| 112 |
* time bits are in the correct bit order, so that it is monotonically increasing). In returning the timestamp |
| 113 |
* value, we put the bits in the order: time_low + time_mid + time_hi. |
| 114 |
*/ |
| 115 |
public function getTimestamp() : Hexadecimal |
| 116 |
{ |
| 117 |
return new Hexadecimal(match ($this->getVersion()) { |
| 118 |
Uuid::UUID_TYPE_DCE_SECURITY => sprintf('%03x%04s%08s', hexdec($this->getTimeHiAndVersion()->toString()) & 0xfff, $this->getTimeMid()->toString(), ''), |
| 119 |
Uuid::UUID_TYPE_REORDERED_TIME => sprintf('%08s%04s%03x', $this->getTimeLow()->toString(), $this->getTimeMid()->toString(), hexdec($this->getTimeHiAndVersion()->toString()) & 0xfff), |
| 120 |
// The Unix timestamp in version 7 UUIDs is a 48-bit number, but for consistency, we will return a 60-bit |
| 121 |
// number, padded to the left with zeros. |
| 122 |
Uuid::UUID_TYPE_UNIX_TIME => sprintf('%011s%04s', $this->getTimeLow()->toString(), $this->getTimeMid()->toString()), |
| 123 |
default => sprintf('%03x%04s%08s', hexdec($this->getTimeHiAndVersion()->toString()) & 0xfff, $this->getTimeMid()->toString(), $this->getTimeLow()->toString()), |
| 124 |
}); |
| 125 |
} |
| 126 |
public function getVersion() : ?int |
| 127 |
{ |
| 128 |
if ($this->isNil() || $this->isMax()) { |
| 129 |
return null; |
| 130 |
} |
| 131 |
/** @var int[] $parts */ |
| 132 |
$parts = unpack('n*', $this->bytes); |
| 133 |
return $parts[4] >> 12; |
| 134 |
} |
| 135 |
private function isCorrectVariant() : bool |
| 136 |
{ |
| 137 |
if ($this->isNil() || $this->isMax()) { |
| 138 |
return \true; |
| 139 |
} |
| 140 |
return $this->getVariant() === Uuid::RFC_4122; |
| 141 |
} |
| 142 |
} |
| 143 |
|