1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Sop\X509\Certificate\Extension\CertificatePolicy; |
6
|
|
|
|
7
|
|
|
use Sop\ASN1\Element; |
8
|
|
|
use Sop\ASN1\Type\Primitive\BMPString; |
9
|
|
|
use Sop\ASN1\Type\Primitive\IA5String; |
10
|
|
|
use Sop\ASN1\Type\Primitive\UTF8String; |
11
|
|
|
use Sop\ASN1\Type\Primitive\VisibleString; |
12
|
|
|
use Sop\ASN1\Type\StringType; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Implements *DisplayText* ASN.1 CHOICE type used by 'Certificate Policies' |
16
|
|
|
* certificate extension. |
17
|
|
|
* |
18
|
|
|
* @see https://tools.ietf.org/html/rfc5280#section-4.2.1.4 |
19
|
|
|
*/ |
20
|
|
|
class DisplayText |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Text. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $_text; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Element tag. |
31
|
|
|
* |
32
|
|
|
* @var int |
33
|
|
|
*/ |
34
|
|
|
protected $_tag; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Constructor. |
38
|
|
|
* |
39
|
|
|
* @param string $text |
40
|
|
|
* @param int $tag |
41
|
|
|
*/ |
42
|
24 |
|
public function __construct(string $text, int $tag) |
43
|
|
|
{ |
44
|
24 |
|
$this->_text = $text; |
45
|
24 |
|
$this->_tag = $tag; |
46
|
24 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
3 |
|
public function __toString(): string |
52
|
|
|
{ |
53
|
3 |
|
return $this->string(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Initialize from ASN.1. |
58
|
|
|
* |
59
|
|
|
* @param StringType $el |
60
|
|
|
* |
61
|
|
|
* @return self |
62
|
|
|
*/ |
63
|
13 |
|
public static function fromASN1(StringType $el): self |
64
|
|
|
{ |
65
|
13 |
|
return new self($el->string(), $el->tag()); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Initialize from a UTF-8 string. |
70
|
|
|
* |
71
|
|
|
* @param string $str |
72
|
|
|
* |
73
|
|
|
* @return self |
74
|
|
|
*/ |
75
|
6 |
|
public static function fromString(string $str): self |
76
|
|
|
{ |
77
|
6 |
|
return new self($str, Element::TYPE_UTF8_STRING); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get the text. |
82
|
|
|
* |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
8 |
|
public function string(): string |
86
|
|
|
{ |
87
|
8 |
|
return $this->_text; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Generate ASN.1 element. |
92
|
|
|
* |
93
|
|
|
* @throws \UnexpectedValueException |
94
|
|
|
* |
95
|
|
|
* @return StringType |
96
|
|
|
*/ |
97
|
25 |
|
public function toASN1(): StringType |
98
|
|
|
{ |
99
|
25 |
|
switch ($this->_tag) { |
100
|
25 |
|
case Element::TYPE_IA5_STRING: |
101
|
1 |
|
return new IA5String($this->_text); |
102
|
24 |
|
case Element::TYPE_VISIBLE_STRING: |
103
|
15 |
|
return new VisibleString($this->_text); |
104
|
9 |
|
case Element::TYPE_BMP_STRING: |
105
|
1 |
|
return new BMPString($this->_text); |
106
|
8 |
|
case Element::TYPE_UTF8_STRING: |
107
|
7 |
|
return new UTF8String($this->_text); |
108
|
|
|
default: |
109
|
1 |
|
throw new \UnexpectedValueException( |
110
|
1 |
|
'Type ' . Element::tagToName($this->_tag) . ' not supported.'); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|