<?php
 declare(strict_types=1); namespace SpomkyLabs\Pki\ASN1; use BadMethodCallException; use SpomkyLabs\Pki\ASN1\Component\Identifier; use SpomkyLabs\Pki\ASN1\Component\Length; use SpomkyLabs\Pki\ASN1\Feature\ElementBase; use function mb_strlen; final class DERData extends Element { private readonly string $der; private readonly Identifier $identifier; private int $contentOffset = 0; private function __construct(string $data) { $this->identifier = Identifier::fromDER($data, $this->contentOffset); Length::expectFromDER($data, $this->contentOffset); $this->der = $data; parent::__construct($this->identifier->intTag()); } public static function create(string $data): self { return new self($data); } public function typeClass(): int { return $this->identifier->typeClass(); } public function isConstructed(): bool { return $this->identifier->isConstructed(); } public function toDER(): string { return $this->der; } protected function encodedAsDER(): string { if (mb_strlen($this->der, '8bit') === $this->contentOffset) { return ''; } return mb_substr($this->der, $this->contentOffset, null, '8bit'); } protected static function decodeFromDER(Identifier $identifier, string $data, int &$offset): ElementBase { throw new BadMethodCallException(__METHOD__ . ' must be implemented in derived class.'); } } 