| 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\Nonstandard; |
| 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\Rfc4122\FieldsInterface; |
| 18 |
use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Rfc4122\VariantTrait; |
| 19 |
use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Type\Hexadecimal; |
| 20 |
use function bin2hex; |
| 21 |
use function dechex; |
| 22 |
use function hexdec; |
| 23 |
use function sprintf; |
| 24 |
use function str_pad; |
| 25 |
use function strlen; |
| 26 |
use function substr; |
| 27 |
use const STR_PAD_LEFT; |
| 28 |
/** |
| 29 |
* Nonstandard UUID fields do not conform to the RFC 9562 (formerly RFC 4122) standard |
| 30 |
* |
| 31 |
* Since some systems may create nonstandard UUIDs, this implements the {@see FieldsInterface}, so that functionality of |
| 32 |
* a nonstandard UUID is not degraded, in the event these UUIDs are expected to contain RFC 9562 (formerly RFC 4122) fields. |
| 33 |
* |
| 34 |
* Internally, this class represents the fields together as a 16-byte binary string. |
| 35 |
* |
| 36 |
* @immutable |
| 37 |
*/ |
| 38 |
final class Fields implements FieldsInterface |
| 39 |
{ |
| 40 |
use SerializableFieldsTrait; |
| 41 |
use VariantTrait; |
| 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 |
*/ |
| 47 |
public function __construct(private string $bytes) |
| 48 |
{ |
| 49 |
if (strlen($this->bytes) !== 16) { |
| 50 |
throw new InvalidArgumentException('The byte string must be 16 bytes long; received ' . strlen($this->bytes) . ' bytes'); |
| 51 |
} |
| 52 |
} |
| 53 |
public function getBytes() : string |
| 54 |
{ |
| 55 |
return $this->bytes; |
| 56 |
} |
| 57 |
public function getClockSeq() : Hexadecimal |
| 58 |
{ |
| 59 |
$clockSeq = hexdec(bin2hex(substr($this->bytes, 8, 2))) & 0x3fff; |
| 60 |
return new Hexadecimal(str_pad(dechex($clockSeq), 4, '0', STR_PAD_LEFT)); |
| 61 |
} |
| 62 |
public function getClockSeqHiAndReserved() : Hexadecimal |
| 63 |
{ |
| 64 |
return new Hexadecimal(bin2hex(substr($this->bytes, 8, 1))); |
| 65 |
} |
| 66 |
public function getClockSeqLow() : Hexadecimal |
| 67 |
{ |
| 68 |
return new Hexadecimal(bin2hex(substr($this->bytes, 9, 1))); |
| 69 |
} |
| 70 |
public function getNode() : Hexadecimal |
| 71 |
{ |
| 72 |
return new Hexadecimal(bin2hex(substr($this->bytes, 10))); |
| 73 |
} |
| 74 |
public function getTimeHiAndVersion() : Hexadecimal |
| 75 |
{ |
| 76 |
return new Hexadecimal(bin2hex(substr($this->bytes, 6, 2))); |
| 77 |
} |
| 78 |
public function getTimeLow() : Hexadecimal |
| 79 |
{ |
| 80 |
return new Hexadecimal(bin2hex(substr($this->bytes, 0, 4))); |
| 81 |
} |
| 82 |
public function getTimeMid() : Hexadecimal |
| 83 |
{ |
| 84 |
return new Hexadecimal(bin2hex(substr($this->bytes, 4, 2))); |
| 85 |
} |
| 86 |
public function getTimestamp() : Hexadecimal |
| 87 |
{ |
| 88 |
return new Hexadecimal(sprintf('%03x%04s%08s', hexdec($this->getTimeHiAndVersion()->toString()) & 0xfff, $this->getTimeMid()->toString(), $this->getTimeLow()->toString())); |
| 89 |
} |
| 90 |
public function getVersion() : ?int |
| 91 |
{ |
| 92 |
return null; |
| 93 |
} |
| 94 |
public function isNil() : bool |
| 95 |
{ |
| 96 |
return \false; |
| 97 |
} |
| 98 |
public function isMax() : bool |
| 99 |
{ |
| 100 |
return \false; |
| 101 |
} |
| 102 |
} |
| 103 |
|