|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types = 1); |
|
3
|
|
|
|
|
4
|
|
|
namespace ASN1\Type\Constructed; |
|
5
|
|
|
|
|
6
|
|
|
use ASN1\Element; |
|
7
|
|
|
use ASN1\Component\Identifier; |
|
8
|
|
|
use ASN1\Feature\ElementBase; |
|
9
|
|
|
use ASN1\Feature\Stringable; |
|
10
|
|
|
use ASN1\Type\StringType; |
|
11
|
|
|
use ASN1\Type\Structure; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Implements constructed type of simple strings. |
|
15
|
|
|
*/ |
|
16
|
|
|
class ConstructedString extends Structure implements Stringable |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Constructor. |
|
20
|
|
|
* |
|
21
|
|
|
* @internal Use create() |
|
22
|
|
|
* |
|
23
|
|
|
* @param Element ...$elements Any number of elements |
|
24
|
|
|
*/ |
|
25
|
8 |
|
public function __construct(Element ...$elements) |
|
26
|
|
|
{ |
|
27
|
8 |
|
parent::__construct(...$elements); |
|
28
|
8 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Create from a list of string type elements |
|
32
|
|
|
* |
|
33
|
|
|
* All strings must have the same type. |
|
34
|
|
|
* |
|
35
|
|
|
* @param StringType ...$elements |
|
36
|
|
|
* @throws \LogicException |
|
37
|
|
|
* @return self |
|
38
|
|
|
*/ |
|
39
|
3 |
|
public static function create(StringType ...$elements): self |
|
40
|
|
|
{ |
|
41
|
3 |
|
if (!count($elements)) { |
|
42
|
1 |
|
throw new \LogicException( |
|
43
|
1 |
|
'No elements, unable to determine type tag.'); |
|
44
|
|
|
} |
|
45
|
2 |
|
$tag = $elements[0]->tag(); |
|
46
|
2 |
|
foreach ($elements as $el) { |
|
47
|
2 |
|
if ($el->tag() !== $tag) { |
|
48
|
1 |
|
throw new \LogicException( |
|
49
|
2 |
|
'All elements in constructed string must have the same type.'); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
1 |
|
return self::createWithTag($tag, ...$elements); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Create from strings with a given type tag. |
|
57
|
|
|
* |
|
58
|
|
|
* @param int $tag Type tag for the constructed string element |
|
59
|
|
|
* @param StringType ...$elements Any number of elements |
|
60
|
|
|
* @return self |
|
61
|
|
|
*/ |
|
62
|
4 |
|
public static function createWithTag(int $tag, StringType ...$elements) |
|
63
|
|
|
{ |
|
64
|
4 |
|
$el = new self(...$elements); |
|
65
|
4 |
|
$el->_typeTag = $tag; |
|
66
|
4 |
|
return $el; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Get a list of strings in this structure. |
|
71
|
|
|
* |
|
72
|
|
|
* @return string[] |
|
73
|
|
|
*/ |
|
74
|
|
|
public function strings(): array |
|
75
|
|
|
{ |
|
76
|
3 |
|
return array_map(function (StringType $el) { |
|
77
|
3 |
|
return $el->string(); |
|
78
|
3 |
|
}, $this->_elements); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get the contained strings concatenated together. |
|
83
|
|
|
* |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
2 |
|
public function concatenated(): string |
|
87
|
|
|
{ |
|
88
|
2 |
|
return implode('', $this->strings()); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Get the contained strings concatenated together. |
|
93
|
|
|
* |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
1 |
|
public function string(): string |
|
97
|
|
|
{ |
|
98
|
1 |
|
return $this->concatenated(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* |
|
103
|
|
|
* @inheritdoc |
|
104
|
|
|
* @return string |
|
105
|
|
|
*/ |
|
106
|
1 |
|
public function __toString(): string |
|
107
|
|
|
{ |
|
108
|
1 |
|
return $this->concatenated(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* |
|
113
|
|
|
* {@inheritdoc} |
|
114
|
|
|
* |
|
115
|
|
|
* @return self |
|
116
|
|
|
*/ |
|
117
|
4 |
|
protected static function _decodeFromDER(Identifier $identifier, |
|
118
|
|
|
string $data, int &$offset): ElementBase |
|
119
|
|
|
{ |
|
120
|
|
|
/** @var ConstructedString $type */ |
|
121
|
4 |
|
$type = forward_static_call_array([parent::class, __FUNCTION__], |
|
122
|
4 |
|
[$identifier, $data, &$offset]); |
|
123
|
4 |
|
$type->_typeTag = $identifier->intTag(); |
|
124
|
4 |
|
return $type; |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|