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