upgrade to laravel 7 and set branch to v2
This commit is contained in:
+68
-62
@@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* This file is part of the ramsey/uuid library
|
||||
*
|
||||
@@ -7,25 +8,35 @@
|
||||
*
|
||||
* @copyright Copyright (c) Ben Ramsey <ben@benramsey.com>
|
||||
* @license http://opensource.org/licenses/MIT MIT
|
||||
* @link https://benramsey.com/projects/ramsey-uuid/ Documentation
|
||||
* @link https://packagist.org/packages/ramsey/uuid Packagist
|
||||
* @link https://github.com/ramsey/uuid GitHub
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Ramsey\Uuid\Generator;
|
||||
|
||||
use Exception;
|
||||
use InvalidArgumentException;
|
||||
use Ramsey\Uuid\BinaryUtils;
|
||||
use Ramsey\Uuid\Converter\TimeConverterInterface;
|
||||
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
|
||||
use Ramsey\Uuid\Exception\InvalidArgumentException;
|
||||
use Ramsey\Uuid\Exception\RandomSourceException;
|
||||
use Ramsey\Uuid\Exception\TimeSourceException;
|
||||
use Ramsey\Uuid\Provider\NodeProviderInterface;
|
||||
use Ramsey\Uuid\Provider\TimeProviderInterface;
|
||||
use Ramsey\Uuid\Type\Hexadecimal;
|
||||
use Throwable;
|
||||
|
||||
use function ctype_xdigit;
|
||||
use function dechex;
|
||||
use function hex2bin;
|
||||
use function is_int;
|
||||
use function pack;
|
||||
use function sprintf;
|
||||
use function str_pad;
|
||||
use function strlen;
|
||||
|
||||
use const STR_PAD_LEFT;
|
||||
|
||||
/**
|
||||
* DefaultTimeGenerator provides functionality to generate strings of binary
|
||||
* data for version 1 UUIDs based on a host ID, sequence number, and the current
|
||||
* time
|
||||
* DefaultTimeGenerator generates strings of binary data based on a node ID,
|
||||
* clock sequence, and the current time
|
||||
*/
|
||||
class DefaultTimeGenerator implements TimeGeneratorInterface
|
||||
{
|
||||
@@ -44,14 +55,6 @@ class DefaultTimeGenerator implements TimeGeneratorInterface
|
||||
*/
|
||||
private $timeProvider;
|
||||
|
||||
/**
|
||||
* Constructs a `DefaultTimeGenerator` using a node provider, time converter,
|
||||
* and time provider
|
||||
*
|
||||
* @param NodeProviderInterface $nodeProvider
|
||||
* @param TimeConverterInterface $timeConverter
|
||||
* @param TimeProviderInterface $timeProvider
|
||||
*/
|
||||
public function __construct(
|
||||
NodeProviderInterface $nodeProvider,
|
||||
TimeConverterInterface $timeConverter,
|
||||
@@ -63,79 +66,82 @@ class DefaultTimeGenerator implements TimeGeneratorInterface
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a version 1 UUID from a host ID, sequence number, and the current time
|
||||
* @throws InvalidArgumentException if the parameters contain invalid values
|
||||
* @throws RandomSourceException if random_int() throws an exception/error
|
||||
*
|
||||
* If $node is not given, we will attempt to obtain the local hardware
|
||||
* address. If $clockSeq is given, it is used as the sequence number;
|
||||
* otherwise a random 14-bit sequence number is chosen.
|
||||
*
|
||||
* @param int|string $node A 48-bit number representing the hardware address
|
||||
* This number may be represented as an integer or a hexadecimal string.
|
||||
* @param int $clockSeq A 14-bit number used to help avoid duplicates that
|
||||
* could arise when the clock is set backwards in time or if the node ID
|
||||
* changes.
|
||||
* @return string A binary string
|
||||
* @throws UnsatisfiedDependencyException if called on a 32-bit system and
|
||||
* `Moontoast\Math\BigNumber` is not present
|
||||
* @throws InvalidArgumentException
|
||||
* @throws Exception if it was not possible to gather sufficient entropy
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function generate($node = null, $clockSeq = null)
|
||||
public function generate($node = null, ?int $clockSeq = null): string
|
||||
{
|
||||
if ($node instanceof Hexadecimal) {
|
||||
$node = $node->toString();
|
||||
}
|
||||
|
||||
$node = $this->getValidNode($node);
|
||||
|
||||
if ($clockSeq === null) {
|
||||
// Not using "stable storage"; see RFC 4122, Section 4.2.1.1
|
||||
$clockSeq = random_int(0, 0x3fff);
|
||||
try {
|
||||
// This does not use "stable storage"; see RFC 4122, Section 4.2.1.1.
|
||||
$clockSeq = random_int(0, 0x3fff);
|
||||
} catch (Throwable $exception) {
|
||||
throw new RandomSourceException(
|
||||
$exception->getMessage(),
|
||||
(int) $exception->getCode(),
|
||||
$exception
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Create a 60-bit time value as a count of 100-nanosecond intervals
|
||||
// since 00:00:00.00, 15 October 1582
|
||||
$timeOfDay = $this->timeProvider->currentTime();
|
||||
$uuidTime = $this->timeConverter->calculateTime($timeOfDay['sec'], $timeOfDay['usec']);
|
||||
$time = $this->timeProvider->getTime();
|
||||
|
||||
$timeHi = BinaryUtils::applyVersion($uuidTime['hi'], 1);
|
||||
$clockSeqHi = BinaryUtils::applyVariant($clockSeq >> 8);
|
||||
|
||||
$hex = vsprintf(
|
||||
'%08s%04s%04s%02s%02s%012s',
|
||||
[
|
||||
$uuidTime['low'],
|
||||
$uuidTime['mid'],
|
||||
sprintf('%04x', $timeHi),
|
||||
sprintf('%02x', $clockSeqHi),
|
||||
sprintf('%02x', $clockSeq & 0xff),
|
||||
$node,
|
||||
]
|
||||
$uuidTime = $this->timeConverter->calculateTime(
|
||||
$time->getSeconds()->toString(),
|
||||
$time->getMicroseconds()->toString()
|
||||
);
|
||||
|
||||
return hex2bin($hex);
|
||||
$timeHex = str_pad($uuidTime->toString(), 16, '0', STR_PAD_LEFT);
|
||||
|
||||
if (strlen($timeHex) !== 16) {
|
||||
throw new TimeSourceException(sprintf(
|
||||
'The generated time of \'%s\' is larger than expected',
|
||||
$timeHex
|
||||
));
|
||||
}
|
||||
|
||||
$timeBytes = (string) hex2bin($timeHex);
|
||||
|
||||
return $timeBytes[4] . $timeBytes[5] . $timeBytes[6] . $timeBytes[7]
|
||||
. $timeBytes[2] . $timeBytes[3]
|
||||
. $timeBytes[0] . $timeBytes[1]
|
||||
. pack('n*', $clockSeq)
|
||||
. $node;
|
||||
}
|
||||
|
||||
/**
|
||||
* Uses the node provider given when constructing this instance to get
|
||||
* the node ID (usually a MAC address)
|
||||
*
|
||||
* @param string|int $node A node value that may be used to override the node provider
|
||||
* @return string Hexadecimal representation of the node ID
|
||||
* @param string|int|null $node A node value that may be used to override the node provider
|
||||
*
|
||||
* @return string 6-byte binary string representation of the node
|
||||
*
|
||||
* @throws InvalidArgumentException
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function getValidNode($node)
|
||||
private function getValidNode($node): string
|
||||
{
|
||||
if ($node === null) {
|
||||
$node = $this->nodeProvider->getNode();
|
||||
}
|
||||
|
||||
// Convert the node to hex, if it is still an integer
|
||||
// Convert the node to hex, if it is still an integer.
|
||||
if (is_int($node)) {
|
||||
$node = sprintf('%012x', $node);
|
||||
$node = dechex($node);
|
||||
}
|
||||
|
||||
if (!ctype_xdigit($node) || strlen($node) > 12) {
|
||||
if (!ctype_xdigit((string) $node) || strlen((string) $node) > 12) {
|
||||
throw new InvalidArgumentException('Invalid node value');
|
||||
}
|
||||
|
||||
return strtolower(sprintf('%012s', $node));
|
||||
return (string) hex2bin(str_pad((string) $node, 12, '0', STR_PAD_LEFT));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user