@@ -15,416 +15,416 @@ |
||
15 | 15 | */ |
16 | 16 | abstract class Structure extends Element implements \Countable, \IteratorAggregate |
17 | 17 | { |
18 | - use UniversalClass; |
|
18 | + use UniversalClass; |
|
19 | 19 | |
20 | - /** |
|
21 | - * Array of elements in the structure. |
|
22 | - * |
|
23 | - * @var Element[] |
|
24 | - */ |
|
25 | - protected $_elements; |
|
20 | + /** |
|
21 | + * Array of elements in the structure. |
|
22 | + * |
|
23 | + * @var Element[] |
|
24 | + */ |
|
25 | + protected $_elements; |
|
26 | 26 | |
27 | - /** |
|
28 | - * Lookup table for the tagged elements. |
|
29 | - * |
|
30 | - * @var null|TaggedType[] |
|
31 | - */ |
|
32 | - private $_taggedMap; |
|
27 | + /** |
|
28 | + * Lookup table for the tagged elements. |
|
29 | + * |
|
30 | + * @var null|TaggedType[] |
|
31 | + */ |
|
32 | + private $_taggedMap; |
|
33 | 33 | |
34 | - /** |
|
35 | - * Cache variable of elements wrapped into UnspecifiedType objects. |
|
36 | - * |
|
37 | - * @var null|UnspecifiedType[] |
|
38 | - */ |
|
39 | - private $_unspecifiedTypes; |
|
34 | + /** |
|
35 | + * Cache variable of elements wrapped into UnspecifiedType objects. |
|
36 | + * |
|
37 | + * @var null|UnspecifiedType[] |
|
38 | + */ |
|
39 | + private $_unspecifiedTypes; |
|
40 | 40 | |
41 | - /** |
|
42 | - * Constructor. |
|
43 | - * |
|
44 | - * @param ElementBase ...$elements Any number of elements |
|
45 | - */ |
|
46 | - public function __construct(ElementBase ...$elements) |
|
47 | - { |
|
48 | - $this->_elements = array_map( |
|
49 | - function (ElementBase $el) { |
|
50 | - return $el->asElement(); |
|
51 | - }, $elements); |
|
52 | - } |
|
41 | + /** |
|
42 | + * Constructor. |
|
43 | + * |
|
44 | + * @param ElementBase ...$elements Any number of elements |
|
45 | + */ |
|
46 | + public function __construct(ElementBase ...$elements) |
|
47 | + { |
|
48 | + $this->_elements = array_map( |
|
49 | + function (ElementBase $el) { |
|
50 | + return $el->asElement(); |
|
51 | + }, $elements); |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * Clone magic method. |
|
56 | - */ |
|
57 | - public function __clone() |
|
58 | - { |
|
59 | - // clear cache-variables |
|
60 | - $this->_taggedMap = null; |
|
61 | - $this->_unspecifiedTypes = null; |
|
62 | - } |
|
54 | + /** |
|
55 | + * Clone magic method. |
|
56 | + */ |
|
57 | + public function __clone() |
|
58 | + { |
|
59 | + // clear cache-variables |
|
60 | + $this->_taggedMap = null; |
|
61 | + $this->_unspecifiedTypes = null; |
|
62 | + } |
|
63 | 63 | |
64 | - /** |
|
65 | - * {@inheritdoc} |
|
66 | - */ |
|
67 | - public function isConstructed(): bool |
|
68 | - { |
|
69 | - return true; |
|
70 | - } |
|
64 | + /** |
|
65 | + * {@inheritdoc} |
|
66 | + */ |
|
67 | + public function isConstructed(): bool |
|
68 | + { |
|
69 | + return true; |
|
70 | + } |
|
71 | 71 | |
72 | - /** |
|
73 | - * Explode DER structure to DER encoded components that it contains. |
|
74 | - * |
|
75 | - * @param string $data |
|
76 | - * |
|
77 | - * @throws DecodeException |
|
78 | - * |
|
79 | - * @return string[] |
|
80 | - */ |
|
81 | - public static function explodeDER(string $data): array |
|
82 | - { |
|
83 | - $offset = 0; |
|
84 | - $identifier = Identifier::fromDER($data, $offset); |
|
85 | - if (!$identifier->isConstructed()) { |
|
86 | - throw new DecodeException('Element is not constructed.'); |
|
87 | - } |
|
88 | - $length = Length::expectFromDER($data, $offset); |
|
89 | - if ($length->isIndefinite()) { |
|
90 | - throw new DecodeException( |
|
91 | - 'Explode not implemented for indefinite length encoding.'); |
|
92 | - } |
|
93 | - $end = $offset + $length->intLength(); |
|
94 | - $parts = []; |
|
95 | - while ($offset < $end) { |
|
96 | - // start of the element |
|
97 | - $idx = $offset; |
|
98 | - // skip identifier |
|
99 | - Identifier::fromDER($data, $offset); |
|
100 | - // decode element length |
|
101 | - $length = Length::expectFromDER($data, $offset)->intLength(); |
|
102 | - // extract der encoding of the element |
|
103 | - $parts[] = substr($data, $idx, $offset - $idx + $length); |
|
104 | - // update offset over content |
|
105 | - $offset += $length; |
|
106 | - } |
|
107 | - return $parts; |
|
108 | - } |
|
72 | + /** |
|
73 | + * Explode DER structure to DER encoded components that it contains. |
|
74 | + * |
|
75 | + * @param string $data |
|
76 | + * |
|
77 | + * @throws DecodeException |
|
78 | + * |
|
79 | + * @return string[] |
|
80 | + */ |
|
81 | + public static function explodeDER(string $data): array |
|
82 | + { |
|
83 | + $offset = 0; |
|
84 | + $identifier = Identifier::fromDER($data, $offset); |
|
85 | + if (!$identifier->isConstructed()) { |
|
86 | + throw new DecodeException('Element is not constructed.'); |
|
87 | + } |
|
88 | + $length = Length::expectFromDER($data, $offset); |
|
89 | + if ($length->isIndefinite()) { |
|
90 | + throw new DecodeException( |
|
91 | + 'Explode not implemented for indefinite length encoding.'); |
|
92 | + } |
|
93 | + $end = $offset + $length->intLength(); |
|
94 | + $parts = []; |
|
95 | + while ($offset < $end) { |
|
96 | + // start of the element |
|
97 | + $idx = $offset; |
|
98 | + // skip identifier |
|
99 | + Identifier::fromDER($data, $offset); |
|
100 | + // decode element length |
|
101 | + $length = Length::expectFromDER($data, $offset)->intLength(); |
|
102 | + // extract der encoding of the element |
|
103 | + $parts[] = substr($data, $idx, $offset - $idx + $length); |
|
104 | + // update offset over content |
|
105 | + $offset += $length; |
|
106 | + } |
|
107 | + return $parts; |
|
108 | + } |
|
109 | 109 | |
110 | - /** |
|
111 | - * Get self with an element at the given index replaced by another. |
|
112 | - * |
|
113 | - * @param int $idx Element index |
|
114 | - * @param Element $el New element to insert into the structure |
|
115 | - * |
|
116 | - * @throws \OutOfBoundsException |
|
117 | - * |
|
118 | - * @return self |
|
119 | - */ |
|
120 | - public function withReplaced(int $idx, Element $el): self |
|
121 | - { |
|
122 | - if (!isset($this->_elements[$idx])) { |
|
123 | - throw new \OutOfBoundsException( |
|
124 | - "Structure doesn't have element at index ${idx}."); |
|
125 | - } |
|
126 | - $obj = clone $this; |
|
127 | - $obj->_elements[$idx] = $el; |
|
128 | - return $obj; |
|
129 | - } |
|
110 | + /** |
|
111 | + * Get self with an element at the given index replaced by another. |
|
112 | + * |
|
113 | + * @param int $idx Element index |
|
114 | + * @param Element $el New element to insert into the structure |
|
115 | + * |
|
116 | + * @throws \OutOfBoundsException |
|
117 | + * |
|
118 | + * @return self |
|
119 | + */ |
|
120 | + public function withReplaced(int $idx, Element $el): self |
|
121 | + { |
|
122 | + if (!isset($this->_elements[$idx])) { |
|
123 | + throw new \OutOfBoundsException( |
|
124 | + "Structure doesn't have element at index ${idx}."); |
|
125 | + } |
|
126 | + $obj = clone $this; |
|
127 | + $obj->_elements[$idx] = $el; |
|
128 | + return $obj; |
|
129 | + } |
|
130 | 130 | |
131 | - /** |
|
132 | - * Get self with an element inserted before the given index. |
|
133 | - * |
|
134 | - * @param int $idx Element index |
|
135 | - * @param Element $el New element to insert into the structure |
|
136 | - * |
|
137 | - * @throws \OutOfBoundsException |
|
138 | - * |
|
139 | - * @return self |
|
140 | - */ |
|
141 | - public function withInserted(int $idx, Element $el): self |
|
142 | - { |
|
143 | - if (count($this->_elements) < $idx || $idx < 0) { |
|
144 | - throw new \OutOfBoundsException("Index ${idx} is out of bounds."); |
|
145 | - } |
|
146 | - $obj = clone $this; |
|
147 | - array_splice($obj->_elements, $idx, 0, [$el]); |
|
148 | - return $obj; |
|
149 | - } |
|
131 | + /** |
|
132 | + * Get self with an element inserted before the given index. |
|
133 | + * |
|
134 | + * @param int $idx Element index |
|
135 | + * @param Element $el New element to insert into the structure |
|
136 | + * |
|
137 | + * @throws \OutOfBoundsException |
|
138 | + * |
|
139 | + * @return self |
|
140 | + */ |
|
141 | + public function withInserted(int $idx, Element $el): self |
|
142 | + { |
|
143 | + if (count($this->_elements) < $idx || $idx < 0) { |
|
144 | + throw new \OutOfBoundsException("Index ${idx} is out of bounds."); |
|
145 | + } |
|
146 | + $obj = clone $this; |
|
147 | + array_splice($obj->_elements, $idx, 0, [$el]); |
|
148 | + return $obj; |
|
149 | + } |
|
150 | 150 | |
151 | - /** |
|
152 | - * Get self with an element appended to the end. |
|
153 | - * |
|
154 | - * @param Element $el Element to insert into the structure |
|
155 | - * |
|
156 | - * @return self |
|
157 | - */ |
|
158 | - public function withAppended(Element $el): self |
|
159 | - { |
|
160 | - $obj = clone $this; |
|
161 | - array_push($obj->_elements, $el); |
|
162 | - return $obj; |
|
163 | - } |
|
151 | + /** |
|
152 | + * Get self with an element appended to the end. |
|
153 | + * |
|
154 | + * @param Element $el Element to insert into the structure |
|
155 | + * |
|
156 | + * @return self |
|
157 | + */ |
|
158 | + public function withAppended(Element $el): self |
|
159 | + { |
|
160 | + $obj = clone $this; |
|
161 | + array_push($obj->_elements, $el); |
|
162 | + return $obj; |
|
163 | + } |
|
164 | 164 | |
165 | - /** |
|
166 | - * Get self with an element prepended in the beginning. |
|
167 | - * |
|
168 | - * @param Element $el Element to insert into the structure |
|
169 | - * |
|
170 | - * @return self |
|
171 | - */ |
|
172 | - public function withPrepended(Element $el): self |
|
173 | - { |
|
174 | - $obj = clone $this; |
|
175 | - array_unshift($obj->_elements, $el); |
|
176 | - return $obj; |
|
177 | - } |
|
165 | + /** |
|
166 | + * Get self with an element prepended in the beginning. |
|
167 | + * |
|
168 | + * @param Element $el Element to insert into the structure |
|
169 | + * |
|
170 | + * @return self |
|
171 | + */ |
|
172 | + public function withPrepended(Element $el): self |
|
173 | + { |
|
174 | + $obj = clone $this; |
|
175 | + array_unshift($obj->_elements, $el); |
|
176 | + return $obj; |
|
177 | + } |
|
178 | 178 | |
179 | - /** |
|
180 | - * Get self with an element at the given index removed. |
|
181 | - * |
|
182 | - * @param int $idx Element index |
|
183 | - * |
|
184 | - * @throws \OutOfBoundsException |
|
185 | - * |
|
186 | - * @return self |
|
187 | - */ |
|
188 | - public function withoutElement(int $idx): self |
|
189 | - { |
|
190 | - if (!isset($this->_elements[$idx])) { |
|
191 | - throw new \OutOfBoundsException( |
|
192 | - "Structure doesn't have element at index ${idx}."); |
|
193 | - } |
|
194 | - $obj = clone $this; |
|
195 | - array_splice($obj->_elements, $idx, 1); |
|
196 | - return $obj; |
|
197 | - } |
|
179 | + /** |
|
180 | + * Get self with an element at the given index removed. |
|
181 | + * |
|
182 | + * @param int $idx Element index |
|
183 | + * |
|
184 | + * @throws \OutOfBoundsException |
|
185 | + * |
|
186 | + * @return self |
|
187 | + */ |
|
188 | + public function withoutElement(int $idx): self |
|
189 | + { |
|
190 | + if (!isset($this->_elements[$idx])) { |
|
191 | + throw new \OutOfBoundsException( |
|
192 | + "Structure doesn't have element at index ${idx}."); |
|
193 | + } |
|
194 | + $obj = clone $this; |
|
195 | + array_splice($obj->_elements, $idx, 1); |
|
196 | + return $obj; |
|
197 | + } |
|
198 | 198 | |
199 | - /** |
|
200 | - * Get elements in the structure. |
|
201 | - * |
|
202 | - * @return UnspecifiedType[] |
|
203 | - */ |
|
204 | - public function elements(): array |
|
205 | - { |
|
206 | - if (!isset($this->_unspecifiedTypes)) { |
|
207 | - $this->_unspecifiedTypes = array_map( |
|
208 | - function (Element $el) { |
|
209 | - return new UnspecifiedType($el); |
|
210 | - }, $this->_elements); |
|
211 | - } |
|
212 | - return $this->_unspecifiedTypes; |
|
213 | - } |
|
199 | + /** |
|
200 | + * Get elements in the structure. |
|
201 | + * |
|
202 | + * @return UnspecifiedType[] |
|
203 | + */ |
|
204 | + public function elements(): array |
|
205 | + { |
|
206 | + if (!isset($this->_unspecifiedTypes)) { |
|
207 | + $this->_unspecifiedTypes = array_map( |
|
208 | + function (Element $el) { |
|
209 | + return new UnspecifiedType($el); |
|
210 | + }, $this->_elements); |
|
211 | + } |
|
212 | + return $this->_unspecifiedTypes; |
|
213 | + } |
|
214 | 214 | |
215 | - /** |
|
216 | - * Check whether the structure has an element at the given index, optionally |
|
217 | - * satisfying given tag expectation. |
|
218 | - * |
|
219 | - * @param int $idx Index 0..n |
|
220 | - * @param null|int $expectedTag Optional type tag expectation |
|
221 | - * |
|
222 | - * @return bool |
|
223 | - */ |
|
224 | - public function has(int $idx, ?int $expectedTag = null): bool |
|
225 | - { |
|
226 | - if (!isset($this->_elements[$idx])) { |
|
227 | - return false; |
|
228 | - } |
|
229 | - if (isset($expectedTag)) { |
|
230 | - if (!$this->_elements[$idx]->isType($expectedTag)) { |
|
231 | - return false; |
|
232 | - } |
|
233 | - } |
|
234 | - return true; |
|
235 | - } |
|
215 | + /** |
|
216 | + * Check whether the structure has an element at the given index, optionally |
|
217 | + * satisfying given tag expectation. |
|
218 | + * |
|
219 | + * @param int $idx Index 0..n |
|
220 | + * @param null|int $expectedTag Optional type tag expectation |
|
221 | + * |
|
222 | + * @return bool |
|
223 | + */ |
|
224 | + public function has(int $idx, ?int $expectedTag = null): bool |
|
225 | + { |
|
226 | + if (!isset($this->_elements[$idx])) { |
|
227 | + return false; |
|
228 | + } |
|
229 | + if (isset($expectedTag)) { |
|
230 | + if (!$this->_elements[$idx]->isType($expectedTag)) { |
|
231 | + return false; |
|
232 | + } |
|
233 | + } |
|
234 | + return true; |
|
235 | + } |
|
236 | 236 | |
237 | - /** |
|
238 | - * Get the element at the given index, optionally checking that the element |
|
239 | - * has a given tag. |
|
240 | - * |
|
241 | - * <strong>NOTE!</strong> Expectation checking is deprecated and should be |
|
242 | - * done with <code>UnspecifiedType</code>. |
|
243 | - * |
|
244 | - * @todo Remove |
|
245 | - * |
|
246 | - * @param int $idx Index 0..n |
|
247 | - * @param null|int $expectedTag Optional type tag expectation |
|
248 | - * |
|
249 | - * @throws \OutOfBoundsException If element doesn't exists |
|
250 | - * @throws \UnexpectedValueException If expectation fails |
|
251 | - * |
|
252 | - * @return UnspecifiedType |
|
253 | - */ |
|
254 | - public function at(int $idx, ?int $expectedTag = null): UnspecifiedType |
|
255 | - { |
|
256 | - if (!isset($this->_elements[$idx])) { |
|
257 | - throw new \OutOfBoundsException( |
|
258 | - "Structure doesn't have an element at index ${idx}."); |
|
259 | - } |
|
260 | - $element = $this->_elements[$idx]; |
|
261 | - if (isset($expectedTag)) { |
|
262 | - $element->expectType($expectedTag); |
|
263 | - } |
|
264 | - return new UnspecifiedType($element); |
|
265 | - } |
|
237 | + /** |
|
238 | + * Get the element at the given index, optionally checking that the element |
|
239 | + * has a given tag. |
|
240 | + * |
|
241 | + * <strong>NOTE!</strong> Expectation checking is deprecated and should be |
|
242 | + * done with <code>UnspecifiedType</code>. |
|
243 | + * |
|
244 | + * @todo Remove |
|
245 | + * |
|
246 | + * @param int $idx Index 0..n |
|
247 | + * @param null|int $expectedTag Optional type tag expectation |
|
248 | + * |
|
249 | + * @throws \OutOfBoundsException If element doesn't exists |
|
250 | + * @throws \UnexpectedValueException If expectation fails |
|
251 | + * |
|
252 | + * @return UnspecifiedType |
|
253 | + */ |
|
254 | + public function at(int $idx, ?int $expectedTag = null): UnspecifiedType |
|
255 | + { |
|
256 | + if (!isset($this->_elements[$idx])) { |
|
257 | + throw new \OutOfBoundsException( |
|
258 | + "Structure doesn't have an element at index ${idx}."); |
|
259 | + } |
|
260 | + $element = $this->_elements[$idx]; |
|
261 | + if (isset($expectedTag)) { |
|
262 | + $element->expectType($expectedTag); |
|
263 | + } |
|
264 | + return new UnspecifiedType($element); |
|
265 | + } |
|
266 | 266 | |
267 | - /** |
|
268 | - * Check whether the structure contains a context specific element with a |
|
269 | - * given tag. |
|
270 | - * |
|
271 | - * @param int $tag Tag number |
|
272 | - * |
|
273 | - * @return bool |
|
274 | - */ |
|
275 | - public function hasTagged(int $tag): bool |
|
276 | - { |
|
277 | - // lazily build lookup map |
|
278 | - if (!isset($this->_taggedMap)) { |
|
279 | - $this->_taggedMap = []; |
|
280 | - foreach ($this->_elements as $element) { |
|
281 | - if ($element->isTagged()) { |
|
282 | - $this->_taggedMap[$element->tag()] = $element; |
|
283 | - } |
|
284 | - } |
|
285 | - } |
|
286 | - return isset($this->_taggedMap[$tag]); |
|
287 | - } |
|
267 | + /** |
|
268 | + * Check whether the structure contains a context specific element with a |
|
269 | + * given tag. |
|
270 | + * |
|
271 | + * @param int $tag Tag number |
|
272 | + * |
|
273 | + * @return bool |
|
274 | + */ |
|
275 | + public function hasTagged(int $tag): bool |
|
276 | + { |
|
277 | + // lazily build lookup map |
|
278 | + if (!isset($this->_taggedMap)) { |
|
279 | + $this->_taggedMap = []; |
|
280 | + foreach ($this->_elements as $element) { |
|
281 | + if ($element->isTagged()) { |
|
282 | + $this->_taggedMap[$element->tag()] = $element; |
|
283 | + } |
|
284 | + } |
|
285 | + } |
|
286 | + return isset($this->_taggedMap[$tag]); |
|
287 | + } |
|
288 | 288 | |
289 | - /** |
|
290 | - * Get a context specific element tagged with a given tag. |
|
291 | - * |
|
292 | - * @param int $tag |
|
293 | - * |
|
294 | - * @throws \LogicException If tag doesn't exists |
|
295 | - * |
|
296 | - * @return TaggedType |
|
297 | - */ |
|
298 | - public function getTagged(int $tag): TaggedType |
|
299 | - { |
|
300 | - if (!$this->hasTagged($tag)) { |
|
301 | - throw new \LogicException("No tagged element for tag ${tag}."); |
|
302 | - } |
|
303 | - return $this->_taggedMap[$tag]; |
|
304 | - } |
|
289 | + /** |
|
290 | + * Get a context specific element tagged with a given tag. |
|
291 | + * |
|
292 | + * @param int $tag |
|
293 | + * |
|
294 | + * @throws \LogicException If tag doesn't exists |
|
295 | + * |
|
296 | + * @return TaggedType |
|
297 | + */ |
|
298 | + public function getTagged(int $tag): TaggedType |
|
299 | + { |
|
300 | + if (!$this->hasTagged($tag)) { |
|
301 | + throw new \LogicException("No tagged element for tag ${tag}."); |
|
302 | + } |
|
303 | + return $this->_taggedMap[$tag]; |
|
304 | + } |
|
305 | 305 | |
306 | - /** |
|
307 | - * @see \Countable::count() |
|
308 | - * |
|
309 | - * @return int |
|
310 | - */ |
|
311 | - public function count(): int |
|
312 | - { |
|
313 | - return count($this->_elements); |
|
314 | - } |
|
306 | + /** |
|
307 | + * @see \Countable::count() |
|
308 | + * |
|
309 | + * @return int |
|
310 | + */ |
|
311 | + public function count(): int |
|
312 | + { |
|
313 | + return count($this->_elements); |
|
314 | + } |
|
315 | 315 | |
316 | - /** |
|
317 | - * Get an iterator for the UnspecifiedElement objects. |
|
318 | - * |
|
319 | - * @see \IteratorAggregate::getIterator() |
|
320 | - * |
|
321 | - * @return \ArrayIterator |
|
322 | - */ |
|
323 | - public function getIterator(): \ArrayIterator |
|
324 | - { |
|
325 | - return new \ArrayIterator($this->elements()); |
|
326 | - } |
|
316 | + /** |
|
317 | + * Get an iterator for the UnspecifiedElement objects. |
|
318 | + * |
|
319 | + * @see \IteratorAggregate::getIterator() |
|
320 | + * |
|
321 | + * @return \ArrayIterator |
|
322 | + */ |
|
323 | + public function getIterator(): \ArrayIterator |
|
324 | + { |
|
325 | + return new \ArrayIterator($this->elements()); |
|
326 | + } |
|
327 | 327 | |
328 | - /** |
|
329 | - * @see \Sop\ASN1\Element::_encodedContentDER() |
|
330 | - * |
|
331 | - * @return string |
|
332 | - */ |
|
333 | - protected function _encodedContentDER(): string |
|
334 | - { |
|
335 | - $data = ''; |
|
336 | - foreach ($this->_elements as $element) { |
|
337 | - $data .= $element->toDER(); |
|
338 | - } |
|
339 | - return $data; |
|
340 | - } |
|
328 | + /** |
|
329 | + * @see \Sop\ASN1\Element::_encodedContentDER() |
|
330 | + * |
|
331 | + * @return string |
|
332 | + */ |
|
333 | + protected function _encodedContentDER(): string |
|
334 | + { |
|
335 | + $data = ''; |
|
336 | + foreach ($this->_elements as $element) { |
|
337 | + $data .= $element->toDER(); |
|
338 | + } |
|
339 | + return $data; |
|
340 | + } |
|
341 | 341 | |
342 | - /** |
|
343 | - * {@inheritdoc} |
|
344 | - * |
|
345 | - * @see \Sop\ASN1\Element::_decodeFromDER() |
|
346 | - * |
|
347 | - * @return self |
|
348 | - */ |
|
349 | - protected static function _decodeFromDER(Identifier $identifier, |
|
350 | - string $data, int &$offset): ElementBase |
|
351 | - { |
|
352 | - if (!$identifier->isConstructed()) { |
|
353 | - throw new DecodeException( |
|
354 | - 'Structured element must have constructed bit set.'); |
|
355 | - } |
|
356 | - $idx = $offset; |
|
357 | - $length = Length::expectFromDER($data, $idx); |
|
358 | - if ($length->isIndefinite()) { |
|
359 | - $type = self::_decodeIndefiniteLength($data, $idx); |
|
360 | - } else { |
|
361 | - $type = self::_decodeDefiniteLength($data, $idx, |
|
362 | - $length->intLength()); |
|
363 | - } |
|
364 | - $offset = $idx; |
|
365 | - return $type; |
|
366 | - } |
|
342 | + /** |
|
343 | + * {@inheritdoc} |
|
344 | + * |
|
345 | + * @see \Sop\ASN1\Element::_decodeFromDER() |
|
346 | + * |
|
347 | + * @return self |
|
348 | + */ |
|
349 | + protected static function _decodeFromDER(Identifier $identifier, |
|
350 | + string $data, int &$offset): ElementBase |
|
351 | + { |
|
352 | + if (!$identifier->isConstructed()) { |
|
353 | + throw new DecodeException( |
|
354 | + 'Structured element must have constructed bit set.'); |
|
355 | + } |
|
356 | + $idx = $offset; |
|
357 | + $length = Length::expectFromDER($data, $idx); |
|
358 | + if ($length->isIndefinite()) { |
|
359 | + $type = self::_decodeIndefiniteLength($data, $idx); |
|
360 | + } else { |
|
361 | + $type = self::_decodeDefiniteLength($data, $idx, |
|
362 | + $length->intLength()); |
|
363 | + } |
|
364 | + $offset = $idx; |
|
365 | + return $type; |
|
366 | + } |
|
367 | 367 | |
368 | - /** |
|
369 | - * Decode elements for a definite length. |
|
370 | - * |
|
371 | - * @param string $data DER data |
|
372 | - * @param int $offset Offset to data |
|
373 | - * @param int $length Number of bytes to decode |
|
374 | - * |
|
375 | - * @throws DecodeException |
|
376 | - * |
|
377 | - * @return ElementBase |
|
378 | - */ |
|
379 | - private static function _decodeDefiniteLength(string $data, int &$offset, |
|
380 | - int $length): ElementBase |
|
381 | - { |
|
382 | - $idx = $offset; |
|
383 | - $end = $idx + $length; |
|
384 | - $elements = []; |
|
385 | - while ($idx < $end) { |
|
386 | - $elements[] = Element::fromDER($data, $idx); |
|
387 | - // check that element didn't overflow length |
|
388 | - if ($idx > $end) { |
|
389 | - throw new DecodeException( |
|
390 | - "Structure's content overflows length."); |
|
391 | - } |
|
392 | - } |
|
393 | - $offset = $idx; |
|
394 | - // return instance by static late binding |
|
395 | - return new static(...$elements); |
|
396 | - } |
|
368 | + /** |
|
369 | + * Decode elements for a definite length. |
|
370 | + * |
|
371 | + * @param string $data DER data |
|
372 | + * @param int $offset Offset to data |
|
373 | + * @param int $length Number of bytes to decode |
|
374 | + * |
|
375 | + * @throws DecodeException |
|
376 | + * |
|
377 | + * @return ElementBase |
|
378 | + */ |
|
379 | + private static function _decodeDefiniteLength(string $data, int &$offset, |
|
380 | + int $length): ElementBase |
|
381 | + { |
|
382 | + $idx = $offset; |
|
383 | + $end = $idx + $length; |
|
384 | + $elements = []; |
|
385 | + while ($idx < $end) { |
|
386 | + $elements[] = Element::fromDER($data, $idx); |
|
387 | + // check that element didn't overflow length |
|
388 | + if ($idx > $end) { |
|
389 | + throw new DecodeException( |
|
390 | + "Structure's content overflows length."); |
|
391 | + } |
|
392 | + } |
|
393 | + $offset = $idx; |
|
394 | + // return instance by static late binding |
|
395 | + return new static(...$elements); |
|
396 | + } |
|
397 | 397 | |
398 | - /** |
|
399 | - * Decode elements for an indefinite length. |
|
400 | - * |
|
401 | - * @param string $data DER data |
|
402 | - * @param int $offset Offset to data |
|
403 | - * |
|
404 | - * @throws DecodeException |
|
405 | - * |
|
406 | - * @return ElementBase |
|
407 | - */ |
|
408 | - private static function _decodeIndefiniteLength( |
|
409 | - string $data, int &$offset): ElementBase |
|
410 | - { |
|
411 | - $idx = $offset; |
|
412 | - $elements = []; |
|
413 | - $end = strlen($data); |
|
414 | - while (true) { |
|
415 | - if ($idx >= $end) { |
|
416 | - throw new DecodeException( |
|
417 | - 'Unexpected end of data while decoding indefinite length structure.'); |
|
418 | - } |
|
419 | - $el = Element::fromDER($data, $idx); |
|
420 | - if ($el->isType(self::TYPE_EOC)) { |
|
421 | - break; |
|
422 | - } |
|
423 | - $elements[] = $el; |
|
424 | - } |
|
425 | - $offset = $idx; |
|
426 | - $type = new static(...$elements); |
|
427 | - $type->_indefiniteLength = true; |
|
428 | - return $type; |
|
429 | - } |
|
398 | + /** |
|
399 | + * Decode elements for an indefinite length. |
|
400 | + * |
|
401 | + * @param string $data DER data |
|
402 | + * @param int $offset Offset to data |
|
403 | + * |
|
404 | + * @throws DecodeException |
|
405 | + * |
|
406 | + * @return ElementBase |
|
407 | + */ |
|
408 | + private static function _decodeIndefiniteLength( |
|
409 | + string $data, int &$offset): ElementBase |
|
410 | + { |
|
411 | + $idx = $offset; |
|
412 | + $elements = []; |
|
413 | + $end = strlen($data); |
|
414 | + while (true) { |
|
415 | + if ($idx >= $end) { |
|
416 | + throw new DecodeException( |
|
417 | + 'Unexpected end of data while decoding indefinite length structure.'); |
|
418 | + } |
|
419 | + $el = Element::fromDER($data, $idx); |
|
420 | + if ($el->isType(self::TYPE_EOC)) { |
|
421 | + break; |
|
422 | + } |
|
423 | + $elements[] = $el; |
|
424 | + } |
|
425 | + $offset = $idx; |
|
426 | + $type = new static(...$elements); |
|
427 | + $type->_indefiniteLength = true; |
|
428 | + return $type; |
|
429 | + } |
|
430 | 430 | } |
@@ -11,93 +11,93 @@ |
||
11 | 11 | */ |
12 | 12 | abstract class TimeType extends Element |
13 | 13 | { |
14 | - /** |
|
15 | - * UTC timezone. |
|
16 | - * |
|
17 | - * @var string |
|
18 | - */ |
|
19 | - const TZ_UTC = 'UTC'; |
|
14 | + /** |
|
15 | + * UTC timezone. |
|
16 | + * |
|
17 | + * @var string |
|
18 | + */ |
|
19 | + const TZ_UTC = 'UTC'; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Date and time. |
|
23 | - * |
|
24 | - * @var \DateTimeImmutable |
|
25 | - */ |
|
26 | - protected $_dateTime; |
|
21 | + /** |
|
22 | + * Date and time. |
|
23 | + * |
|
24 | + * @var \DateTimeImmutable |
|
25 | + */ |
|
26 | + protected $_dateTime; |
|
27 | 27 | |
28 | - /** |
|
29 | - * Constructor. |
|
30 | - * |
|
31 | - * @param \DateTimeImmutable $dt |
|
32 | - */ |
|
33 | - public function __construct(\DateTimeImmutable $dt) |
|
34 | - { |
|
35 | - $this->_dateTime = $dt; |
|
36 | - } |
|
28 | + /** |
|
29 | + * Constructor. |
|
30 | + * |
|
31 | + * @param \DateTimeImmutable $dt |
|
32 | + */ |
|
33 | + public function __construct(\DateTimeImmutable $dt) |
|
34 | + { |
|
35 | + $this->_dateTime = $dt; |
|
36 | + } |
|
37 | 37 | |
38 | - /** |
|
39 | - * Initialize from datetime string. |
|
40 | - * |
|
41 | - * @see http://php.net/manual/en/datetime.formats.php |
|
42 | - * |
|
43 | - * @param string $time Time string |
|
44 | - * @param null|string $tz timezone, if null use default |
|
45 | - * |
|
46 | - * @throws \RuntimeException |
|
47 | - * |
|
48 | - * @return self |
|
49 | - */ |
|
50 | - public static function fromString(string $time, ?string $tz = null): self |
|
51 | - { |
|
52 | - try { |
|
53 | - if (!isset($tz)) { |
|
54 | - $tz = date_default_timezone_get(); |
|
55 | - } |
|
56 | - return new static( |
|
57 | - new \DateTimeImmutable($time, self::_createTimeZone($tz))); |
|
58 | - } catch (\Exception $e) { |
|
59 | - throw new \RuntimeException( |
|
60 | - 'Failed to create DateTime: ' . |
|
61 | - self::_getLastDateTimeImmutableErrorsStr(), 0, $e); |
|
62 | - } |
|
63 | - } |
|
38 | + /** |
|
39 | + * Initialize from datetime string. |
|
40 | + * |
|
41 | + * @see http://php.net/manual/en/datetime.formats.php |
|
42 | + * |
|
43 | + * @param string $time Time string |
|
44 | + * @param null|string $tz timezone, if null use default |
|
45 | + * |
|
46 | + * @throws \RuntimeException |
|
47 | + * |
|
48 | + * @return self |
|
49 | + */ |
|
50 | + public static function fromString(string $time, ?string $tz = null): self |
|
51 | + { |
|
52 | + try { |
|
53 | + if (!isset($tz)) { |
|
54 | + $tz = date_default_timezone_get(); |
|
55 | + } |
|
56 | + return new static( |
|
57 | + new \DateTimeImmutable($time, self::_createTimeZone($tz))); |
|
58 | + } catch (\Exception $e) { |
|
59 | + throw new \RuntimeException( |
|
60 | + 'Failed to create DateTime: ' . |
|
61 | + self::_getLastDateTimeImmutableErrorsStr(), 0, $e); |
|
62 | + } |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * Get the date and time. |
|
67 | - * |
|
68 | - * @return \DateTimeImmutable |
|
69 | - */ |
|
70 | - public function dateTime(): \DateTimeImmutable |
|
71 | - { |
|
72 | - return $this->_dateTime; |
|
73 | - } |
|
65 | + /** |
|
66 | + * Get the date and time. |
|
67 | + * |
|
68 | + * @return \DateTimeImmutable |
|
69 | + */ |
|
70 | + public function dateTime(): \DateTimeImmutable |
|
71 | + { |
|
72 | + return $this->_dateTime; |
|
73 | + } |
|
74 | 74 | |
75 | - /** |
|
76 | - * Create DateTimeZone object from string. |
|
77 | - * |
|
78 | - * @param string $tz |
|
79 | - * |
|
80 | - * @throws \UnexpectedValueException If timezone is invalid |
|
81 | - * |
|
82 | - * @return \DateTimeZone |
|
83 | - */ |
|
84 | - protected static function _createTimeZone(string $tz): \DateTimeZone |
|
85 | - { |
|
86 | - try { |
|
87 | - return new \DateTimeZone($tz); |
|
88 | - } catch (\Exception $e) { |
|
89 | - throw new \UnexpectedValueException('Invalid timezone.', 0, $e); |
|
90 | - } |
|
91 | - } |
|
75 | + /** |
|
76 | + * Create DateTimeZone object from string. |
|
77 | + * |
|
78 | + * @param string $tz |
|
79 | + * |
|
80 | + * @throws \UnexpectedValueException If timezone is invalid |
|
81 | + * |
|
82 | + * @return \DateTimeZone |
|
83 | + */ |
|
84 | + protected static function _createTimeZone(string $tz): \DateTimeZone |
|
85 | + { |
|
86 | + try { |
|
87 | + return new \DateTimeZone($tz); |
|
88 | + } catch (\Exception $e) { |
|
89 | + throw new \UnexpectedValueException('Invalid timezone.', 0, $e); |
|
90 | + } |
|
91 | + } |
|
92 | 92 | |
93 | - /** |
|
94 | - * Get last error caused by DateTimeImmutable. |
|
95 | - * |
|
96 | - * @return string |
|
97 | - */ |
|
98 | - protected static function _getLastDateTimeImmutableErrorsStr(): string |
|
99 | - { |
|
100 | - $errors = \DateTimeImmutable::getLastErrors()['errors']; |
|
101 | - return implode(', ', $errors); |
|
102 | - } |
|
93 | + /** |
|
94 | + * Get last error caused by DateTimeImmutable. |
|
95 | + * |
|
96 | + * @return string |
|
97 | + */ |
|
98 | + protected static function _getLastDateTimeImmutableErrorsStr(): string |
|
99 | + { |
|
100 | + $errors = \DateTimeImmutable::getLastErrors()['errors']; |
|
101 | + return implode(', ', $errors); |
|
102 | + } |
|
103 | 103 | } |
@@ -16,46 +16,46 @@ |
||
16 | 16 | */ |
17 | 17 | class ExplicitlyTaggedType extends TaggedTypeWrap implements ExplicitTagging |
18 | 18 | { |
19 | - /** |
|
20 | - * Constructor. |
|
21 | - * |
|
22 | - * @param int $tag Tag number |
|
23 | - * @param Element $element Wrapped element |
|
24 | - * @param int $class Type class |
|
25 | - */ |
|
26 | - public function __construct(int $tag, Element $element, |
|
27 | - int $class = Identifier::CLASS_CONTEXT_SPECIFIC) |
|
28 | - { |
|
29 | - $this->_typeTag = $tag; |
|
30 | - $this->_element = $element; |
|
31 | - $this->_class = $class; |
|
32 | - } |
|
19 | + /** |
|
20 | + * Constructor. |
|
21 | + * |
|
22 | + * @param int $tag Tag number |
|
23 | + * @param Element $element Wrapped element |
|
24 | + * @param int $class Type class |
|
25 | + */ |
|
26 | + public function __construct(int $tag, Element $element, |
|
27 | + int $class = Identifier::CLASS_CONTEXT_SPECIFIC) |
|
28 | + { |
|
29 | + $this->_typeTag = $tag; |
|
30 | + $this->_element = $element; |
|
31 | + $this->_class = $class; |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * {@inheritdoc} |
|
36 | - */ |
|
37 | - public function isConstructed(): bool |
|
38 | - { |
|
39 | - return true; |
|
40 | - } |
|
34 | + /** |
|
35 | + * {@inheritdoc} |
|
36 | + */ |
|
37 | + public function isConstructed(): bool |
|
38 | + { |
|
39 | + return true; |
|
40 | + } |
|
41 | 41 | |
42 | - /** |
|
43 | - * {@inheritdoc} |
|
44 | - */ |
|
45 | - public function explicit(?int $expectedTag = null): UnspecifiedType |
|
46 | - { |
|
47 | - if (isset($expectedTag)) { |
|
48 | - $this->_element->expectType($expectedTag); |
|
49 | - } |
|
50 | - return $this->_element->asUnspecified(); |
|
51 | - } |
|
42 | + /** |
|
43 | + * {@inheritdoc} |
|
44 | + */ |
|
45 | + public function explicit(?int $expectedTag = null): UnspecifiedType |
|
46 | + { |
|
47 | + if (isset($expectedTag)) { |
|
48 | + $this->_element->expectType($expectedTag); |
|
49 | + } |
|
50 | + return $this->_element->asUnspecified(); |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * {@inheritdoc} |
|
55 | - */ |
|
56 | - protected function _encodedContentDER(): string |
|
57 | - { |
|
58 | - // get the full encoding of the wrapped element |
|
59 | - return $this->_element->toDER(); |
|
60 | - } |
|
53 | + /** |
|
54 | + * {@inheritdoc} |
|
55 | + */ |
|
56 | + protected function _encodedContentDER(): string |
|
57 | + { |
|
58 | + // get the full encoding of the wrapped element |
|
59 | + return $this->_element->toDER(); |
|
60 | + } |
|
61 | 61 | } |
@@ -13,15 +13,15 @@ |
||
13 | 13 | */ |
14 | 14 | interface ImplicitTagging extends ElementBase |
15 | 15 | { |
16 | - /** |
|
17 | - * Get implicitly tagged wrapped element. |
|
18 | - * |
|
19 | - * @param int $tag Tag of the element |
|
20 | - * @param int $class Expected type class of the element |
|
21 | - * |
|
22 | - * @throws \UnexpectedValueException If expectation fails |
|
23 | - * |
|
24 | - * @return \Sop\ASN1\Type\UnspecifiedType |
|
25 | - */ |
|
26 | - public function implicit(int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType; |
|
16 | + /** |
|
17 | + * Get implicitly tagged wrapped element. |
|
18 | + * |
|
19 | + * @param int $tag Tag of the element |
|
20 | + * @param int $class Expected type class of the element |
|
21 | + * |
|
22 | + * @throws \UnexpectedValueException If expectation fails |
|
23 | + * |
|
24 | + * @return \Sop\ASN1\Type\UnspecifiedType |
|
25 | + */ |
|
26 | + public function implicit(int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType; |
|
27 | 27 | } |
@@ -17,52 +17,52 @@ |
||
17 | 17 | */ |
18 | 18 | class ImplicitlyTaggedType extends TaggedTypeWrap implements ImplicitTagging |
19 | 19 | { |
20 | - /** |
|
21 | - * Constructor. |
|
22 | - * |
|
23 | - * @param int $tag Tag number |
|
24 | - * @param Element $element Wrapped element |
|
25 | - * @param int $class Type class |
|
26 | - */ |
|
27 | - public function __construct(int $tag, Element $element, |
|
28 | - int $class = Identifier::CLASS_CONTEXT_SPECIFIC) |
|
29 | - { |
|
30 | - $this->_typeTag = $tag; |
|
31 | - $this->_element = $element; |
|
32 | - $this->_class = $class; |
|
33 | - } |
|
20 | + /** |
|
21 | + * Constructor. |
|
22 | + * |
|
23 | + * @param int $tag Tag number |
|
24 | + * @param Element $element Wrapped element |
|
25 | + * @param int $class Type class |
|
26 | + */ |
|
27 | + public function __construct(int $tag, Element $element, |
|
28 | + int $class = Identifier::CLASS_CONTEXT_SPECIFIC) |
|
29 | + { |
|
30 | + $this->_typeTag = $tag; |
|
31 | + $this->_element = $element; |
|
32 | + $this->_class = $class; |
|
33 | + } |
|
34 | 34 | |
35 | - /** |
|
36 | - * {@inheritdoc} |
|
37 | - */ |
|
38 | - public function isConstructed(): bool |
|
39 | - { |
|
40 | - // depends on the underlying type |
|
41 | - return $this->_element->isConstructed(); |
|
42 | - } |
|
35 | + /** |
|
36 | + * {@inheritdoc} |
|
37 | + */ |
|
38 | + public function isConstructed(): bool |
|
39 | + { |
|
40 | + // depends on the underlying type |
|
41 | + return $this->_element->isConstructed(); |
|
42 | + } |
|
43 | 43 | |
44 | - /** |
|
45 | - * {@inheritdoc} |
|
46 | - */ |
|
47 | - public function implicit( |
|
48 | - int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
49 | - { |
|
50 | - $this->_element->expectType($tag); |
|
51 | - if ($this->_element->typeClass() != $class) { |
|
52 | - throw new \UnexpectedValueException( |
|
53 | - sprintf('Type class %s expected, got %s.', |
|
54 | - Identifier::classToName($class), |
|
55 | - Identifier::classToName($this->_element->typeClass()))); |
|
56 | - } |
|
57 | - return $this->_element->asUnspecified(); |
|
58 | - } |
|
44 | + /** |
|
45 | + * {@inheritdoc} |
|
46 | + */ |
|
47 | + public function implicit( |
|
48 | + int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
49 | + { |
|
50 | + $this->_element->expectType($tag); |
|
51 | + if ($this->_element->typeClass() != $class) { |
|
52 | + throw new \UnexpectedValueException( |
|
53 | + sprintf('Type class %s expected, got %s.', |
|
54 | + Identifier::classToName($class), |
|
55 | + Identifier::classToName($this->_element->typeClass()))); |
|
56 | + } |
|
57 | + return $this->_element->asUnspecified(); |
|
58 | + } |
|
59 | 59 | |
60 | - /** |
|
61 | - * {@inheritdoc} |
|
62 | - */ |
|
63 | - protected function _encodedContentDER(): string |
|
64 | - { |
|
65 | - // get only the content of the wrapped element. |
|
66 | - return $this->_element->_encodedContentDER(); |
|
67 | - } |
|
60 | + /** |
|
61 | + * {@inheritdoc} |
|
62 | + */ |
|
63 | + protected function _encodedContentDER(): string |
|
64 | + { |
|
65 | + // get only the content of the wrapped element. |
|
66 | + return $this->_element->_encodedContentDER(); |
|
67 | + } |
|
68 | 68 | } |
@@ -23,139 +23,139 @@ |
||
23 | 23 | */ |
24 | 24 | class DERTaggedType extends TaggedType implements ExplicitTagging, ImplicitTagging |
25 | 25 | { |
26 | - /** |
|
27 | - * Identifier. |
|
28 | - * |
|
29 | - * @var Identifier |
|
30 | - */ |
|
31 | - private $_identifier; |
|
26 | + /** |
|
27 | + * Identifier. |
|
28 | + * |
|
29 | + * @var Identifier |
|
30 | + */ |
|
31 | + private $_identifier; |
|
32 | 32 | |
33 | - /** |
|
34 | - * DER data. |
|
35 | - * |
|
36 | - * @var string |
|
37 | - */ |
|
38 | - private $_data; |
|
33 | + /** |
|
34 | + * DER data. |
|
35 | + * |
|
36 | + * @var string |
|
37 | + */ |
|
38 | + private $_data; |
|
39 | 39 | |
40 | - /** |
|
41 | - * Offset to next byte after identifier. |
|
42 | - * |
|
43 | - * @var int |
|
44 | - */ |
|
45 | - private $_offset; |
|
40 | + /** |
|
41 | + * Offset to next byte after identifier. |
|
42 | + * |
|
43 | + * @var int |
|
44 | + */ |
|
45 | + private $_offset; |
|
46 | 46 | |
47 | - /** |
|
48 | - * Offset to content. |
|
49 | - * |
|
50 | - * @var int |
|
51 | - */ |
|
52 | - private $_valueOffset; |
|
47 | + /** |
|
48 | + * Offset to content. |
|
49 | + * |
|
50 | + * @var int |
|
51 | + */ |
|
52 | + private $_valueOffset; |
|
53 | 53 | |
54 | - /** |
|
55 | - * Length of the content. |
|
56 | - * |
|
57 | - * @var int |
|
58 | - */ |
|
59 | - private $_valueLength; |
|
54 | + /** |
|
55 | + * Length of the content. |
|
56 | + * |
|
57 | + * @var int |
|
58 | + */ |
|
59 | + private $_valueLength; |
|
60 | 60 | |
61 | - /** |
|
62 | - * Constructor. |
|
63 | - * |
|
64 | - * @param Identifier $identifier Pre-parsed identifier |
|
65 | - * @param string $data DER data |
|
66 | - * @param int $offset Offset to next byte after identifier |
|
67 | - * @param int $value_offset Offset to content |
|
68 | - * @param int $value_length Content length |
|
69 | - */ |
|
70 | - public function __construct(Identifier $identifier, string $data, |
|
71 | - int $offset, int $value_offset, int $value_length, |
|
72 | - bool $indefinite_length) |
|
73 | - { |
|
74 | - $this->_identifier = $identifier; |
|
75 | - $this->_data = $data; |
|
76 | - $this->_offset = $offset; |
|
77 | - $this->_valueOffset = $value_offset; |
|
78 | - $this->_valueLength = $value_length; |
|
79 | - $this->_indefiniteLength = $indefinite_length; |
|
80 | - $this->_typeTag = $identifier->intTag(); |
|
81 | - } |
|
61 | + /** |
|
62 | + * Constructor. |
|
63 | + * |
|
64 | + * @param Identifier $identifier Pre-parsed identifier |
|
65 | + * @param string $data DER data |
|
66 | + * @param int $offset Offset to next byte after identifier |
|
67 | + * @param int $value_offset Offset to content |
|
68 | + * @param int $value_length Content length |
|
69 | + */ |
|
70 | + public function __construct(Identifier $identifier, string $data, |
|
71 | + int $offset, int $value_offset, int $value_length, |
|
72 | + bool $indefinite_length) |
|
73 | + { |
|
74 | + $this->_identifier = $identifier; |
|
75 | + $this->_data = $data; |
|
76 | + $this->_offset = $offset; |
|
77 | + $this->_valueOffset = $value_offset; |
|
78 | + $this->_valueLength = $value_length; |
|
79 | + $this->_indefiniteLength = $indefinite_length; |
|
80 | + $this->_typeTag = $identifier->intTag(); |
|
81 | + } |
|
82 | 82 | |
83 | - /** |
|
84 | - * {@inheritdoc} |
|
85 | - */ |
|
86 | - public function typeClass(): int |
|
87 | - { |
|
88 | - return $this->_identifier->typeClass(); |
|
89 | - } |
|
83 | + /** |
|
84 | + * {@inheritdoc} |
|
85 | + */ |
|
86 | + public function typeClass(): int |
|
87 | + { |
|
88 | + return $this->_identifier->typeClass(); |
|
89 | + } |
|
90 | 90 | |
91 | - /** |
|
92 | - * {@inheritdoc} |
|
93 | - */ |
|
94 | - public function isConstructed(): bool |
|
95 | - { |
|
96 | - return $this->_identifier->isConstructed(); |
|
97 | - } |
|
91 | + /** |
|
92 | + * {@inheritdoc} |
|
93 | + */ |
|
94 | + public function isConstructed(): bool |
|
95 | + { |
|
96 | + return $this->_identifier->isConstructed(); |
|
97 | + } |
|
98 | 98 | |
99 | - /** |
|
100 | - * {@inheritdoc} |
|
101 | - */ |
|
102 | - public function implicit(int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
103 | - { |
|
104 | - $identifier = $this->_identifier->withClass($class)->withTag($tag); |
|
105 | - $cls = self::_determineImplClass($identifier); |
|
106 | - $idx = $this->_offset; |
|
107 | - /** @var \Sop\ASN1\Feature\ElementBase $element */ |
|
108 | - $element = $cls::_decodeFromDER($identifier, $this->_data, $idx); |
|
109 | - return $element->asUnspecified(); |
|
110 | - } |
|
99 | + /** |
|
100 | + * {@inheritdoc} |
|
101 | + */ |
|
102 | + public function implicit(int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
103 | + { |
|
104 | + $identifier = $this->_identifier->withClass($class)->withTag($tag); |
|
105 | + $cls = self::_determineImplClass($identifier); |
|
106 | + $idx = $this->_offset; |
|
107 | + /** @var \Sop\ASN1\Feature\ElementBase $element */ |
|
108 | + $element = $cls::_decodeFromDER($identifier, $this->_data, $idx); |
|
109 | + return $element->asUnspecified(); |
|
110 | + } |
|
111 | 111 | |
112 | - /** |
|
113 | - * {@inheritdoc} |
|
114 | - */ |
|
115 | - public function explicit(?int $expectedTag = null): UnspecifiedType |
|
116 | - { |
|
117 | - $idx = $this->_valueOffset; |
|
118 | - $element = Element::fromDER($this->_data, $idx); |
|
119 | - if (isset($expectedTag)) { |
|
120 | - $element->expectType($expectedTag); |
|
121 | - } |
|
122 | - return $element->asUnspecified(); |
|
123 | - } |
|
112 | + /** |
|
113 | + * {@inheritdoc} |
|
114 | + */ |
|
115 | + public function explicit(?int $expectedTag = null): UnspecifiedType |
|
116 | + { |
|
117 | + $idx = $this->_valueOffset; |
|
118 | + $element = Element::fromDER($this->_data, $idx); |
|
119 | + if (isset($expectedTag)) { |
|
120 | + $element->expectType($expectedTag); |
|
121 | + } |
|
122 | + return $element->asUnspecified(); |
|
123 | + } |
|
124 | 124 | |
125 | - /** |
|
126 | - * {@inheritdoc} |
|
127 | - */ |
|
128 | - protected static function _decodeFromDER(Identifier $identifier, |
|
129 | - string $data, int &$offset): ElementBase |
|
130 | - { |
|
131 | - $idx = $offset; |
|
132 | - $length = Length::expectFromDER($data, $idx); |
|
133 | - // offset to inner value |
|
134 | - $value_offset = $idx; |
|
135 | - if ($length->isIndefinite()) { |
|
136 | - if ($identifier->isPrimitive()) { |
|
137 | - throw new DecodeException( |
|
138 | - 'Primitive type with indefinite length is not supported.'); |
|
139 | - } |
|
140 | - while (!Element::fromDER($data, $idx)->isType(self::TYPE_EOC)); |
|
141 | - // EOC consists of two octets. |
|
142 | - $value_length = $idx - $value_offset - 2; |
|
143 | - } else { |
|
144 | - $value_length = $length->intLength(); |
|
145 | - $idx += $value_length; |
|
146 | - } |
|
147 | - // late static binding since ApplicationType and PrivateType extend this class |
|
148 | - $type = new static($identifier, $data, $offset, $value_offset, |
|
149 | - $value_length, $length->isIndefinite()); |
|
150 | - $offset = $idx; |
|
151 | - return $type; |
|
152 | - } |
|
125 | + /** |
|
126 | + * {@inheritdoc} |
|
127 | + */ |
|
128 | + protected static function _decodeFromDER(Identifier $identifier, |
|
129 | + string $data, int &$offset): ElementBase |
|
130 | + { |
|
131 | + $idx = $offset; |
|
132 | + $length = Length::expectFromDER($data, $idx); |
|
133 | + // offset to inner value |
|
134 | + $value_offset = $idx; |
|
135 | + if ($length->isIndefinite()) { |
|
136 | + if ($identifier->isPrimitive()) { |
|
137 | + throw new DecodeException( |
|
138 | + 'Primitive type with indefinite length is not supported.'); |
|
139 | + } |
|
140 | + while (!Element::fromDER($data, $idx)->isType(self::TYPE_EOC)); |
|
141 | + // EOC consists of two octets. |
|
142 | + $value_length = $idx - $value_offset - 2; |
|
143 | + } else { |
|
144 | + $value_length = $length->intLength(); |
|
145 | + $idx += $value_length; |
|
146 | + } |
|
147 | + // late static binding since ApplicationType and PrivateType extend this class |
|
148 | + $type = new static($identifier, $data, $offset, $value_offset, |
|
149 | + $value_length, $length->isIndefinite()); |
|
150 | + $offset = $idx; |
|
151 | + return $type; |
|
152 | + } |
|
153 | 153 | |
154 | - /** |
|
155 | - * {@inheritdoc} |
|
156 | - */ |
|
157 | - protected function _encodedContentDER(): string |
|
158 | - { |
|
159 | - return substr($this->_data, $this->_valueOffset, $this->_valueLength); |
|
160 | - } |
|
154 | + /** |
|
155 | + * {@inheritdoc} |
|
156 | + */ |
|
157 | + protected function _encodedContentDER(): string |
|
158 | + { |
|
159 | + return substr($this->_data, $this->_valueOffset, $this->_valueLength); |
|
160 | + } |
|
161 | 161 | } |
@@ -12,19 +12,19 @@ |
||
12 | 12 | */ |
13 | 13 | interface ExplicitTagging extends ElementBase |
14 | 14 | { |
15 | - /** |
|
16 | - * Get explicitly tagged wrapped element. |
|
17 | - * |
|
18 | - * <strong>NOTE!</strong> Expectation checking is deprecated and shall be |
|
19 | - * done with <code>UnspecifiedType</code>. |
|
20 | - * |
|
21 | - * @todo Remove expectation checking |
|
22 | - * |
|
23 | - * @param null|int $expectedTag Expected tag of the underlying type |
|
24 | - * |
|
25 | - * @throws \UnexpectedValueException If expectation fails |
|
26 | - * |
|
27 | - * @return \Sop\ASN1\Type\UnspecifiedType |
|
28 | - */ |
|
29 | - public function explicit(?int $expectedTag = null): UnspecifiedType; |
|
15 | + /** |
|
16 | + * Get explicitly tagged wrapped element. |
|
17 | + * |
|
18 | + * <strong>NOTE!</strong> Expectation checking is deprecated and shall be |
|
19 | + * done with <code>UnspecifiedType</code>. |
|
20 | + * |
|
21 | + * @todo Remove expectation checking |
|
22 | + * |
|
23 | + * @param null|int $expectedTag Expected tag of the underlying type |
|
24 | + * |
|
25 | + * @throws \UnexpectedValueException If expectation fails |
|
26 | + * |
|
27 | + * @return \Sop\ASN1\Type\UnspecifiedType |
|
28 | + */ |
|
29 | + public function explicit(?int $expectedTag = null): UnspecifiedType; |
|
30 | 30 | } |
@@ -11,27 +11,27 @@ |
||
11 | 11 | */ |
12 | 12 | abstract class TaggedTypeWrap extends TaggedType |
13 | 13 | { |
14 | - /** |
|
15 | - * Wrapped element. |
|
16 | - * |
|
17 | - * @var \Sop\ASN1\Element |
|
18 | - */ |
|
19 | - protected $_element; |
|
14 | + /** |
|
15 | + * Wrapped element. |
|
16 | + * |
|
17 | + * @var \Sop\ASN1\Element |
|
18 | + */ |
|
19 | + protected $_element; |
|
20 | 20 | |
21 | - /** |
|
22 | - * Type class. |
|
23 | - * |
|
24 | - * @var int |
|
25 | - */ |
|
26 | - protected $_class; |
|
21 | + /** |
|
22 | + * Type class. |
|
23 | + * |
|
24 | + * @var int |
|
25 | + */ |
|
26 | + protected $_class; |
|
27 | 27 | |
28 | - /** |
|
29 | - * @see \Sop\ASN1\Element::typeClass() |
|
30 | - * |
|
31 | - * @return int |
|
32 | - */ |
|
33 | - public function typeClass(): int |
|
34 | - { |
|
35 | - return $this->_class; |
|
36 | - } |
|
28 | + /** |
|
29 | + * @see \Sop\ASN1\Element::typeClass() |
|
30 | + * |
|
31 | + * @return int |
|
32 | + */ |
|
33 | + public function typeClass(): int |
|
34 | + { |
|
35 | + return $this->_class; |
|
36 | + } |
|
37 | 37 | } |
@@ -17,692 +17,692 @@ |
||
17 | 17 | */ |
18 | 18 | class UnspecifiedType implements ElementBase |
19 | 19 | { |
20 | - /** |
|
21 | - * The wrapped element. |
|
22 | - * |
|
23 | - * @var Element |
|
24 | - */ |
|
25 | - private $_element; |
|
26 | - |
|
27 | - /** |
|
28 | - * Constructor. |
|
29 | - * |
|
30 | - * @param Element $el |
|
31 | - */ |
|
32 | - public function __construct(Element $el) |
|
33 | - { |
|
34 | - $this->_element = $el; |
|
35 | - } |
|
36 | - |
|
37 | - /** |
|
38 | - * Compatibility method to dispatch calls to the wrapped element. |
|
39 | - * |
|
40 | - * @todo Remove |
|
41 | - * |
|
42 | - * @deprecated Use <code>as*</code> accessor methods to ensure strict type |
|
43 | - * |
|
44 | - * @param string $mtd Method name |
|
45 | - * @param array $args Arguments |
|
46 | - * |
|
47 | - * @return mixed |
|
48 | - */ |
|
49 | - public function __call($mtd, array $args) |
|
50 | - { |
|
51 | - return call_user_func_array([$this->_element, $mtd], $args); |
|
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * Initialize from DER data. |
|
56 | - * |
|
57 | - * @param string $data DER encoded data |
|
58 | - * |
|
59 | - * @return self |
|
60 | - */ |
|
61 | - public static function fromDER(string $data): self |
|
62 | - { |
|
63 | - return Element::fromDER($data)->asUnspecified(); |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * Initialize from ElementBase interface. |
|
68 | - * |
|
69 | - * @param ElementBase $el |
|
70 | - * |
|
71 | - * @return self |
|
72 | - */ |
|
73 | - public static function fromElementBase(ElementBase $el): self |
|
74 | - { |
|
75 | - // if element is already wrapped |
|
76 | - if ($el instanceof self) { |
|
77 | - return $el; |
|
78 | - } |
|
79 | - return new self($el->asElement()); |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * Get the wrapped element as a context specific tagged type. |
|
84 | - * |
|
85 | - * @throws \UnexpectedValueException If the element is not tagged |
|
86 | - * |
|
87 | - * @return TaggedType |
|
88 | - */ |
|
89 | - public function asTagged(): TaggedType |
|
90 | - { |
|
91 | - if (!$this->_element instanceof TaggedType) { |
|
92 | - throw new \UnexpectedValueException( |
|
93 | - 'Tagged element expected, got ' . $this->_typeDescriptorString()); |
|
94 | - } |
|
95 | - return $this->_element; |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * Get the wrapped element as an application specific type. |
|
100 | - * |
|
101 | - * @throws \UnexpectedValueException If element is not application specific |
|
102 | - * |
|
103 | - * @return \Sop\ASN1\Type\Tagged\ApplicationType |
|
104 | - */ |
|
105 | - public function asApplication(): Tagged\ApplicationType |
|
106 | - { |
|
107 | - if (!$this->_element instanceof Tagged\ApplicationType) { |
|
108 | - throw new \UnexpectedValueException( |
|
109 | - 'Application type expected, got ' . |
|
110 | - $this->_typeDescriptorString()); |
|
111 | - } |
|
112 | - return $this->_element; |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Get the wrapped element as a private tagged type. |
|
117 | - * |
|
118 | - * @throws \UnexpectedValueException If element is not using private tagging |
|
119 | - * |
|
120 | - * @return \Sop\ASN1\Type\Tagged\PrivateType |
|
121 | - */ |
|
122 | - public function asPrivate(): Tagged\PrivateType |
|
123 | - { |
|
124 | - if (!$this->_element instanceof Tagged\PrivateType) { |
|
125 | - throw new \UnexpectedValueException( |
|
126 | - 'Private type expected, got ' . $this->_typeDescriptorString()); |
|
127 | - } |
|
128 | - return $this->_element; |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * Get the wrapped element as a boolean type. |
|
133 | - * |
|
134 | - * @throws \UnexpectedValueException If the element is not a boolean |
|
135 | - * |
|
136 | - * @return \Sop\ASN1\Type\Primitive\Boolean |
|
137 | - */ |
|
138 | - public function asBoolean(): Primitive\Boolean |
|
139 | - { |
|
140 | - if (!$this->_element instanceof Primitive\Boolean) { |
|
141 | - throw new \UnexpectedValueException( |
|
142 | - $this->_generateExceptionMessage(Element::TYPE_BOOLEAN)); |
|
143 | - } |
|
144 | - return $this->_element; |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * Get the wrapped element as an integer type. |
|
149 | - * |
|
150 | - * @throws \UnexpectedValueException If the element is not an integer |
|
151 | - * |
|
152 | - * @return \Sop\ASN1\Type\Primitive\Integer |
|
153 | - */ |
|
154 | - public function asInteger(): Primitive\Integer |
|
155 | - { |
|
156 | - if (!$this->_element instanceof Primitive\Integer) { |
|
157 | - throw new \UnexpectedValueException( |
|
158 | - $this->_generateExceptionMessage(Element::TYPE_INTEGER)); |
|
159 | - } |
|
160 | - return $this->_element; |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * Get the wrapped element as a bit string type. |
|
165 | - * |
|
166 | - * @throws \UnexpectedValueException If the element is not a bit string |
|
167 | - * |
|
168 | - * @return \Sop\ASN1\Type\Primitive\BitString |
|
169 | - */ |
|
170 | - public function asBitString(): Primitive\BitString |
|
171 | - { |
|
172 | - if (!$this->_element instanceof Primitive\BitString) { |
|
173 | - throw new \UnexpectedValueException( |
|
174 | - $this->_generateExceptionMessage(Element::TYPE_BIT_STRING)); |
|
175 | - } |
|
176 | - return $this->_element; |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * Get the wrapped element as an octet string type. |
|
181 | - * |
|
182 | - * @throws \UnexpectedValueException If the element is not an octet string |
|
183 | - * |
|
184 | - * @return \Sop\ASN1\Type\Primitive\OctetString |
|
185 | - */ |
|
186 | - public function asOctetString(): Primitive\OctetString |
|
187 | - { |
|
188 | - if (!$this->_element instanceof Primitive\OctetString) { |
|
189 | - throw new \UnexpectedValueException( |
|
190 | - $this->_generateExceptionMessage(Element::TYPE_OCTET_STRING)); |
|
191 | - } |
|
192 | - return $this->_element; |
|
193 | - } |
|
194 | - |
|
195 | - /** |
|
196 | - * Get the wrapped element as a null type. |
|
197 | - * |
|
198 | - * @throws \UnexpectedValueException If the element is not a null |
|
199 | - * |
|
200 | - * @return \Sop\ASN1\Type\Primitive\NullType |
|
201 | - */ |
|
202 | - public function asNull(): Primitive\NullType |
|
203 | - { |
|
204 | - if (!$this->_element instanceof Primitive\NullType) { |
|
205 | - throw new \UnexpectedValueException( |
|
206 | - $this->_generateExceptionMessage(Element::TYPE_NULL)); |
|
207 | - } |
|
208 | - return $this->_element; |
|
209 | - } |
|
210 | - |
|
211 | - /** |
|
212 | - * Get the wrapped element as an object identifier type. |
|
213 | - * |
|
214 | - * @throws \UnexpectedValueException If the element is not an object |
|
215 | - * identifier |
|
216 | - * |
|
217 | - * @return \Sop\ASN1\Type\Primitive\ObjectIdentifier |
|
218 | - */ |
|
219 | - public function asObjectIdentifier(): Primitive\ObjectIdentifier |
|
220 | - { |
|
221 | - if (!$this->_element instanceof Primitive\ObjectIdentifier) { |
|
222 | - throw new \UnexpectedValueException( |
|
223 | - $this->_generateExceptionMessage( |
|
224 | - Element::TYPE_OBJECT_IDENTIFIER)); |
|
225 | - } |
|
226 | - return $this->_element; |
|
227 | - } |
|
228 | - |
|
229 | - /** |
|
230 | - * Get the wrapped element as an object descriptor type. |
|
231 | - * |
|
232 | - * @throws \UnexpectedValueException If the element is not an object |
|
233 | - * descriptor |
|
234 | - * |
|
235 | - * @return \Sop\ASN1\Type\Primitive\ObjectDescriptor |
|
236 | - */ |
|
237 | - public function asObjectDescriptor(): Primitive\ObjectDescriptor |
|
238 | - { |
|
239 | - if (!$this->_element instanceof Primitive\ObjectDescriptor) { |
|
240 | - throw new \UnexpectedValueException( |
|
241 | - $this->_generateExceptionMessage( |
|
242 | - Element::TYPE_OBJECT_DESCRIPTOR)); |
|
243 | - } |
|
244 | - return $this->_element; |
|
245 | - } |
|
246 | - |
|
247 | - /** |
|
248 | - * Get the wrapped element as a real type. |
|
249 | - * |
|
250 | - * @throws \UnexpectedValueException If the element is not a real |
|
251 | - * |
|
252 | - * @return \Sop\ASN1\Type\Primitive\Real |
|
253 | - */ |
|
254 | - public function asReal(): Primitive\Real |
|
255 | - { |
|
256 | - if (!$this->_element instanceof Primitive\Real) { |
|
257 | - throw new \UnexpectedValueException( |
|
258 | - $this->_generateExceptionMessage(Element::TYPE_REAL)); |
|
259 | - } |
|
260 | - return $this->_element; |
|
261 | - } |
|
262 | - |
|
263 | - /** |
|
264 | - * Get the wrapped element as an enumerated type. |
|
265 | - * |
|
266 | - * @throws \UnexpectedValueException If the element is not an enumerated |
|
267 | - * |
|
268 | - * @return \Sop\ASN1\Type\Primitive\Enumerated |
|
269 | - */ |
|
270 | - public function asEnumerated(): Primitive\Enumerated |
|
271 | - { |
|
272 | - if (!$this->_element instanceof Primitive\Enumerated) { |
|
273 | - throw new \UnexpectedValueException( |
|
274 | - $this->_generateExceptionMessage(Element::TYPE_ENUMERATED)); |
|
275 | - } |
|
276 | - return $this->_element; |
|
277 | - } |
|
278 | - |
|
279 | - /** |
|
280 | - * Get the wrapped element as a UTF8 string type. |
|
281 | - * |
|
282 | - * @throws \UnexpectedValueException If the element is not a UTF8 string |
|
283 | - * |
|
284 | - * @return \Sop\ASN1\Type\Primitive\UTF8String |
|
285 | - */ |
|
286 | - public function asUTF8String(): Primitive\UTF8String |
|
287 | - { |
|
288 | - if (!$this->_element instanceof Primitive\UTF8String) { |
|
289 | - throw new \UnexpectedValueException( |
|
290 | - $this->_generateExceptionMessage(Element::TYPE_UTF8_STRING)); |
|
291 | - } |
|
292 | - return $this->_element; |
|
293 | - } |
|
294 | - |
|
295 | - /** |
|
296 | - * Get the wrapped element as a relative OID type. |
|
297 | - * |
|
298 | - * @throws \UnexpectedValueException If the element is not a relative OID |
|
299 | - * |
|
300 | - * @return \Sop\ASN1\Type\Primitive\RelativeOID |
|
301 | - */ |
|
302 | - public function asRelativeOID(): Primitive\RelativeOID |
|
303 | - { |
|
304 | - if (!$this->_element instanceof Primitive\RelativeOID) { |
|
305 | - throw new \UnexpectedValueException( |
|
306 | - $this->_generateExceptionMessage(Element::TYPE_RELATIVE_OID)); |
|
307 | - } |
|
308 | - return $this->_element; |
|
309 | - } |
|
310 | - |
|
311 | - /** |
|
312 | - * Get the wrapped element as a sequence type. |
|
313 | - * |
|
314 | - * @throws \UnexpectedValueException If the element is not a sequence |
|
315 | - * |
|
316 | - * @return \Sop\ASN1\Type\Constructed\Sequence |
|
317 | - */ |
|
318 | - public function asSequence(): Constructed\Sequence |
|
319 | - { |
|
320 | - if (!$this->_element instanceof Constructed\Sequence) { |
|
321 | - throw new \UnexpectedValueException( |
|
322 | - $this->_generateExceptionMessage(Element::TYPE_SEQUENCE)); |
|
323 | - } |
|
324 | - return $this->_element; |
|
325 | - } |
|
326 | - |
|
327 | - /** |
|
328 | - * Get the wrapped element as a set type. |
|
329 | - * |
|
330 | - * @throws \UnexpectedValueException If the element is not a set |
|
331 | - * |
|
332 | - * @return \Sop\ASN1\Type\Constructed\Set |
|
333 | - */ |
|
334 | - public function asSet(): Constructed\Set |
|
335 | - { |
|
336 | - if (!$this->_element instanceof Constructed\Set) { |
|
337 | - throw new \UnexpectedValueException( |
|
338 | - $this->_generateExceptionMessage(Element::TYPE_SET)); |
|
339 | - } |
|
340 | - return $this->_element; |
|
341 | - } |
|
342 | - |
|
343 | - /** |
|
344 | - * Get the wrapped element as a numeric string type. |
|
345 | - * |
|
346 | - * @throws \UnexpectedValueException If the element is not a numeric string |
|
347 | - * |
|
348 | - * @return \Sop\ASN1\Type\Primitive\NumericString |
|
349 | - */ |
|
350 | - public function asNumericString(): Primitive\NumericString |
|
351 | - { |
|
352 | - if (!$this->_element instanceof Primitive\NumericString) { |
|
353 | - throw new \UnexpectedValueException( |
|
354 | - $this->_generateExceptionMessage(Element::TYPE_NUMERIC_STRING)); |
|
355 | - } |
|
356 | - return $this->_element; |
|
357 | - } |
|
358 | - |
|
359 | - /** |
|
360 | - * Get the wrapped element as a printable string type. |
|
361 | - * |
|
362 | - * @throws \UnexpectedValueException If the element is not a printable |
|
363 | - * string |
|
364 | - * |
|
365 | - * @return \Sop\ASN1\Type\Primitive\PrintableString |
|
366 | - */ |
|
367 | - public function asPrintableString(): Primitive\PrintableString |
|
368 | - { |
|
369 | - if (!$this->_element instanceof Primitive\PrintableString) { |
|
370 | - throw new \UnexpectedValueException( |
|
371 | - $this->_generateExceptionMessage(Element::TYPE_PRINTABLE_STRING)); |
|
372 | - } |
|
373 | - return $this->_element; |
|
374 | - } |
|
375 | - |
|
376 | - /** |
|
377 | - * Get the wrapped element as a T61 string type. |
|
378 | - * |
|
379 | - * @throws \UnexpectedValueException If the element is not a T61 string |
|
380 | - * |
|
381 | - * @return \Sop\ASN1\Type\Primitive\T61String |
|
382 | - */ |
|
383 | - public function asT61String(): Primitive\T61String |
|
384 | - { |
|
385 | - if (!$this->_element instanceof Primitive\T61String) { |
|
386 | - throw new \UnexpectedValueException( |
|
387 | - $this->_generateExceptionMessage(Element::TYPE_T61_STRING)); |
|
388 | - } |
|
389 | - return $this->_element; |
|
390 | - } |
|
391 | - |
|
392 | - /** |
|
393 | - * Get the wrapped element as a videotex string type. |
|
394 | - * |
|
395 | - * @throws \UnexpectedValueException If the element is not a videotex string |
|
396 | - * |
|
397 | - * @return \Sop\ASN1\Type\Primitive\VideotexString |
|
398 | - */ |
|
399 | - public function asVideotexString(): Primitive\VideotexString |
|
400 | - { |
|
401 | - if (!$this->_element instanceof Primitive\VideotexString) { |
|
402 | - throw new \UnexpectedValueException( |
|
403 | - $this->_generateExceptionMessage(Element::TYPE_VIDEOTEX_STRING)); |
|
404 | - } |
|
405 | - return $this->_element; |
|
406 | - } |
|
407 | - |
|
408 | - /** |
|
409 | - * Get the wrapped element as a IA5 string type. |
|
410 | - * |
|
411 | - * @throws \UnexpectedValueException If the element is not a IA5 string |
|
412 | - * |
|
413 | - * @return \Sop\ASN1\Type\Primitive\IA5String |
|
414 | - */ |
|
415 | - public function asIA5String(): Primitive\IA5String |
|
416 | - { |
|
417 | - if (!$this->_element instanceof Primitive\IA5String) { |
|
418 | - throw new \UnexpectedValueException( |
|
419 | - $this->_generateExceptionMessage(Element::TYPE_IA5_STRING)); |
|
420 | - } |
|
421 | - return $this->_element; |
|
422 | - } |
|
423 | - |
|
424 | - /** |
|
425 | - * Get the wrapped element as an UTC time type. |
|
426 | - * |
|
427 | - * @throws \UnexpectedValueException If the element is not a UTC time |
|
428 | - * |
|
429 | - * @return \Sop\ASN1\Type\Primitive\UTCTime |
|
430 | - */ |
|
431 | - public function asUTCTime(): Primitive\UTCTime |
|
432 | - { |
|
433 | - if (!$this->_element instanceof Primitive\UTCTime) { |
|
434 | - throw new \UnexpectedValueException( |
|
435 | - $this->_generateExceptionMessage(Element::TYPE_UTC_TIME)); |
|
436 | - } |
|
437 | - return $this->_element; |
|
438 | - } |
|
439 | - |
|
440 | - /** |
|
441 | - * Get the wrapped element as a generalized time type. |
|
442 | - * |
|
443 | - * @throws \UnexpectedValueException If the element is not a generalized |
|
444 | - * time |
|
445 | - * |
|
446 | - * @return \Sop\ASN1\Type\Primitive\GeneralizedTime |
|
447 | - */ |
|
448 | - public function asGeneralizedTime(): Primitive\GeneralizedTime |
|
449 | - { |
|
450 | - if (!$this->_element instanceof Primitive\GeneralizedTime) { |
|
451 | - throw new \UnexpectedValueException( |
|
452 | - $this->_generateExceptionMessage(Element::TYPE_GENERALIZED_TIME)); |
|
453 | - } |
|
454 | - return $this->_element; |
|
455 | - } |
|
456 | - |
|
457 | - /** |
|
458 | - * Get the wrapped element as a graphic string type. |
|
459 | - * |
|
460 | - * @throws \UnexpectedValueException If the element is not a graphic string |
|
461 | - * |
|
462 | - * @return \Sop\ASN1\Type\Primitive\GraphicString |
|
463 | - */ |
|
464 | - public function asGraphicString(): Primitive\GraphicString |
|
465 | - { |
|
466 | - if (!$this->_element instanceof Primitive\GraphicString) { |
|
467 | - throw new \UnexpectedValueException( |
|
468 | - $this->_generateExceptionMessage(Element::TYPE_GRAPHIC_STRING)); |
|
469 | - } |
|
470 | - return $this->_element; |
|
471 | - } |
|
472 | - |
|
473 | - /** |
|
474 | - * Get the wrapped element as a visible string type. |
|
475 | - * |
|
476 | - * @throws \UnexpectedValueException If the element is not a visible string |
|
477 | - * |
|
478 | - * @return \Sop\ASN1\Type\Primitive\VisibleString |
|
479 | - */ |
|
480 | - public function asVisibleString(): Primitive\VisibleString |
|
481 | - { |
|
482 | - if (!$this->_element instanceof Primitive\VisibleString) { |
|
483 | - throw new \UnexpectedValueException( |
|
484 | - $this->_generateExceptionMessage(Element::TYPE_VISIBLE_STRING)); |
|
485 | - } |
|
486 | - return $this->_element; |
|
487 | - } |
|
488 | - |
|
489 | - /** |
|
490 | - * Get the wrapped element as a general string type. |
|
491 | - * |
|
492 | - * @throws \UnexpectedValueException If the element is not general string |
|
493 | - * |
|
494 | - * @return \Sop\ASN1\Type\Primitive\GeneralString |
|
495 | - */ |
|
496 | - public function asGeneralString(): Primitive\GeneralString |
|
497 | - { |
|
498 | - if (!$this->_element instanceof Primitive\GeneralString) { |
|
499 | - throw new \UnexpectedValueException( |
|
500 | - $this->_generateExceptionMessage(Element::TYPE_GENERAL_STRING)); |
|
501 | - } |
|
502 | - return $this->_element; |
|
503 | - } |
|
504 | - |
|
505 | - /** |
|
506 | - * Get the wrapped element as a universal string type. |
|
507 | - * |
|
508 | - * @throws \UnexpectedValueException If the element is not a universal |
|
509 | - * string |
|
510 | - * |
|
511 | - * @return \Sop\ASN1\Type\Primitive\UniversalString |
|
512 | - */ |
|
513 | - public function asUniversalString(): Primitive\UniversalString |
|
514 | - { |
|
515 | - if (!$this->_element instanceof Primitive\UniversalString) { |
|
516 | - throw new \UnexpectedValueException( |
|
517 | - $this->_generateExceptionMessage(Element::TYPE_UNIVERSAL_STRING)); |
|
518 | - } |
|
519 | - return $this->_element; |
|
520 | - } |
|
521 | - |
|
522 | - /** |
|
523 | - * Get the wrapped element as a character string type. |
|
524 | - * |
|
525 | - * @throws \UnexpectedValueException If the element is not a character |
|
526 | - * string |
|
527 | - * |
|
528 | - * @return \Sop\ASN1\Type\Primitive\CharacterString |
|
529 | - */ |
|
530 | - public function asCharacterString(): Primitive\CharacterString |
|
531 | - { |
|
532 | - if (!$this->_element instanceof Primitive\CharacterString) { |
|
533 | - throw new \UnexpectedValueException( |
|
534 | - $this->_generateExceptionMessage(Element::TYPE_CHARACTER_STRING)); |
|
535 | - } |
|
536 | - return $this->_element; |
|
537 | - } |
|
538 | - |
|
539 | - /** |
|
540 | - * Get the wrapped element as a BMP string type. |
|
541 | - * |
|
542 | - * @throws \UnexpectedValueException If the element is not a bmp string |
|
543 | - * |
|
544 | - * @return \Sop\ASN1\Type\Primitive\BMPString |
|
545 | - */ |
|
546 | - public function asBMPString(): Primitive\BMPString |
|
547 | - { |
|
548 | - if (!$this->_element instanceof Primitive\BMPString) { |
|
549 | - throw new \UnexpectedValueException( |
|
550 | - $this->_generateExceptionMessage(Element::TYPE_BMP_STRING)); |
|
551 | - } |
|
552 | - return $this->_element; |
|
553 | - } |
|
554 | - |
|
555 | - /** |
|
556 | - * Get the wrapped element as any string type. |
|
557 | - * |
|
558 | - * @throws \UnexpectedValueException If the element is not a string |
|
559 | - * |
|
560 | - * @return StringType |
|
561 | - */ |
|
562 | - public function asString(): StringType |
|
563 | - { |
|
564 | - if (!$this->_element instanceof StringType) { |
|
565 | - throw new \UnexpectedValueException( |
|
566 | - $this->_generateExceptionMessage(Element::TYPE_STRING)); |
|
567 | - } |
|
568 | - return $this->_element; |
|
569 | - } |
|
570 | - |
|
571 | - /** |
|
572 | - * Get the wrapped element as any time type. |
|
573 | - * |
|
574 | - * @throws \UnexpectedValueException If the element is not a time |
|
575 | - * |
|
576 | - * @return TimeType |
|
577 | - */ |
|
578 | - public function asTime(): TimeType |
|
579 | - { |
|
580 | - if (!$this->_element instanceof TimeType) { |
|
581 | - throw new \UnexpectedValueException( |
|
582 | - $this->_generateExceptionMessage(Element::TYPE_TIME)); |
|
583 | - } |
|
584 | - return $this->_element; |
|
585 | - } |
|
586 | - |
|
587 | - /** |
|
588 | - * {@inheritdoc} |
|
589 | - */ |
|
590 | - public function toDER(): string |
|
591 | - { |
|
592 | - return $this->_element->toDER(); |
|
593 | - } |
|
594 | - |
|
595 | - /** |
|
596 | - * {@inheritdoc} |
|
597 | - */ |
|
598 | - public function typeClass(): int |
|
599 | - { |
|
600 | - return $this->_element->typeClass(); |
|
601 | - } |
|
602 | - |
|
603 | - /** |
|
604 | - * {@inheritdoc} |
|
605 | - */ |
|
606 | - public function isConstructed(): bool |
|
607 | - { |
|
608 | - return $this->_element->isConstructed(); |
|
609 | - } |
|
610 | - |
|
611 | - /** |
|
612 | - * {@inheritdoc} |
|
613 | - */ |
|
614 | - public function tag(): int |
|
615 | - { |
|
616 | - return $this->_element->tag(); |
|
617 | - } |
|
618 | - |
|
619 | - /** |
|
620 | - * {@inheritdoc} |
|
621 | - */ |
|
622 | - public function isType(int $tag): bool |
|
623 | - { |
|
624 | - return $this->_element->isType($tag); |
|
625 | - } |
|
626 | - |
|
627 | - /** |
|
628 | - * @todo Remove |
|
629 | - * |
|
630 | - * @deprecated use any <code>as*</code> accessor method first to ensure |
|
631 | - * type strictness |
|
632 | - * @see \Sop\ASN1\Feature\ElementBase::expectType() |
|
633 | - * |
|
634 | - * @return ElementBase |
|
635 | - */ |
|
636 | - public function expectType(int $tag): ElementBase |
|
637 | - { |
|
638 | - return $this->_element->expectType($tag); |
|
639 | - } |
|
640 | - |
|
641 | - /** |
|
642 | - * {@inheritdoc} |
|
643 | - */ |
|
644 | - public function isTagged(): bool |
|
645 | - { |
|
646 | - return $this->_element->isTagged(); |
|
647 | - } |
|
648 | - |
|
649 | - /** |
|
650 | - * @todo Remove |
|
651 | - * |
|
652 | - * @deprecated use any <code>as*</code> accessor method first to ensure |
|
653 | - * type strictness |
|
654 | - * @see \Sop\ASN1\Feature\ElementBase::expectTagged() |
|
655 | - * |
|
656 | - * @param null|mixed $tag |
|
657 | - * |
|
658 | - * @return TaggedType |
|
659 | - */ |
|
660 | - public function expectTagged(?int $tag = null): TaggedType |
|
661 | - { |
|
662 | - return $this->_element->expectTagged($tag); |
|
663 | - } |
|
664 | - |
|
665 | - /** |
|
666 | - * {@inheritdoc} |
|
667 | - */ |
|
668 | - public function asElement(): Element |
|
669 | - { |
|
670 | - return $this->_element; |
|
671 | - } |
|
672 | - |
|
673 | - /** |
|
674 | - * {@inheritdoc} |
|
675 | - */ |
|
676 | - public function asUnspecified(): UnspecifiedType |
|
677 | - { |
|
678 | - return $this; |
|
679 | - } |
|
680 | - |
|
681 | - /** |
|
682 | - * Generate message for exceptions thrown by <code>as*</code> methods. |
|
683 | - * |
|
684 | - * @param int $tag Type tag of the expected element |
|
685 | - * |
|
686 | - * @return string |
|
687 | - */ |
|
688 | - private function _generateExceptionMessage(int $tag): string |
|
689 | - { |
|
690 | - return sprintf('%s expected, got %s.', Element::tagToName($tag), |
|
691 | - $this->_typeDescriptorString()); |
|
692 | - } |
|
693 | - |
|
694 | - /** |
|
695 | - * Get textual description of the wrapped element for debugging purposes. |
|
696 | - * |
|
697 | - * @return string |
|
698 | - */ |
|
699 | - private function _typeDescriptorString(): string |
|
700 | - { |
|
701 | - $type_cls = $this->_element->typeClass(); |
|
702 | - $tag = $this->_element->tag(); |
|
703 | - if (Identifier::CLASS_UNIVERSAL == $type_cls) { |
|
704 | - return Element::tagToName($tag); |
|
705 | - } |
|
706 | - return Identifier::classToName($type_cls) . " TAG ${tag}"; |
|
707 | - } |
|
20 | + /** |
|
21 | + * The wrapped element. |
|
22 | + * |
|
23 | + * @var Element |
|
24 | + */ |
|
25 | + private $_element; |
|
26 | + |
|
27 | + /** |
|
28 | + * Constructor. |
|
29 | + * |
|
30 | + * @param Element $el |
|
31 | + */ |
|
32 | + public function __construct(Element $el) |
|
33 | + { |
|
34 | + $this->_element = $el; |
|
35 | + } |
|
36 | + |
|
37 | + /** |
|
38 | + * Compatibility method to dispatch calls to the wrapped element. |
|
39 | + * |
|
40 | + * @todo Remove |
|
41 | + * |
|
42 | + * @deprecated Use <code>as*</code> accessor methods to ensure strict type |
|
43 | + * |
|
44 | + * @param string $mtd Method name |
|
45 | + * @param array $args Arguments |
|
46 | + * |
|
47 | + * @return mixed |
|
48 | + */ |
|
49 | + public function __call($mtd, array $args) |
|
50 | + { |
|
51 | + return call_user_func_array([$this->_element, $mtd], $args); |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * Initialize from DER data. |
|
56 | + * |
|
57 | + * @param string $data DER encoded data |
|
58 | + * |
|
59 | + * @return self |
|
60 | + */ |
|
61 | + public static function fromDER(string $data): self |
|
62 | + { |
|
63 | + return Element::fromDER($data)->asUnspecified(); |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * Initialize from ElementBase interface. |
|
68 | + * |
|
69 | + * @param ElementBase $el |
|
70 | + * |
|
71 | + * @return self |
|
72 | + */ |
|
73 | + public static function fromElementBase(ElementBase $el): self |
|
74 | + { |
|
75 | + // if element is already wrapped |
|
76 | + if ($el instanceof self) { |
|
77 | + return $el; |
|
78 | + } |
|
79 | + return new self($el->asElement()); |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * Get the wrapped element as a context specific tagged type. |
|
84 | + * |
|
85 | + * @throws \UnexpectedValueException If the element is not tagged |
|
86 | + * |
|
87 | + * @return TaggedType |
|
88 | + */ |
|
89 | + public function asTagged(): TaggedType |
|
90 | + { |
|
91 | + if (!$this->_element instanceof TaggedType) { |
|
92 | + throw new \UnexpectedValueException( |
|
93 | + 'Tagged element expected, got ' . $this->_typeDescriptorString()); |
|
94 | + } |
|
95 | + return $this->_element; |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * Get the wrapped element as an application specific type. |
|
100 | + * |
|
101 | + * @throws \UnexpectedValueException If element is not application specific |
|
102 | + * |
|
103 | + * @return \Sop\ASN1\Type\Tagged\ApplicationType |
|
104 | + */ |
|
105 | + public function asApplication(): Tagged\ApplicationType |
|
106 | + { |
|
107 | + if (!$this->_element instanceof Tagged\ApplicationType) { |
|
108 | + throw new \UnexpectedValueException( |
|
109 | + 'Application type expected, got ' . |
|
110 | + $this->_typeDescriptorString()); |
|
111 | + } |
|
112 | + return $this->_element; |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Get the wrapped element as a private tagged type. |
|
117 | + * |
|
118 | + * @throws \UnexpectedValueException If element is not using private tagging |
|
119 | + * |
|
120 | + * @return \Sop\ASN1\Type\Tagged\PrivateType |
|
121 | + */ |
|
122 | + public function asPrivate(): Tagged\PrivateType |
|
123 | + { |
|
124 | + if (!$this->_element instanceof Tagged\PrivateType) { |
|
125 | + throw new \UnexpectedValueException( |
|
126 | + 'Private type expected, got ' . $this->_typeDescriptorString()); |
|
127 | + } |
|
128 | + return $this->_element; |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * Get the wrapped element as a boolean type. |
|
133 | + * |
|
134 | + * @throws \UnexpectedValueException If the element is not a boolean |
|
135 | + * |
|
136 | + * @return \Sop\ASN1\Type\Primitive\Boolean |
|
137 | + */ |
|
138 | + public function asBoolean(): Primitive\Boolean |
|
139 | + { |
|
140 | + if (!$this->_element instanceof Primitive\Boolean) { |
|
141 | + throw new \UnexpectedValueException( |
|
142 | + $this->_generateExceptionMessage(Element::TYPE_BOOLEAN)); |
|
143 | + } |
|
144 | + return $this->_element; |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * Get the wrapped element as an integer type. |
|
149 | + * |
|
150 | + * @throws \UnexpectedValueException If the element is not an integer |
|
151 | + * |
|
152 | + * @return \Sop\ASN1\Type\Primitive\Integer |
|
153 | + */ |
|
154 | + public function asInteger(): Primitive\Integer |
|
155 | + { |
|
156 | + if (!$this->_element instanceof Primitive\Integer) { |
|
157 | + throw new \UnexpectedValueException( |
|
158 | + $this->_generateExceptionMessage(Element::TYPE_INTEGER)); |
|
159 | + } |
|
160 | + return $this->_element; |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * Get the wrapped element as a bit string type. |
|
165 | + * |
|
166 | + * @throws \UnexpectedValueException If the element is not a bit string |
|
167 | + * |
|
168 | + * @return \Sop\ASN1\Type\Primitive\BitString |
|
169 | + */ |
|
170 | + public function asBitString(): Primitive\BitString |
|
171 | + { |
|
172 | + if (!$this->_element instanceof Primitive\BitString) { |
|
173 | + throw new \UnexpectedValueException( |
|
174 | + $this->_generateExceptionMessage(Element::TYPE_BIT_STRING)); |
|
175 | + } |
|
176 | + return $this->_element; |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * Get the wrapped element as an octet string type. |
|
181 | + * |
|
182 | + * @throws \UnexpectedValueException If the element is not an octet string |
|
183 | + * |
|
184 | + * @return \Sop\ASN1\Type\Primitive\OctetString |
|
185 | + */ |
|
186 | + public function asOctetString(): Primitive\OctetString |
|
187 | + { |
|
188 | + if (!$this->_element instanceof Primitive\OctetString) { |
|
189 | + throw new \UnexpectedValueException( |
|
190 | + $this->_generateExceptionMessage(Element::TYPE_OCTET_STRING)); |
|
191 | + } |
|
192 | + return $this->_element; |
|
193 | + } |
|
194 | + |
|
195 | + /** |
|
196 | + * Get the wrapped element as a null type. |
|
197 | + * |
|
198 | + * @throws \UnexpectedValueException If the element is not a null |
|
199 | + * |
|
200 | + * @return \Sop\ASN1\Type\Primitive\NullType |
|
201 | + */ |
|
202 | + public function asNull(): Primitive\NullType |
|
203 | + { |
|
204 | + if (!$this->_element instanceof Primitive\NullType) { |
|
205 | + throw new \UnexpectedValueException( |
|
206 | + $this->_generateExceptionMessage(Element::TYPE_NULL)); |
|
207 | + } |
|
208 | + return $this->_element; |
|
209 | + } |
|
210 | + |
|
211 | + /** |
|
212 | + * Get the wrapped element as an object identifier type. |
|
213 | + * |
|
214 | + * @throws \UnexpectedValueException If the element is not an object |
|
215 | + * identifier |
|
216 | + * |
|
217 | + * @return \Sop\ASN1\Type\Primitive\ObjectIdentifier |
|
218 | + */ |
|
219 | + public function asObjectIdentifier(): Primitive\ObjectIdentifier |
|
220 | + { |
|
221 | + if (!$this->_element instanceof Primitive\ObjectIdentifier) { |
|
222 | + throw new \UnexpectedValueException( |
|
223 | + $this->_generateExceptionMessage( |
|
224 | + Element::TYPE_OBJECT_IDENTIFIER)); |
|
225 | + } |
|
226 | + return $this->_element; |
|
227 | + } |
|
228 | + |
|
229 | + /** |
|
230 | + * Get the wrapped element as an object descriptor type. |
|
231 | + * |
|
232 | + * @throws \UnexpectedValueException If the element is not an object |
|
233 | + * descriptor |
|
234 | + * |
|
235 | + * @return \Sop\ASN1\Type\Primitive\ObjectDescriptor |
|
236 | + */ |
|
237 | + public function asObjectDescriptor(): Primitive\ObjectDescriptor |
|
238 | + { |
|
239 | + if (!$this->_element instanceof Primitive\ObjectDescriptor) { |
|
240 | + throw new \UnexpectedValueException( |
|
241 | + $this->_generateExceptionMessage( |
|
242 | + Element::TYPE_OBJECT_DESCRIPTOR)); |
|
243 | + } |
|
244 | + return $this->_element; |
|
245 | + } |
|
246 | + |
|
247 | + /** |
|
248 | + * Get the wrapped element as a real type. |
|
249 | + * |
|
250 | + * @throws \UnexpectedValueException If the element is not a real |
|
251 | + * |
|
252 | + * @return \Sop\ASN1\Type\Primitive\Real |
|
253 | + */ |
|
254 | + public function asReal(): Primitive\Real |
|
255 | + { |
|
256 | + if (!$this->_element instanceof Primitive\Real) { |
|
257 | + throw new \UnexpectedValueException( |
|
258 | + $this->_generateExceptionMessage(Element::TYPE_REAL)); |
|
259 | + } |
|
260 | + return $this->_element; |
|
261 | + } |
|
262 | + |
|
263 | + /** |
|
264 | + * Get the wrapped element as an enumerated type. |
|
265 | + * |
|
266 | + * @throws \UnexpectedValueException If the element is not an enumerated |
|
267 | + * |
|
268 | + * @return \Sop\ASN1\Type\Primitive\Enumerated |
|
269 | + */ |
|
270 | + public function asEnumerated(): Primitive\Enumerated |
|
271 | + { |
|
272 | + if (!$this->_element instanceof Primitive\Enumerated) { |
|
273 | + throw new \UnexpectedValueException( |
|
274 | + $this->_generateExceptionMessage(Element::TYPE_ENUMERATED)); |
|
275 | + } |
|
276 | + return $this->_element; |
|
277 | + } |
|
278 | + |
|
279 | + /** |
|
280 | + * Get the wrapped element as a UTF8 string type. |
|
281 | + * |
|
282 | + * @throws \UnexpectedValueException If the element is not a UTF8 string |
|
283 | + * |
|
284 | + * @return \Sop\ASN1\Type\Primitive\UTF8String |
|
285 | + */ |
|
286 | + public function asUTF8String(): Primitive\UTF8String |
|
287 | + { |
|
288 | + if (!$this->_element instanceof Primitive\UTF8String) { |
|
289 | + throw new \UnexpectedValueException( |
|
290 | + $this->_generateExceptionMessage(Element::TYPE_UTF8_STRING)); |
|
291 | + } |
|
292 | + return $this->_element; |
|
293 | + } |
|
294 | + |
|
295 | + /** |
|
296 | + * Get the wrapped element as a relative OID type. |
|
297 | + * |
|
298 | + * @throws \UnexpectedValueException If the element is not a relative OID |
|
299 | + * |
|
300 | + * @return \Sop\ASN1\Type\Primitive\RelativeOID |
|
301 | + */ |
|
302 | + public function asRelativeOID(): Primitive\RelativeOID |
|
303 | + { |
|
304 | + if (!$this->_element instanceof Primitive\RelativeOID) { |
|
305 | + throw new \UnexpectedValueException( |
|
306 | + $this->_generateExceptionMessage(Element::TYPE_RELATIVE_OID)); |
|
307 | + } |
|
308 | + return $this->_element; |
|
309 | + } |
|
310 | + |
|
311 | + /** |
|
312 | + * Get the wrapped element as a sequence type. |
|
313 | + * |
|
314 | + * @throws \UnexpectedValueException If the element is not a sequence |
|
315 | + * |
|
316 | + * @return \Sop\ASN1\Type\Constructed\Sequence |
|
317 | + */ |
|
318 | + public function asSequence(): Constructed\Sequence |
|
319 | + { |
|
320 | + if (!$this->_element instanceof Constructed\Sequence) { |
|
321 | + throw new \UnexpectedValueException( |
|
322 | + $this->_generateExceptionMessage(Element::TYPE_SEQUENCE)); |
|
323 | + } |
|
324 | + return $this->_element; |
|
325 | + } |
|
326 | + |
|
327 | + /** |
|
328 | + * Get the wrapped element as a set type. |
|
329 | + * |
|
330 | + * @throws \UnexpectedValueException If the element is not a set |
|
331 | + * |
|
332 | + * @return \Sop\ASN1\Type\Constructed\Set |
|
333 | + */ |
|
334 | + public function asSet(): Constructed\Set |
|
335 | + { |
|
336 | + if (!$this->_element instanceof Constructed\Set) { |
|
337 | + throw new \UnexpectedValueException( |
|
338 | + $this->_generateExceptionMessage(Element::TYPE_SET)); |
|
339 | + } |
|
340 | + return $this->_element; |
|
341 | + } |
|
342 | + |
|
343 | + /** |
|
344 | + * Get the wrapped element as a numeric string type. |
|
345 | + * |
|
346 | + * @throws \UnexpectedValueException If the element is not a numeric string |
|
347 | + * |
|
348 | + * @return \Sop\ASN1\Type\Primitive\NumericString |
|
349 | + */ |
|
350 | + public function asNumericString(): Primitive\NumericString |
|
351 | + { |
|
352 | + if (!$this->_element instanceof Primitive\NumericString) { |
|
353 | + throw new \UnexpectedValueException( |
|
354 | + $this->_generateExceptionMessage(Element::TYPE_NUMERIC_STRING)); |
|
355 | + } |
|
356 | + return $this->_element; |
|
357 | + } |
|
358 | + |
|
359 | + /** |
|
360 | + * Get the wrapped element as a printable string type. |
|
361 | + * |
|
362 | + * @throws \UnexpectedValueException If the element is not a printable |
|
363 | + * string |
|
364 | + * |
|
365 | + * @return \Sop\ASN1\Type\Primitive\PrintableString |
|
366 | + */ |
|
367 | + public function asPrintableString(): Primitive\PrintableString |
|
368 | + { |
|
369 | + if (!$this->_element instanceof Primitive\PrintableString) { |
|
370 | + throw new \UnexpectedValueException( |
|
371 | + $this->_generateExceptionMessage(Element::TYPE_PRINTABLE_STRING)); |
|
372 | + } |
|
373 | + return $this->_element; |
|
374 | + } |
|
375 | + |
|
376 | + /** |
|
377 | + * Get the wrapped element as a T61 string type. |
|
378 | + * |
|
379 | + * @throws \UnexpectedValueException If the element is not a T61 string |
|
380 | + * |
|
381 | + * @return \Sop\ASN1\Type\Primitive\T61String |
|
382 | + */ |
|
383 | + public function asT61String(): Primitive\T61String |
|
384 | + { |
|
385 | + if (!$this->_element instanceof Primitive\T61String) { |
|
386 | + throw new \UnexpectedValueException( |
|
387 | + $this->_generateExceptionMessage(Element::TYPE_T61_STRING)); |
|
388 | + } |
|
389 | + return $this->_element; |
|
390 | + } |
|
391 | + |
|
392 | + /** |
|
393 | + * Get the wrapped element as a videotex string type. |
|
394 | + * |
|
395 | + * @throws \UnexpectedValueException If the element is not a videotex string |
|
396 | + * |
|
397 | + * @return \Sop\ASN1\Type\Primitive\VideotexString |
|
398 | + */ |
|
399 | + public function asVideotexString(): Primitive\VideotexString |
|
400 | + { |
|
401 | + if (!$this->_element instanceof Primitive\VideotexString) { |
|
402 | + throw new \UnexpectedValueException( |
|
403 | + $this->_generateExceptionMessage(Element::TYPE_VIDEOTEX_STRING)); |
|
404 | + } |
|
405 | + return $this->_element; |
|
406 | + } |
|
407 | + |
|
408 | + /** |
|
409 | + * Get the wrapped element as a IA5 string type. |
|
410 | + * |
|
411 | + * @throws \UnexpectedValueException If the element is not a IA5 string |
|
412 | + * |
|
413 | + * @return \Sop\ASN1\Type\Primitive\IA5String |
|
414 | + */ |
|
415 | + public function asIA5String(): Primitive\IA5String |
|
416 | + { |
|
417 | + if (!$this->_element instanceof Primitive\IA5String) { |
|
418 | + throw new \UnexpectedValueException( |
|
419 | + $this->_generateExceptionMessage(Element::TYPE_IA5_STRING)); |
|
420 | + } |
|
421 | + return $this->_element; |
|
422 | + } |
|
423 | + |
|
424 | + /** |
|
425 | + * Get the wrapped element as an UTC time type. |
|
426 | + * |
|
427 | + * @throws \UnexpectedValueException If the element is not a UTC time |
|
428 | + * |
|
429 | + * @return \Sop\ASN1\Type\Primitive\UTCTime |
|
430 | + */ |
|
431 | + public function asUTCTime(): Primitive\UTCTime |
|
432 | + { |
|
433 | + if (!$this->_element instanceof Primitive\UTCTime) { |
|
434 | + throw new \UnexpectedValueException( |
|
435 | + $this->_generateExceptionMessage(Element::TYPE_UTC_TIME)); |
|
436 | + } |
|
437 | + return $this->_element; |
|
438 | + } |
|
439 | + |
|
440 | + /** |
|
441 | + * Get the wrapped element as a generalized time type. |
|
442 | + * |
|
443 | + * @throws \UnexpectedValueException If the element is not a generalized |
|
444 | + * time |
|
445 | + * |
|
446 | + * @return \Sop\ASN1\Type\Primitive\GeneralizedTime |
|
447 | + */ |
|
448 | + public function asGeneralizedTime(): Primitive\GeneralizedTime |
|
449 | + { |
|
450 | + if (!$this->_element instanceof Primitive\GeneralizedTime) { |
|
451 | + throw new \UnexpectedValueException( |
|
452 | + $this->_generateExceptionMessage(Element::TYPE_GENERALIZED_TIME)); |
|
453 | + } |
|
454 | + return $this->_element; |
|
455 | + } |
|
456 | + |
|
457 | + /** |
|
458 | + * Get the wrapped element as a graphic string type. |
|
459 | + * |
|
460 | + * @throws \UnexpectedValueException If the element is not a graphic string |
|
461 | + * |
|
462 | + * @return \Sop\ASN1\Type\Primitive\GraphicString |
|
463 | + */ |
|
464 | + public function asGraphicString(): Primitive\GraphicString |
|
465 | + { |
|
466 | + if (!$this->_element instanceof Primitive\GraphicString) { |
|
467 | + throw new \UnexpectedValueException( |
|
468 | + $this->_generateExceptionMessage(Element::TYPE_GRAPHIC_STRING)); |
|
469 | + } |
|
470 | + return $this->_element; |
|
471 | + } |
|
472 | + |
|
473 | + /** |
|
474 | + * Get the wrapped element as a visible string type. |
|
475 | + * |
|
476 | + * @throws \UnexpectedValueException If the element is not a visible string |
|
477 | + * |
|
478 | + * @return \Sop\ASN1\Type\Primitive\VisibleString |
|
479 | + */ |
|
480 | + public function asVisibleString(): Primitive\VisibleString |
|
481 | + { |
|
482 | + if (!$this->_element instanceof Primitive\VisibleString) { |
|
483 | + throw new \UnexpectedValueException( |
|
484 | + $this->_generateExceptionMessage(Element::TYPE_VISIBLE_STRING)); |
|
485 | + } |
|
486 | + return $this->_element; |
|
487 | + } |
|
488 | + |
|
489 | + /** |
|
490 | + * Get the wrapped element as a general string type. |
|
491 | + * |
|
492 | + * @throws \UnexpectedValueException If the element is not general string |
|
493 | + * |
|
494 | + * @return \Sop\ASN1\Type\Primitive\GeneralString |
|
495 | + */ |
|
496 | + public function asGeneralString(): Primitive\GeneralString |
|
497 | + { |
|
498 | + if (!$this->_element instanceof Primitive\GeneralString) { |
|
499 | + throw new \UnexpectedValueException( |
|
500 | + $this->_generateExceptionMessage(Element::TYPE_GENERAL_STRING)); |
|
501 | + } |
|
502 | + return $this->_element; |
|
503 | + } |
|
504 | + |
|
505 | + /** |
|
506 | + * Get the wrapped element as a universal string type. |
|
507 | + * |
|
508 | + * @throws \UnexpectedValueException If the element is not a universal |
|
509 | + * string |
|
510 | + * |
|
511 | + * @return \Sop\ASN1\Type\Primitive\UniversalString |
|
512 | + */ |
|
513 | + public function asUniversalString(): Primitive\UniversalString |
|
514 | + { |
|
515 | + if (!$this->_element instanceof Primitive\UniversalString) { |
|
516 | + throw new \UnexpectedValueException( |
|
517 | + $this->_generateExceptionMessage(Element::TYPE_UNIVERSAL_STRING)); |
|
518 | + } |
|
519 | + return $this->_element; |
|
520 | + } |
|
521 | + |
|
522 | + /** |
|
523 | + * Get the wrapped element as a character string type. |
|
524 | + * |
|
525 | + * @throws \UnexpectedValueException If the element is not a character |
|
526 | + * string |
|
527 | + * |
|
528 | + * @return \Sop\ASN1\Type\Primitive\CharacterString |
|
529 | + */ |
|
530 | + public function asCharacterString(): Primitive\CharacterString |
|
531 | + { |
|
532 | + if (!$this->_element instanceof Primitive\CharacterString) { |
|
533 | + throw new \UnexpectedValueException( |
|
534 | + $this->_generateExceptionMessage(Element::TYPE_CHARACTER_STRING)); |
|
535 | + } |
|
536 | + return $this->_element; |
|
537 | + } |
|
538 | + |
|
539 | + /** |
|
540 | + * Get the wrapped element as a BMP string type. |
|
541 | + * |
|
542 | + * @throws \UnexpectedValueException If the element is not a bmp string |
|
543 | + * |
|
544 | + * @return \Sop\ASN1\Type\Primitive\BMPString |
|
545 | + */ |
|
546 | + public function asBMPString(): Primitive\BMPString |
|
547 | + { |
|
548 | + if (!$this->_element instanceof Primitive\BMPString) { |
|
549 | + throw new \UnexpectedValueException( |
|
550 | + $this->_generateExceptionMessage(Element::TYPE_BMP_STRING)); |
|
551 | + } |
|
552 | + return $this->_element; |
|
553 | + } |
|
554 | + |
|
555 | + /** |
|
556 | + * Get the wrapped element as any string type. |
|
557 | + * |
|
558 | + * @throws \UnexpectedValueException If the element is not a string |
|
559 | + * |
|
560 | + * @return StringType |
|
561 | + */ |
|
562 | + public function asString(): StringType |
|
563 | + { |
|
564 | + if (!$this->_element instanceof StringType) { |
|
565 | + throw new \UnexpectedValueException( |
|
566 | + $this->_generateExceptionMessage(Element::TYPE_STRING)); |
|
567 | + } |
|
568 | + return $this->_element; |
|
569 | + } |
|
570 | + |
|
571 | + /** |
|
572 | + * Get the wrapped element as any time type. |
|
573 | + * |
|
574 | + * @throws \UnexpectedValueException If the element is not a time |
|
575 | + * |
|
576 | + * @return TimeType |
|
577 | + */ |
|
578 | + public function asTime(): TimeType |
|
579 | + { |
|
580 | + if (!$this->_element instanceof TimeType) { |
|
581 | + throw new \UnexpectedValueException( |
|
582 | + $this->_generateExceptionMessage(Element::TYPE_TIME)); |
|
583 | + } |
|
584 | + return $this->_element; |
|
585 | + } |
|
586 | + |
|
587 | + /** |
|
588 | + * {@inheritdoc} |
|
589 | + */ |
|
590 | + public function toDER(): string |
|
591 | + { |
|
592 | + return $this->_element->toDER(); |
|
593 | + } |
|
594 | + |
|
595 | + /** |
|
596 | + * {@inheritdoc} |
|
597 | + */ |
|
598 | + public function typeClass(): int |
|
599 | + { |
|
600 | + return $this->_element->typeClass(); |
|
601 | + } |
|
602 | + |
|
603 | + /** |
|
604 | + * {@inheritdoc} |
|
605 | + */ |
|
606 | + public function isConstructed(): bool |
|
607 | + { |
|
608 | + return $this->_element->isConstructed(); |
|
609 | + } |
|
610 | + |
|
611 | + /** |
|
612 | + * {@inheritdoc} |
|
613 | + */ |
|
614 | + public function tag(): int |
|
615 | + { |
|
616 | + return $this->_element->tag(); |
|
617 | + } |
|
618 | + |
|
619 | + /** |
|
620 | + * {@inheritdoc} |
|
621 | + */ |
|
622 | + public function isType(int $tag): bool |
|
623 | + { |
|
624 | + return $this->_element->isType($tag); |
|
625 | + } |
|
626 | + |
|
627 | + /** |
|
628 | + * @todo Remove |
|
629 | + * |
|
630 | + * @deprecated use any <code>as*</code> accessor method first to ensure |
|
631 | + * type strictness |
|
632 | + * @see \Sop\ASN1\Feature\ElementBase::expectType() |
|
633 | + * |
|
634 | + * @return ElementBase |
|
635 | + */ |
|
636 | + public function expectType(int $tag): ElementBase |
|
637 | + { |
|
638 | + return $this->_element->expectType($tag); |
|
639 | + } |
|
640 | + |
|
641 | + /** |
|
642 | + * {@inheritdoc} |
|
643 | + */ |
|
644 | + public function isTagged(): bool |
|
645 | + { |
|
646 | + return $this->_element->isTagged(); |
|
647 | + } |
|
648 | + |
|
649 | + /** |
|
650 | + * @todo Remove |
|
651 | + * |
|
652 | + * @deprecated use any <code>as*</code> accessor method first to ensure |
|
653 | + * type strictness |
|
654 | + * @see \Sop\ASN1\Feature\ElementBase::expectTagged() |
|
655 | + * |
|
656 | + * @param null|mixed $tag |
|
657 | + * |
|
658 | + * @return TaggedType |
|
659 | + */ |
|
660 | + public function expectTagged(?int $tag = null): TaggedType |
|
661 | + { |
|
662 | + return $this->_element->expectTagged($tag); |
|
663 | + } |
|
664 | + |
|
665 | + /** |
|
666 | + * {@inheritdoc} |
|
667 | + */ |
|
668 | + public function asElement(): Element |
|
669 | + { |
|
670 | + return $this->_element; |
|
671 | + } |
|
672 | + |
|
673 | + /** |
|
674 | + * {@inheritdoc} |
|
675 | + */ |
|
676 | + public function asUnspecified(): UnspecifiedType |
|
677 | + { |
|
678 | + return $this; |
|
679 | + } |
|
680 | + |
|
681 | + /** |
|
682 | + * Generate message for exceptions thrown by <code>as*</code> methods. |
|
683 | + * |
|
684 | + * @param int $tag Type tag of the expected element |
|
685 | + * |
|
686 | + * @return string |
|
687 | + */ |
|
688 | + private function _generateExceptionMessage(int $tag): string |
|
689 | + { |
|
690 | + return sprintf('%s expected, got %s.', Element::tagToName($tag), |
|
691 | + $this->_typeDescriptorString()); |
|
692 | + } |
|
693 | + |
|
694 | + /** |
|
695 | + * Get textual description of the wrapped element for debugging purposes. |
|
696 | + * |
|
697 | + * @return string |
|
698 | + */ |
|
699 | + private function _typeDescriptorString(): string |
|
700 | + { |
|
701 | + $type_cls = $this->_element->typeClass(); |
|
702 | + $tag = $this->_element->tag(); |
|
703 | + if (Identifier::CLASS_UNIVERSAL == $type_cls) { |
|
704 | + return Element::tagToName($tag); |
|
705 | + } |
|
706 | + return Identifier::classToName($type_cls) . " TAG ${tag}"; |
|
707 | + } |
|
708 | 708 | } |