PluginProbe
Media Cloud Sync / 1.4.1
Media Cloud Sync v1.4.1
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 / Generator / DefaultTimeGenerator.php

DefaultTimeGenerator.php in Media Cloud Sync 1.4.1, at includes/sdk/google/ramsey/uuid/src/Generator/DefaultTimeGenerator.php

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