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