PluginProbe
Media Cloud Sync / 1.4.1
Media Cloud Sync v1.4.1
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 / TimestampFirstCombCodec.php

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

95 lines 3.3 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\GCP\Ramsey\Uuid\Codec;
14
15 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\Exception\InvalidUuidStringException;
16 use Dudlewebs\WPMCS\GCP\Ramsey\Uuid\UuidInterface;
17 use function bin2hex;
18 use function sprintf;
19 use function substr;
20 use function substr_replace;
21 /**
22 * TimestampFirstCombCodec encodes and decodes COMBs, with the timestamp as the first 48 bits
23 *
24 * In contrast with the TimestampLastCombCodec, the TimestampFirstCombCodec adds the timestamp to the first 48 bits of
25 * the COMB. To generate a timestamp-first COMB, set the TimestampFirstCombCodec as the codec, along with the
26 * CombGenerator as the random generator.
27 *
28 * ```
29 * $factory = new UuidFactory();
30 *
31 * $factory->setCodec(new TimestampFirstCombCodec($factory->getUuidBuilder()));
32 *
33 * $factory->setRandomGenerator(new CombGenerator(
34 * $factory->getRandomGenerator(),
35 * $factory->getNumberConverter(),
36 * ));
37 *
38 * $timestampFirstComb = $factory->uuid4();
39 * ```
40 *
41 * @deprecated Please migrate to {@link https://uuid.ramsey.dev/en/stable/rfc4122/version7.html Version 7, Unix Epoch Time UUIDs}.
42 *
43 * @link https://web.archive.org/web/20240118030355/https://www.informit.com/articles/printerfriendly/25862 The Cost of GUIDs as Primary Keys
44 *
45 * @immutable
46 */
47 class TimestampFirstCombCodec extends StringCodec
48 {
49 /**
50 * @return non-empty-string
51 */
52 public function encode(UuidInterface $uuid) : string
53 {
54 /** @phpstan-ignore possiblyImpure.methodCall */
55 $bytes = $this->swapBytes($uuid->getFields()->getBytes());
56 return sprintf('%08s-%04s-%04s-%04s-%012s', bin2hex(substr($bytes, 0, 4)), bin2hex(substr($bytes, 4, 2)), bin2hex(substr($bytes, 6, 2)), bin2hex(substr($bytes, 8, 2)), bin2hex(substr($bytes, 10)));
57 }
58 /**
59 * @return non-empty-string
60 */
61 public function encodeBinary(UuidInterface $uuid) : string
62 {
63 /** @phpstan-ignore-next-line PHPStan complains that this is not a non-empty-string. */
64 return $this->swapBytes($uuid->getFields()->getBytes());
65 }
66 /**
67 * @throws InvalidUuidStringException
68 *
69 * @inheritDoc
70 */
71 public function decode(string $encodedUuid) : UuidInterface
72 {
73 /** @phpstan-ignore possiblyImpure.methodCall */
74 $bytes = $this->getBytes($encodedUuid);
75 /** @phpstan-ignore possiblyImpure.methodCall */
76 return $this->getBuilder()->build($this, $this->swapBytes($bytes));
77 }
78 public function decodeBytes(string $bytes) : UuidInterface
79 {
80 /** @phpstan-ignore possiblyImpure.methodCall */
81 return $this->getBuilder()->build($this, $this->swapBytes($bytes));
82 }
83 /**
84 * Swaps bytes according to the timestamp-first COMB rules
85 *
86 * @pure
87 */
88 private function swapBytes(string $bytes) : string
89 {
90 $first48Bits = substr($bytes, 0, 6);
91 $last48Bits = substr($bytes, -6);
92 return substr_replace(substr_replace($bytes, $last48Bits, 0, 6), $first48Bits, -6);
93 }
94 }
95