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

Breadcrumb

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

class StringCodec

StringCodec encodes and decodes RFC 4122 UUIDs

@link http://tools.ietf.org/html/rfc4122

@psalm-immutable

Hierarchy

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

Expanded class hierarchy of StringCodec

1 file declares its use of StringCodec
FeatureSet.php in vendor/ramsey/uuid/src/FeatureSet.php

File

vendor/ramsey/uuid/src/Codec/StringCodec.php, line 38

Namespace

Ramsey\Uuid\Codec
View source
class StringCodec implements CodecInterface {
    
    /**
     * Constructs a StringCodec
     *
     * @param UuidBuilderInterface $builder The builder to use when encoding UUIDs
     */
    public function __construct(UuidBuilderInterface $builder) {
    }
    public function encode(UuidInterface $uuid) : string {
        $hex = bin2hex($uuid->getFields()
            ->getBytes());
        
        /** @var non-empty-string */
        return sprintf('%08s-%04s-%04s-%04s-%012s', substr($hex, 0, 8), substr($hex, 8, 4), substr($hex, 12, 4), substr($hex, 16, 4), substr($hex, 20));
    }
    
    /**
     * @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 $uuid->getFields()
            ->getBytes();
    }
    
    /**
     * @throws InvalidUuidStringException
     *
     * @inheritDoc
     */
    public function decode(string $encodedUuid) : UuidInterface {
        return $this->builder
            ->build($this, $this->getBytes($encodedUuid));
    }
    public function decodeBytes(string $bytes) : UuidInterface {
        if (strlen($bytes) !== 16) {
            throw new InvalidArgumentException('$bytes string should contain 16 characters.');
        }
        return $this->builder
            ->build($this, $bytes);
    }
    
    /**
     * Returns the UUID builder
     */
    protected function getBuilder() : UuidBuilderInterface {
        return $this->builder;
    }
    
    /**
     * Returns a byte string of the UUID
     */
    protected function getBytes(string $encodedUuid) : string {
        $parsedUuid = str_replace([
            'urn:',
            'uuid:',
            'URN:',
            'UUID:',
            '{',
            '}',
            '-',
        ], '', $encodedUuid);
        $components = [
            substr($parsedUuid, 0, 8),
            substr($parsedUuid, 8, 4),
            substr($parsedUuid, 12, 4),
            substr($parsedUuid, 16, 4),
            substr($parsedUuid, 20),
        ];
        if (!Uuid::isValid(implode('-', $components))) {
            throw new InvalidUuidStringException('Invalid UUID string: ' . $encodedUuid);
        }
        return (string) hex2bin($parsedUuid);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
StringCodec::decode public function @inheritDoc Overrides CodecInterface::decode 2
StringCodec::decodeBytes public function Returns a UuidInterface derived from a binary string representation Overrides CodecInterface::decodeBytes 3
StringCodec::encode public function Returns a hexadecimal string representation of a UuidInterface Overrides CodecInterface::encode 2
StringCodec::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 CodecInterface::encodeBinary 2
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
RSS feed
Powered by Drupal