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