PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.7.2
Defender Security – Malware Scanner, Login Security & Firewall v5.7.2
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / vendor / ramsey / uuid / src / Codec / TimestampFirstCombCodec.php

TimestampFirstCombCodec.php in Defender Security – Malware Scanner, Login Security & Firewall 5.7.2, at vendor/ramsey/uuid/src/Codec/TimestampFirstCombCodec.php

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