googleanalytics
/
lib
/
analytics-admin
/
vendor
/
ramsey
/
uuid
/
src
/
Nonstandard
Last commit date
Fields.php
3 years ago
Uuid.php
3 years ago
UuidBuilder.php
3 years ago
UuidV6.php
3 years ago
Fields.php
134 lines
| 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 <ben@benramsey.com> |
| 10 | * @license http://opensource.org/licenses/MIT MIT |
| 11 | */ |
| 12 | |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace Ramsey\Uuid\Nonstandard; |
| 16 | |
| 17 | use Ramsey\Uuid\Exception\InvalidArgumentException; |
| 18 | use Ramsey\Uuid\Fields\SerializableFieldsTrait; |
| 19 | use Ramsey\Uuid\Rfc4122\FieldsInterface; |
| 20 | use Ramsey\Uuid\Rfc4122\VariantTrait; |
| 21 | use Ramsey\Uuid\Type\Hexadecimal; |
| 22 | |
| 23 | use function bin2hex; |
| 24 | use function dechex; |
| 25 | use function hexdec; |
| 26 | use function sprintf; |
| 27 | use function str_pad; |
| 28 | use function strlen; |
| 29 | use function substr; |
| 30 | |
| 31 | use const STR_PAD_LEFT; |
| 32 | |
| 33 | /** |
| 34 | * Nonstandard UUID fields do not conform to the RFC 4122 standard |
| 35 | * |
| 36 | * Since some systems may create nonstandard UUIDs, this implements the |
| 37 | * Rfc4122\FieldsInterface, so that functionality of a nonstandard UUID is not |
| 38 | * degraded, in the event these UUIDs are expected to contain RFC 4122 fields. |
| 39 | * |
| 40 | * Internally, this class represents the fields together as a 16-byte binary |
| 41 | * string. |
| 42 | * |
| 43 | * @psalm-immutable |
| 44 | */ |
| 45 | final class Fields implements FieldsInterface |
| 46 | { |
| 47 | use SerializableFieldsTrait; |
| 48 | use VariantTrait; |
| 49 | |
| 50 | /** |
| 51 | * @var string |
| 52 | */ |
| 53 | private $bytes; |
| 54 | |
| 55 | /** |
| 56 | * @param string $bytes A 16-byte binary string representation of a UUID |
| 57 | * |
| 58 | * @throws InvalidArgumentException if the byte string is not exactly 16 bytes |
| 59 | */ |
| 60 | public function __construct(string $bytes) |
| 61 | { |
| 62 | if (strlen($bytes) !== 16) { |
| 63 | throw new InvalidArgumentException( |
| 64 | 'The byte string must be 16 bytes long; ' |
| 65 | . 'received ' . strlen($bytes) . ' bytes' |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | $this->bytes = $bytes; |
| 70 | } |
| 71 | |
| 72 | public function getBytes(): string |
| 73 | { |
| 74 | return $this->bytes; |
| 75 | } |
| 76 | |
| 77 | public function getClockSeq(): Hexadecimal |
| 78 | { |
| 79 | $clockSeq = hexdec(bin2hex(substr($this->bytes, 8, 2))) & 0x3fff; |
| 80 | |
| 81 | return new Hexadecimal(str_pad(dechex($clockSeq), 4, '0', STR_PAD_LEFT)); |
| 82 | } |
| 83 | |
| 84 | public function getClockSeqHiAndReserved(): Hexadecimal |
| 85 | { |
| 86 | return new Hexadecimal(bin2hex(substr($this->bytes, 8, 1))); |
| 87 | } |
| 88 | |
| 89 | public function getClockSeqLow(): Hexadecimal |
| 90 | { |
| 91 | return new Hexadecimal(bin2hex(substr($this->bytes, 9, 1))); |
| 92 | } |
| 93 | |
| 94 | public function getNode(): Hexadecimal |
| 95 | { |
| 96 | return new Hexadecimal(bin2hex(substr($this->bytes, 10))); |
| 97 | } |
| 98 | |
| 99 | public function getTimeHiAndVersion(): Hexadecimal |
| 100 | { |
| 101 | return new Hexadecimal(bin2hex(substr($this->bytes, 6, 2))); |
| 102 | } |
| 103 | |
| 104 | public function getTimeLow(): Hexadecimal |
| 105 | { |
| 106 | return new Hexadecimal(bin2hex(substr($this->bytes, 0, 4))); |
| 107 | } |
| 108 | |
| 109 | public function getTimeMid(): Hexadecimal |
| 110 | { |
| 111 | return new Hexadecimal(bin2hex(substr($this->bytes, 4, 2))); |
| 112 | } |
| 113 | |
| 114 | public function getTimestamp(): Hexadecimal |
| 115 | { |
| 116 | return new Hexadecimal(sprintf( |
| 117 | '%03x%04s%08s', |
| 118 | hexdec($this->getTimeHiAndVersion()->toString()) & 0x0fff, |
| 119 | $this->getTimeMid()->toString(), |
| 120 | $this->getTimeLow()->toString() |
| 121 | )); |
| 122 | } |
| 123 | |
| 124 | public function getVersion(): ?int |
| 125 | { |
| 126 | return null; |
| 127 | } |
| 128 | |
| 129 | public function isNil(): bool |
| 130 | { |
| 131 | return false; |
| 132 | } |
| 133 | } |
| 134 |