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

Breadcrumb

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

class TimestampFirstCombCodec

TimestampFirstCombCodec encodes and decodes COMBs, with the timestamp as the first 48 bits

In contrast with the TimestampLastCombCodec, the TimestampFirstCombCodec adds the timestamp to the first 48 bits of the COMB. To generate a timestamp-first COMB, set the TimestampFirstCombCodec as the codec, along with the CombGenerator as the random generator.

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

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

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

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

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

@psalm-immutable

Hierarchy

  • class \Ramsey\Uuid\Codec\StringCodec implements \Ramsey\Uuid\Codec\CodecInterface
    • class \Ramsey\Uuid\Codec\TimestampFirstCombCodec extends \Ramsey\Uuid\Codec\StringCodec

Expanded class hierarchy of TimestampFirstCombCodec

File

vendor/ramsey/uuid/src/Codec/TimestampFirstCombCodec.php, line 51

Namespace

Ramsey\Uuid\Codec
View source
class TimestampFirstCombCodec extends StringCodec {
    
    /**
     * @psalm-return non-empty-string
     * @psalm-suppress MoreSpecificReturnType we know that the retrieved `string` is never empty
     * @psalm-suppress LessSpecificReturnStatement we know that the retrieved `string` is never empty
     */
    public function encode(UuidInterface $uuid) : string {
        $bytes = $this->swapBytes($uuid->getFields()
            ->getBytes());
        return sprintf('%08s-%04s-%04s-%04s-%012s', bin2hex(substr($bytes, 0, 4)), bin2hex(substr($bytes, 4, 2)), bin2hex(substr($bytes, 6, 2)), bin2hex(substr($bytes, 8, 2)), bin2hex(substr($bytes, 10)));
    }
    
    /**
     * @psalm-return non-empty-string
     * @psalm-suppress MoreSpecificReturnType we know that the retrieved `string` is never empty
     * @psalm-suppress LessSpecificReturnStatement we know that the retrieved `string` is never empty
     */
    public function encodeBinary(UuidInterface $uuid) : string {
        
        /** @phpstan-ignore-next-line PHPStan complains that this is not a non-empty-string. */
        return $this->swapBytes($uuid->getFields()
            ->getBytes());
    }
    
    /**
     * @throws InvalidUuidStringException
     *
     * @inheritDoc
     */
    public function decode(string $encodedUuid) : UuidInterface {
        $bytes = $this->getBytes($encodedUuid);
        return $this->getBuilder()
            ->build($this, $this->swapBytes($bytes));
    }
    public function decodeBytes(string $bytes) : UuidInterface {
        return $this->getBuilder()
            ->build($this, $this->swapBytes($bytes));
    }
    
    /**
     * Swaps bytes according to the timestamp-first COMB rules
     */
    private function swapBytes(string $bytes) : string {
        $first48Bits = substr($bytes, 0, 6);
        $last48Bits = substr($bytes, -6);
        $bytes = substr_replace($bytes, $last48Bits, 0, 6);
        $bytes = substr_replace($bytes, $first48Bits, -6);
        return $bytes;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
StringCodec::getBuilder protected function Returns the UUID builder
StringCodec::getBytes protected function Returns a byte string of the UUID
StringCodec::__construct public function Constructs a StringCodec
TimestampFirstCombCodec::decode public function @inheritDoc Overrides StringCodec::decode
TimestampFirstCombCodec::decodeBytes public function Returns a UuidInterface derived from a binary string representation Overrides StringCodec::decodeBytes
TimestampFirstCombCodec::encode public function @psalm-return non-empty-string
@psalm-suppress MoreSpecificReturnType we know that the retrieved `string` is never empty
@psalm-suppress LessSpecificReturnStatement we know that the retrieved `string` is never empty
Overrides StringCodec::encode
TimestampFirstCombCodec::encodeBinary public function @psalm-return non-empty-string
@psalm-suppress MoreSpecificReturnType we know that the retrieved `string` is never empty
@psalm-suppress LessSpecificReturnStatement we know that the retrieved `string` is never empty
Overrides StringCodec::encodeBinary
TimestampFirstCombCodec::swapBytes private function Swaps bytes according to the timestamp-first COMB rules
RSS feed
Powered by Drupal