PluginProbe
Media Cloud Sync / 1.2.10
Media Cloud Sync v1.2.10
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.2.10, 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\Ramsey\Uuid\Codec;
14
15 use Dudlewebs\WPMCS\Ramsey\Uuid\Exception\InvalidUuidStringException;
16 use Dudlewebs\WPMCS\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
23 * first 48 bits
24 *
25 * In contrast with the TimestampLastCombCodec, the TimestampFirstCombCodec
26 * adds the timestamp to the first 48 bits of the COMB. To generate a
27 * timestamp-first COMB, set the TimestampFirstCombCodec as the codec, along
28 * with the CombGenerator as the random generator.
29 *
30 * ``` php
31 * $factory = new UuidFactory();
32 *
33 * $factory->setCodec(new TimestampFirstCombCodec($factory->getUuidBuilder()));
34 *
35 * $factory->setRandomGenerator(new CombGenerator(
36 * $factory->getRandomGenerator(),
37 * $factory->getNumberConverter()
38 * ));
39 *
40 * $timestampFirstComb = $factory->uuid4();
41 * ```
42 *
43 * @link https://www.informit.com/articles/printerfriendly/25862 The Cost of GUIDs as Primary Keys
44 *
45 * @psalm-immutable
46 */
47 class TimestampFirstCombCodec extends StringCodec
48 {
49 /**
50 * @psalm-return non-empty-string
51 * @psalm-suppress MoreSpecificReturnType we know that the retrieved `string` is never empty
52 * @psalm-suppress LessSpecificReturnStatement we know that the retrieved `string` is never empty
53 */
54 public function encode(UuidInterface $uuid): string
55 {
56 $bytes = $this->swapBytes($uuid->getFields()->getBytes());
57 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)));
58 }
59 /**
60 * @psalm-return non-empty-string
61 * @psalm-suppress MoreSpecificReturnType we know that the retrieved `string` is never empty
62 * @psalm-suppress LessSpecificReturnStatement we know that the retrieved `string` is never empty
63 */
64 public function encodeBinary(UuidInterface $uuid): string
65 {
66 /** @phpstan-ignore-next-line PHPStan complains that this is not a non-empty-string. */
67 return $this->swapBytes($uuid->getFields()->getBytes());
68 }
69 /**
70 * @throws InvalidUuidStringException
71 *
72 * @inheritDoc
73 */
74 public function decode(string $encodedUuid): UuidInterface
75 {
76 $bytes = $this->getBytes($encodedUuid);
77 return $this->getBuilder()->build($this, $this->swapBytes($bytes));
78 }
79 public function decodeBytes(string $bytes): UuidInterface
80 {
81 return $this->getBuilder()->build($this, $this->swapBytes($bytes));
82 }
83 /**
84 * Swaps bytes according to the timestamp-first COMB rules
85 */
86 private function swapBytes(string $bytes): string
87 {
88 $first48Bits = substr($bytes, 0, 6);
89 $last48Bits = substr($bytes, -6);
90 $bytes = substr_replace($bytes, $last48Bits, 0, 6);
91 $bytes = substr_replace($bytes, $first48Bits, -6);
92 return $bytes;
93 }
94 }
95