<?php
 declare(strict_types=1); namespace SpomkyLabs\Pki\X501\ASN1\AttributeValue; use BadMethodCallException; use SpomkyLabs\Pki\ASN1\Element; use SpomkyLabs\Pki\ASN1\Type\UnspecifiedType; use SpomkyLabs\Pki\X501\DN\DNParser; use SpomkyLabs\Pki\X501\MatchingRule\BinaryMatch; use SpomkyLabs\Pki\X501\MatchingRule\MatchingRule; use SpomkyLabs\Pki\X501\StringPrep\TranscodeStep; final class UnknownAttributeValue extends AttributeValue { protected function __construct( string $oid, protected Element $_element ) { parent::__construct($oid); $this->oid = $oid; } public static function create(string $oid, Element $_element): self { return new self($oid, $_element); } public function toASN1(): Element { return $this->_element; } public function stringValue(): string { if ($this->_element->isType(Element::TYPE_STRING)) { return $this->_element->asUnspecified() ->asString() ->string(); } return '#' . bin2hex($this->_element->toDER()); } public function equalityMatchingRule(): MatchingRule { return new BinaryMatch(); } public function rfc2253String(): string { $str = $this->_transcodedString(); if ($this->_element->isType(Element::TYPE_STRING)) { $str = DNParser::escapeString($str); } return $str; } public static function fromASN1(UnspecifiedType $el): AttributeValue { throw new BadMethodCallException('ASN.1 parsing must be implemented in a concrete class.'); } protected function _transcodedString(): string { if (TranscodeStep::isTypeSupported($this->_element->tag())) { $step = TranscodeStep::create($this->_element->tag()); return $step->apply($this->stringValue()); } return $this->stringValue(); } } 