PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.3.0
Defender Security – Malware Scanner, Login Security & Firewall v6.3.0
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 / lib / packages / Ramsey / Uuid / Generator / CombGenerator.php

CombGenerator.php in Defender Security – Malware Scanner, Login Security & Firewall 6.3.0, at lib/packages/Ramsey/Uuid/Generator/CombGenerator.php

113 lines 3.5 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 WP_DEFENDER_VENDOR\Ramsey\Uuid\Generator;
16
17 use WP_DEFENDER_VENDOR\Ramsey\Uuid\Converter\NumberConverterInterface;
18 use WP_DEFENDER_VENDOR\Ramsey\Uuid\Exception\InvalidArgumentException;
19
20 use function bin2hex;
21 use function explode;
22 use function hex2bin;
23 use function microtime;
24 use function str_pad;
25 use function substr;
26
27 use const STR_PAD_LEFT;
28
29 /**
30 * CombGenerator generates COMBs (combined UUID/timestamp)
31 *
32 * The CombGenerator, when used with the StringCodec (and, by proxy, the TimestampLastCombCodec) or the
33 * TimestampFirstCombCodec, combines the current timestamp with a UUID (hence the name "COMB"). The timestamp either
34 * appears as the first or last 48 bits of the COMB, depending on the codec used.
35 *
36 * By default, COMBs will have the timestamp set as the last 48 bits of the identifier.
37 *
38 * ```
39 * $factory = new UuidFactory();
40 *
41 * $factory->setRandomGenerator(new CombGenerator(
42 * $factory->getRandomGenerator(),
43 * $factory->getNumberConverter(),
44 * ));
45 *
46 * $comb = $factory->uuid4();
47 * ```
48 *
49 * To generate a COMB with the timestamp as the first 48 bits, set the TimestampFirstCombCodec as the codec.
50 *
51 * ```
52 * $factory->setCodec(new TimestampFirstCombCodec($factory->getUuidBuilder()));
53 * ```
54 *
55 * @deprecated Please migrate to {@link https://uuid.ramsey.dev/en/stable/rfc4122/version7.html Version 7, Unix Epoch Time UUIDs}.
56 *
57 * @link https://web.archive.org/web/20240118030355/https://www.informit.com/articles/printerfriendly/25862 The Cost of GUIDs as Primary Keys
58 */
59 class CombGenerator implements RandomGeneratorInterface
60 {
61 public const TIMESTAMP_BYTES = 6;
62
63 public function __construct(
64 private RandomGeneratorInterface $generator,
65 private NumberConverterInterface $numberConverter
66 ) {
67 }
68
69 /**
70 * @throws InvalidArgumentException if $length is not a positive integer greater than or equal to CombGenerator::TIMESTAMP_BYTES
71 *
72 * @inheritDoc
73 */
74 public function generate(int $length): string
75 {
76 if ($length < self::TIMESTAMP_BYTES) {
77 throw new InvalidArgumentException(
78 'Length must be a positive integer greater than or equal to ' . self::TIMESTAMP_BYTES
79 );
80 }
81
82 if ($length % 2 !== 0) {
83 throw new InvalidArgumentException('Length must be an even number');
84 }
85
86 $hash = '';
87
88 /** @phpstan-ignore greater.alwaysTrue (TIMESTAMP_BYTES constant could change in child classes) */
89 if (self::TIMESTAMP_BYTES > 0 && $length > self::TIMESTAMP_BYTES) {
90 $hash = $this->generator->generate($length - self::TIMESTAMP_BYTES);
91 }
92
93 $lsbTime = str_pad(
94 $this->numberConverter->toHex($this->timestamp()),
95 self::TIMESTAMP_BYTES * 2,
96 '0',
97 STR_PAD_LEFT,
98 );
99
100 return (string) hex2bin(str_pad(bin2hex($hash), $length - self::TIMESTAMP_BYTES, '0') . $lsbTime);
101 }
102
103 /**
104 * Returns the current timestamp as a string integer, precise to 0.00001 seconds
105 */
106 private function timestamp(): string
107 {
108 $time = explode(' ', microtime(false));
109
110 return $time[1] . substr($time[0], 2, 5);
111 }
112 }
113