PluginProbe ʕ •ᴥ•ʔ
ShareThis Dashboard for Google Analytics / 3.1.4
ShareThis Dashboard for Google Analytics v3.1.4
3.3.2 trunk 1.0.7 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.1 2.1.2 2.1.3 2.1.4 2.1.5 2.2.5 2.3.5 2.3.6 2.3.7 2.3.8 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 3.0.0 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.2.0 3.2.1 3.2.2 3.2.3 3.2.4 3.3.0 3.3.1
googleanalytics / lib / analytics-admin / vendor / ramsey / uuid / src / Codec / TimestampFirstCombCodec.php
googleanalytics / lib / analytics-admin / vendor / ramsey / uuid / src / Codec Last commit date
CodecInterface.php 3 years ago GuidStringCodec.php 3 years ago OrderedTimeCodec.php 3 years ago StringCodec.php 3 years ago TimestampFirstCombCodec.php 3 years ago TimestampLastCombCodec.php 3 years ago
TimestampFirstCombCodec.php
114 lines
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\InvalidUuidStringException;
18 use Ramsey\Uuid\UuidInterface;
19
20 use function bin2hex;
21 use function sprintf;
22 use function substr;
23 use function substr_replace;
24
25 /**
26 * TimestampFirstCombCodec encodes and decodes COMBs, with the timestamp as the
27 * first 48 bits
28 *
29 * In contrast with the TimestampLastCombCodec, the TimestampFirstCombCodec
30 * adds the timestamp to the first 48 bits of the COMB. To generate a
31 * timestamp-first COMB, set the TimestampFirstCombCodec as the codec, along
32 * with the CombGenerator as the random generator.
33 *
34 * ``` php
35 * $factory = new UuidFactory();
36 *
37 * $factory->setCodec(new TimestampFirstCombCodec($factory->getUuidBuilder()));
38 *
39 * $factory->setRandomGenerator(new CombGenerator(
40 * $factory->getRandomGenerator(),
41 * $factory->getNumberConverter()
42 * ));
43 *
44 * $timestampFirstComb = $factory->uuid4();
45 * ```
46 *
47 * @link https://www.informit.com/articles/printerfriendly/25862 The Cost of GUIDs as Primary Keys
48 *
49 * @psalm-immutable
50 */
51 class TimestampFirstCombCodec extends StringCodec
52 {
53 /**
54 * @psalm-return non-empty-string
55 * @psalm-suppress MoreSpecificReturnType we know that the retrieved `string` is never empty
56 * @psalm-suppress LessSpecificReturnStatement we know that the retrieved `string` is never empty
57 */
58 public function encode(UuidInterface $uuid): string
59 {
60 $bytes = $this->swapBytes($uuid->getFields()->getBytes());
61
62 return sprintf(
63 '%08s-%04s-%04s-%04s-%012s',
64 bin2hex(substr($bytes, 0, 4)),
65 bin2hex(substr($bytes, 4, 2)),
66 bin2hex(substr($bytes, 6, 2)),
67 bin2hex(substr($bytes, 8, 2)),
68 bin2hex(substr($bytes, 10))
69 );
70 }
71
72 /**
73 * @psalm-return non-empty-string
74 * @psalm-suppress MoreSpecificReturnType we know that the retrieved `string` is never empty
75 * @psalm-suppress LessSpecificReturnStatement we know that the retrieved `string` is never empty
76 */
77 public function encodeBinary(UuidInterface $uuid): string
78 {
79 /** @phpstan-ignore-next-line PHPStan complains that this is not a non-empty-string. */
80 return $this->swapBytes($uuid->getFields()->getBytes());
81 }
82
83 /**
84 * @throws InvalidUuidStringException
85 *
86 * @inheritDoc
87 */
88 public function decode(string $encodedUuid): UuidInterface
89 {
90 $bytes = $this->getBytes($encodedUuid);
91
92 return $this->getBuilder()->build($this, $this->swapBytes($bytes));
93 }
94
95 public function decodeBytes(string $bytes): UuidInterface
96 {
97 return $this->getBuilder()->build($this, $this->swapBytes($bytes));
98 }
99
100 /**
101 * Swaps bytes according to the timestamp-first COMB rules
102 */
103 private function swapBytes(string $bytes): string
104 {
105 $first48Bits = substr($bytes, 0, 6);
106 $last48Bits = substr($bytes, -6);
107
108 $bytes = substr_replace($bytes, $last48Bits, 0, 6);
109 $bytes = substr_replace($bytes, $first48Bits, -6);
110
111 return $bytes;
112 }
113 }
114