Debian Codegraph @

vendor/ramsey/uuid/src/Generator/DefaultTimeGenerator.php

raw
1 <?php
2 /**
3 * This file is part of the ramsey/uuid library
4 *
5 * For the full copyright and license information, please view the LICENSE
6 * file that was distributed with this source code.
7 *
8 * @copyright Copyright (c) Ben Ramsey <[email protected]>
9 * @license http://opensource.org/licenses/MIT MIT
10 * @link https://benramsey.com/projects/ramsey-uuid/ Documentation
11 * @link https://packagist.org/packages/ramsey/uuid Packagist
12 * @link https://github.com/ramsey/uuid GitHub
13 */
14
15 namespace Ramsey\Uuid\Generator;
16
17 use Exception;
18 use InvalidArgumentException;
19 use Ramsey\Uuid\BinaryUtils;
20 use Ramsey\Uuid\Converter\TimeConverterInterface;
21 use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
22 use Ramsey\Uuid\Provider\NodeProviderInterface;
23 use Ramsey\Uuid\Provider\TimeProviderInterface;
24
25 /**
26 * DefaultTimeGenerator provides functionality to generate strings of binary
27 * data for version 1 UUIDs based on a host ID, sequence number, and the current
28 * time
29 */
30 class DefaultTimeGenerator implements TimeGeneratorInterface
31 {
32 /**
33 * @var NodeProviderInterface
34 */
35 private $nodeProvider;
36
37 /**
38 * @var TimeConverterInterface
39 */
40 private $timeConverter;
41
42 /**
43 * @var TimeProviderInterface
44 */
45 private $timeProvider;
46
47 /**
48 * Constructs a `DefaultTimeGenerator` using a node provider, time converter,
49 * and time provider
50 *
51 * @param NodeProviderInterface $nodeProvider
52 * @param TimeConverterInterface $timeConverter
53 * @param TimeProviderInterface $timeProvider
54 */
55 public function __construct(
56 NodeProviderInterface $nodeProvider,
57 TimeConverterInterface $timeConverter,
58 TimeProviderInterface $timeProvider
59 ) {
60 $this->nodeProvider = $nodeProvider;
61 $this->timeConverter = $timeConverter;
62 $this->timeProvider = $timeProvider;
63 }
64
65 /**
66 * Generate a version 1 UUID from a host ID, sequence number, and the current time
67 *
68 * If $node is not given, we will attempt to obtain the local hardware
69 * address. If $clockSeq is given, it is used as the sequence number;
70 * otherwise a random 14-bit sequence number is chosen.
71 *
72 * @param int|string $node A 48-bit number representing the hardware address
73 * This number may be represented as an integer or a hexadecimal string.
74 * @param int $clockSeq A 14-bit number used to help avoid duplicates that
75 * could arise when the clock is set backwards in time or if the node ID
76 * changes.
77 * @return string A binary string
78 * @throws UnsatisfiedDependencyException if called on a 32-bit system and
79 * `Moontoast\Math\BigNumber` is not present
80 * @throws InvalidArgumentException
81 * @throws Exception if it was not possible to gather sufficient entropy
82 */
83 public function generate($node = null, $clockSeq = null)
84 {
85 $node = $this->getValidNode($node);
86
87 if ($clockSeq === null) {
88 // Not using "stable storage"; see RFC 4122, Section 4.2.1.1
89 $clockSeq = random_int(0, 0x3fff);
90 }
91
92 // Create a 60-bit time value as a count of 100-nanosecond intervals
93 // since 00:00:00.00, 15 October 1582
94 $timeOfDay = $this->timeProvider->currentTime();
95 $uuidTime = $this->timeConverter->calculateTime($timeOfDay['sec'], $timeOfDay['usec']);
96
97 $timeHi = BinaryUtils::applyVersion($uuidTime['hi'], 1);
98 $clockSeqHi = BinaryUtils::applyVariant($clockSeq >> 8);
99
100 $hex = vsprintf(
101 '%08s%04s%04s%02s%02s%012s',
102 [
103 $uuidTime['low'],
104 $uuidTime['mid'],
105 sprintf('%04x', $timeHi),
106 sprintf('%02x', $clockSeqHi),
107 sprintf('%02x', $clockSeq & 0xff),
108 $node,
109 ]
110 );
111
112 return hex2bin($hex);
113 }
114
115 /**
116 * Uses the node provider given when constructing this instance to get
117 * the node ID (usually a MAC address)
118 *
119 * @param string|int $node A node value that may be used to override the node provider
120 * @return string Hexadecimal representation of the node ID
121 * @throws InvalidArgumentException
122 * @throws Exception
123 */
124 protected function getValidNode($node)
125 {
126 if ($node === null) {
127 $node = $this->nodeProvider->getNode();
128 }
129
130 // Convert the node to hex, if it is still an integer
131 if (is_int($node)) {
132 $node = sprintf('%012x', $node);
133 }
134
135 if (!ctype_xdigit($node) || strlen($node) > 12) {
136 throw new InvalidArgumentException('Invalid node value');
137 }
138
139 return strtolower(sprintf('%012s', $node));
140 }
141 }