- md5
- 1443457459b070ebd696b69b47e6f5ef
- sha1
- 84c6daa4e0227a57d0c04039ae6f8d8b3875b498
- sha256
- 79f1b58b1eed5100882a859c5e59bffe87afb76e11053a37d300a2c3c5bc36ed
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
<?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;
/**
* Implements 'Transcode' step of the Internationalized String Preparation as specified by RFC 4518.
*
* @see https://tools.ietf.org/html/rfc4518#section-2.1
*/
final class TranscodeStep implements PrepareStep
{
/**
* Supported ASN.1 types.
*
* @var array<int>
*/
private const SUPPORTED_TYPES = [
Element::TYPE_UTF8_STRING,
Element::TYPE_PRINTABLE_STRING,
Element::TYPE_BMP_STRING,
Element::TYPE_UNIVERSAL_STRING,
Element::TYPE_T61_STRING,
];
/**
* @param int $_type ASN.1 type tag of the string
*/
private function __construct(
private readonly int $_type
) {
}
public static function create(int $_type): self
{
return new self($_type);
}
/**
* Check whether transcoding from given ASN.1 type tag is supported.
*
* @param int $type ASN.1 type tag
*/
public static function isTypeSupported(int $type): bool
{
return in_array($type, self::SUPPORTED_TYPES, true);
}
/**
* @param string $string String to prepare
*
* @return string UTF-8 encoded string
*/
public function apply(string $string): string
{
switch ($this->_type) {
// UTF-8 string as is
case Element::TYPE_UTF8_STRING:
// PrintableString maps directly to UTF-8
case Element::TYPE_PRINTABLE_STRING:
return $string;
// UCS-2 to UTF-8
case Element::TYPE_BMP_STRING:
return mb_convert_encoding($string, 'UTF-8', 'UCS-2BE');
// UCS-4 to UTF-8
case Element::TYPE_UNIVERSAL_STRING:
return mb_convert_encoding($string, 'UTF-8', 'UCS-4BE');
// TeletexString mapping is a local matter.
// We take a shortcut here and encode it as a hexstring.
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)));
}
}
The same content is in 39 releases, Joomla 5.1.0-beta1 to 5.3.0-alpha3. See them all, or every version of this file.