| 148 |
lars |
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 |
|
|
|
13 |
declare(strict_types=1);
|
|
|
14 |
|
|
|
15 |
namespace Ramsey\Uuid\Codec;
|
|
|
16 |
|
|
|
17 |
use Ramsey\Uuid\Exception\InvalidArgumentException;
|
|
|
18 |
use Ramsey\Uuid\Exception\UnsupportedOperationException;
|
|
|
19 |
use Ramsey\Uuid\Rfc4122\FieldsInterface as Rfc4122FieldsInterface;
|
|
|
20 |
use Ramsey\Uuid\Uuid;
|
|
|
21 |
use Ramsey\Uuid\UuidInterface;
|
|
|
22 |
|
|
|
23 |
use function strlen;
|
|
|
24 |
use 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 |
*/
|
|
|
46 |
class 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 |
/** @phpstan-ignore-next-line PHPStan complains that this is not a non-empty-string. */
|
|
|
71 |
return $bytes[6] . $bytes[7]
|
|
|
72 |
. $bytes[4] . $bytes[5]
|
|
|
73 |
. $bytes[0] . $bytes[1] . $bytes[2] . $bytes[3]
|
|
|
74 |
. substr($bytes, 8);
|
|
|
75 |
}
|
|
|
76 |
|
|
|
77 |
/**
|
|
|
78 |
* Returns a UuidInterface derived from an ordered-time binary string
|
|
|
79 |
* representation
|
|
|
80 |
*
|
|
|
81 |
* @throws InvalidArgumentException if $bytes is an invalid length
|
|
|
82 |
*
|
|
|
83 |
* @inheritDoc
|
|
|
84 |
*/
|
|
|
85 |
public function decodeBytes(string $bytes): UuidInterface
|
|
|
86 |
{
|
|
|
87 |
if (strlen($bytes) !== 16) {
|
|
|
88 |
throw new InvalidArgumentException(
|
|
|
89 |
'$bytes string should contain 16 characters.'
|
|
|
90 |
);
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
// Rearrange the bytes to their original order.
|
|
|
94 |
$rearrangedBytes = $bytes[4] . $bytes[5] . $bytes[6] . $bytes[7]
|
|
|
95 |
. $bytes[2] . $bytes[3]
|
|
|
96 |
. $bytes[0] . $bytes[1]
|
|
|
97 |
. substr($bytes, 8);
|
|
|
98 |
|
|
|
99 |
$uuid = parent::decodeBytes($rearrangedBytes);
|
|
|
100 |
|
|
|
101 |
if (
|
|
|
102 |
!($uuid->getFields() instanceof Rfc4122FieldsInterface)
|
|
|
103 |
|| $uuid->getFields()->getVersion() !== Uuid::UUID_TYPE_TIME
|
|
|
104 |
) {
|
|
|
105 |
throw new UnsupportedOperationException(
|
|
|
106 |
'Attempting to decode a non-time-based UUID using '
|
|
|
107 |
. 'OrderedTimeCodec'
|
|
|
108 |
);
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
return $uuid;
|
|
|
112 |
}
|
|
|
113 |
}
|