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
|
2 |
|
return array_map(function (StringType $el) { |
77
|
2 |
|
return $el->string(); |
78
|
2 |
|
}, $this->_elements); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get the contained strings concatenated together. |
83
|
|
|
* |
84
|
|
|
* NOTE: It's unclear how bit strings with unused bits should be |
85
|
|
|
* concatentated. |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
1 |
|
public function string(): string |
90
|
|
|
{ |
91
|
1 |
|
return implode('', $this->strings()); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
* |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
1 |
|
public function __toString(): string |
101
|
|
|
{ |
102
|
1 |
|
return $this->string(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
* |
109
|
|
|
* @return self |
110
|
|
|
*/ |
111
|
4 |
|
protected static function _decodeFromDER(Identifier $identifier, |
112
|
|
|
string $data, int &$offset): ElementBase |
113
|
|
|
{ |
114
|
|
|
/** @var ConstructedString $type */ |
115
|
4 |
|
$type = forward_static_call_array([parent::class, __FUNCTION__], |
116
|
4 |
|
[$identifier, $data, &$offset]); |
117
|
4 |
|
$type->_typeTag = $identifier->intTag(); |
118
|
4 |
|
return $type; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|