<?php
 declare(strict_types=1); namespace SpomkyLabs\Pki\X509\Certificate\Extension\CertificatePolicy; use SpomkyLabs\Pki\ASN1\Element; use SpomkyLabs\Pki\ASN1\Type\Primitive\BMPString; use SpomkyLabs\Pki\ASN1\Type\Primitive\IA5String; use SpomkyLabs\Pki\ASN1\Type\Primitive\UTF8String; use SpomkyLabs\Pki\ASN1\Type\Primitive\VisibleString; use SpomkyLabs\Pki\ASN1\Type\StringType; use Stringable; use UnexpectedValueException; final class DisplayText implements Stringable { private function __construct( private readonly string $text, private readonly int $tag ) { } public function __toString(): string { return $this->string(); } public static function create(string $text, int $tag): self { return new self($text, $tag); } public static function fromASN1(StringType $el): self { return self::create($el->string(), $el->tag()); } public static function fromString(string $str): self { return self::create($str, Element::TYPE_UTF8_STRING); } public function string(): string { return $this->text; } public function toASN1(): StringType { return match ($this->tag) { Element::TYPE_IA5_STRING => IA5String::create($this->text), Element::TYPE_VISIBLE_STRING => VisibleString::create($this->text), Element::TYPE_BMP_STRING => BMPString::create($this->text), Element::TYPE_UTF8_STRING => UTF8String::create($this->text), default => throw new UnexpectedValueException('Type ' . Element::tagToName( $this->tag ) . ' not supported.'), }; } } 