Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. DefaultTimeGenerator.php

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\Generator

Code

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;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal