| 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 |
declare (strict_types=1); |
| 13 |
namespace Dudlewebs\WPMCS\Ramsey\Uuid\Codec; |
| 14 |
|
| 15 |
use Dudlewebs\WPMCS\Ramsey\Uuid\Exception\InvalidArgumentException; |
| 16 |
use Dudlewebs\WPMCS\Ramsey\Uuid\Exception\UnsupportedOperationException; |
| 17 |
use Dudlewebs\WPMCS\Ramsey\Uuid\Rfc4122\FieldsInterface as Rfc4122FieldsInterface; |
| 18 |
use Dudlewebs\WPMCS\Ramsey\Uuid\Uuid; |
| 19 |
use Dudlewebs\WPMCS\Ramsey\Uuid\UuidInterface; |
| 20 |
use function strlen; |
| 21 |
use function substr; |
| 22 |
/** |
| 23 |
* OrderedTimeCodec encodes and decodes a UUID, optimizing the byte order for |
| 24 |
* more efficient storage |
| 25 |
* |
| 26 |
* For binary representations of version 1 UUID, this codec may be used to |
| 27 |
* reorganize the time fields, making the UUID closer to sequential when storing |
| 28 |
* the bytes. According to Percona, this optimization can improve database |
| 29 |
* INSERTs and SELECTs using the UUID column as a key. |
| 30 |
* |
| 31 |
* The string representation of the UUID will remain unchanged. Only the binary |
| 32 |
* representation is reordered. |
| 33 |
* |
| 34 |
* **PLEASE NOTE:** Binary representations of UUIDs encoded with this codec must |
| 35 |
* be decoded with this codec. Decoding using another codec can result in |
| 36 |
* malformed UUIDs. |
| 37 |
* |
| 38 |
* @link https://www.percona.com/blog/2014/12/19/store-uuid-optimized-way/ Storing UUID Values in MySQL |
| 39 |
* |
| 40 |
* @psalm-immutable |
| 41 |
*/ |
| 42 |
class OrderedTimeCodec extends StringCodec |
| 43 |
{ |
| 44 |
/** |
| 45 |
* Returns a binary string representation of a UUID, with the timestamp |
| 46 |
* fields rearranged for optimized storage |
| 47 |
* |
| 48 |
* @inheritDoc |
| 49 |
* @psalm-return non-empty-string |
| 50 |
* @psalm-suppress MoreSpecificReturnType we know that the retrieved `string` is never empty |
| 51 |
* @psalm-suppress LessSpecificReturnStatement we know that the retrieved `string` is never empty |
| 52 |
*/ |
| 53 |
public function encodeBinary(UuidInterface $uuid): string |
| 54 |
{ |
| 55 |
if (!$uuid->getFields() instanceof Rfc4122FieldsInterface || $uuid->getFields()->getVersion() !== Uuid::UUID_TYPE_TIME) { |
| 56 |
throw new InvalidArgumentException('Expected RFC 4122 version 1 (time-based) UUID'); |
| 57 |
} |
| 58 |
$bytes = $uuid->getFields()->getBytes(); |
| 59 |
/** @phpstan-ignore-next-line PHPStan complains that this is not a non-empty-string. */ |
| 60 |
return $bytes[6] . $bytes[7] . $bytes[4] . $bytes[5] . $bytes[0] . $bytes[1] . $bytes[2] . $bytes[3] . substr($bytes, 8); |
| 61 |
} |
| 62 |
/** |
| 63 |
* Returns a UuidInterface derived from an ordered-time binary string |
| 64 |
* representation |
| 65 |
* |
| 66 |
* @throws InvalidArgumentException if $bytes is an invalid length |
| 67 |
* |
| 68 |
* @inheritDoc |
| 69 |
*/ |
| 70 |
public function decodeBytes(string $bytes): UuidInterface |
| 71 |
{ |
| 72 |
if (strlen($bytes) !== 16) { |
| 73 |
throw new InvalidArgumentException('$bytes string should contain 16 characters.'); |
| 74 |
} |
| 75 |
// Rearrange the bytes to their original order. |
| 76 |
$rearrangedBytes = $bytes[4] . $bytes[5] . $bytes[6] . $bytes[7] . $bytes[2] . $bytes[3] . $bytes[0] . $bytes[1] . substr($bytes, 8); |
| 77 |
$uuid = parent::decodeBytes($rearrangedBytes); |
| 78 |
if (!$uuid->getFields() instanceof Rfc4122FieldsInterface || $uuid->getFields()->getVersion() !== Uuid::UUID_TYPE_TIME) { |
| 79 |
throw new UnsupportedOperationException('Attempting to decode a non-time-based UUID using ' . 'OrderedTimeCodec'); |
| 80 |
} |
| 81 |
return $uuid; |
| 82 |
} |
| 83 |
} |
| 84 |
|