PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.7.1
Defender Security – Malware Scanner, Login Security & Firewall v5.7.1
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 / Generator / DefaultTimeGenerator.php

DefaultTimeGenerator.php in Defender Security – Malware Scanner, Login Security & Firewall 5.7.1, at vendor/ramsey/uuid/src/Generator/DefaultTimeGenerator.php

119 lines 3.6 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\Generator;
16
17 use Ramsey\Uuid\Converter\TimeConverterInterface;
18 use Ramsey\Uuid\Exception\InvalidArgumentException;
19 use Ramsey\Uuid\Exception\RandomSourceException;
20 use Ramsey\Uuid\Exception\TimeSourceException;
21 use Ramsey\Uuid\Provider\NodeProviderInterface;
22 use Ramsey\Uuid\Provider\TimeProviderInterface;
23 use Ramsey\Uuid\Type\Hexadecimal;
24 use Throwable;
25
26 use function dechex;
27 use function hex2bin;
28 use function is_int;
29 use function pack;
30 use function preg_match;
31 use function sprintf;
32 use function str_pad;
33 use function strlen;
34
35 use const STR_PAD_LEFT;
36
37 /**
38 * DefaultTimeGenerator generates strings of binary data based on a node ID, clock sequence, and the current time
39 */
40 class DefaultTimeGenerator implements TimeGeneratorInterface
41 {
42 public function __construct(
43 private NodeProviderInterface $nodeProvider,
44 private TimeConverterInterface $timeConverter,
45 private TimeProviderInterface $timeProvider,
46 ) {
47 }
48
49 /**
50 * @throws InvalidArgumentException if the parameters contain invalid values
51 * @throws RandomSourceException if random_int() throws an exception/error
52 *
53 * @inheritDoc
54 */
55 public function generate($node = null, ?int $clockSeq = null): string
56 {
57 if ($node instanceof Hexadecimal) {
58 $node = $node->toString();
59 }
60
61 $node = $this->getValidNode($node);
62
63 if ($clockSeq === null) {
64 try {
65 // This does not use "stable storage"; see RFC 9562, section 6.3.
66 $clockSeq = random_int(0, 0x3fff);
67 } catch (Throwable $exception) {
68 throw new RandomSourceException($exception->getMessage(), (int) $exception->getCode(), $exception);
69 }
70 }
71
72 $time = $this->timeProvider->getTime();
73
74 $uuidTime = $this->timeConverter->calculateTime(
75 $time->getSeconds()->toString(),
76 $time->getMicroseconds()->toString()
77 );
78
79 $timeHex = str_pad($uuidTime->toString(), 16, '0', STR_PAD_LEFT);
80
81 if (strlen($timeHex) !== 16) {
82 throw new TimeSourceException(sprintf('The generated time of \'%s\' is larger than expected', $timeHex));
83 }
84
85 $timeBytes = (string) hex2bin($timeHex);
86
87 return $timeBytes[4] . $timeBytes[5] . $timeBytes[6] . $timeBytes[7]
88 . $timeBytes[2] . $timeBytes[3] . $timeBytes[0] . $timeBytes[1]
89 . pack('n*', $clockSeq) . $node;
90 }
91
92 /**
93 * Uses the node provider given when constructing this instance to get the node ID (usually a MAC address)
94 *
95 * @param int | string | null $node A node value that may be used to override the node provider
96 *
97 * @return string 6-byte binary string representation of the node
98 *
99 * @throws InvalidArgumentException
100 */
101 private function getValidNode(int | string | null $node): string
102 {
103 if ($node === null) {
104 $node = $this->nodeProvider->getNode();
105 }
106
107 // Convert the node to hex if it is still an integer.
108 if (is_int($node)) {
109 $node = dechex($node);
110 }
111
112 if (!preg_match('/^[A-Fa-f0-9]+$/', (string) $node) || strlen((string) $node) > 12) {
113 throw new InvalidArgumentException('Invalid node value');
114 }
115
116 return (string) hex2bin(str_pad((string) $node, 12, '0', STR_PAD_LEFT));
117 }
118 }
119