PluginProbe
Media Cloud Sync / 1.2.2
Media Cloud Sync v1.2.2
1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 1.2.9 All 35 releases
media-cloud-sync / includes / sdk / google / ramsey / uuid / src / Codec / OrderedTimeCodec.php

OrderedTimeCodec.php in Media Cloud Sync 1.2.2, at includes/sdk/google/ramsey/uuid/src/Codec/OrderedTimeCodec.php

84 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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