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

Breadcrumb

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

class DefaultTimeGenerator

DefaultTimeGenerator generates strings of binary data based on a node ID, clock sequence, and the current time

Hierarchy

  • class \Ramsey\Uuid\Generator\DefaultTimeGenerator implements \Ramsey\Uuid\Generator\TimeGeneratorInterface

Expanded class hierarchy of DefaultTimeGenerator

1 file declares its use of DefaultTimeGenerator
UuidFactory.php in vendor/ramsey/uuid/src/UuidFactory.php

File

vendor/ramsey/uuid/src/Generator/DefaultTimeGenerator.php, line 41

Namespace

Ramsey\Uuid\Generator
View source
class DefaultTimeGenerator implements TimeGeneratorInterface {
    public function __construct(NodeProviderInterface $nodeProvider, TimeConverterInterface $timeConverter, TimeProviderInterface $timeProvider) {
    }
    
    /**
     * @throws InvalidArgumentException if the parameters contain invalid values
     * @throws RandomSourceException if random_int() throws an exception/error
     *
     * @inheritDoc
     */
    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;
    }
    
    /**
     * Uses the node provider given when constructing this instance to get
     * the node ID (usually a MAC address)
     *
     * @param int|string|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
     */
    private function getValidNode(int|string|null $node) : string {
        if ($node === null) {
            $node = $this->nodeProvider
                ->getNode();
        }
        // Convert the node to hex, if it is still an integer.
        if (is_int($node)) {
            $node = dechex($node);
        }
        if (!preg_match('/^[A-Fa-f0-9]+$/', (string) $node) || strlen((string) $node) > 12) {
            throw new InvalidArgumentException('Invalid node value');
        }
        return (string) hex2bin(str_pad((string) $node, 12, '0', STR_PAD_LEFT));
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
DefaultTimeGenerator::generate public function @inheritDoc Overrides TimeGeneratorInterface::generate
DefaultTimeGenerator::getValidNode private function Uses the node provider given when constructing this instance to get
the node ID (usually a MAC address)
DefaultTimeGenerator::__construct public function
RSS feed
Powered by Drupal