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

Breadcrumb

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

class CombGenerator

CombGenerator generates COMBs (combined UUID/timestamp)

The CombGenerator, when used with the StringCodec (and, by proxy, the TimestampLastCombCodec) or the TimestampFirstCombCodec, combines the current timestamp with a UUID (hence the name "COMB"). The timestamp either appears as the first or last 48 bits of the COMB, depending on the codec used.

By default, COMBs will have the timestamp set as the last 48 bits of the identifier.

``` php $factory = new UuidFactory();

$factory->setRandomGenerator(new CombGenerator( $factory->getRandomGenerator(), $factory->getNumberConverter() ));

$comb = $factory->uuid4(); ```

To generate a COMB with the timestamp as the first 48 bits, set the TimestampFirstCombCodec as the codec.

``` php $factory->setCodec(new TimestampFirstCombCodec($factory->getUuidBuilder())); ```

@link https://www.informit.com/articles/printerfriendly/25862 The Cost of GUIDs as Primary Keys

Hierarchy

  • class \Ramsey\Uuid\Generator\CombGenerator implements \Ramsey\Uuid\Generator\RandomGeneratorInterface

Expanded class hierarchy of CombGenerator

File

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

Namespace

Ramsey\Uuid\Generator
View source
class CombGenerator implements RandomGeneratorInterface {
    public const TIMESTAMP_BYTES = 6;
    public function __construct(RandomGeneratorInterface $generator, NumberConverterInterface $numberConverter) {
    }
    
    /**
     * @throws InvalidArgumentException if $length is not a positive integer
     *     greater than or equal to CombGenerator::TIMESTAMP_BYTES
     *
     * @inheritDoc
     */
    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);
    }
    
    /**
     * Returns current timestamp a string integer, precise to 0.00001 seconds
     */
    private function timestamp() : string {
        $time = explode(' ', microtime(false));
        return $time[1] . substr($time[0], 2, 5);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
CombGenerator::generate public function @inheritDoc Overrides RandomGeneratorInterface::generate
CombGenerator::timestamp private function Returns current timestamp a string integer, precise to 0.00001 seconds
CombGenerator::TIMESTAMP_BYTES public constant
CombGenerator::__construct public function
RSS feed
Powered by Drupal