function DefaultTimeGenerator::generate
@inheritDoc
Throws
InvalidArgumentException if the parameters contain invalid values
RandomSourceException if random_int() throws an exception/error
Overrides TimeGeneratorInterface::generate
File
-
vendor/
ramsey/ uuid/ src/ Generator/ DefaultTimeGenerator.php, line 56
Class
- DefaultTimeGenerator
- DefaultTimeGenerator generates strings of binary data based on a node ID, clock sequence, and the current time
Namespace
Ramsey\Uuid\GeneratorCode
public function generate($node = null, ?int $clockSeq = null) : string {
if ($node instanceof Hexadecimal) {
$node = $node->toString();
}
$node = $this->getValidNode($node);
if ($clockSeq === null) {
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);
}
}
$time = $this->timeProvider
->getTime();
$uuidTime = $this->timeConverter
->calculateTime($time->getSeconds()
->toString(), $time->getMicroseconds()
->toString());
$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;
}