<?php
 declare(strict_types=1); namespace SpomkyLabs\Pki\X501\StringPrep; use LogicException; use SpomkyLabs\Pki\ASN1\Element; use SpomkyLabs\Pki\ASN1\Type\Primitive\T61String; use function in_array; use function sprintf; final class TranscodeStep implements PrepareStep { private const SUPPORTED_TYPES = [ Element::TYPE_UTF8_STRING, Element::TYPE_PRINTABLE_STRING, Element::TYPE_BMP_STRING, Element::TYPE_UNIVERSAL_STRING, Element::TYPE_T61_STRING, ]; private function __construct( private readonly int $_type ) { } public static function create(int $_type): self { return new self($_type); } public static function isTypeSupported(int $type): bool { return in_array($type, self::SUPPORTED_TYPES, true); } public function apply(string $string): string { switch ($this->_type) { case Element::TYPE_UTF8_STRING: case Element::TYPE_PRINTABLE_STRING: return $string; case Element::TYPE_BMP_STRING: return mb_convert_encoding($string, 'UTF-8', 'UCS-2BE'); case Element::TYPE_UNIVERSAL_STRING: return mb_convert_encoding($string, 'UTF-8', 'UCS-4BE'); case Element::TYPE_T61_STRING: $el = T61String::create($string); return '#' . bin2hex($el->toDER()); } throw new LogicException(sprintf('Unsupported string type %s.', Element::tagToName($this->_type))); } } 