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