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