Debian Codegraph @

lib/3rdParty/composer/spomky-labs/pki-framework/src/X509/Certificate/Extension/CertificatePolicy/DisplayText.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace SpomkyLabs\Pki\X509\Certificate\Extension\CertificatePolicy;
6
7 use SpomkyLabs\Pki\ASN1\Element;
8 use SpomkyLabs\Pki\ASN1\Type\Primitive\BMPString;
9 use SpomkyLabs\Pki\ASN1\Type\Primitive\IA5String;
10 use SpomkyLabs\Pki\ASN1\Type\Primitive\UTF8String;
11 use SpomkyLabs\Pki\ASN1\Type\Primitive\VisibleString;
12 use SpomkyLabs\Pki\ASN1\Type\StringType;
13 use Stringable;
14 use UnexpectedValueException;
15
16 /**
17 * Implements *DisplayText* ASN.1 CHOICE type used by 'Certificate Policies' certificate extension.
18 *
19 * @see https://tools.ietf.org/html/rfc5280#section-4.2.1.4
20 */
21 final class DisplayText implements Stringable
22 {
23 private function __construct(
24 private readonly string $text,
25 private readonly int $tag
26 ) {
27 }
28
29 public function __toString(): string
30 {
31 return $this->string();
32 }
33
34 public static function create(string $text, int $tag): self
35 {
36 return new self($text, $tag);
37 }
38
39 /**
40 * Initialize from ASN.1.
41 */
42 public static function fromASN1(StringType $el): self
43 {
44 return self::create($el->string(), $el->tag());
45 }
46
47 /**
48 * Initialize from a UTF-8 string.
49 */
50 public static function fromString(string $str): self
51 {
52 return self::create($str, Element::TYPE_UTF8_STRING);
53 }
54
55 /**
56 * Get the text.
57 */
58 public function string(): string
59 {
60 return $this->text;
61 }
62
63 /**
64 * Generate ASN.1 element.
65 */
66 public function toASN1(): StringType
67 {
68 return match ($this->tag) {
69 Element::TYPE_IA5_STRING => IA5String::create($this->text),
70 Element::TYPE_VISIBLE_STRING => VisibleString::create($this->text),
71 Element::TYPE_BMP_STRING => BMPString::create($this->text),
72 Element::TYPE_UTF8_STRING => UTF8String::create($this->text),
73 default => throw new UnexpectedValueException('Type ' . Element::tagToName(
74 $this->tag
75 ) . ' not supported.'),
76 };
77 }
78 }