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

Breadcrumb

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

function CombGenerator::generate

@inheritDoc

Throws

InvalidArgumentException if $length is not a positive integer greater than or equal to CombGenerator::TIMESTAMP_BYTES

Overrides RandomGeneratorInterface::generate

File

vendor/ramsey/uuid/src/Generator/CombGenerator.php, line 76

Class

CombGenerator
CombGenerator generates COMBs (combined UUID/timestamp)

Namespace

Ramsey\Uuid\Generator

Code

public function generate(int $length) : string {
    if ($length < self::TIMESTAMP_BYTES) {
        throw new InvalidArgumentException('Length must be a positive integer greater than or equal to ' . self::TIMESTAMP_BYTES);
    }
    $hash = '';
    if (self::TIMESTAMP_BYTES > 0 && $length > self::TIMESTAMP_BYTES) {
        $hash = $this->generator
            ->generate($length - self::TIMESTAMP_BYTES);
    }
    $lsbTime = str_pad($this->numberConverter
        ->toHex($this->timestamp()), self::TIMESTAMP_BYTES * 2, '0', STR_PAD_LEFT);
    return (string) hex2bin(str_pad(bin2hex($hash), $length - self::TIMESTAMP_BYTES, '0') . $lsbTime);
}

API Navigation

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