PluginProbe
Media Cloud Sync / 1.4.2
Media Cloud Sync v1.4.2
1.4.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 All 36 releases
media-cloud-sync / includes / sdk / google / ramsey / uuid / src / Codec / OrderedTimeCodec.php

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

79 lines 3.4 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 <[email protected]>
10 * @license http://opensource.org/licenses/MIT MIT
11 */
12 declare (strict_types=1);
13 namespace Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Codec;
14
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 use function strlen;
21 use function substr;
22 /**
23 * OrderedTimeCodec encodes and decodes a UUID, optimizing the byte order for more efficient storage
24 *
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.
28 *
29 * The string representation of the UUID will remain unchanged. Only the binary representation is reordered.
30 *
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.
33 *
34 * @deprecated Please migrate to {@link https://uuid.ramsey.dev/en/stable/rfc4122/version6.html Version 6, reordered time-based UUIDs}.
35 *
36 * @link https://www.percona.com/blog/2014/12/19/store-uuid-optimized-way/ Storing UUID Values in MySQL
37 *
38 * @immutable
39 */
40 class OrderedTimeCodec extends StringCodec
41 {
42 /**
43 * Returns a binary string representation of a UUID, with the timestamp fields rearranged for optimized storage
44 *
45 * @return non-empty-string
46 */
47 public function encodeBinary(UuidInterface $uuid) : string
48 {
49 if (!$uuid->getFields() instanceof Rfc4122FieldsInterface || $uuid->getFields()->getVersion() !== Uuid::UUID_TYPE_TIME) {
50 throw new InvalidArgumentException('Expected version 1 (time-based) UUID');
51 }
52 /** @phpstan-ignore possiblyImpure.methodCall */
53 $bytes = $uuid->getFields()->getBytes();
54 return $bytes[6] . $bytes[7] . $bytes[4] . $bytes[5] . $bytes[0] . $bytes[1] . $bytes[2] . $bytes[3] . substr($bytes, 8);
55 }
56 /**
57 * Returns a UuidInterface derived from an ordered-time binary string representation
58 *
59 * @throws InvalidArgumentException if $bytes is an invalid length
60 *
61 * @inheritDoc
62 */
63 public function decodeBytes(string $bytes) : UuidInterface
64 {
65 if (strlen($bytes) !== 16) {
66 throw new InvalidArgumentException('$bytes string should contain 16 characters.');
67 }
68 // Rearrange the bytes to their original order.
69 $rearrangedBytes = $bytes[4] . $bytes[5] . $bytes[6] . $bytes[7] . $bytes[2] . $bytes[3] . $bytes[0] . $bytes[1] . substr($bytes, 8);
70 $uuid = parent::decodeBytes($rearrangedBytes);
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');
75 }
76 return $uuid;
77 }
78 }
79