@@ -25,482 +25,482 @@ |
||
25 | 25 | */ |
26 | 26 | abstract class Element implements ElementBase |
27 | 27 | { |
28 | - // Universal type tags |
|
29 | - const TYPE_EOC = 0x00; |
|
30 | - const TYPE_BOOLEAN = 0x01; |
|
31 | - const TYPE_INTEGER = 0x02; |
|
32 | - const TYPE_BIT_STRING = 0x03; |
|
33 | - const TYPE_OCTET_STRING = 0x04; |
|
34 | - const TYPE_NULL = 0x05; |
|
35 | - const TYPE_OBJECT_IDENTIFIER = 0x06; |
|
36 | - const TYPE_OBJECT_DESCRIPTOR = 0x07; |
|
37 | - const TYPE_EXTERNAL = 0x08; |
|
38 | - const TYPE_REAL = 0x09; |
|
39 | - const TYPE_ENUMERATED = 0x0a; |
|
40 | - const TYPE_EMBEDDED_PDV = 0x0b; |
|
41 | - const TYPE_UTF8_STRING = 0x0c; |
|
42 | - const TYPE_RELATIVE_OID = 0x0d; |
|
43 | - const TYPE_SEQUENCE = 0x10; |
|
44 | - const TYPE_SET = 0x11; |
|
45 | - const TYPE_NUMERIC_STRING = 0x12; |
|
46 | - const TYPE_PRINTABLE_STRING = 0x13; |
|
47 | - const TYPE_T61_STRING = 0x14; |
|
48 | - const TYPE_VIDEOTEX_STRING = 0x15; |
|
49 | - const TYPE_IA5_STRING = 0x16; |
|
50 | - const TYPE_UTC_TIME = 0x17; |
|
51 | - const TYPE_GENERALIZED_TIME = 0x18; |
|
52 | - const TYPE_GRAPHIC_STRING = 0x19; |
|
53 | - const TYPE_VISIBLE_STRING = 0x1a; |
|
54 | - const TYPE_GENERAL_STRING = 0x1b; |
|
55 | - const TYPE_UNIVERSAL_STRING = 0x1c; |
|
56 | - const TYPE_CHARACTER_STRING = 0x1d; |
|
57 | - const TYPE_BMP_STRING = 0x1e; |
|
58 | - |
|
59 | - /** |
|
60 | - * Mapping from universal type tag to implementation class name. |
|
61 | - * |
|
62 | - * @internal |
|
63 | - * |
|
64 | - * @var array |
|
65 | - */ |
|
66 | - const MAP_TAG_TO_CLASS = [ |
|
67 | - self::TYPE_EOC => Primitive\EOC::class, |
|
68 | - self::TYPE_BOOLEAN => Primitive\Boolean::class, |
|
69 | - self::TYPE_INTEGER => Primitive\Integer::class, |
|
70 | - self::TYPE_BIT_STRING => Primitive\BitString::class, |
|
71 | - self::TYPE_OCTET_STRING => Primitive\OctetString::class, |
|
72 | - self::TYPE_NULL => Primitive\NullType::class, |
|
73 | - self::TYPE_OBJECT_IDENTIFIER => Primitive\ObjectIdentifier::class, |
|
74 | - self::TYPE_OBJECT_DESCRIPTOR => Primitive\ObjectDescriptor::class, |
|
75 | - self::TYPE_REAL => Primitive\Real::class, |
|
76 | - self::TYPE_ENUMERATED => Primitive\Enumerated::class, |
|
77 | - self::TYPE_UTF8_STRING => Primitive\UTF8String::class, |
|
78 | - self::TYPE_RELATIVE_OID => Primitive\RelativeOID::class, |
|
79 | - self::TYPE_SEQUENCE => Constructed\Sequence::class, |
|
80 | - self::TYPE_SET => Constructed\Set::class, |
|
81 | - self::TYPE_NUMERIC_STRING => Primitive\NumericString::class, |
|
82 | - self::TYPE_PRINTABLE_STRING => Primitive\PrintableString::class, |
|
83 | - self::TYPE_T61_STRING => Primitive\T61String::class, |
|
84 | - self::TYPE_VIDEOTEX_STRING => Primitive\VideotexString::class, |
|
85 | - self::TYPE_IA5_STRING => Primitive\IA5String::class, |
|
86 | - self::TYPE_UTC_TIME => Primitive\UTCTime::class, |
|
87 | - self::TYPE_GENERALIZED_TIME => Primitive\GeneralizedTime::class, |
|
88 | - self::TYPE_GRAPHIC_STRING => Primitive\GraphicString::class, |
|
89 | - self::TYPE_VISIBLE_STRING => Primitive\VisibleString::class, |
|
90 | - self::TYPE_GENERAL_STRING => Primitive\GeneralString::class, |
|
91 | - self::TYPE_UNIVERSAL_STRING => Primitive\UniversalString::class, |
|
92 | - self::TYPE_CHARACTER_STRING => Primitive\CharacterString::class, |
|
93 | - self::TYPE_BMP_STRING => Primitive\BMPString::class, |
|
94 | - ]; |
|
95 | - |
|
96 | - /** |
|
97 | - * Pseudotype for all string types. |
|
98 | - * |
|
99 | - * May be used as an expectation parameter. |
|
100 | - * |
|
101 | - * @var int |
|
102 | - */ |
|
103 | - const TYPE_STRING = -1; |
|
104 | - |
|
105 | - /** |
|
106 | - * Pseudotype for all time types. |
|
107 | - * |
|
108 | - * May be used as an expectation parameter. |
|
109 | - * |
|
110 | - * @var int |
|
111 | - */ |
|
112 | - const TYPE_TIME = -2; |
|
113 | - |
|
114 | - /** |
|
115 | - * Pseudotype for constructed strings. |
|
116 | - * |
|
117 | - * May be used as an expectation parameter. |
|
118 | - * |
|
119 | - * @var int |
|
120 | - */ |
|
121 | - const TYPE_CONSTRUCTED_STRING = -3; |
|
122 | - |
|
123 | - /** |
|
124 | - * Mapping from universal type tag to human readable name. |
|
125 | - * |
|
126 | - * @internal |
|
127 | - * |
|
128 | - * @var array |
|
129 | - */ |
|
130 | - const MAP_TYPE_TO_NAME = [ |
|
131 | - self::TYPE_EOC => 'EOC', |
|
132 | - self::TYPE_BOOLEAN => 'BOOLEAN', |
|
133 | - self::TYPE_INTEGER => 'INTEGER', |
|
134 | - self::TYPE_BIT_STRING => 'BIT STRING', |
|
135 | - self::TYPE_OCTET_STRING => 'OCTET STRING', |
|
136 | - self::TYPE_NULL => 'NULL', |
|
137 | - self::TYPE_OBJECT_IDENTIFIER => 'OBJECT IDENTIFIER', |
|
138 | - self::TYPE_OBJECT_DESCRIPTOR => 'ObjectDescriptor', |
|
139 | - self::TYPE_EXTERNAL => 'EXTERNAL', |
|
140 | - self::TYPE_REAL => 'REAL', |
|
141 | - self::TYPE_ENUMERATED => 'ENUMERATED', |
|
142 | - self::TYPE_EMBEDDED_PDV => 'EMBEDDED PDV', |
|
143 | - self::TYPE_UTF8_STRING => 'UTF8String', |
|
144 | - self::TYPE_RELATIVE_OID => 'RELATIVE-OID', |
|
145 | - self::TYPE_SEQUENCE => 'SEQUENCE', |
|
146 | - self::TYPE_SET => 'SET', |
|
147 | - self::TYPE_NUMERIC_STRING => 'NumericString', |
|
148 | - self::TYPE_PRINTABLE_STRING => 'PrintableString', |
|
149 | - self::TYPE_T61_STRING => 'T61String', |
|
150 | - self::TYPE_VIDEOTEX_STRING => 'VideotexString', |
|
151 | - self::TYPE_IA5_STRING => 'IA5String', |
|
152 | - self::TYPE_UTC_TIME => 'UTCTime', |
|
153 | - self::TYPE_GENERALIZED_TIME => 'GeneralizedTime', |
|
154 | - self::TYPE_GRAPHIC_STRING => 'GraphicString', |
|
155 | - self::TYPE_VISIBLE_STRING => 'VisibleString', |
|
156 | - self::TYPE_GENERAL_STRING => 'GeneralString', |
|
157 | - self::TYPE_UNIVERSAL_STRING => 'UniversalString', |
|
158 | - self::TYPE_CHARACTER_STRING => 'CHARACTER STRING', |
|
159 | - self::TYPE_BMP_STRING => 'BMPString', |
|
160 | - self::TYPE_STRING => 'Any String', |
|
161 | - self::TYPE_TIME => 'Any Time', |
|
162 | - self::TYPE_CONSTRUCTED_STRING => 'Constructed String', |
|
163 | - ]; |
|
164 | - |
|
165 | - /** |
|
166 | - * Element's type tag. |
|
167 | - * |
|
168 | - * @var int |
|
169 | - */ |
|
170 | - protected $_typeTag; |
|
171 | - |
|
172 | - /** |
|
173 | - * Whether type shall be encoded with indefinite length. |
|
174 | - * |
|
175 | - * @var bool |
|
176 | - */ |
|
177 | - protected $_indefiniteLength = false; |
|
178 | - |
|
179 | - /** |
|
180 | - * {@inheritdoc} |
|
181 | - */ |
|
182 | - abstract public function typeClass(): int; |
|
183 | - |
|
184 | - /** |
|
185 | - * {@inheritdoc} |
|
186 | - */ |
|
187 | - abstract public function isConstructed(): bool; |
|
188 | - |
|
189 | - /** |
|
190 | - * Decode element from DER data. |
|
191 | - * |
|
192 | - * @param string $data DER encoded data |
|
193 | - * @param null|int $offset Reference to the variable that contains offset |
|
194 | - * into the data where to start parsing. |
|
195 | - * Variable is updated to the offset next to the |
|
196 | - * parsed element. If null, start from offset 0. |
|
197 | - * |
|
198 | - * @throws DecodeException If decoding fails |
|
199 | - * @throws \UnexpectedValueException If called in the context of an expected |
|
200 | - * type, but decoding yields another type |
|
201 | - * |
|
202 | - * @return ElementBase |
|
203 | - */ |
|
204 | - public static function fromDER(string $data, int &$offset = null): ElementBase |
|
205 | - { |
|
206 | - $idx = $offset ?? 0; |
|
207 | - // decode identifier |
|
208 | - $identifier = Identifier::fromDER($data, $idx); |
|
209 | - // determine class that implements type specific decoding |
|
210 | - $cls = self::_determineImplClass($identifier); |
|
211 | - try { |
|
212 | - // decode remaining element |
|
213 | - $element = $cls::_decodeFromDER($identifier, $data, $idx); |
|
214 | - } catch (\LogicException $e) { |
|
215 | - // rethrow as a RuntimeException for unified exception handling |
|
216 | - throw new DecodeException( |
|
217 | - sprintf('Error while decoding %s.', |
|
218 | - self::tagToName($identifier->intTag())), 0, $e); |
|
219 | - } |
|
220 | - // if called in the context of a concrete class, check |
|
221 | - // that decoded type matches the type of a calling class |
|
222 | - $called_class = get_called_class(); |
|
223 | - if (self::class !== $called_class) { |
|
224 | - if (!$element instanceof $called_class) { |
|
225 | - throw new \UnexpectedValueException( |
|
226 | - sprintf('%s expected, got %s.', $called_class, |
|
227 | - get_class($element))); |
|
228 | - } |
|
229 | - } |
|
230 | - // update offset for the caller |
|
231 | - if (isset($offset)) { |
|
232 | - $offset = $idx; |
|
233 | - } |
|
234 | - return $element; |
|
235 | - } |
|
236 | - |
|
237 | - /** |
|
238 | - * {@inheritdoc} |
|
239 | - */ |
|
240 | - public function toDER(): string |
|
241 | - { |
|
242 | - $identifier = new Identifier($this->typeClass(), |
|
243 | - $this->isConstructed() ? Identifier::CONSTRUCTED : Identifier::PRIMITIVE, |
|
244 | - $this->_typeTag); |
|
245 | - $content = $this->_encodedContentDER(); |
|
246 | - if ($this->_indefiniteLength) { |
|
247 | - $length = new Length(0, true); |
|
248 | - $eoc = new Primitive\EOC(); |
|
249 | - return $identifier->toDER() . $length->toDER() . $content . $eoc->toDER(); |
|
250 | - } |
|
251 | - $length = new Length(strlen($content)); |
|
252 | - return $identifier->toDER() . $length->toDER() . $content; |
|
253 | - } |
|
254 | - |
|
255 | - /** |
|
256 | - * {@inheritdoc} |
|
257 | - */ |
|
258 | - public function tag(): int |
|
259 | - { |
|
260 | - return $this->_typeTag; |
|
261 | - } |
|
262 | - |
|
263 | - /** |
|
264 | - * {@inheritdoc} |
|
265 | - */ |
|
266 | - public function isType(int $tag): bool |
|
267 | - { |
|
268 | - // if element is context specific |
|
269 | - if (Identifier::CLASS_CONTEXT_SPECIFIC === $this->typeClass()) { |
|
270 | - return false; |
|
271 | - } |
|
272 | - // negative tags identify an abstract pseudotype |
|
273 | - if ($tag < 0) { |
|
274 | - return $this->_isPseudoType($tag); |
|
275 | - } |
|
276 | - return $this->_isConcreteType($tag); |
|
277 | - } |
|
278 | - |
|
279 | - /** |
|
280 | - * {@inheritdoc} |
|
281 | - */ |
|
282 | - public function expectType(int $tag): ElementBase |
|
283 | - { |
|
284 | - if (!$this->isType($tag)) { |
|
285 | - throw new \UnexpectedValueException( |
|
286 | - sprintf('%s expected, got %s.', self::tagToName($tag), |
|
287 | - $this->_typeDescriptorString())); |
|
288 | - } |
|
289 | - return $this; |
|
290 | - } |
|
291 | - |
|
292 | - /** |
|
293 | - * {@inheritdoc} |
|
294 | - */ |
|
295 | - public function isTagged(): bool |
|
296 | - { |
|
297 | - return $this instanceof TaggedType; |
|
298 | - } |
|
299 | - |
|
300 | - /** |
|
301 | - * {@inheritdoc} |
|
302 | - */ |
|
303 | - public function expectTagged(?int $tag = null): TaggedType |
|
304 | - { |
|
305 | - if (!$this->isTagged()) { |
|
306 | - throw new \UnexpectedValueException( |
|
307 | - sprintf('Context specific element expected, got %s.', |
|
308 | - Identifier::classToName($this->typeClass()))); |
|
309 | - } |
|
310 | - if (isset($tag) && $this->tag() !== $tag) { |
|
311 | - throw new \UnexpectedValueException( |
|
312 | - sprintf('Tag %d expected, got %d.', $tag, $this->tag())); |
|
313 | - } |
|
314 | - return $this; |
|
315 | - } |
|
316 | - |
|
317 | - /** |
|
318 | - * Whether element has indefinite length. |
|
319 | - * |
|
320 | - * @return bool |
|
321 | - */ |
|
322 | - public function hasIndefiniteLength(): bool |
|
323 | - { |
|
324 | - return $this->_indefiniteLength; |
|
325 | - } |
|
326 | - |
|
327 | - /** |
|
328 | - * Get self with indefinite length encoding set. |
|
329 | - * |
|
330 | - * @param bool $indefinite True for indefinite length, false for definite length |
|
331 | - * |
|
332 | - * @return self |
|
333 | - */ |
|
334 | - public function withIndefiniteLength(bool $indefinite = true): self |
|
335 | - { |
|
336 | - $obj = clone $this; |
|
337 | - $obj->_indefiniteLength = $indefinite; |
|
338 | - return $obj; |
|
339 | - } |
|
340 | - |
|
341 | - /** |
|
342 | - * {@inheritdoc} |
|
343 | - */ |
|
344 | - final public function asElement(): Element |
|
345 | - { |
|
346 | - return $this; |
|
347 | - } |
|
348 | - |
|
349 | - /** |
|
350 | - * Get element decorated with UnspecifiedType object. |
|
351 | - * |
|
352 | - * @return UnspecifiedType |
|
353 | - */ |
|
354 | - public function asUnspecified(): UnspecifiedType |
|
355 | - { |
|
356 | - return new UnspecifiedType($this); |
|
357 | - } |
|
358 | - |
|
359 | - /** |
|
360 | - * Get human readable name for an universal tag. |
|
361 | - * |
|
362 | - * @param int $tag |
|
363 | - * |
|
364 | - * @return string |
|
365 | - */ |
|
366 | - public static function tagToName(int $tag): string |
|
367 | - { |
|
368 | - if (!array_key_exists($tag, self::MAP_TYPE_TO_NAME)) { |
|
369 | - return "TAG {$tag}"; |
|
370 | - } |
|
371 | - return self::MAP_TYPE_TO_NAME[$tag]; |
|
372 | - } |
|
373 | - |
|
374 | - /** |
|
375 | - * Get the content encoded in DER. |
|
376 | - * |
|
377 | - * Returns the DER encoded content without identifier and length header octets. |
|
378 | - * |
|
379 | - * @return string |
|
380 | - */ |
|
381 | - abstract protected function _encodedContentDER(): string; |
|
382 | - |
|
383 | - /** |
|
384 | - * Decode type-specific element from DER. |
|
385 | - * |
|
386 | - * @param Identifier $identifier Pre-parsed identifier |
|
387 | - * @param string $data DER data |
|
388 | - * @param int $offset Offset in data to the next byte after identifier |
|
389 | - * |
|
390 | - * @throws DecodeException If decoding fails |
|
391 | - * |
|
392 | - * @return ElementBase |
|
393 | - */ |
|
394 | - protected static function _decodeFromDER(Identifier $identifier, |
|
395 | - string $data, int &$offset): ElementBase |
|
396 | - { |
|
397 | - throw new \BadMethodCallException( |
|
398 | - __METHOD__ . ' must be implemented in derived class.'); |
|
399 | - } |
|
400 | - |
|
401 | - /** |
|
402 | - * Determine the class that implements the type. |
|
403 | - * |
|
404 | - * @param Identifier $identifier |
|
405 | - * |
|
406 | - * @return string Class name |
|
407 | - */ |
|
408 | - protected static function _determineImplClass(Identifier $identifier): string |
|
409 | - { |
|
410 | - switch ($identifier->typeClass()) { |
|
411 | - case Identifier::CLASS_UNIVERSAL: |
|
412 | - $cls = self::_determineUniversalImplClass($identifier->intTag()); |
|
413 | - // constructed strings may be present in BER |
|
414 | - if ($identifier->isConstructed() && |
|
415 | - is_subclass_of($cls, PrimitiveString::class)) { |
|
416 | - $cls = ConstructedString::class; |
|
417 | - } |
|
418 | - return $cls; |
|
419 | - case Identifier::CLASS_CONTEXT_SPECIFIC: |
|
420 | - return ContextSpecificType::class; |
|
421 | - case Identifier::CLASS_APPLICATION: |
|
422 | - return ApplicationType::class; |
|
423 | - case Identifier::CLASS_PRIVATE: |
|
424 | - return PrivateType::class; |
|
425 | - } |
|
426 | - throw new \UnexpectedValueException( |
|
427 | - sprintf('%s %d not implemented.', |
|
428 | - Identifier::classToName($identifier->typeClass()), |
|
429 | - $identifier->tag())); |
|
430 | - } |
|
431 | - |
|
432 | - /** |
|
433 | - * Determine the class that implements an universal type of the given tag. |
|
434 | - * |
|
435 | - * @param int $tag |
|
436 | - * |
|
437 | - * @throws \UnexpectedValueException |
|
438 | - * |
|
439 | - * @return string Class name |
|
440 | - */ |
|
441 | - protected static function _determineUniversalImplClass(int $tag): string |
|
442 | - { |
|
443 | - if (!array_key_exists($tag, self::MAP_TAG_TO_CLASS)) { |
|
444 | - throw new \UnexpectedValueException( |
|
445 | - "Universal tag {$tag} not implemented."); |
|
446 | - } |
|
447 | - return self::MAP_TAG_TO_CLASS[$tag]; |
|
448 | - } |
|
449 | - |
|
450 | - /** |
|
451 | - * Get textual description of the type for debugging purposes. |
|
452 | - * |
|
453 | - * @return string |
|
454 | - */ |
|
455 | - protected function _typeDescriptorString(): string |
|
456 | - { |
|
457 | - if (Identifier::CLASS_UNIVERSAL === $this->typeClass()) { |
|
458 | - return self::tagToName($this->_typeTag); |
|
459 | - } |
|
460 | - return sprintf('%s TAG %d', Identifier::classToName($this->typeClass()), |
|
461 | - $this->_typeTag); |
|
462 | - } |
|
463 | - |
|
464 | - /** |
|
465 | - * Check whether the element is a concrete type of a given tag. |
|
466 | - * |
|
467 | - * @param int $tag |
|
468 | - * |
|
469 | - * @return bool |
|
470 | - */ |
|
471 | - private function _isConcreteType(int $tag): bool |
|
472 | - { |
|
473 | - // if tag doesn't match |
|
474 | - if ($this->tag() !== $tag) { |
|
475 | - return false; |
|
476 | - } |
|
477 | - // if type is universal check that instance is of a correct class |
|
478 | - if (Identifier::CLASS_UNIVERSAL === $this->typeClass()) { |
|
479 | - $cls = self::_determineUniversalImplClass($tag); |
|
480 | - if (!$this instanceof $cls) { |
|
481 | - return false; |
|
482 | - } |
|
483 | - } |
|
484 | - return true; |
|
485 | - } |
|
486 | - |
|
487 | - /** |
|
488 | - * Check whether the element is a pseudotype. |
|
489 | - * |
|
490 | - * @param int $tag |
|
491 | - * |
|
492 | - * @return bool |
|
493 | - */ |
|
494 | - private function _isPseudoType(int $tag): bool |
|
495 | - { |
|
496 | - switch ($tag) { |
|
497 | - case self::TYPE_STRING: |
|
498 | - return $this instanceof StringType; |
|
499 | - case self::TYPE_TIME: |
|
500 | - return $this instanceof TimeType; |
|
501 | - case self::TYPE_CONSTRUCTED_STRING: |
|
502 | - return $this instanceof ConstructedString; |
|
503 | - } |
|
504 | - return false; |
|
505 | - } |
|
28 | + // Universal type tags |
|
29 | + const TYPE_EOC = 0x00; |
|
30 | + const TYPE_BOOLEAN = 0x01; |
|
31 | + const TYPE_INTEGER = 0x02; |
|
32 | + const TYPE_BIT_STRING = 0x03; |
|
33 | + const TYPE_OCTET_STRING = 0x04; |
|
34 | + const TYPE_NULL = 0x05; |
|
35 | + const TYPE_OBJECT_IDENTIFIER = 0x06; |
|
36 | + const TYPE_OBJECT_DESCRIPTOR = 0x07; |
|
37 | + const TYPE_EXTERNAL = 0x08; |
|
38 | + const TYPE_REAL = 0x09; |
|
39 | + const TYPE_ENUMERATED = 0x0a; |
|
40 | + const TYPE_EMBEDDED_PDV = 0x0b; |
|
41 | + const TYPE_UTF8_STRING = 0x0c; |
|
42 | + const TYPE_RELATIVE_OID = 0x0d; |
|
43 | + const TYPE_SEQUENCE = 0x10; |
|
44 | + const TYPE_SET = 0x11; |
|
45 | + const TYPE_NUMERIC_STRING = 0x12; |
|
46 | + const TYPE_PRINTABLE_STRING = 0x13; |
|
47 | + const TYPE_T61_STRING = 0x14; |
|
48 | + const TYPE_VIDEOTEX_STRING = 0x15; |
|
49 | + const TYPE_IA5_STRING = 0x16; |
|
50 | + const TYPE_UTC_TIME = 0x17; |
|
51 | + const TYPE_GENERALIZED_TIME = 0x18; |
|
52 | + const TYPE_GRAPHIC_STRING = 0x19; |
|
53 | + const TYPE_VISIBLE_STRING = 0x1a; |
|
54 | + const TYPE_GENERAL_STRING = 0x1b; |
|
55 | + const TYPE_UNIVERSAL_STRING = 0x1c; |
|
56 | + const TYPE_CHARACTER_STRING = 0x1d; |
|
57 | + const TYPE_BMP_STRING = 0x1e; |
|
58 | + |
|
59 | + /** |
|
60 | + * Mapping from universal type tag to implementation class name. |
|
61 | + * |
|
62 | + * @internal |
|
63 | + * |
|
64 | + * @var array |
|
65 | + */ |
|
66 | + const MAP_TAG_TO_CLASS = [ |
|
67 | + self::TYPE_EOC => Primitive\EOC::class, |
|
68 | + self::TYPE_BOOLEAN => Primitive\Boolean::class, |
|
69 | + self::TYPE_INTEGER => Primitive\Integer::class, |
|
70 | + self::TYPE_BIT_STRING => Primitive\BitString::class, |
|
71 | + self::TYPE_OCTET_STRING => Primitive\OctetString::class, |
|
72 | + self::TYPE_NULL => Primitive\NullType::class, |
|
73 | + self::TYPE_OBJECT_IDENTIFIER => Primitive\ObjectIdentifier::class, |
|
74 | + self::TYPE_OBJECT_DESCRIPTOR => Primitive\ObjectDescriptor::class, |
|
75 | + self::TYPE_REAL => Primitive\Real::class, |
|
76 | + self::TYPE_ENUMERATED => Primitive\Enumerated::class, |
|
77 | + self::TYPE_UTF8_STRING => Primitive\UTF8String::class, |
|
78 | + self::TYPE_RELATIVE_OID => Primitive\RelativeOID::class, |
|
79 | + self::TYPE_SEQUENCE => Constructed\Sequence::class, |
|
80 | + self::TYPE_SET => Constructed\Set::class, |
|
81 | + self::TYPE_NUMERIC_STRING => Primitive\NumericString::class, |
|
82 | + self::TYPE_PRINTABLE_STRING => Primitive\PrintableString::class, |
|
83 | + self::TYPE_T61_STRING => Primitive\T61String::class, |
|
84 | + self::TYPE_VIDEOTEX_STRING => Primitive\VideotexString::class, |
|
85 | + self::TYPE_IA5_STRING => Primitive\IA5String::class, |
|
86 | + self::TYPE_UTC_TIME => Primitive\UTCTime::class, |
|
87 | + self::TYPE_GENERALIZED_TIME => Primitive\GeneralizedTime::class, |
|
88 | + self::TYPE_GRAPHIC_STRING => Primitive\GraphicString::class, |
|
89 | + self::TYPE_VISIBLE_STRING => Primitive\VisibleString::class, |
|
90 | + self::TYPE_GENERAL_STRING => Primitive\GeneralString::class, |
|
91 | + self::TYPE_UNIVERSAL_STRING => Primitive\UniversalString::class, |
|
92 | + self::TYPE_CHARACTER_STRING => Primitive\CharacterString::class, |
|
93 | + self::TYPE_BMP_STRING => Primitive\BMPString::class, |
|
94 | + ]; |
|
95 | + |
|
96 | + /** |
|
97 | + * Pseudotype for all string types. |
|
98 | + * |
|
99 | + * May be used as an expectation parameter. |
|
100 | + * |
|
101 | + * @var int |
|
102 | + */ |
|
103 | + const TYPE_STRING = -1; |
|
104 | + |
|
105 | + /** |
|
106 | + * Pseudotype for all time types. |
|
107 | + * |
|
108 | + * May be used as an expectation parameter. |
|
109 | + * |
|
110 | + * @var int |
|
111 | + */ |
|
112 | + const TYPE_TIME = -2; |
|
113 | + |
|
114 | + /** |
|
115 | + * Pseudotype for constructed strings. |
|
116 | + * |
|
117 | + * May be used as an expectation parameter. |
|
118 | + * |
|
119 | + * @var int |
|
120 | + */ |
|
121 | + const TYPE_CONSTRUCTED_STRING = -3; |
|
122 | + |
|
123 | + /** |
|
124 | + * Mapping from universal type tag to human readable name. |
|
125 | + * |
|
126 | + * @internal |
|
127 | + * |
|
128 | + * @var array |
|
129 | + */ |
|
130 | + const MAP_TYPE_TO_NAME = [ |
|
131 | + self::TYPE_EOC => 'EOC', |
|
132 | + self::TYPE_BOOLEAN => 'BOOLEAN', |
|
133 | + self::TYPE_INTEGER => 'INTEGER', |
|
134 | + self::TYPE_BIT_STRING => 'BIT STRING', |
|
135 | + self::TYPE_OCTET_STRING => 'OCTET STRING', |
|
136 | + self::TYPE_NULL => 'NULL', |
|
137 | + self::TYPE_OBJECT_IDENTIFIER => 'OBJECT IDENTIFIER', |
|
138 | + self::TYPE_OBJECT_DESCRIPTOR => 'ObjectDescriptor', |
|
139 | + self::TYPE_EXTERNAL => 'EXTERNAL', |
|
140 | + self::TYPE_REAL => 'REAL', |
|
141 | + self::TYPE_ENUMERATED => 'ENUMERATED', |
|
142 | + self::TYPE_EMBEDDED_PDV => 'EMBEDDED PDV', |
|
143 | + self::TYPE_UTF8_STRING => 'UTF8String', |
|
144 | + self::TYPE_RELATIVE_OID => 'RELATIVE-OID', |
|
145 | + self::TYPE_SEQUENCE => 'SEQUENCE', |
|
146 | + self::TYPE_SET => 'SET', |
|
147 | + self::TYPE_NUMERIC_STRING => 'NumericString', |
|
148 | + self::TYPE_PRINTABLE_STRING => 'PrintableString', |
|
149 | + self::TYPE_T61_STRING => 'T61String', |
|
150 | + self::TYPE_VIDEOTEX_STRING => 'VideotexString', |
|
151 | + self::TYPE_IA5_STRING => 'IA5String', |
|
152 | + self::TYPE_UTC_TIME => 'UTCTime', |
|
153 | + self::TYPE_GENERALIZED_TIME => 'GeneralizedTime', |
|
154 | + self::TYPE_GRAPHIC_STRING => 'GraphicString', |
|
155 | + self::TYPE_VISIBLE_STRING => 'VisibleString', |
|
156 | + self::TYPE_GENERAL_STRING => 'GeneralString', |
|
157 | + self::TYPE_UNIVERSAL_STRING => 'UniversalString', |
|
158 | + self::TYPE_CHARACTER_STRING => 'CHARACTER STRING', |
|
159 | + self::TYPE_BMP_STRING => 'BMPString', |
|
160 | + self::TYPE_STRING => 'Any String', |
|
161 | + self::TYPE_TIME => 'Any Time', |
|
162 | + self::TYPE_CONSTRUCTED_STRING => 'Constructed String', |
|
163 | + ]; |
|
164 | + |
|
165 | + /** |
|
166 | + * Element's type tag. |
|
167 | + * |
|
168 | + * @var int |
|
169 | + */ |
|
170 | + protected $_typeTag; |
|
171 | + |
|
172 | + /** |
|
173 | + * Whether type shall be encoded with indefinite length. |
|
174 | + * |
|
175 | + * @var bool |
|
176 | + */ |
|
177 | + protected $_indefiniteLength = false; |
|
178 | + |
|
179 | + /** |
|
180 | + * {@inheritdoc} |
|
181 | + */ |
|
182 | + abstract public function typeClass(): int; |
|
183 | + |
|
184 | + /** |
|
185 | + * {@inheritdoc} |
|
186 | + */ |
|
187 | + abstract public function isConstructed(): bool; |
|
188 | + |
|
189 | + /** |
|
190 | + * Decode element from DER data. |
|
191 | + * |
|
192 | + * @param string $data DER encoded data |
|
193 | + * @param null|int $offset Reference to the variable that contains offset |
|
194 | + * into the data where to start parsing. |
|
195 | + * Variable is updated to the offset next to the |
|
196 | + * parsed element. If null, start from offset 0. |
|
197 | + * |
|
198 | + * @throws DecodeException If decoding fails |
|
199 | + * @throws \UnexpectedValueException If called in the context of an expected |
|
200 | + * type, but decoding yields another type |
|
201 | + * |
|
202 | + * @return ElementBase |
|
203 | + */ |
|
204 | + public static function fromDER(string $data, int &$offset = null): ElementBase |
|
205 | + { |
|
206 | + $idx = $offset ?? 0; |
|
207 | + // decode identifier |
|
208 | + $identifier = Identifier::fromDER($data, $idx); |
|
209 | + // determine class that implements type specific decoding |
|
210 | + $cls = self::_determineImplClass($identifier); |
|
211 | + try { |
|
212 | + // decode remaining element |
|
213 | + $element = $cls::_decodeFromDER($identifier, $data, $idx); |
|
214 | + } catch (\LogicException $e) { |
|
215 | + // rethrow as a RuntimeException for unified exception handling |
|
216 | + throw new DecodeException( |
|
217 | + sprintf('Error while decoding %s.', |
|
218 | + self::tagToName($identifier->intTag())), 0, $e); |
|
219 | + } |
|
220 | + // if called in the context of a concrete class, check |
|
221 | + // that decoded type matches the type of a calling class |
|
222 | + $called_class = get_called_class(); |
|
223 | + if (self::class !== $called_class) { |
|
224 | + if (!$element instanceof $called_class) { |
|
225 | + throw new \UnexpectedValueException( |
|
226 | + sprintf('%s expected, got %s.', $called_class, |
|
227 | + get_class($element))); |
|
228 | + } |
|
229 | + } |
|
230 | + // update offset for the caller |
|
231 | + if (isset($offset)) { |
|
232 | + $offset = $idx; |
|
233 | + } |
|
234 | + return $element; |
|
235 | + } |
|
236 | + |
|
237 | + /** |
|
238 | + * {@inheritdoc} |
|
239 | + */ |
|
240 | + public function toDER(): string |
|
241 | + { |
|
242 | + $identifier = new Identifier($this->typeClass(), |
|
243 | + $this->isConstructed() ? Identifier::CONSTRUCTED : Identifier::PRIMITIVE, |
|
244 | + $this->_typeTag); |
|
245 | + $content = $this->_encodedContentDER(); |
|
246 | + if ($this->_indefiniteLength) { |
|
247 | + $length = new Length(0, true); |
|
248 | + $eoc = new Primitive\EOC(); |
|
249 | + return $identifier->toDER() . $length->toDER() . $content . $eoc->toDER(); |
|
250 | + } |
|
251 | + $length = new Length(strlen($content)); |
|
252 | + return $identifier->toDER() . $length->toDER() . $content; |
|
253 | + } |
|
254 | + |
|
255 | + /** |
|
256 | + * {@inheritdoc} |
|
257 | + */ |
|
258 | + public function tag(): int |
|
259 | + { |
|
260 | + return $this->_typeTag; |
|
261 | + } |
|
262 | + |
|
263 | + /** |
|
264 | + * {@inheritdoc} |
|
265 | + */ |
|
266 | + public function isType(int $tag): bool |
|
267 | + { |
|
268 | + // if element is context specific |
|
269 | + if (Identifier::CLASS_CONTEXT_SPECIFIC === $this->typeClass()) { |
|
270 | + return false; |
|
271 | + } |
|
272 | + // negative tags identify an abstract pseudotype |
|
273 | + if ($tag < 0) { |
|
274 | + return $this->_isPseudoType($tag); |
|
275 | + } |
|
276 | + return $this->_isConcreteType($tag); |
|
277 | + } |
|
278 | + |
|
279 | + /** |
|
280 | + * {@inheritdoc} |
|
281 | + */ |
|
282 | + public function expectType(int $tag): ElementBase |
|
283 | + { |
|
284 | + if (!$this->isType($tag)) { |
|
285 | + throw new \UnexpectedValueException( |
|
286 | + sprintf('%s expected, got %s.', self::tagToName($tag), |
|
287 | + $this->_typeDescriptorString())); |
|
288 | + } |
|
289 | + return $this; |
|
290 | + } |
|
291 | + |
|
292 | + /** |
|
293 | + * {@inheritdoc} |
|
294 | + */ |
|
295 | + public function isTagged(): bool |
|
296 | + { |
|
297 | + return $this instanceof TaggedType; |
|
298 | + } |
|
299 | + |
|
300 | + /** |
|
301 | + * {@inheritdoc} |
|
302 | + */ |
|
303 | + public function expectTagged(?int $tag = null): TaggedType |
|
304 | + { |
|
305 | + if (!$this->isTagged()) { |
|
306 | + throw new \UnexpectedValueException( |
|
307 | + sprintf('Context specific element expected, got %s.', |
|
308 | + Identifier::classToName($this->typeClass()))); |
|
309 | + } |
|
310 | + if (isset($tag) && $this->tag() !== $tag) { |
|
311 | + throw new \UnexpectedValueException( |
|
312 | + sprintf('Tag %d expected, got %d.', $tag, $this->tag())); |
|
313 | + } |
|
314 | + return $this; |
|
315 | + } |
|
316 | + |
|
317 | + /** |
|
318 | + * Whether element has indefinite length. |
|
319 | + * |
|
320 | + * @return bool |
|
321 | + */ |
|
322 | + public function hasIndefiniteLength(): bool |
|
323 | + { |
|
324 | + return $this->_indefiniteLength; |
|
325 | + } |
|
326 | + |
|
327 | + /** |
|
328 | + * Get self with indefinite length encoding set. |
|
329 | + * |
|
330 | + * @param bool $indefinite True for indefinite length, false for definite length |
|
331 | + * |
|
332 | + * @return self |
|
333 | + */ |
|
334 | + public function withIndefiniteLength(bool $indefinite = true): self |
|
335 | + { |
|
336 | + $obj = clone $this; |
|
337 | + $obj->_indefiniteLength = $indefinite; |
|
338 | + return $obj; |
|
339 | + } |
|
340 | + |
|
341 | + /** |
|
342 | + * {@inheritdoc} |
|
343 | + */ |
|
344 | + final public function asElement(): Element |
|
345 | + { |
|
346 | + return $this; |
|
347 | + } |
|
348 | + |
|
349 | + /** |
|
350 | + * Get element decorated with UnspecifiedType object. |
|
351 | + * |
|
352 | + * @return UnspecifiedType |
|
353 | + */ |
|
354 | + public function asUnspecified(): UnspecifiedType |
|
355 | + { |
|
356 | + return new UnspecifiedType($this); |
|
357 | + } |
|
358 | + |
|
359 | + /** |
|
360 | + * Get human readable name for an universal tag. |
|
361 | + * |
|
362 | + * @param int $tag |
|
363 | + * |
|
364 | + * @return string |
|
365 | + */ |
|
366 | + public static function tagToName(int $tag): string |
|
367 | + { |
|
368 | + if (!array_key_exists($tag, self::MAP_TYPE_TO_NAME)) { |
|
369 | + return "TAG {$tag}"; |
|
370 | + } |
|
371 | + return self::MAP_TYPE_TO_NAME[$tag]; |
|
372 | + } |
|
373 | + |
|
374 | + /** |
|
375 | + * Get the content encoded in DER. |
|
376 | + * |
|
377 | + * Returns the DER encoded content without identifier and length header octets. |
|
378 | + * |
|
379 | + * @return string |
|
380 | + */ |
|
381 | + abstract protected function _encodedContentDER(): string; |
|
382 | + |
|
383 | + /** |
|
384 | + * Decode type-specific element from DER. |
|
385 | + * |
|
386 | + * @param Identifier $identifier Pre-parsed identifier |
|
387 | + * @param string $data DER data |
|
388 | + * @param int $offset Offset in data to the next byte after identifier |
|
389 | + * |
|
390 | + * @throws DecodeException If decoding fails |
|
391 | + * |
|
392 | + * @return ElementBase |
|
393 | + */ |
|
394 | + protected static function _decodeFromDER(Identifier $identifier, |
|
395 | + string $data, int &$offset): ElementBase |
|
396 | + { |
|
397 | + throw new \BadMethodCallException( |
|
398 | + __METHOD__ . ' must be implemented in derived class.'); |
|
399 | + } |
|
400 | + |
|
401 | + /** |
|
402 | + * Determine the class that implements the type. |
|
403 | + * |
|
404 | + * @param Identifier $identifier |
|
405 | + * |
|
406 | + * @return string Class name |
|
407 | + */ |
|
408 | + protected static function _determineImplClass(Identifier $identifier): string |
|
409 | + { |
|
410 | + switch ($identifier->typeClass()) { |
|
411 | + case Identifier::CLASS_UNIVERSAL: |
|
412 | + $cls = self::_determineUniversalImplClass($identifier->intTag()); |
|
413 | + // constructed strings may be present in BER |
|
414 | + if ($identifier->isConstructed() && |
|
415 | + is_subclass_of($cls, PrimitiveString::class)) { |
|
416 | + $cls = ConstructedString::class; |
|
417 | + } |
|
418 | + return $cls; |
|
419 | + case Identifier::CLASS_CONTEXT_SPECIFIC: |
|
420 | + return ContextSpecificType::class; |
|
421 | + case Identifier::CLASS_APPLICATION: |
|
422 | + return ApplicationType::class; |
|
423 | + case Identifier::CLASS_PRIVATE: |
|
424 | + return PrivateType::class; |
|
425 | + } |
|
426 | + throw new \UnexpectedValueException( |
|
427 | + sprintf('%s %d not implemented.', |
|
428 | + Identifier::classToName($identifier->typeClass()), |
|
429 | + $identifier->tag())); |
|
430 | + } |
|
431 | + |
|
432 | + /** |
|
433 | + * Determine the class that implements an universal type of the given tag. |
|
434 | + * |
|
435 | + * @param int $tag |
|
436 | + * |
|
437 | + * @throws \UnexpectedValueException |
|
438 | + * |
|
439 | + * @return string Class name |
|
440 | + */ |
|
441 | + protected static function _determineUniversalImplClass(int $tag): string |
|
442 | + { |
|
443 | + if (!array_key_exists($tag, self::MAP_TAG_TO_CLASS)) { |
|
444 | + throw new \UnexpectedValueException( |
|
445 | + "Universal tag {$tag} not implemented."); |
|
446 | + } |
|
447 | + return self::MAP_TAG_TO_CLASS[$tag]; |
|
448 | + } |
|
449 | + |
|
450 | + /** |
|
451 | + * Get textual description of the type for debugging purposes. |
|
452 | + * |
|
453 | + * @return string |
|
454 | + */ |
|
455 | + protected function _typeDescriptorString(): string |
|
456 | + { |
|
457 | + if (Identifier::CLASS_UNIVERSAL === $this->typeClass()) { |
|
458 | + return self::tagToName($this->_typeTag); |
|
459 | + } |
|
460 | + return sprintf('%s TAG %d', Identifier::classToName($this->typeClass()), |
|
461 | + $this->_typeTag); |
|
462 | + } |
|
463 | + |
|
464 | + /** |
|
465 | + * Check whether the element is a concrete type of a given tag. |
|
466 | + * |
|
467 | + * @param int $tag |
|
468 | + * |
|
469 | + * @return bool |
|
470 | + */ |
|
471 | + private function _isConcreteType(int $tag): bool |
|
472 | + { |
|
473 | + // if tag doesn't match |
|
474 | + if ($this->tag() !== $tag) { |
|
475 | + return false; |
|
476 | + } |
|
477 | + // if type is universal check that instance is of a correct class |
|
478 | + if (Identifier::CLASS_UNIVERSAL === $this->typeClass()) { |
|
479 | + $cls = self::_determineUniversalImplClass($tag); |
|
480 | + if (!$this instanceof $cls) { |
|
481 | + return false; |
|
482 | + } |
|
483 | + } |
|
484 | + return true; |
|
485 | + } |
|
486 | + |
|
487 | + /** |
|
488 | + * Check whether the element is a pseudotype. |
|
489 | + * |
|
490 | + * @param int $tag |
|
491 | + * |
|
492 | + * @return bool |
|
493 | + */ |
|
494 | + private function _isPseudoType(int $tag): bool |
|
495 | + { |
|
496 | + switch ($tag) { |
|
497 | + case self::TYPE_STRING: |
|
498 | + return $this instanceof StringType; |
|
499 | + case self::TYPE_TIME: |
|
500 | + return $this instanceof TimeType; |
|
501 | + case self::TYPE_CONSTRUCTED_STRING: |
|
502 | + return $this instanceof ConstructedString; |
|
503 | + } |
|
504 | + return false; |
|
505 | + } |
|
506 | 506 | } |
@@ -408,20 +408,20 @@ discard block |
||
408 | 408 | protected static function _determineImplClass(Identifier $identifier): string |
409 | 409 | { |
410 | 410 | switch ($identifier->typeClass()) { |
411 | - case Identifier::CLASS_UNIVERSAL: |
|
412 | - $cls = self::_determineUniversalImplClass($identifier->intTag()); |
|
413 | - // constructed strings may be present in BER |
|
414 | - if ($identifier->isConstructed() && |
|
415 | - is_subclass_of($cls, PrimitiveString::class)) { |
|
416 | - $cls = ConstructedString::class; |
|
417 | - } |
|
418 | - return $cls; |
|
419 | - case Identifier::CLASS_CONTEXT_SPECIFIC: |
|
420 | - return ContextSpecificType::class; |
|
421 | - case Identifier::CLASS_APPLICATION: |
|
422 | - return ApplicationType::class; |
|
423 | - case Identifier::CLASS_PRIVATE: |
|
424 | - return PrivateType::class; |
|
411 | + case Identifier::CLASS_UNIVERSAL: |
|
412 | + $cls = self::_determineUniversalImplClass($identifier->intTag()); |
|
413 | + // constructed strings may be present in BER |
|
414 | + if ($identifier->isConstructed() && |
|
415 | + is_subclass_of($cls, PrimitiveString::class)) { |
|
416 | + $cls = ConstructedString::class; |
|
417 | + } |
|
418 | + return $cls; |
|
419 | + case Identifier::CLASS_CONTEXT_SPECIFIC: |
|
420 | + return ContextSpecificType::class; |
|
421 | + case Identifier::CLASS_APPLICATION: |
|
422 | + return ApplicationType::class; |
|
423 | + case Identifier::CLASS_PRIVATE: |
|
424 | + return PrivateType::class; |
|
425 | 425 | } |
426 | 426 | throw new \UnexpectedValueException( |
427 | 427 | sprintf('%s %d not implemented.', |
@@ -494,12 +494,12 @@ discard block |
||
494 | 494 | private function _isPseudoType(int $tag): bool |
495 | 495 | { |
496 | 496 | switch ($tag) { |
497 | - case self::TYPE_STRING: |
|
498 | - return $this instanceof StringType; |
|
499 | - case self::TYPE_TIME: |
|
500 | - return $this instanceof TimeType; |
|
501 | - case self::TYPE_CONSTRUCTED_STRING: |
|
502 | - return $this instanceof ConstructedString; |
|
497 | + case self::TYPE_STRING: |
|
498 | + return $this instanceof StringType; |
|
499 | + case self::TYPE_TIME: |
|
500 | + return $this instanceof TimeType; |
|
501 | + case self::TYPE_CONSTRUCTED_STRING: |
|
502 | + return $this instanceof ConstructedString; |
|
503 | 503 | } |
504 | 504 | return false; |
505 | 505 | } |
@@ -12,56 +12,56 @@ |
||
12 | 12 | */ |
13 | 13 | class Set extends Structure |
14 | 14 | { |
15 | - /** |
|
16 | - * Constructor. |
|
17 | - * |
|
18 | - * @param Element ...$elements Any number of elements |
|
19 | - */ |
|
20 | - public function __construct(Element ...$elements) |
|
21 | - { |
|
22 | - $this->_typeTag = self::TYPE_SET; |
|
23 | - parent::__construct(...$elements); |
|
24 | - } |
|
15 | + /** |
|
16 | + * Constructor. |
|
17 | + * |
|
18 | + * @param Element ...$elements Any number of elements |
|
19 | + */ |
|
20 | + public function __construct(Element ...$elements) |
|
21 | + { |
|
22 | + $this->_typeTag = self::TYPE_SET; |
|
23 | + parent::__construct(...$elements); |
|
24 | + } |
|
25 | 25 | |
26 | - /** |
|
27 | - * Sort by canonical ascending order. |
|
28 | - * |
|
29 | - * Used for DER encoding of SET type. |
|
30 | - * |
|
31 | - * @return self |
|
32 | - */ |
|
33 | - public function sortedSet(): self |
|
34 | - { |
|
35 | - $obj = clone $this; |
|
36 | - usort($obj->_elements, |
|
37 | - function (Element $a, Element $b) { |
|
38 | - if ($a->typeClass() !== $b->typeClass()) { |
|
39 | - return $a->typeClass() < $b->typeClass() ? -1 : 1; |
|
40 | - } |
|
41 | - if ($a->tag() === $b->tag()) { |
|
42 | - return 0; |
|
43 | - } |
|
44 | - return $a->tag() < $b->tag() ? -1 : 1; |
|
45 | - }); |
|
46 | - return $obj; |
|
47 | - } |
|
26 | + /** |
|
27 | + * Sort by canonical ascending order. |
|
28 | + * |
|
29 | + * Used for DER encoding of SET type. |
|
30 | + * |
|
31 | + * @return self |
|
32 | + */ |
|
33 | + public function sortedSet(): self |
|
34 | + { |
|
35 | + $obj = clone $this; |
|
36 | + usort($obj->_elements, |
|
37 | + function (Element $a, Element $b) { |
|
38 | + if ($a->typeClass() !== $b->typeClass()) { |
|
39 | + return $a->typeClass() < $b->typeClass() ? -1 : 1; |
|
40 | + } |
|
41 | + if ($a->tag() === $b->tag()) { |
|
42 | + return 0; |
|
43 | + } |
|
44 | + return $a->tag() < $b->tag() ? -1 : 1; |
|
45 | + }); |
|
46 | + return $obj; |
|
47 | + } |
|
48 | 48 | |
49 | - /** |
|
50 | - * Sort by encoding ascending order. |
|
51 | - * |
|
52 | - * Used for DER encoding of SET OF type. |
|
53 | - * |
|
54 | - * @return self |
|
55 | - */ |
|
56 | - public function sortedSetOf(): self |
|
57 | - { |
|
58 | - $obj = clone $this; |
|
59 | - usort($obj->_elements, |
|
60 | - function (Element $a, Element $b) { |
|
61 | - $a_der = $a->toDER(); |
|
62 | - $b_der = $b->toDER(); |
|
63 | - return strcmp($a_der, $b_der); |
|
64 | - }); |
|
65 | - return $obj; |
|
66 | - } |
|
49 | + /** |
|
50 | + * Sort by encoding ascending order. |
|
51 | + * |
|
52 | + * Used for DER encoding of SET OF type. |
|
53 | + * |
|
54 | + * @return self |
|
55 | + */ |
|
56 | + public function sortedSetOf(): self |
|
57 | + { |
|
58 | + $obj = clone $this; |
|
59 | + usort($obj->_elements, |
|
60 | + function (Element $a, Element $b) { |
|
61 | + $a_der = $a->toDER(); |
|
62 | + $b_der = $b->toDER(); |
|
63 | + return strcmp($a_der, $b_der); |
|
64 | + }); |
|
65 | + return $obj; |
|
66 | + } |
|
67 | 67 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\ASN1\Type\Constructed; |
6 | 6 | |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | { |
35 | 35 | $obj = clone $this; |
36 | 36 | usort($obj->_elements, |
37 | - function (Element $a, Element $b) { |
|
37 | + function(Element $a, Element $b) { |
|
38 | 38 | if ($a->typeClass() !== $b->typeClass()) { |
39 | 39 | return $a->typeClass() < $b->typeClass() ? -1 : 1; |
40 | 40 | } |
@@ -57,7 +57,7 @@ discard block |
||
57 | 57 | { |
58 | 58 | $obj = clone $this; |
59 | 59 | usort($obj->_elements, |
60 | - function (Element $a, Element $b) { |
|
60 | + function(Element $a, Element $b) { |
|
61 | 61 | $a_der = $a->toDER(); |
62 | 62 | $b_der = $b->toDER(); |
63 | 63 | return strcmp($a_der, $b_der); |
@@ -26,108 +26,108 @@ |
||
26 | 26 | */ |
27 | 27 | class ConstructedString extends Structure implements Stringable |
28 | 28 | { |
29 | - /** |
|
30 | - * Constructor. |
|
31 | - * |
|
32 | - * @internal Use create() method instead |
|
33 | - * |
|
34 | - * @param Element ...$elements Any number of elements |
|
35 | - */ |
|
36 | - protected function __construct(Element ...$elements) |
|
37 | - { |
|
38 | - parent::__construct(...$elements); |
|
39 | - } |
|
29 | + /** |
|
30 | + * Constructor. |
|
31 | + * |
|
32 | + * @internal Use create() method instead |
|
33 | + * |
|
34 | + * @param Element ...$elements Any number of elements |
|
35 | + */ |
|
36 | + protected function __construct(Element ...$elements) |
|
37 | + { |
|
38 | + parent::__construct(...$elements); |
|
39 | + } |
|
40 | 40 | |
41 | - /** |
|
42 | - * {@inheritdoc} |
|
43 | - * |
|
44 | - * @return string |
|
45 | - */ |
|
46 | - public function __toString(): string |
|
47 | - { |
|
48 | - return $this->string(); |
|
49 | - } |
|
41 | + /** |
|
42 | + * {@inheritdoc} |
|
43 | + * |
|
44 | + * @return string |
|
45 | + */ |
|
46 | + public function __toString(): string |
|
47 | + { |
|
48 | + return $this->string(); |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * Create from a list of string type elements. |
|
53 | - * |
|
54 | - * All strings must have the same type. |
|
55 | - * |
|
56 | - * @param StringType ...$elements |
|
57 | - * |
|
58 | - * @throws \LogicException |
|
59 | - * |
|
60 | - * @return self |
|
61 | - */ |
|
62 | - public static function create(StringType ...$elements): self |
|
63 | - { |
|
64 | - if (!count($elements)) { |
|
65 | - throw new \LogicException( |
|
66 | - 'No elements, unable to determine type tag.'); |
|
67 | - } |
|
68 | - $tag = $elements[0]->tag(); |
|
69 | - foreach ($elements as $el) { |
|
70 | - if ($el->tag() !== $tag) { |
|
71 | - throw new \LogicException( |
|
72 | - 'All elements in constructed string must have the same type.'); |
|
73 | - } |
|
74 | - } |
|
75 | - return self::createWithTag($tag, ...$elements); |
|
76 | - } |
|
51 | + /** |
|
52 | + * Create from a list of string type elements. |
|
53 | + * |
|
54 | + * All strings must have the same type. |
|
55 | + * |
|
56 | + * @param StringType ...$elements |
|
57 | + * |
|
58 | + * @throws \LogicException |
|
59 | + * |
|
60 | + * @return self |
|
61 | + */ |
|
62 | + public static function create(StringType ...$elements): self |
|
63 | + { |
|
64 | + if (!count($elements)) { |
|
65 | + throw new \LogicException( |
|
66 | + 'No elements, unable to determine type tag.'); |
|
67 | + } |
|
68 | + $tag = $elements[0]->tag(); |
|
69 | + foreach ($elements as $el) { |
|
70 | + if ($el->tag() !== $tag) { |
|
71 | + throw new \LogicException( |
|
72 | + 'All elements in constructed string must have the same type.'); |
|
73 | + } |
|
74 | + } |
|
75 | + return self::createWithTag($tag, ...$elements); |
|
76 | + } |
|
77 | 77 | |
78 | - /** |
|
79 | - * Create from strings with a given type tag. |
|
80 | - * |
|
81 | - * Does not perform any validation on types. |
|
82 | - * |
|
83 | - * @param int $tag Type tag for the constructed string element |
|
84 | - * @param StringType ...$elements Any number of elements |
|
85 | - * |
|
86 | - * @return self |
|
87 | - */ |
|
88 | - public static function createWithTag(int $tag, StringType ...$elements) |
|
89 | - { |
|
90 | - $el = new self(...$elements); |
|
91 | - $el->_typeTag = $tag; |
|
92 | - return $el; |
|
93 | - } |
|
78 | + /** |
|
79 | + * Create from strings with a given type tag. |
|
80 | + * |
|
81 | + * Does not perform any validation on types. |
|
82 | + * |
|
83 | + * @param int $tag Type tag for the constructed string element |
|
84 | + * @param StringType ...$elements Any number of elements |
|
85 | + * |
|
86 | + * @return self |
|
87 | + */ |
|
88 | + public static function createWithTag(int $tag, StringType ...$elements) |
|
89 | + { |
|
90 | + $el = new self(...$elements); |
|
91 | + $el->_typeTag = $tag; |
|
92 | + return $el; |
|
93 | + } |
|
94 | 94 | |
95 | - /** |
|
96 | - * Get a list of strings in this structure. |
|
97 | - * |
|
98 | - * @return string[] |
|
99 | - */ |
|
100 | - public function strings(): array |
|
101 | - { |
|
102 | - return array_map(function (StringType $el) { |
|
103 | - return $el->string(); |
|
104 | - }, $this->_elements); |
|
105 | - } |
|
95 | + /** |
|
96 | + * Get a list of strings in this structure. |
|
97 | + * |
|
98 | + * @return string[] |
|
99 | + */ |
|
100 | + public function strings(): array |
|
101 | + { |
|
102 | + return array_map(function (StringType $el) { |
|
103 | + return $el->string(); |
|
104 | + }, $this->_elements); |
|
105 | + } |
|
106 | 106 | |
107 | - /** |
|
108 | - * Get the contained strings concatenated together. |
|
109 | - * |
|
110 | - * NOTE: It's unclear how bit strings with unused bits should be concatentated. |
|
111 | - * |
|
112 | - * @return string |
|
113 | - */ |
|
114 | - public function string(): string |
|
115 | - { |
|
116 | - return implode('', $this->strings()); |
|
117 | - } |
|
107 | + /** |
|
108 | + * Get the contained strings concatenated together. |
|
109 | + * |
|
110 | + * NOTE: It's unclear how bit strings with unused bits should be concatentated. |
|
111 | + * |
|
112 | + * @return string |
|
113 | + */ |
|
114 | + public function string(): string |
|
115 | + { |
|
116 | + return implode('', $this->strings()); |
|
117 | + } |
|
118 | 118 | |
119 | - /** |
|
120 | - * {@inheritdoc} |
|
121 | - * |
|
122 | - * @return self |
|
123 | - */ |
|
124 | - protected static function _decodeFromDER(Identifier $identifier, |
|
125 | - string $data, int &$offset): ElementBase |
|
126 | - { |
|
127 | - /** @var ConstructedString $type */ |
|
128 | - $type = forward_static_call_array([parent::class, __FUNCTION__], |
|
129 | - [$identifier, $data, &$offset]); |
|
130 | - $type->_typeTag = $identifier->intTag(); |
|
131 | - return $type; |
|
132 | - } |
|
119 | + /** |
|
120 | + * {@inheritdoc} |
|
121 | + * |
|
122 | + * @return self |
|
123 | + */ |
|
124 | + protected static function _decodeFromDER(Identifier $identifier, |
|
125 | + string $data, int &$offset): ElementBase |
|
126 | + { |
|
127 | + /** @var ConstructedString $type */ |
|
128 | + $type = forward_static_call_array([parent::class, __FUNCTION__], |
|
129 | + [$identifier, $data, &$offset]); |
|
130 | + $type->_typeTag = $identifier->intTag(); |
|
131 | + return $type; |
|
132 | + } |
|
133 | 133 | } |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\ASN1\Type\Constructed; |
6 | 6 | |
@@ -99,7 +99,7 @@ discard block |
||
99 | 99 | */ |
100 | 100 | public function strings(): array |
101 | 101 | { |
102 | - return array_map(function (StringType $el) { |
|
102 | + return array_map(function(StringType $el) { |
|
103 | 103 | return $el->string(); |
104 | 104 | }, $this->_elements); |
105 | 105 | } |
@@ -15,402 +15,402 @@ |
||
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 | - * @param int $idx Index 0..n |
|
242 | - * |
|
243 | - * @throws \OutOfBoundsException If element doesn't exists |
|
244 | - * @throws \UnexpectedValueException If expectation fails |
|
245 | - * |
|
246 | - * @return UnspecifiedType |
|
247 | - */ |
|
248 | - public function at(int $idx): UnspecifiedType |
|
249 | - { |
|
250 | - if (!isset($this->_elements[$idx])) { |
|
251 | - throw new \OutOfBoundsException( |
|
252 | - "Structure doesn't have an element at index {$idx}."); |
|
253 | - } |
|
254 | - return new UnspecifiedType($this->_elements[$idx]); |
|
255 | - } |
|
237 | + /** |
|
238 | + * Get the element at the given index, optionally checking that the element |
|
239 | + * has a given tag. |
|
240 | + * |
|
241 | + * @param int $idx Index 0..n |
|
242 | + * |
|
243 | + * @throws \OutOfBoundsException If element doesn't exists |
|
244 | + * @throws \UnexpectedValueException If expectation fails |
|
245 | + * |
|
246 | + * @return UnspecifiedType |
|
247 | + */ |
|
248 | + public function at(int $idx): UnspecifiedType |
|
249 | + { |
|
250 | + if (!isset($this->_elements[$idx])) { |
|
251 | + throw new \OutOfBoundsException( |
|
252 | + "Structure doesn't have an element at index {$idx}."); |
|
253 | + } |
|
254 | + return new UnspecifiedType($this->_elements[$idx]); |
|
255 | + } |
|
256 | 256 | |
257 | - /** |
|
258 | - * Check whether the structure contains a context specific element with a |
|
259 | - * given tag. |
|
260 | - * |
|
261 | - * @param int $tag Tag number |
|
262 | - * |
|
263 | - * @return bool |
|
264 | - */ |
|
265 | - public function hasTagged(int $tag): bool |
|
266 | - { |
|
267 | - // lazily build lookup map |
|
268 | - if (!isset($this->_taggedMap)) { |
|
269 | - $this->_taggedMap = []; |
|
270 | - foreach ($this->_elements as $element) { |
|
271 | - if ($element->isTagged()) { |
|
272 | - $this->_taggedMap[$element->tag()] = $element; |
|
273 | - } |
|
274 | - } |
|
275 | - } |
|
276 | - return isset($this->_taggedMap[$tag]); |
|
277 | - } |
|
257 | + /** |
|
258 | + * Check whether the structure contains a context specific element with a |
|
259 | + * given tag. |
|
260 | + * |
|
261 | + * @param int $tag Tag number |
|
262 | + * |
|
263 | + * @return bool |
|
264 | + */ |
|
265 | + public function hasTagged(int $tag): bool |
|
266 | + { |
|
267 | + // lazily build lookup map |
|
268 | + if (!isset($this->_taggedMap)) { |
|
269 | + $this->_taggedMap = []; |
|
270 | + foreach ($this->_elements as $element) { |
|
271 | + if ($element->isTagged()) { |
|
272 | + $this->_taggedMap[$element->tag()] = $element; |
|
273 | + } |
|
274 | + } |
|
275 | + } |
|
276 | + return isset($this->_taggedMap[$tag]); |
|
277 | + } |
|
278 | 278 | |
279 | - /** |
|
280 | - * Get a context specific element tagged with a given tag. |
|
281 | - * |
|
282 | - * @param int $tag |
|
283 | - * |
|
284 | - * @throws \LogicException If tag doesn't exists |
|
285 | - * |
|
286 | - * @return TaggedType |
|
287 | - */ |
|
288 | - public function getTagged(int $tag): TaggedType |
|
289 | - { |
|
290 | - if (!$this->hasTagged($tag)) { |
|
291 | - throw new \LogicException("No tagged element for tag {$tag}."); |
|
292 | - } |
|
293 | - return $this->_taggedMap[$tag]; |
|
294 | - } |
|
279 | + /** |
|
280 | + * Get a context specific element tagged with a given tag. |
|
281 | + * |
|
282 | + * @param int $tag |
|
283 | + * |
|
284 | + * @throws \LogicException If tag doesn't exists |
|
285 | + * |
|
286 | + * @return TaggedType |
|
287 | + */ |
|
288 | + public function getTagged(int $tag): TaggedType |
|
289 | + { |
|
290 | + if (!$this->hasTagged($tag)) { |
|
291 | + throw new \LogicException("No tagged element for tag {$tag}."); |
|
292 | + } |
|
293 | + return $this->_taggedMap[$tag]; |
|
294 | + } |
|
295 | 295 | |
296 | - /** |
|
297 | - * @see \Countable::count() |
|
298 | - * |
|
299 | - * @return int |
|
300 | - */ |
|
301 | - public function count(): int |
|
302 | - { |
|
303 | - return count($this->_elements); |
|
304 | - } |
|
296 | + /** |
|
297 | + * @see \Countable::count() |
|
298 | + * |
|
299 | + * @return int |
|
300 | + */ |
|
301 | + public function count(): int |
|
302 | + { |
|
303 | + return count($this->_elements); |
|
304 | + } |
|
305 | 305 | |
306 | - /** |
|
307 | - * Get an iterator for the UnspecifiedElement objects. |
|
308 | - * |
|
309 | - * @see \IteratorAggregate::getIterator() |
|
310 | - * |
|
311 | - * @return \ArrayIterator |
|
312 | - */ |
|
313 | - public function getIterator(): \ArrayIterator |
|
314 | - { |
|
315 | - return new \ArrayIterator($this->elements()); |
|
316 | - } |
|
306 | + /** |
|
307 | + * Get an iterator for the UnspecifiedElement objects. |
|
308 | + * |
|
309 | + * @see \IteratorAggregate::getIterator() |
|
310 | + * |
|
311 | + * @return \ArrayIterator |
|
312 | + */ |
|
313 | + public function getIterator(): \ArrayIterator |
|
314 | + { |
|
315 | + return new \ArrayIterator($this->elements()); |
|
316 | + } |
|
317 | 317 | |
318 | - /** |
|
319 | - * {@inheritdoc} |
|
320 | - */ |
|
321 | - protected function _encodedContentDER(): string |
|
322 | - { |
|
323 | - $data = ''; |
|
324 | - foreach ($this->_elements as $element) { |
|
325 | - $data .= $element->toDER(); |
|
326 | - } |
|
327 | - return $data; |
|
328 | - } |
|
318 | + /** |
|
319 | + * {@inheritdoc} |
|
320 | + */ |
|
321 | + protected function _encodedContentDER(): string |
|
322 | + { |
|
323 | + $data = ''; |
|
324 | + foreach ($this->_elements as $element) { |
|
325 | + $data .= $element->toDER(); |
|
326 | + } |
|
327 | + return $data; |
|
328 | + } |
|
329 | 329 | |
330 | - /** |
|
331 | - * {@inheritdoc} |
|
332 | - * |
|
333 | - * @return self |
|
334 | - */ |
|
335 | - protected static function _decodeFromDER(Identifier $identifier, |
|
336 | - string $data, int &$offset): ElementBase |
|
337 | - { |
|
338 | - if (!$identifier->isConstructed()) { |
|
339 | - throw new DecodeException( |
|
340 | - 'Structured element must have constructed bit set.'); |
|
341 | - } |
|
342 | - $idx = $offset; |
|
343 | - $length = Length::expectFromDER($data, $idx); |
|
344 | - if ($length->isIndefinite()) { |
|
345 | - $type = self::_decodeIndefiniteLength($data, $idx); |
|
346 | - } else { |
|
347 | - $type = self::_decodeDefiniteLength($data, $idx, |
|
348 | - $length->intLength()); |
|
349 | - } |
|
350 | - $offset = $idx; |
|
351 | - return $type; |
|
352 | - } |
|
330 | + /** |
|
331 | + * {@inheritdoc} |
|
332 | + * |
|
333 | + * @return self |
|
334 | + */ |
|
335 | + protected static function _decodeFromDER(Identifier $identifier, |
|
336 | + string $data, int &$offset): ElementBase |
|
337 | + { |
|
338 | + if (!$identifier->isConstructed()) { |
|
339 | + throw new DecodeException( |
|
340 | + 'Structured element must have constructed bit set.'); |
|
341 | + } |
|
342 | + $idx = $offset; |
|
343 | + $length = Length::expectFromDER($data, $idx); |
|
344 | + if ($length->isIndefinite()) { |
|
345 | + $type = self::_decodeIndefiniteLength($data, $idx); |
|
346 | + } else { |
|
347 | + $type = self::_decodeDefiniteLength($data, $idx, |
|
348 | + $length->intLength()); |
|
349 | + } |
|
350 | + $offset = $idx; |
|
351 | + return $type; |
|
352 | + } |
|
353 | 353 | |
354 | - /** |
|
355 | - * Decode elements for a definite length. |
|
356 | - * |
|
357 | - * @param string $data DER data |
|
358 | - * @param int $offset Offset to data |
|
359 | - * @param int $length Number of bytes to decode |
|
360 | - * |
|
361 | - * @throws DecodeException |
|
362 | - * |
|
363 | - * @return ElementBase |
|
364 | - */ |
|
365 | - private static function _decodeDefiniteLength(string $data, int &$offset, |
|
366 | - int $length): ElementBase |
|
367 | - { |
|
368 | - $idx = $offset; |
|
369 | - $end = $idx + $length; |
|
370 | - $elements = []; |
|
371 | - while ($idx < $end) { |
|
372 | - $elements[] = Element::fromDER($data, $idx); |
|
373 | - // check that element didn't overflow length |
|
374 | - if ($idx > $end) { |
|
375 | - throw new DecodeException( |
|
376 | - "Structure's content overflows length."); |
|
377 | - } |
|
378 | - } |
|
379 | - $offset = $idx; |
|
380 | - // return instance by static late binding |
|
381 | - return new static(...$elements); |
|
382 | - } |
|
354 | + /** |
|
355 | + * Decode elements for a definite length. |
|
356 | + * |
|
357 | + * @param string $data DER data |
|
358 | + * @param int $offset Offset to data |
|
359 | + * @param int $length Number of bytes to decode |
|
360 | + * |
|
361 | + * @throws DecodeException |
|
362 | + * |
|
363 | + * @return ElementBase |
|
364 | + */ |
|
365 | + private static function _decodeDefiniteLength(string $data, int &$offset, |
|
366 | + int $length): ElementBase |
|
367 | + { |
|
368 | + $idx = $offset; |
|
369 | + $end = $idx + $length; |
|
370 | + $elements = []; |
|
371 | + while ($idx < $end) { |
|
372 | + $elements[] = Element::fromDER($data, $idx); |
|
373 | + // check that element didn't overflow length |
|
374 | + if ($idx > $end) { |
|
375 | + throw new DecodeException( |
|
376 | + "Structure's content overflows length."); |
|
377 | + } |
|
378 | + } |
|
379 | + $offset = $idx; |
|
380 | + // return instance by static late binding |
|
381 | + return new static(...$elements); |
|
382 | + } |
|
383 | 383 | |
384 | - /** |
|
385 | - * Decode elements for an indefinite length. |
|
386 | - * |
|
387 | - * @param string $data DER data |
|
388 | - * @param int $offset Offset to data |
|
389 | - * |
|
390 | - * @throws DecodeException |
|
391 | - * |
|
392 | - * @return ElementBase |
|
393 | - */ |
|
394 | - private static function _decodeIndefiniteLength( |
|
395 | - string $data, int &$offset): ElementBase |
|
396 | - { |
|
397 | - $idx = $offset; |
|
398 | - $elements = []; |
|
399 | - $end = strlen($data); |
|
400 | - while (true) { |
|
401 | - if ($idx >= $end) { |
|
402 | - throw new DecodeException( |
|
403 | - 'Unexpected end of data while decoding indefinite length structure.'); |
|
404 | - } |
|
405 | - $el = Element::fromDER($data, $idx); |
|
406 | - if ($el->isType(self::TYPE_EOC)) { |
|
407 | - break; |
|
408 | - } |
|
409 | - $elements[] = $el; |
|
410 | - } |
|
411 | - $offset = $idx; |
|
412 | - $type = new static(...$elements); |
|
413 | - $type->_indefiniteLength = true; |
|
414 | - return $type; |
|
415 | - } |
|
384 | + /** |
|
385 | + * Decode elements for an indefinite length. |
|
386 | + * |
|
387 | + * @param string $data DER data |
|
388 | + * @param int $offset Offset to data |
|
389 | + * |
|
390 | + * @throws DecodeException |
|
391 | + * |
|
392 | + * @return ElementBase |
|
393 | + */ |
|
394 | + private static function _decodeIndefiniteLength( |
|
395 | + string $data, int &$offset): ElementBase |
|
396 | + { |
|
397 | + $idx = $offset; |
|
398 | + $elements = []; |
|
399 | + $end = strlen($data); |
|
400 | + while (true) { |
|
401 | + if ($idx >= $end) { |
|
402 | + throw new DecodeException( |
|
403 | + 'Unexpected end of data while decoding indefinite length structure.'); |
|
404 | + } |
|
405 | + $el = Element::fromDER($data, $idx); |
|
406 | + if ($el->isType(self::TYPE_EOC)) { |
|
407 | + break; |
|
408 | + } |
|
409 | + $elements[] = $el; |
|
410 | + } |
|
411 | + $offset = $idx; |
|
412 | + $type = new static(...$elements); |
|
413 | + $type->_indefiniteLength = true; |
|
414 | + return $type; |
|
415 | + } |
|
416 | 416 | } |
@@ -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 | } |
@@ -11,25 +11,25 @@ |
||
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 | - * {@inheritdoc} |
|
30 | - */ |
|
31 | - public function typeClass(): int |
|
32 | - { |
|
33 | - return $this->_class; |
|
34 | - } |
|
28 | + /** |
|
29 | + * {@inheritdoc} |
|
30 | + */ |
|
31 | + public function typeClass(): int |
|
32 | + { |
|
33 | + return $this->_class; |
|
34 | + } |
|
35 | 35 | } |
@@ -17,675 +17,675 @@ |
||
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 | - * Initialize from DER data. |
|
39 | - * |
|
40 | - * @param string $data DER encoded data |
|
41 | - * |
|
42 | - * @return self |
|
43 | - */ |
|
44 | - public static function fromDER(string $data): self |
|
45 | - { |
|
46 | - return Element::fromDER($data)->asUnspecified(); |
|
47 | - } |
|
48 | - |
|
49 | - /** |
|
50 | - * Initialize from ElementBase interface. |
|
51 | - * |
|
52 | - * @param ElementBase $el |
|
53 | - * |
|
54 | - * @return self |
|
55 | - */ |
|
56 | - public static function fromElementBase(ElementBase $el): self |
|
57 | - { |
|
58 | - // if element is already wrapped |
|
59 | - if ($el instanceof self) { |
|
60 | - return $el; |
|
61 | - } |
|
62 | - return new self($el->asElement()); |
|
63 | - } |
|
64 | - |
|
65 | - /** |
|
66 | - * Get the wrapped element as a context specific tagged type. |
|
67 | - * |
|
68 | - * @throws \UnexpectedValueException If the element is not tagged |
|
69 | - * |
|
70 | - * @return TaggedType |
|
71 | - */ |
|
72 | - public function asTagged(): TaggedType |
|
73 | - { |
|
74 | - if (!$this->_element instanceof TaggedType) { |
|
75 | - throw new \UnexpectedValueException( |
|
76 | - 'Tagged element expected, got ' . $this->_typeDescriptorString()); |
|
77 | - } |
|
78 | - return $this->_element; |
|
79 | - } |
|
80 | - |
|
81 | - /** |
|
82 | - * Get the wrapped element as an application specific type. |
|
83 | - * |
|
84 | - * @throws \UnexpectedValueException If element is not application specific |
|
85 | - * |
|
86 | - * @return \Sop\ASN1\Type\Tagged\ApplicationType |
|
87 | - */ |
|
88 | - public function asApplication(): Tagged\ApplicationType |
|
89 | - { |
|
90 | - if (!$this->_element instanceof Tagged\ApplicationType) { |
|
91 | - throw new \UnexpectedValueException( |
|
92 | - 'Application type expected, got ' . $this->_typeDescriptorString()); |
|
93 | - } |
|
94 | - return $this->_element; |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * Get the wrapped element as a private tagged type. |
|
99 | - * |
|
100 | - * @throws \UnexpectedValueException If element is not using private tagging |
|
101 | - * |
|
102 | - * @return \Sop\ASN1\Type\Tagged\PrivateType |
|
103 | - */ |
|
104 | - public function asPrivate(): Tagged\PrivateType |
|
105 | - { |
|
106 | - if (!$this->_element instanceof Tagged\PrivateType) { |
|
107 | - throw new \UnexpectedValueException( |
|
108 | - 'Private type expected, got ' . $this->_typeDescriptorString()); |
|
109 | - } |
|
110 | - return $this->_element; |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * Get the wrapped element as a boolean type. |
|
115 | - * |
|
116 | - * @throws \UnexpectedValueException If the element is not a boolean |
|
117 | - * |
|
118 | - * @return \Sop\ASN1\Type\Primitive\Boolean |
|
119 | - */ |
|
120 | - public function asBoolean(): Primitive\Boolean |
|
121 | - { |
|
122 | - if (!$this->_element instanceof Primitive\Boolean) { |
|
123 | - throw new \UnexpectedValueException( |
|
124 | - $this->_generateExceptionMessage(Element::TYPE_BOOLEAN)); |
|
125 | - } |
|
126 | - return $this->_element; |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * Get the wrapped element as an integer type. |
|
131 | - * |
|
132 | - * @throws \UnexpectedValueException If the element is not an integer |
|
133 | - * |
|
134 | - * @return \Sop\ASN1\Type\Primitive\Integer |
|
135 | - */ |
|
136 | - public function asInteger(): Primitive\Integer |
|
137 | - { |
|
138 | - if (!$this->_element instanceof Primitive\Integer) { |
|
139 | - throw new \UnexpectedValueException( |
|
140 | - $this->_generateExceptionMessage(Element::TYPE_INTEGER)); |
|
141 | - } |
|
142 | - return $this->_element; |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * Get the wrapped element as a bit string type. |
|
147 | - * |
|
148 | - * @throws \UnexpectedValueException If the element is not a bit string |
|
149 | - * |
|
150 | - * @return \Sop\ASN1\Type\Primitive\BitString |
|
151 | - */ |
|
152 | - public function asBitString(): Primitive\BitString |
|
153 | - { |
|
154 | - if (!$this->_element instanceof Primitive\BitString) { |
|
155 | - throw new \UnexpectedValueException( |
|
156 | - $this->_generateExceptionMessage(Element::TYPE_BIT_STRING)); |
|
157 | - } |
|
158 | - return $this->_element; |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * Get the wrapped element as an octet string type. |
|
163 | - * |
|
164 | - * @throws \UnexpectedValueException If the element is not an octet string |
|
165 | - * |
|
166 | - * @return \Sop\ASN1\Type\Primitive\OctetString |
|
167 | - */ |
|
168 | - public function asOctetString(): Primitive\OctetString |
|
169 | - { |
|
170 | - if (!$this->_element instanceof Primitive\OctetString) { |
|
171 | - throw new \UnexpectedValueException( |
|
172 | - $this->_generateExceptionMessage(Element::TYPE_OCTET_STRING)); |
|
173 | - } |
|
174 | - return $this->_element; |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * Get the wrapped element as a null type. |
|
179 | - * |
|
180 | - * @throws \UnexpectedValueException If the element is not a null |
|
181 | - * |
|
182 | - * @return \Sop\ASN1\Type\Primitive\NullType |
|
183 | - */ |
|
184 | - public function asNull(): Primitive\NullType |
|
185 | - { |
|
186 | - if (!$this->_element instanceof Primitive\NullType) { |
|
187 | - throw new \UnexpectedValueException( |
|
188 | - $this->_generateExceptionMessage(Element::TYPE_NULL)); |
|
189 | - } |
|
190 | - return $this->_element; |
|
191 | - } |
|
192 | - |
|
193 | - /** |
|
194 | - * Get the wrapped element as an object identifier type. |
|
195 | - * |
|
196 | - * @throws \UnexpectedValueException If the element is not an object identifier |
|
197 | - * |
|
198 | - * @return \Sop\ASN1\Type\Primitive\ObjectIdentifier |
|
199 | - */ |
|
200 | - public function asObjectIdentifier(): Primitive\ObjectIdentifier |
|
201 | - { |
|
202 | - if (!$this->_element instanceof Primitive\ObjectIdentifier) { |
|
203 | - throw new \UnexpectedValueException( |
|
204 | - $this->_generateExceptionMessage(Element::TYPE_OBJECT_IDENTIFIER)); |
|
205 | - } |
|
206 | - return $this->_element; |
|
207 | - } |
|
208 | - |
|
209 | - /** |
|
210 | - * Get the wrapped element as an object descriptor type. |
|
211 | - * |
|
212 | - * @throws \UnexpectedValueException If the element is not an object descriptor |
|
213 | - * |
|
214 | - * @return \Sop\ASN1\Type\Primitive\ObjectDescriptor |
|
215 | - */ |
|
216 | - public function asObjectDescriptor(): Primitive\ObjectDescriptor |
|
217 | - { |
|
218 | - if (!$this->_element instanceof Primitive\ObjectDescriptor) { |
|
219 | - throw new \UnexpectedValueException( |
|
220 | - $this->_generateExceptionMessage(Element::TYPE_OBJECT_DESCRIPTOR)); |
|
221 | - } |
|
222 | - return $this->_element; |
|
223 | - } |
|
224 | - |
|
225 | - /** |
|
226 | - * Get the wrapped element as a real type. |
|
227 | - * |
|
228 | - * @throws \UnexpectedValueException If the element is not a real |
|
229 | - * |
|
230 | - * @return \Sop\ASN1\Type\Primitive\Real |
|
231 | - */ |
|
232 | - public function asReal(): Primitive\Real |
|
233 | - { |
|
234 | - if (!$this->_element instanceof Primitive\Real) { |
|
235 | - throw new \UnexpectedValueException( |
|
236 | - $this->_generateExceptionMessage(Element::TYPE_REAL)); |
|
237 | - } |
|
238 | - return $this->_element; |
|
239 | - } |
|
240 | - |
|
241 | - /** |
|
242 | - * Get the wrapped element as an enumerated type. |
|
243 | - * |
|
244 | - * @throws \UnexpectedValueException If the element is not an enumerated |
|
245 | - * |
|
246 | - * @return \Sop\ASN1\Type\Primitive\Enumerated |
|
247 | - */ |
|
248 | - public function asEnumerated(): Primitive\Enumerated |
|
249 | - { |
|
250 | - if (!$this->_element instanceof Primitive\Enumerated) { |
|
251 | - throw new \UnexpectedValueException( |
|
252 | - $this->_generateExceptionMessage(Element::TYPE_ENUMERATED)); |
|
253 | - } |
|
254 | - return $this->_element; |
|
255 | - } |
|
256 | - |
|
257 | - /** |
|
258 | - * Get the wrapped element as a UTF8 string type. |
|
259 | - * |
|
260 | - * @throws \UnexpectedValueException If the element is not a UTF8 string |
|
261 | - * |
|
262 | - * @return \Sop\ASN1\Type\Primitive\UTF8String |
|
263 | - */ |
|
264 | - public function asUTF8String(): Primitive\UTF8String |
|
265 | - { |
|
266 | - if (!$this->_element instanceof Primitive\UTF8String) { |
|
267 | - throw new \UnexpectedValueException( |
|
268 | - $this->_generateExceptionMessage(Element::TYPE_UTF8_STRING)); |
|
269 | - } |
|
270 | - return $this->_element; |
|
271 | - } |
|
272 | - |
|
273 | - /** |
|
274 | - * Get the wrapped element as a relative OID type. |
|
275 | - * |
|
276 | - * @throws \UnexpectedValueException If the element is not a relative OID |
|
277 | - * |
|
278 | - * @return \Sop\ASN1\Type\Primitive\RelativeOID |
|
279 | - */ |
|
280 | - public function asRelativeOID(): Primitive\RelativeOID |
|
281 | - { |
|
282 | - if (!$this->_element instanceof Primitive\RelativeOID) { |
|
283 | - throw new \UnexpectedValueException( |
|
284 | - $this->_generateExceptionMessage(Element::TYPE_RELATIVE_OID)); |
|
285 | - } |
|
286 | - return $this->_element; |
|
287 | - } |
|
288 | - |
|
289 | - /** |
|
290 | - * Get the wrapped element as a sequence type. |
|
291 | - * |
|
292 | - * @throws \UnexpectedValueException If the element is not a sequence |
|
293 | - * |
|
294 | - * @return \Sop\ASN1\Type\Constructed\Sequence |
|
295 | - */ |
|
296 | - public function asSequence(): Constructed\Sequence |
|
297 | - { |
|
298 | - if (!$this->_element instanceof Constructed\Sequence) { |
|
299 | - throw new \UnexpectedValueException( |
|
300 | - $this->_generateExceptionMessage(Element::TYPE_SEQUENCE)); |
|
301 | - } |
|
302 | - return $this->_element; |
|
303 | - } |
|
304 | - |
|
305 | - /** |
|
306 | - * Get the wrapped element as a set type. |
|
307 | - * |
|
308 | - * @throws \UnexpectedValueException If the element is not a set |
|
309 | - * |
|
310 | - * @return \Sop\ASN1\Type\Constructed\Set |
|
311 | - */ |
|
312 | - public function asSet(): Constructed\Set |
|
313 | - { |
|
314 | - if (!$this->_element instanceof Constructed\Set) { |
|
315 | - throw new \UnexpectedValueException( |
|
316 | - $this->_generateExceptionMessage(Element::TYPE_SET)); |
|
317 | - } |
|
318 | - return $this->_element; |
|
319 | - } |
|
320 | - |
|
321 | - /** |
|
322 | - * Get the wrapped element as a numeric string type. |
|
323 | - * |
|
324 | - * @throws \UnexpectedValueException If the element is not a numeric string |
|
325 | - * |
|
326 | - * @return \Sop\ASN1\Type\Primitive\NumericString |
|
327 | - */ |
|
328 | - public function asNumericString(): Primitive\NumericString |
|
329 | - { |
|
330 | - if (!$this->_element instanceof Primitive\NumericString) { |
|
331 | - throw new \UnexpectedValueException( |
|
332 | - $this->_generateExceptionMessage(Element::TYPE_NUMERIC_STRING)); |
|
333 | - } |
|
334 | - return $this->_element; |
|
335 | - } |
|
336 | - |
|
337 | - /** |
|
338 | - * Get the wrapped element as a printable string type. |
|
339 | - * |
|
340 | - * @throws \UnexpectedValueException If the element is not a printable |
|
341 | - * string |
|
342 | - * |
|
343 | - * @return \Sop\ASN1\Type\Primitive\PrintableString |
|
344 | - */ |
|
345 | - public function asPrintableString(): Primitive\PrintableString |
|
346 | - { |
|
347 | - if (!$this->_element instanceof Primitive\PrintableString) { |
|
348 | - throw new \UnexpectedValueException( |
|
349 | - $this->_generateExceptionMessage(Element::TYPE_PRINTABLE_STRING)); |
|
350 | - } |
|
351 | - return $this->_element; |
|
352 | - } |
|
353 | - |
|
354 | - /** |
|
355 | - * Get the wrapped element as a T61 string type. |
|
356 | - * |
|
357 | - * @throws \UnexpectedValueException If the element is not a T61 string |
|
358 | - * |
|
359 | - * @return \Sop\ASN1\Type\Primitive\T61String |
|
360 | - */ |
|
361 | - public function asT61String(): Primitive\T61String |
|
362 | - { |
|
363 | - if (!$this->_element instanceof Primitive\T61String) { |
|
364 | - throw new \UnexpectedValueException( |
|
365 | - $this->_generateExceptionMessage(Element::TYPE_T61_STRING)); |
|
366 | - } |
|
367 | - return $this->_element; |
|
368 | - } |
|
369 | - |
|
370 | - /** |
|
371 | - * Get the wrapped element as a videotex string type. |
|
372 | - * |
|
373 | - * @throws \UnexpectedValueException If the element is not a videotex string |
|
374 | - * |
|
375 | - * @return \Sop\ASN1\Type\Primitive\VideotexString |
|
376 | - */ |
|
377 | - public function asVideotexString(): Primitive\VideotexString |
|
378 | - { |
|
379 | - if (!$this->_element instanceof Primitive\VideotexString) { |
|
380 | - throw new \UnexpectedValueException( |
|
381 | - $this->_generateExceptionMessage(Element::TYPE_VIDEOTEX_STRING)); |
|
382 | - } |
|
383 | - return $this->_element; |
|
384 | - } |
|
385 | - |
|
386 | - /** |
|
387 | - * Get the wrapped element as a IA5 string type. |
|
388 | - * |
|
389 | - * @throws \UnexpectedValueException If the element is not a IA5 string |
|
390 | - * |
|
391 | - * @return \Sop\ASN1\Type\Primitive\IA5String |
|
392 | - */ |
|
393 | - public function asIA5String(): Primitive\IA5String |
|
394 | - { |
|
395 | - if (!$this->_element instanceof Primitive\IA5String) { |
|
396 | - throw new \UnexpectedValueException( |
|
397 | - $this->_generateExceptionMessage(Element::TYPE_IA5_STRING)); |
|
398 | - } |
|
399 | - return $this->_element; |
|
400 | - } |
|
401 | - |
|
402 | - /** |
|
403 | - * Get the wrapped element as an UTC time type. |
|
404 | - * |
|
405 | - * @throws \UnexpectedValueException If the element is not a UTC time |
|
406 | - * |
|
407 | - * @return \Sop\ASN1\Type\Primitive\UTCTime |
|
408 | - */ |
|
409 | - public function asUTCTime(): Primitive\UTCTime |
|
410 | - { |
|
411 | - if (!$this->_element instanceof Primitive\UTCTime) { |
|
412 | - throw new \UnexpectedValueException( |
|
413 | - $this->_generateExceptionMessage(Element::TYPE_UTC_TIME)); |
|
414 | - } |
|
415 | - return $this->_element; |
|
416 | - } |
|
417 | - |
|
418 | - /** |
|
419 | - * Get the wrapped element as a generalized time type. |
|
420 | - * |
|
421 | - * @throws \UnexpectedValueException If the element is not a generalized time |
|
422 | - * |
|
423 | - * @return \Sop\ASN1\Type\Primitive\GeneralizedTime |
|
424 | - */ |
|
425 | - public function asGeneralizedTime(): Primitive\GeneralizedTime |
|
426 | - { |
|
427 | - if (!$this->_element instanceof Primitive\GeneralizedTime) { |
|
428 | - throw new \UnexpectedValueException( |
|
429 | - $this->_generateExceptionMessage(Element::TYPE_GENERALIZED_TIME)); |
|
430 | - } |
|
431 | - return $this->_element; |
|
432 | - } |
|
433 | - |
|
434 | - /** |
|
435 | - * Get the wrapped element as a graphic string type. |
|
436 | - * |
|
437 | - * @throws \UnexpectedValueException If the element is not a graphic string |
|
438 | - * |
|
439 | - * @return \Sop\ASN1\Type\Primitive\GraphicString |
|
440 | - */ |
|
441 | - public function asGraphicString(): Primitive\GraphicString |
|
442 | - { |
|
443 | - if (!$this->_element instanceof Primitive\GraphicString) { |
|
444 | - throw new \UnexpectedValueException( |
|
445 | - $this->_generateExceptionMessage(Element::TYPE_GRAPHIC_STRING)); |
|
446 | - } |
|
447 | - return $this->_element; |
|
448 | - } |
|
449 | - |
|
450 | - /** |
|
451 | - * Get the wrapped element as a visible string type. |
|
452 | - * |
|
453 | - * @throws \UnexpectedValueException If the element is not a visible string |
|
454 | - * |
|
455 | - * @return \Sop\ASN1\Type\Primitive\VisibleString |
|
456 | - */ |
|
457 | - public function asVisibleString(): Primitive\VisibleString |
|
458 | - { |
|
459 | - if (!$this->_element instanceof Primitive\VisibleString) { |
|
460 | - throw new \UnexpectedValueException( |
|
461 | - $this->_generateExceptionMessage(Element::TYPE_VISIBLE_STRING)); |
|
462 | - } |
|
463 | - return $this->_element; |
|
464 | - } |
|
465 | - |
|
466 | - /** |
|
467 | - * Get the wrapped element as a general string type. |
|
468 | - * |
|
469 | - * @throws \UnexpectedValueException If the element is not general string |
|
470 | - * |
|
471 | - * @return \Sop\ASN1\Type\Primitive\GeneralString |
|
472 | - */ |
|
473 | - public function asGeneralString(): Primitive\GeneralString |
|
474 | - { |
|
475 | - if (!$this->_element instanceof Primitive\GeneralString) { |
|
476 | - throw new \UnexpectedValueException( |
|
477 | - $this->_generateExceptionMessage(Element::TYPE_GENERAL_STRING)); |
|
478 | - } |
|
479 | - return $this->_element; |
|
480 | - } |
|
481 | - |
|
482 | - /** |
|
483 | - * Get the wrapped element as a universal string type. |
|
484 | - * |
|
485 | - * @throws \UnexpectedValueException If the element is not a universal string |
|
486 | - * |
|
487 | - * @return \Sop\ASN1\Type\Primitive\UniversalString |
|
488 | - */ |
|
489 | - public function asUniversalString(): Primitive\UniversalString |
|
490 | - { |
|
491 | - if (!$this->_element instanceof Primitive\UniversalString) { |
|
492 | - throw new \UnexpectedValueException( |
|
493 | - $this->_generateExceptionMessage(Element::TYPE_UNIVERSAL_STRING)); |
|
494 | - } |
|
495 | - return $this->_element; |
|
496 | - } |
|
497 | - |
|
498 | - /** |
|
499 | - * Get the wrapped element as a character string type. |
|
500 | - * |
|
501 | - * @throws \UnexpectedValueException If the element is not a character string |
|
502 | - * |
|
503 | - * @return \Sop\ASN1\Type\Primitive\CharacterString |
|
504 | - */ |
|
505 | - public function asCharacterString(): Primitive\CharacterString |
|
506 | - { |
|
507 | - if (!$this->_element instanceof Primitive\CharacterString) { |
|
508 | - throw new \UnexpectedValueException( |
|
509 | - $this->_generateExceptionMessage(Element::TYPE_CHARACTER_STRING)); |
|
510 | - } |
|
511 | - return $this->_element; |
|
512 | - } |
|
513 | - |
|
514 | - /** |
|
515 | - * Get the wrapped element as a BMP string type. |
|
516 | - * |
|
517 | - * @throws \UnexpectedValueException If the element is not a bmp string |
|
518 | - * |
|
519 | - * @return \Sop\ASN1\Type\Primitive\BMPString |
|
520 | - */ |
|
521 | - public function asBMPString(): Primitive\BMPString |
|
522 | - { |
|
523 | - if (!$this->_element instanceof Primitive\BMPString) { |
|
524 | - throw new \UnexpectedValueException( |
|
525 | - $this->_generateExceptionMessage(Element::TYPE_BMP_STRING)); |
|
526 | - } |
|
527 | - return $this->_element; |
|
528 | - } |
|
529 | - |
|
530 | - /** |
|
531 | - * Get the wrapped element as a constructed string type. |
|
532 | - * |
|
533 | - * @throws \UnexpectedValueException If the element is not a constructed string |
|
534 | - * |
|
535 | - * @return \Sop\ASN1\Type\Constructed\ConstructedString |
|
536 | - */ |
|
537 | - public function asConstructedString(): Constructed\ConstructedString |
|
538 | - { |
|
539 | - if (!$this->_element instanceof Constructed\ConstructedString) { |
|
540 | - throw new \UnexpectedValueException( |
|
541 | - $this->_generateExceptionMessage(Element::TYPE_CONSTRUCTED_STRING)); |
|
542 | - } |
|
543 | - return $this->_element; |
|
544 | - } |
|
545 | - |
|
546 | - /** |
|
547 | - * Get the wrapped element as any string type. |
|
548 | - * |
|
549 | - * @throws \UnexpectedValueException If the element is not a string |
|
550 | - * |
|
551 | - * @return StringType |
|
552 | - */ |
|
553 | - public function asString(): StringType |
|
554 | - { |
|
555 | - if (!$this->_element instanceof StringType) { |
|
556 | - throw new \UnexpectedValueException( |
|
557 | - $this->_generateExceptionMessage(Element::TYPE_STRING)); |
|
558 | - } |
|
559 | - return $this->_element; |
|
560 | - } |
|
561 | - |
|
562 | - /** |
|
563 | - * Get the wrapped element as any time type. |
|
564 | - * |
|
565 | - * @throws \UnexpectedValueException If the element is not a time |
|
566 | - * |
|
567 | - * @return TimeType |
|
568 | - */ |
|
569 | - public function asTime(): TimeType |
|
570 | - { |
|
571 | - if (!$this->_element instanceof TimeType) { |
|
572 | - throw new \UnexpectedValueException( |
|
573 | - $this->_generateExceptionMessage(Element::TYPE_TIME)); |
|
574 | - } |
|
575 | - return $this->_element; |
|
576 | - } |
|
577 | - |
|
578 | - /** |
|
579 | - * {@inheritdoc} |
|
580 | - */ |
|
581 | - public function toDER(): string |
|
582 | - { |
|
583 | - return $this->_element->toDER(); |
|
584 | - } |
|
585 | - |
|
586 | - /** |
|
587 | - * {@inheritdoc} |
|
588 | - */ |
|
589 | - public function typeClass(): int |
|
590 | - { |
|
591 | - return $this->_element->typeClass(); |
|
592 | - } |
|
593 | - |
|
594 | - /** |
|
595 | - * {@inheritdoc} |
|
596 | - */ |
|
597 | - public function isConstructed(): bool |
|
598 | - { |
|
599 | - return $this->_element->isConstructed(); |
|
600 | - } |
|
601 | - |
|
602 | - /** |
|
603 | - * {@inheritdoc} |
|
604 | - */ |
|
605 | - public function tag(): int |
|
606 | - { |
|
607 | - return $this->_element->tag(); |
|
608 | - } |
|
609 | - |
|
610 | - /** |
|
611 | - * {@inheritdoc} |
|
612 | - */ |
|
613 | - public function isType(int $tag): bool |
|
614 | - { |
|
615 | - return $this->_element->isType($tag); |
|
616 | - } |
|
617 | - |
|
618 | - /** |
|
619 | - * {@inheritdoc} |
|
620 | - * |
|
621 | - * Consider using any of the <code>as*</code> accessor methods instead. |
|
622 | - */ |
|
623 | - public function expectType(int $tag): ElementBase |
|
624 | - { |
|
625 | - return $this->_element->expectType($tag); |
|
626 | - } |
|
627 | - |
|
628 | - /** |
|
629 | - * {@inheritdoc} |
|
630 | - */ |
|
631 | - public function isTagged(): bool |
|
632 | - { |
|
633 | - return $this->_element->isTagged(); |
|
634 | - } |
|
635 | - |
|
636 | - /** |
|
637 | - * {@inheritdoc} |
|
638 | - * |
|
639 | - * Consider using <code>asTagged()</code> method instead and chaining |
|
640 | - * with <code>TaggedType::asExplicit()</code> or |
|
641 | - * <code>TaggedType::asImplicit()</code>. |
|
642 | - */ |
|
643 | - public function expectTagged(?int $tag = null): TaggedType |
|
644 | - { |
|
645 | - return $this->_element->expectTagged($tag); |
|
646 | - } |
|
647 | - |
|
648 | - /** |
|
649 | - * {@inheritdoc} |
|
650 | - */ |
|
651 | - public function asElement(): Element |
|
652 | - { |
|
653 | - return $this->_element; |
|
654 | - } |
|
655 | - |
|
656 | - /** |
|
657 | - * {@inheritdoc} |
|
658 | - */ |
|
659 | - public function asUnspecified(): UnspecifiedType |
|
660 | - { |
|
661 | - return $this; |
|
662 | - } |
|
663 | - |
|
664 | - /** |
|
665 | - * Generate message for exceptions thrown by <code>as*</code> methods. |
|
666 | - * |
|
667 | - * @param int $tag Type tag of the expected element |
|
668 | - * |
|
669 | - * @return string |
|
670 | - */ |
|
671 | - private function _generateExceptionMessage(int $tag): string |
|
672 | - { |
|
673 | - return sprintf('%s expected, got %s.', Element::tagToName($tag), |
|
674 | - $this->_typeDescriptorString()); |
|
675 | - } |
|
676 | - |
|
677 | - /** |
|
678 | - * Get textual description of the wrapped element for debugging purposes. |
|
679 | - * |
|
680 | - * @return string |
|
681 | - */ |
|
682 | - private function _typeDescriptorString(): string |
|
683 | - { |
|
684 | - $type_cls = $this->_element->typeClass(); |
|
685 | - $tag = $this->_element->tag(); |
|
686 | - if (Identifier::CLASS_UNIVERSAL === $type_cls) { |
|
687 | - return Element::tagToName($tag); |
|
688 | - } |
|
689 | - return Identifier::classToName($type_cls) . " TAG {$tag}"; |
|
690 | - } |
|
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 | + * Initialize from DER data. |
|
39 | + * |
|
40 | + * @param string $data DER encoded data |
|
41 | + * |
|
42 | + * @return self |
|
43 | + */ |
|
44 | + public static function fromDER(string $data): self |
|
45 | + { |
|
46 | + return Element::fromDER($data)->asUnspecified(); |
|
47 | + } |
|
48 | + |
|
49 | + /** |
|
50 | + * Initialize from ElementBase interface. |
|
51 | + * |
|
52 | + * @param ElementBase $el |
|
53 | + * |
|
54 | + * @return self |
|
55 | + */ |
|
56 | + public static function fromElementBase(ElementBase $el): self |
|
57 | + { |
|
58 | + // if element is already wrapped |
|
59 | + if ($el instanceof self) { |
|
60 | + return $el; |
|
61 | + } |
|
62 | + return new self($el->asElement()); |
|
63 | + } |
|
64 | + |
|
65 | + /** |
|
66 | + * Get the wrapped element as a context specific tagged type. |
|
67 | + * |
|
68 | + * @throws \UnexpectedValueException If the element is not tagged |
|
69 | + * |
|
70 | + * @return TaggedType |
|
71 | + */ |
|
72 | + public function asTagged(): TaggedType |
|
73 | + { |
|
74 | + if (!$this->_element instanceof TaggedType) { |
|
75 | + throw new \UnexpectedValueException( |
|
76 | + 'Tagged element expected, got ' . $this->_typeDescriptorString()); |
|
77 | + } |
|
78 | + return $this->_element; |
|
79 | + } |
|
80 | + |
|
81 | + /** |
|
82 | + * Get the wrapped element as an application specific type. |
|
83 | + * |
|
84 | + * @throws \UnexpectedValueException If element is not application specific |
|
85 | + * |
|
86 | + * @return \Sop\ASN1\Type\Tagged\ApplicationType |
|
87 | + */ |
|
88 | + public function asApplication(): Tagged\ApplicationType |
|
89 | + { |
|
90 | + if (!$this->_element instanceof Tagged\ApplicationType) { |
|
91 | + throw new \UnexpectedValueException( |
|
92 | + 'Application type expected, got ' . $this->_typeDescriptorString()); |
|
93 | + } |
|
94 | + return $this->_element; |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * Get the wrapped element as a private tagged type. |
|
99 | + * |
|
100 | + * @throws \UnexpectedValueException If element is not using private tagging |
|
101 | + * |
|
102 | + * @return \Sop\ASN1\Type\Tagged\PrivateType |
|
103 | + */ |
|
104 | + public function asPrivate(): Tagged\PrivateType |
|
105 | + { |
|
106 | + if (!$this->_element instanceof Tagged\PrivateType) { |
|
107 | + throw new \UnexpectedValueException( |
|
108 | + 'Private type expected, got ' . $this->_typeDescriptorString()); |
|
109 | + } |
|
110 | + return $this->_element; |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * Get the wrapped element as a boolean type. |
|
115 | + * |
|
116 | + * @throws \UnexpectedValueException If the element is not a boolean |
|
117 | + * |
|
118 | + * @return \Sop\ASN1\Type\Primitive\Boolean |
|
119 | + */ |
|
120 | + public function asBoolean(): Primitive\Boolean |
|
121 | + { |
|
122 | + if (!$this->_element instanceof Primitive\Boolean) { |
|
123 | + throw new \UnexpectedValueException( |
|
124 | + $this->_generateExceptionMessage(Element::TYPE_BOOLEAN)); |
|
125 | + } |
|
126 | + return $this->_element; |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * Get the wrapped element as an integer type. |
|
131 | + * |
|
132 | + * @throws \UnexpectedValueException If the element is not an integer |
|
133 | + * |
|
134 | + * @return \Sop\ASN1\Type\Primitive\Integer |
|
135 | + */ |
|
136 | + public function asInteger(): Primitive\Integer |
|
137 | + { |
|
138 | + if (!$this->_element instanceof Primitive\Integer) { |
|
139 | + throw new \UnexpectedValueException( |
|
140 | + $this->_generateExceptionMessage(Element::TYPE_INTEGER)); |
|
141 | + } |
|
142 | + return $this->_element; |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * Get the wrapped element as a bit string type. |
|
147 | + * |
|
148 | + * @throws \UnexpectedValueException If the element is not a bit string |
|
149 | + * |
|
150 | + * @return \Sop\ASN1\Type\Primitive\BitString |
|
151 | + */ |
|
152 | + public function asBitString(): Primitive\BitString |
|
153 | + { |
|
154 | + if (!$this->_element instanceof Primitive\BitString) { |
|
155 | + throw new \UnexpectedValueException( |
|
156 | + $this->_generateExceptionMessage(Element::TYPE_BIT_STRING)); |
|
157 | + } |
|
158 | + return $this->_element; |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * Get the wrapped element as an octet string type. |
|
163 | + * |
|
164 | + * @throws \UnexpectedValueException If the element is not an octet string |
|
165 | + * |
|
166 | + * @return \Sop\ASN1\Type\Primitive\OctetString |
|
167 | + */ |
|
168 | + public function asOctetString(): Primitive\OctetString |
|
169 | + { |
|
170 | + if (!$this->_element instanceof Primitive\OctetString) { |
|
171 | + throw new \UnexpectedValueException( |
|
172 | + $this->_generateExceptionMessage(Element::TYPE_OCTET_STRING)); |
|
173 | + } |
|
174 | + return $this->_element; |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * Get the wrapped element as a null type. |
|
179 | + * |
|
180 | + * @throws \UnexpectedValueException If the element is not a null |
|
181 | + * |
|
182 | + * @return \Sop\ASN1\Type\Primitive\NullType |
|
183 | + */ |
|
184 | + public function asNull(): Primitive\NullType |
|
185 | + { |
|
186 | + if (!$this->_element instanceof Primitive\NullType) { |
|
187 | + throw new \UnexpectedValueException( |
|
188 | + $this->_generateExceptionMessage(Element::TYPE_NULL)); |
|
189 | + } |
|
190 | + return $this->_element; |
|
191 | + } |
|
192 | + |
|
193 | + /** |
|
194 | + * Get the wrapped element as an object identifier type. |
|
195 | + * |
|
196 | + * @throws \UnexpectedValueException If the element is not an object identifier |
|
197 | + * |
|
198 | + * @return \Sop\ASN1\Type\Primitive\ObjectIdentifier |
|
199 | + */ |
|
200 | + public function asObjectIdentifier(): Primitive\ObjectIdentifier |
|
201 | + { |
|
202 | + if (!$this->_element instanceof Primitive\ObjectIdentifier) { |
|
203 | + throw new \UnexpectedValueException( |
|
204 | + $this->_generateExceptionMessage(Element::TYPE_OBJECT_IDENTIFIER)); |
|
205 | + } |
|
206 | + return $this->_element; |
|
207 | + } |
|
208 | + |
|
209 | + /** |
|
210 | + * Get the wrapped element as an object descriptor type. |
|
211 | + * |
|
212 | + * @throws \UnexpectedValueException If the element is not an object descriptor |
|
213 | + * |
|
214 | + * @return \Sop\ASN1\Type\Primitive\ObjectDescriptor |
|
215 | + */ |
|
216 | + public function asObjectDescriptor(): Primitive\ObjectDescriptor |
|
217 | + { |
|
218 | + if (!$this->_element instanceof Primitive\ObjectDescriptor) { |
|
219 | + throw new \UnexpectedValueException( |
|
220 | + $this->_generateExceptionMessage(Element::TYPE_OBJECT_DESCRIPTOR)); |
|
221 | + } |
|
222 | + return $this->_element; |
|
223 | + } |
|
224 | + |
|
225 | + /** |
|
226 | + * Get the wrapped element as a real type. |
|
227 | + * |
|
228 | + * @throws \UnexpectedValueException If the element is not a real |
|
229 | + * |
|
230 | + * @return \Sop\ASN1\Type\Primitive\Real |
|
231 | + */ |
|
232 | + public function asReal(): Primitive\Real |
|
233 | + { |
|
234 | + if (!$this->_element instanceof Primitive\Real) { |
|
235 | + throw new \UnexpectedValueException( |
|
236 | + $this->_generateExceptionMessage(Element::TYPE_REAL)); |
|
237 | + } |
|
238 | + return $this->_element; |
|
239 | + } |
|
240 | + |
|
241 | + /** |
|
242 | + * Get the wrapped element as an enumerated type. |
|
243 | + * |
|
244 | + * @throws \UnexpectedValueException If the element is not an enumerated |
|
245 | + * |
|
246 | + * @return \Sop\ASN1\Type\Primitive\Enumerated |
|
247 | + */ |
|
248 | + public function asEnumerated(): Primitive\Enumerated |
|
249 | + { |
|
250 | + if (!$this->_element instanceof Primitive\Enumerated) { |
|
251 | + throw new \UnexpectedValueException( |
|
252 | + $this->_generateExceptionMessage(Element::TYPE_ENUMERATED)); |
|
253 | + } |
|
254 | + return $this->_element; |
|
255 | + } |
|
256 | + |
|
257 | + /** |
|
258 | + * Get the wrapped element as a UTF8 string type. |
|
259 | + * |
|
260 | + * @throws \UnexpectedValueException If the element is not a UTF8 string |
|
261 | + * |
|
262 | + * @return \Sop\ASN1\Type\Primitive\UTF8String |
|
263 | + */ |
|
264 | + public function asUTF8String(): Primitive\UTF8String |
|
265 | + { |
|
266 | + if (!$this->_element instanceof Primitive\UTF8String) { |
|
267 | + throw new \UnexpectedValueException( |
|
268 | + $this->_generateExceptionMessage(Element::TYPE_UTF8_STRING)); |
|
269 | + } |
|
270 | + return $this->_element; |
|
271 | + } |
|
272 | + |
|
273 | + /** |
|
274 | + * Get the wrapped element as a relative OID type. |
|
275 | + * |
|
276 | + * @throws \UnexpectedValueException If the element is not a relative OID |
|
277 | + * |
|
278 | + * @return \Sop\ASN1\Type\Primitive\RelativeOID |
|
279 | + */ |
|
280 | + public function asRelativeOID(): Primitive\RelativeOID |
|
281 | + { |
|
282 | + if (!$this->_element instanceof Primitive\RelativeOID) { |
|
283 | + throw new \UnexpectedValueException( |
|
284 | + $this->_generateExceptionMessage(Element::TYPE_RELATIVE_OID)); |
|
285 | + } |
|
286 | + return $this->_element; |
|
287 | + } |
|
288 | + |
|
289 | + /** |
|
290 | + * Get the wrapped element as a sequence type. |
|
291 | + * |
|
292 | + * @throws \UnexpectedValueException If the element is not a sequence |
|
293 | + * |
|
294 | + * @return \Sop\ASN1\Type\Constructed\Sequence |
|
295 | + */ |
|
296 | + public function asSequence(): Constructed\Sequence |
|
297 | + { |
|
298 | + if (!$this->_element instanceof Constructed\Sequence) { |
|
299 | + throw new \UnexpectedValueException( |
|
300 | + $this->_generateExceptionMessage(Element::TYPE_SEQUENCE)); |
|
301 | + } |
|
302 | + return $this->_element; |
|
303 | + } |
|
304 | + |
|
305 | + /** |
|
306 | + * Get the wrapped element as a set type. |
|
307 | + * |
|
308 | + * @throws \UnexpectedValueException If the element is not a set |
|
309 | + * |
|
310 | + * @return \Sop\ASN1\Type\Constructed\Set |
|
311 | + */ |
|
312 | + public function asSet(): Constructed\Set |
|
313 | + { |
|
314 | + if (!$this->_element instanceof Constructed\Set) { |
|
315 | + throw new \UnexpectedValueException( |
|
316 | + $this->_generateExceptionMessage(Element::TYPE_SET)); |
|
317 | + } |
|
318 | + return $this->_element; |
|
319 | + } |
|
320 | + |
|
321 | + /** |
|
322 | + * Get the wrapped element as a numeric string type. |
|
323 | + * |
|
324 | + * @throws \UnexpectedValueException If the element is not a numeric string |
|
325 | + * |
|
326 | + * @return \Sop\ASN1\Type\Primitive\NumericString |
|
327 | + */ |
|
328 | + public function asNumericString(): Primitive\NumericString |
|
329 | + { |
|
330 | + if (!$this->_element instanceof Primitive\NumericString) { |
|
331 | + throw new \UnexpectedValueException( |
|
332 | + $this->_generateExceptionMessage(Element::TYPE_NUMERIC_STRING)); |
|
333 | + } |
|
334 | + return $this->_element; |
|
335 | + } |
|
336 | + |
|
337 | + /** |
|
338 | + * Get the wrapped element as a printable string type. |
|
339 | + * |
|
340 | + * @throws \UnexpectedValueException If the element is not a printable |
|
341 | + * string |
|
342 | + * |
|
343 | + * @return \Sop\ASN1\Type\Primitive\PrintableString |
|
344 | + */ |
|
345 | + public function asPrintableString(): Primitive\PrintableString |
|
346 | + { |
|
347 | + if (!$this->_element instanceof Primitive\PrintableString) { |
|
348 | + throw new \UnexpectedValueException( |
|
349 | + $this->_generateExceptionMessage(Element::TYPE_PRINTABLE_STRING)); |
|
350 | + } |
|
351 | + return $this->_element; |
|
352 | + } |
|
353 | + |
|
354 | + /** |
|
355 | + * Get the wrapped element as a T61 string type. |
|
356 | + * |
|
357 | + * @throws \UnexpectedValueException If the element is not a T61 string |
|
358 | + * |
|
359 | + * @return \Sop\ASN1\Type\Primitive\T61String |
|
360 | + */ |
|
361 | + public function asT61String(): Primitive\T61String |
|
362 | + { |
|
363 | + if (!$this->_element instanceof Primitive\T61String) { |
|
364 | + throw new \UnexpectedValueException( |
|
365 | + $this->_generateExceptionMessage(Element::TYPE_T61_STRING)); |
|
366 | + } |
|
367 | + return $this->_element; |
|
368 | + } |
|
369 | + |
|
370 | + /** |
|
371 | + * Get the wrapped element as a videotex string type. |
|
372 | + * |
|
373 | + * @throws \UnexpectedValueException If the element is not a videotex string |
|
374 | + * |
|
375 | + * @return \Sop\ASN1\Type\Primitive\VideotexString |
|
376 | + */ |
|
377 | + public function asVideotexString(): Primitive\VideotexString |
|
378 | + { |
|
379 | + if (!$this->_element instanceof Primitive\VideotexString) { |
|
380 | + throw new \UnexpectedValueException( |
|
381 | + $this->_generateExceptionMessage(Element::TYPE_VIDEOTEX_STRING)); |
|
382 | + } |
|
383 | + return $this->_element; |
|
384 | + } |
|
385 | + |
|
386 | + /** |
|
387 | + * Get the wrapped element as a IA5 string type. |
|
388 | + * |
|
389 | + * @throws \UnexpectedValueException If the element is not a IA5 string |
|
390 | + * |
|
391 | + * @return \Sop\ASN1\Type\Primitive\IA5String |
|
392 | + */ |
|
393 | + public function asIA5String(): Primitive\IA5String |
|
394 | + { |
|
395 | + if (!$this->_element instanceof Primitive\IA5String) { |
|
396 | + throw new \UnexpectedValueException( |
|
397 | + $this->_generateExceptionMessage(Element::TYPE_IA5_STRING)); |
|
398 | + } |
|
399 | + return $this->_element; |
|
400 | + } |
|
401 | + |
|
402 | + /** |
|
403 | + * Get the wrapped element as an UTC time type. |
|
404 | + * |
|
405 | + * @throws \UnexpectedValueException If the element is not a UTC time |
|
406 | + * |
|
407 | + * @return \Sop\ASN1\Type\Primitive\UTCTime |
|
408 | + */ |
|
409 | + public function asUTCTime(): Primitive\UTCTime |
|
410 | + { |
|
411 | + if (!$this->_element instanceof Primitive\UTCTime) { |
|
412 | + throw new \UnexpectedValueException( |
|
413 | + $this->_generateExceptionMessage(Element::TYPE_UTC_TIME)); |
|
414 | + } |
|
415 | + return $this->_element; |
|
416 | + } |
|
417 | + |
|
418 | + /** |
|
419 | + * Get the wrapped element as a generalized time type. |
|
420 | + * |
|
421 | + * @throws \UnexpectedValueException If the element is not a generalized time |
|
422 | + * |
|
423 | + * @return \Sop\ASN1\Type\Primitive\GeneralizedTime |
|
424 | + */ |
|
425 | + public function asGeneralizedTime(): Primitive\GeneralizedTime |
|
426 | + { |
|
427 | + if (!$this->_element instanceof Primitive\GeneralizedTime) { |
|
428 | + throw new \UnexpectedValueException( |
|
429 | + $this->_generateExceptionMessage(Element::TYPE_GENERALIZED_TIME)); |
|
430 | + } |
|
431 | + return $this->_element; |
|
432 | + } |
|
433 | + |
|
434 | + /** |
|
435 | + * Get the wrapped element as a graphic string type. |
|
436 | + * |
|
437 | + * @throws \UnexpectedValueException If the element is not a graphic string |
|
438 | + * |
|
439 | + * @return \Sop\ASN1\Type\Primitive\GraphicString |
|
440 | + */ |
|
441 | + public function asGraphicString(): Primitive\GraphicString |
|
442 | + { |
|
443 | + if (!$this->_element instanceof Primitive\GraphicString) { |
|
444 | + throw new \UnexpectedValueException( |
|
445 | + $this->_generateExceptionMessage(Element::TYPE_GRAPHIC_STRING)); |
|
446 | + } |
|
447 | + return $this->_element; |
|
448 | + } |
|
449 | + |
|
450 | + /** |
|
451 | + * Get the wrapped element as a visible string type. |
|
452 | + * |
|
453 | + * @throws \UnexpectedValueException If the element is not a visible string |
|
454 | + * |
|
455 | + * @return \Sop\ASN1\Type\Primitive\VisibleString |
|
456 | + */ |
|
457 | + public function asVisibleString(): Primitive\VisibleString |
|
458 | + { |
|
459 | + if (!$this->_element instanceof Primitive\VisibleString) { |
|
460 | + throw new \UnexpectedValueException( |
|
461 | + $this->_generateExceptionMessage(Element::TYPE_VISIBLE_STRING)); |
|
462 | + } |
|
463 | + return $this->_element; |
|
464 | + } |
|
465 | + |
|
466 | + /** |
|
467 | + * Get the wrapped element as a general string type. |
|
468 | + * |
|
469 | + * @throws \UnexpectedValueException If the element is not general string |
|
470 | + * |
|
471 | + * @return \Sop\ASN1\Type\Primitive\GeneralString |
|
472 | + */ |
|
473 | + public function asGeneralString(): Primitive\GeneralString |
|
474 | + { |
|
475 | + if (!$this->_element instanceof Primitive\GeneralString) { |
|
476 | + throw new \UnexpectedValueException( |
|
477 | + $this->_generateExceptionMessage(Element::TYPE_GENERAL_STRING)); |
|
478 | + } |
|
479 | + return $this->_element; |
|
480 | + } |
|
481 | + |
|
482 | + /** |
|
483 | + * Get the wrapped element as a universal string type. |
|
484 | + * |
|
485 | + * @throws \UnexpectedValueException If the element is not a universal string |
|
486 | + * |
|
487 | + * @return \Sop\ASN1\Type\Primitive\UniversalString |
|
488 | + */ |
|
489 | + public function asUniversalString(): Primitive\UniversalString |
|
490 | + { |
|
491 | + if (!$this->_element instanceof Primitive\UniversalString) { |
|
492 | + throw new \UnexpectedValueException( |
|
493 | + $this->_generateExceptionMessage(Element::TYPE_UNIVERSAL_STRING)); |
|
494 | + } |
|
495 | + return $this->_element; |
|
496 | + } |
|
497 | + |
|
498 | + /** |
|
499 | + * Get the wrapped element as a character string type. |
|
500 | + * |
|
501 | + * @throws \UnexpectedValueException If the element is not a character string |
|
502 | + * |
|
503 | + * @return \Sop\ASN1\Type\Primitive\CharacterString |
|
504 | + */ |
|
505 | + public function asCharacterString(): Primitive\CharacterString |
|
506 | + { |
|
507 | + if (!$this->_element instanceof Primitive\CharacterString) { |
|
508 | + throw new \UnexpectedValueException( |
|
509 | + $this->_generateExceptionMessage(Element::TYPE_CHARACTER_STRING)); |
|
510 | + } |
|
511 | + return $this->_element; |
|
512 | + } |
|
513 | + |
|
514 | + /** |
|
515 | + * Get the wrapped element as a BMP string type. |
|
516 | + * |
|
517 | + * @throws \UnexpectedValueException If the element is not a bmp string |
|
518 | + * |
|
519 | + * @return \Sop\ASN1\Type\Primitive\BMPString |
|
520 | + */ |
|
521 | + public function asBMPString(): Primitive\BMPString |
|
522 | + { |
|
523 | + if (!$this->_element instanceof Primitive\BMPString) { |
|
524 | + throw new \UnexpectedValueException( |
|
525 | + $this->_generateExceptionMessage(Element::TYPE_BMP_STRING)); |
|
526 | + } |
|
527 | + return $this->_element; |
|
528 | + } |
|
529 | + |
|
530 | + /** |
|
531 | + * Get the wrapped element as a constructed string type. |
|
532 | + * |
|
533 | + * @throws \UnexpectedValueException If the element is not a constructed string |
|
534 | + * |
|
535 | + * @return \Sop\ASN1\Type\Constructed\ConstructedString |
|
536 | + */ |
|
537 | + public function asConstructedString(): Constructed\ConstructedString |
|
538 | + { |
|
539 | + if (!$this->_element instanceof Constructed\ConstructedString) { |
|
540 | + throw new \UnexpectedValueException( |
|
541 | + $this->_generateExceptionMessage(Element::TYPE_CONSTRUCTED_STRING)); |
|
542 | + } |
|
543 | + return $this->_element; |
|
544 | + } |
|
545 | + |
|
546 | + /** |
|
547 | + * Get the wrapped element as any string type. |
|
548 | + * |
|
549 | + * @throws \UnexpectedValueException If the element is not a string |
|
550 | + * |
|
551 | + * @return StringType |
|
552 | + */ |
|
553 | + public function asString(): StringType |
|
554 | + { |
|
555 | + if (!$this->_element instanceof StringType) { |
|
556 | + throw new \UnexpectedValueException( |
|
557 | + $this->_generateExceptionMessage(Element::TYPE_STRING)); |
|
558 | + } |
|
559 | + return $this->_element; |
|
560 | + } |
|
561 | + |
|
562 | + /** |
|
563 | + * Get the wrapped element as any time type. |
|
564 | + * |
|
565 | + * @throws \UnexpectedValueException If the element is not a time |
|
566 | + * |
|
567 | + * @return TimeType |
|
568 | + */ |
|
569 | + public function asTime(): TimeType |
|
570 | + { |
|
571 | + if (!$this->_element instanceof TimeType) { |
|
572 | + throw new \UnexpectedValueException( |
|
573 | + $this->_generateExceptionMessage(Element::TYPE_TIME)); |
|
574 | + } |
|
575 | + return $this->_element; |
|
576 | + } |
|
577 | + |
|
578 | + /** |
|
579 | + * {@inheritdoc} |
|
580 | + */ |
|
581 | + public function toDER(): string |
|
582 | + { |
|
583 | + return $this->_element->toDER(); |
|
584 | + } |
|
585 | + |
|
586 | + /** |
|
587 | + * {@inheritdoc} |
|
588 | + */ |
|
589 | + public function typeClass(): int |
|
590 | + { |
|
591 | + return $this->_element->typeClass(); |
|
592 | + } |
|
593 | + |
|
594 | + /** |
|
595 | + * {@inheritdoc} |
|
596 | + */ |
|
597 | + public function isConstructed(): bool |
|
598 | + { |
|
599 | + return $this->_element->isConstructed(); |
|
600 | + } |
|
601 | + |
|
602 | + /** |
|
603 | + * {@inheritdoc} |
|
604 | + */ |
|
605 | + public function tag(): int |
|
606 | + { |
|
607 | + return $this->_element->tag(); |
|
608 | + } |
|
609 | + |
|
610 | + /** |
|
611 | + * {@inheritdoc} |
|
612 | + */ |
|
613 | + public function isType(int $tag): bool |
|
614 | + { |
|
615 | + return $this->_element->isType($tag); |
|
616 | + } |
|
617 | + |
|
618 | + /** |
|
619 | + * {@inheritdoc} |
|
620 | + * |
|
621 | + * Consider using any of the <code>as*</code> accessor methods instead. |
|
622 | + */ |
|
623 | + public function expectType(int $tag): ElementBase |
|
624 | + { |
|
625 | + return $this->_element->expectType($tag); |
|
626 | + } |
|
627 | + |
|
628 | + /** |
|
629 | + * {@inheritdoc} |
|
630 | + */ |
|
631 | + public function isTagged(): bool |
|
632 | + { |
|
633 | + return $this->_element->isTagged(); |
|
634 | + } |
|
635 | + |
|
636 | + /** |
|
637 | + * {@inheritdoc} |
|
638 | + * |
|
639 | + * Consider using <code>asTagged()</code> method instead and chaining |
|
640 | + * with <code>TaggedType::asExplicit()</code> or |
|
641 | + * <code>TaggedType::asImplicit()</code>. |
|
642 | + */ |
|
643 | + public function expectTagged(?int $tag = null): TaggedType |
|
644 | + { |
|
645 | + return $this->_element->expectTagged($tag); |
|
646 | + } |
|
647 | + |
|
648 | + /** |
|
649 | + * {@inheritdoc} |
|
650 | + */ |
|
651 | + public function asElement(): Element |
|
652 | + { |
|
653 | + return $this->_element; |
|
654 | + } |
|
655 | + |
|
656 | + /** |
|
657 | + * {@inheritdoc} |
|
658 | + */ |
|
659 | + public function asUnspecified(): UnspecifiedType |
|
660 | + { |
|
661 | + return $this; |
|
662 | + } |
|
663 | + |
|
664 | + /** |
|
665 | + * Generate message for exceptions thrown by <code>as*</code> methods. |
|
666 | + * |
|
667 | + * @param int $tag Type tag of the expected element |
|
668 | + * |
|
669 | + * @return string |
|
670 | + */ |
|
671 | + private function _generateExceptionMessage(int $tag): string |
|
672 | + { |
|
673 | + return sprintf('%s expected, got %s.', Element::tagToName($tag), |
|
674 | + $this->_typeDescriptorString()); |
|
675 | + } |
|
676 | + |
|
677 | + /** |
|
678 | + * Get textual description of the wrapped element for debugging purposes. |
|
679 | + * |
|
680 | + * @return string |
|
681 | + */ |
|
682 | + private function _typeDescriptorString(): string |
|
683 | + { |
|
684 | + $type_cls = $this->_element->typeClass(); |
|
685 | + $tag = $this->_element->tag(); |
|
686 | + if (Identifier::CLASS_UNIVERSAL === $type_cls) { |
|
687 | + return Element::tagToName($tag); |
|
688 | + } |
|
689 | + return Identifier::classToName($type_cls) . " TAG {$tag}"; |
|
690 | + } |
|
691 | 691 | } |
@@ -17,276 +17,276 @@ |
||
17 | 17 | */ |
18 | 18 | class Real extends Element |
19 | 19 | { |
20 | - use UniversalClass; |
|
21 | - use PrimitiveType; |
|
20 | + use UniversalClass; |
|
21 | + use PrimitiveType; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Regex pattern to parse NR3 form number conforming to DER. |
|
25 | - * |
|
26 | - * @var string |
|
27 | - */ |
|
28 | - const NR3_REGEX = '/^(-?)(\d+)?\.E([+\-]?\d+)$/'; |
|
23 | + /** |
|
24 | + * Regex pattern to parse NR3 form number conforming to DER. |
|
25 | + * |
|
26 | + * @var string |
|
27 | + */ |
|
28 | + const NR3_REGEX = '/^(-?)(\d+)?\.E([+\-]?\d+)$/'; |
|
29 | 29 | |
30 | - /** |
|
31 | - * Regex pattern to parse PHP exponent number format. |
|
32 | - * |
|
33 | - * @see http://php.net/manual/en/language.types.float.php |
|
34 | - * |
|
35 | - * @var string |
|
36 | - */ |
|
37 | - const PHP_EXPONENT_DNUM = '/^' . |
|
38 | - '([+\-]?' . // sign |
|
39 | - '(?:' . |
|
40 | - '\d+' . // LNUM |
|
41 | - '|' . |
|
42 | - '(?:\d*\.\d+|\d+\.\d*)' . // DNUM |
|
43 | - '))[eE]' . |
|
44 | - '([+\-]?\d+)' . // exponent |
|
45 | - '$/'; |
|
30 | + /** |
|
31 | + * Regex pattern to parse PHP exponent number format. |
|
32 | + * |
|
33 | + * @see http://php.net/manual/en/language.types.float.php |
|
34 | + * |
|
35 | + * @var string |
|
36 | + */ |
|
37 | + const PHP_EXPONENT_DNUM = '/^' . |
|
38 | + '([+\-]?' . // sign |
|
39 | + '(?:' . |
|
40 | + '\d+' . // LNUM |
|
41 | + '|' . |
|
42 | + '(?:\d*\.\d+|\d+\.\d*)' . // DNUM |
|
43 | + '))[eE]' . |
|
44 | + '([+\-]?\d+)' . // exponent |
|
45 | + '$/'; |
|
46 | 46 | |
47 | - /** |
|
48 | - * Number zero represented in NR3 form. |
|
49 | - * |
|
50 | - * @var string |
|
51 | - */ |
|
52 | - const NR3_ZERO = '.E+0'; |
|
47 | + /** |
|
48 | + * Number zero represented in NR3 form. |
|
49 | + * |
|
50 | + * @var string |
|
51 | + */ |
|
52 | + const NR3_ZERO = '.E+0'; |
|
53 | 53 | |
54 | - /** |
|
55 | - * Number in NR3 form. |
|
56 | - * |
|
57 | - * @var string |
|
58 | - */ |
|
59 | - private $_number; |
|
54 | + /** |
|
55 | + * Number in NR3 form. |
|
56 | + * |
|
57 | + * @var string |
|
58 | + */ |
|
59 | + private $_number; |
|
60 | 60 | |
61 | - /** |
|
62 | - * Constructor. |
|
63 | - * |
|
64 | - * @param string $number number in NR3 form |
|
65 | - */ |
|
66 | - public function __construct(string $number) |
|
67 | - { |
|
68 | - $this->_typeTag = self::TYPE_REAL; |
|
69 | - if (!self::_validateNumber($number)) { |
|
70 | - throw new \InvalidArgumentException( |
|
71 | - "'{$number}' is not a valid NR3 form real."); |
|
72 | - } |
|
73 | - $this->_number = $number; |
|
74 | - } |
|
61 | + /** |
|
62 | + * Constructor. |
|
63 | + * |
|
64 | + * @param string $number number in NR3 form |
|
65 | + */ |
|
66 | + public function __construct(string $number) |
|
67 | + { |
|
68 | + $this->_typeTag = self::TYPE_REAL; |
|
69 | + if (!self::_validateNumber($number)) { |
|
70 | + throw new \InvalidArgumentException( |
|
71 | + "'{$number}' is not a valid NR3 form real."); |
|
72 | + } |
|
73 | + $this->_number = $number; |
|
74 | + } |
|
75 | 75 | |
76 | - /** |
|
77 | - * Initialize from float. |
|
78 | - * |
|
79 | - * @param float $number |
|
80 | - * |
|
81 | - * @return self |
|
82 | - */ |
|
83 | - public static function fromFloat(float $number): self |
|
84 | - { |
|
85 | - return new self(self::_decimalToNR3(strval($number))); |
|
86 | - } |
|
76 | + /** |
|
77 | + * Initialize from float. |
|
78 | + * |
|
79 | + * @param float $number |
|
80 | + * |
|
81 | + * @return self |
|
82 | + */ |
|
83 | + public static function fromFloat(float $number): self |
|
84 | + { |
|
85 | + return new self(self::_decimalToNR3(strval($number))); |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * Get number as a float. |
|
90 | - * |
|
91 | - * @return float |
|
92 | - */ |
|
93 | - public function float(): float |
|
94 | - { |
|
95 | - return self::_nr3ToDecimal($this->_number); |
|
96 | - } |
|
88 | + /** |
|
89 | + * Get number as a float. |
|
90 | + * |
|
91 | + * @return float |
|
92 | + */ |
|
93 | + public function float(): float |
|
94 | + { |
|
95 | + return self::_nr3ToDecimal($this->_number); |
|
96 | + } |
|
97 | 97 | |
98 | - /** |
|
99 | - * {@inheritdoc} |
|
100 | - */ |
|
101 | - protected function _encodedContentDER(): string |
|
102 | - { |
|
103 | - /* if the real value is the value zero, there shall be no contents |
|
98 | + /** |
|
99 | + * {@inheritdoc} |
|
100 | + */ |
|
101 | + protected function _encodedContentDER(): string |
|
102 | + { |
|
103 | + /* if the real value is the value zero, there shall be no contents |
|
104 | 104 | octets in the encoding. (X.690 07-2002, section 8.5.2) */ |
105 | - if (self::NR3_ZERO === $this->_number) { |
|
106 | - return ''; |
|
107 | - } |
|
108 | - // encode in NR3 decimal encoding |
|
109 | - return chr(0x03) . $this->_number; |
|
110 | - } |
|
105 | + if (self::NR3_ZERO === $this->_number) { |
|
106 | + return ''; |
|
107 | + } |
|
108 | + // encode in NR3 decimal encoding |
|
109 | + return chr(0x03) . $this->_number; |
|
110 | + } |
|
111 | 111 | |
112 | - /** |
|
113 | - * {@inheritdoc} |
|
114 | - */ |
|
115 | - protected static function _decodeFromDER(Identifier $identifier, |
|
116 | - string $data, int &$offset): ElementBase |
|
117 | - { |
|
118 | - $idx = $offset; |
|
119 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
120 | - // if length is zero, value is zero (spec 8.5.2) |
|
121 | - if (!$length) { |
|
122 | - $obj = new self(self::NR3_ZERO); |
|
123 | - } else { |
|
124 | - $bytes = substr($data, $idx, $length); |
|
125 | - $byte = ord($bytes[0]); |
|
126 | - if (0x80 & $byte) { // bit 8 = 1 |
|
127 | - $obj = self::_decodeBinaryEncoding($bytes); |
|
128 | - } elseif (0x00 === $byte >> 6) { // bit 8 = 0, bit 7 = 0 |
|
129 | - $obj = self::_decodeDecimalEncoding($bytes); |
|
130 | - } else { // bit 8 = 0, bit 7 = 1 |
|
131 | - $obj = self::_decodeSpecialRealValue($bytes); |
|
132 | - } |
|
133 | - } |
|
134 | - $offset = $idx + $length; |
|
135 | - return $obj; |
|
136 | - } |
|
112 | + /** |
|
113 | + * {@inheritdoc} |
|
114 | + */ |
|
115 | + protected static function _decodeFromDER(Identifier $identifier, |
|
116 | + string $data, int &$offset): ElementBase |
|
117 | + { |
|
118 | + $idx = $offset; |
|
119 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
120 | + // if length is zero, value is zero (spec 8.5.2) |
|
121 | + if (!$length) { |
|
122 | + $obj = new self(self::NR3_ZERO); |
|
123 | + } else { |
|
124 | + $bytes = substr($data, $idx, $length); |
|
125 | + $byte = ord($bytes[0]); |
|
126 | + if (0x80 & $byte) { // bit 8 = 1 |
|
127 | + $obj = self::_decodeBinaryEncoding($bytes); |
|
128 | + } elseif (0x00 === $byte >> 6) { // bit 8 = 0, bit 7 = 0 |
|
129 | + $obj = self::_decodeDecimalEncoding($bytes); |
|
130 | + } else { // bit 8 = 0, bit 7 = 1 |
|
131 | + $obj = self::_decodeSpecialRealValue($bytes); |
|
132 | + } |
|
133 | + } |
|
134 | + $offset = $idx + $length; |
|
135 | + return $obj; |
|
136 | + } |
|
137 | 137 | |
138 | - /** |
|
139 | - * @todo Implement |
|
140 | - * |
|
141 | - * @param string $data |
|
142 | - */ |
|
143 | - protected static function _decodeBinaryEncoding(string $data) |
|
144 | - { |
|
145 | - throw new \RuntimeException( |
|
146 | - 'Binary encoding of REAL is not implemented.'); |
|
147 | - } |
|
138 | + /** |
|
139 | + * @todo Implement |
|
140 | + * |
|
141 | + * @param string $data |
|
142 | + */ |
|
143 | + protected static function _decodeBinaryEncoding(string $data) |
|
144 | + { |
|
145 | + throw new \RuntimeException( |
|
146 | + 'Binary encoding of REAL is not implemented.'); |
|
147 | + } |
|
148 | 148 | |
149 | - /** |
|
150 | - * @param string $data |
|
151 | - * |
|
152 | - * @throws \RuntimeException |
|
153 | - * |
|
154 | - * @return self |
|
155 | - */ |
|
156 | - protected static function _decodeDecimalEncoding(string $data): self |
|
157 | - { |
|
158 | - $nr = ord($data[0]) & 0x03; |
|
159 | - if (0x03 !== $nr) { |
|
160 | - throw new \RuntimeException('Only NR3 form supported.'); |
|
161 | - } |
|
162 | - $str = substr($data, 1); |
|
163 | - return new self($str); |
|
164 | - } |
|
149 | + /** |
|
150 | + * @param string $data |
|
151 | + * |
|
152 | + * @throws \RuntimeException |
|
153 | + * |
|
154 | + * @return self |
|
155 | + */ |
|
156 | + protected static function _decodeDecimalEncoding(string $data): self |
|
157 | + { |
|
158 | + $nr = ord($data[0]) & 0x03; |
|
159 | + if (0x03 !== $nr) { |
|
160 | + throw new \RuntimeException('Only NR3 form supported.'); |
|
161 | + } |
|
162 | + $str = substr($data, 1); |
|
163 | + return new self($str); |
|
164 | + } |
|
165 | 165 | |
166 | - /** |
|
167 | - * @todo Implement |
|
168 | - * |
|
169 | - * @param string $data |
|
170 | - */ |
|
171 | - protected static function _decodeSpecialRealValue(string $data) |
|
172 | - { |
|
173 | - if (1 !== strlen($data)) { |
|
174 | - throw new DecodeException( |
|
175 | - 'SpecialRealValue must have one content octet.'); |
|
176 | - } |
|
177 | - $byte = ord($data[0]); |
|
178 | - if (0x40 === $byte) { // positive infinity |
|
179 | - throw new \RuntimeException('PLUS-INFINITY not supported.'); |
|
180 | - } |
|
181 | - if (0x41 === $byte) { // negative infinity |
|
182 | - throw new \RuntimeException('MINUS-INFINITY not supported.'); |
|
183 | - } |
|
184 | - throw new DecodeException('Invalid SpecialRealValue encoding.'); |
|
185 | - } |
|
166 | + /** |
|
167 | + * @todo Implement |
|
168 | + * |
|
169 | + * @param string $data |
|
170 | + */ |
|
171 | + protected static function _decodeSpecialRealValue(string $data) |
|
172 | + { |
|
173 | + if (1 !== strlen($data)) { |
|
174 | + throw new DecodeException( |
|
175 | + 'SpecialRealValue must have one content octet.'); |
|
176 | + } |
|
177 | + $byte = ord($data[0]); |
|
178 | + if (0x40 === $byte) { // positive infinity |
|
179 | + throw new \RuntimeException('PLUS-INFINITY not supported.'); |
|
180 | + } |
|
181 | + if (0x41 === $byte) { // negative infinity |
|
182 | + throw new \RuntimeException('MINUS-INFINITY not supported.'); |
|
183 | + } |
|
184 | + throw new DecodeException('Invalid SpecialRealValue encoding.'); |
|
185 | + } |
|
186 | 186 | |
187 | - /** |
|
188 | - * Convert decimal number string to NR3 form. |
|
189 | - * |
|
190 | - * @param string $str |
|
191 | - * |
|
192 | - * @return string |
|
193 | - */ |
|
194 | - private static function _decimalToNR3(string $str): string |
|
195 | - { |
|
196 | - // if number is in exponent form |
|
197 | - /** @var string[] $match */ |
|
198 | - if (preg_match(self::PHP_EXPONENT_DNUM, $str, $match)) { |
|
199 | - $parts = explode('.', $match[1]); |
|
200 | - $m = ltrim($parts[0], '0'); |
|
201 | - $e = intval($match[2]); |
|
202 | - // if mantissa had decimals |
|
203 | - if (2 === count($parts)) { |
|
204 | - $d = rtrim($parts[1], '0'); |
|
205 | - $e -= strlen($d); |
|
206 | - $m .= $d; |
|
207 | - } |
|
208 | - } else { |
|
209 | - // explode from decimal |
|
210 | - $parts = explode('.', $str); |
|
211 | - $m = ltrim($parts[0], '0'); |
|
212 | - // if number had decimals |
|
213 | - if (2 === count($parts)) { |
|
214 | - // exponent is negative number of the decimals |
|
215 | - $e = -strlen($parts[1]); |
|
216 | - // append decimals to the mantissa |
|
217 | - $m .= $parts[1]; |
|
218 | - } else { |
|
219 | - $e = 0; |
|
220 | - } |
|
221 | - // shift trailing zeroes from the mantissa to the exponent |
|
222 | - while ('0' === substr($m, -1)) { |
|
223 | - ++$e; |
|
224 | - $m = substr($m, 0, -1); |
|
225 | - } |
|
226 | - } |
|
227 | - /* if exponent is zero, it must be prefixed with a "+" sign |
|
187 | + /** |
|
188 | + * Convert decimal number string to NR3 form. |
|
189 | + * |
|
190 | + * @param string $str |
|
191 | + * |
|
192 | + * @return string |
|
193 | + */ |
|
194 | + private static function _decimalToNR3(string $str): string |
|
195 | + { |
|
196 | + // if number is in exponent form |
|
197 | + /** @var string[] $match */ |
|
198 | + if (preg_match(self::PHP_EXPONENT_DNUM, $str, $match)) { |
|
199 | + $parts = explode('.', $match[1]); |
|
200 | + $m = ltrim($parts[0], '0'); |
|
201 | + $e = intval($match[2]); |
|
202 | + // if mantissa had decimals |
|
203 | + if (2 === count($parts)) { |
|
204 | + $d = rtrim($parts[1], '0'); |
|
205 | + $e -= strlen($d); |
|
206 | + $m .= $d; |
|
207 | + } |
|
208 | + } else { |
|
209 | + // explode from decimal |
|
210 | + $parts = explode('.', $str); |
|
211 | + $m = ltrim($parts[0], '0'); |
|
212 | + // if number had decimals |
|
213 | + if (2 === count($parts)) { |
|
214 | + // exponent is negative number of the decimals |
|
215 | + $e = -strlen($parts[1]); |
|
216 | + // append decimals to the mantissa |
|
217 | + $m .= $parts[1]; |
|
218 | + } else { |
|
219 | + $e = 0; |
|
220 | + } |
|
221 | + // shift trailing zeroes from the mantissa to the exponent |
|
222 | + while ('0' === substr($m, -1)) { |
|
223 | + ++$e; |
|
224 | + $m = substr($m, 0, -1); |
|
225 | + } |
|
226 | + } |
|
227 | + /* if exponent is zero, it must be prefixed with a "+" sign |
|
228 | 228 | (X.690 07-2002, section 11.3.2.6) */ |
229 | - if (0 === $e) { |
|
230 | - $es = '+'; |
|
231 | - } else { |
|
232 | - $es = $e < 0 ? '-' : ''; |
|
233 | - } |
|
234 | - return sprintf('%s.E%s%d', $m, $es, abs($e)); |
|
235 | - } |
|
229 | + if (0 === $e) { |
|
230 | + $es = '+'; |
|
231 | + } else { |
|
232 | + $es = $e < 0 ? '-' : ''; |
|
233 | + } |
|
234 | + return sprintf('%s.E%s%d', $m, $es, abs($e)); |
|
235 | + } |
|
236 | 236 | |
237 | - /** |
|
238 | - * Convert NR3 form number to decimal. |
|
239 | - * |
|
240 | - * @param string $str |
|
241 | - * |
|
242 | - * @throws \UnexpectedValueException |
|
243 | - * |
|
244 | - * @return float |
|
245 | - */ |
|
246 | - private static function _nr3ToDecimal(string $str): float |
|
247 | - { |
|
248 | - /** @var string[] $match */ |
|
249 | - if (!preg_match(self::NR3_REGEX, $str, $match)) { |
|
250 | - throw new \UnexpectedValueException( |
|
251 | - "'{$str}' is not a valid NR3 form real."); |
|
252 | - } |
|
253 | - $m = $match[2]; |
|
254 | - // if number started with minus sign |
|
255 | - $inv = '-' === $match[1]; |
|
256 | - $e = intval($match[3]); |
|
257 | - // positive exponent |
|
258 | - if ($e > 0) { |
|
259 | - // pad with trailing zeroes |
|
260 | - $num = $m . str_repeat('0', $e); |
|
261 | - } elseif ($e < 0) { |
|
262 | - // pad with leading zeroes |
|
263 | - if (strlen($m) < abs($e)) { |
|
264 | - $m = str_repeat('0', intval(abs($e)) - strlen($m)) . $m; |
|
265 | - } |
|
266 | - // insert decimal point |
|
267 | - $num = substr($m, 0, $e) . '.' . substr($m, $e); |
|
268 | - } else { |
|
269 | - $num = empty($m) ? '0' : $m; |
|
270 | - } |
|
271 | - // if number is negative |
|
272 | - if ($inv) { |
|
273 | - $num = "-{$num}"; |
|
274 | - } |
|
275 | - return floatval($num); |
|
276 | - } |
|
237 | + /** |
|
238 | + * Convert NR3 form number to decimal. |
|
239 | + * |
|
240 | + * @param string $str |
|
241 | + * |
|
242 | + * @throws \UnexpectedValueException |
|
243 | + * |
|
244 | + * @return float |
|
245 | + */ |
|
246 | + private static function _nr3ToDecimal(string $str): float |
|
247 | + { |
|
248 | + /** @var string[] $match */ |
|
249 | + if (!preg_match(self::NR3_REGEX, $str, $match)) { |
|
250 | + throw new \UnexpectedValueException( |
|
251 | + "'{$str}' is not a valid NR3 form real."); |
|
252 | + } |
|
253 | + $m = $match[2]; |
|
254 | + // if number started with minus sign |
|
255 | + $inv = '-' === $match[1]; |
|
256 | + $e = intval($match[3]); |
|
257 | + // positive exponent |
|
258 | + if ($e > 0) { |
|
259 | + // pad with trailing zeroes |
|
260 | + $num = $m . str_repeat('0', $e); |
|
261 | + } elseif ($e < 0) { |
|
262 | + // pad with leading zeroes |
|
263 | + if (strlen($m) < abs($e)) { |
|
264 | + $m = str_repeat('0', intval(abs($e)) - strlen($m)) . $m; |
|
265 | + } |
|
266 | + // insert decimal point |
|
267 | + $num = substr($m, 0, $e) . '.' . substr($m, $e); |
|
268 | + } else { |
|
269 | + $num = empty($m) ? '0' : $m; |
|
270 | + } |
|
271 | + // if number is negative |
|
272 | + if ($inv) { |
|
273 | + $num = "-{$num}"; |
|
274 | + } |
|
275 | + return floatval($num); |
|
276 | + } |
|
277 | 277 | |
278 | - /** |
|
279 | - * Test that number is valid for this context. |
|
280 | - * |
|
281 | - * @param mixed $num |
|
282 | - * |
|
283 | - * @return bool |
|
284 | - */ |
|
285 | - private static function _validateNumber($num): bool |
|
286 | - { |
|
287 | - if (!preg_match(self::NR3_REGEX, $num)) { |
|
288 | - return false; |
|
289 | - } |
|
290 | - return true; |
|
291 | - } |
|
278 | + /** |
|
279 | + * Test that number is valid for this context. |
|
280 | + * |
|
281 | + * @param mixed $num |
|
282 | + * |
|
283 | + * @return bool |
|
284 | + */ |
|
285 | + private static function _validateNumber($num): bool |
|
286 | + { |
|
287 | + if (!preg_match(self::NR3_REGEX, $num)) { |
|
288 | + return false; |
|
289 | + } |
|
290 | + return true; |
|
291 | + } |
|
292 | 292 | } |
@@ -17,61 +17,61 @@ |
||
17 | 17 | */ |
18 | 18 | class Boolean extends Element |
19 | 19 | { |
20 | - use UniversalClass; |
|
21 | - use PrimitiveType; |
|
20 | + use UniversalClass; |
|
21 | + use PrimitiveType; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Value. |
|
25 | - * |
|
26 | - * @var bool |
|
27 | - */ |
|
28 | - private $_bool; |
|
23 | + /** |
|
24 | + * Value. |
|
25 | + * |
|
26 | + * @var bool |
|
27 | + */ |
|
28 | + private $_bool; |
|
29 | 29 | |
30 | - /** |
|
31 | - * Constructor. |
|
32 | - * |
|
33 | - * @param bool $bool |
|
34 | - */ |
|
35 | - public function __construct(bool $bool) |
|
36 | - { |
|
37 | - $this->_typeTag = self::TYPE_BOOLEAN; |
|
38 | - $this->_bool = $bool; |
|
39 | - } |
|
30 | + /** |
|
31 | + * Constructor. |
|
32 | + * |
|
33 | + * @param bool $bool |
|
34 | + */ |
|
35 | + public function __construct(bool $bool) |
|
36 | + { |
|
37 | + $this->_typeTag = self::TYPE_BOOLEAN; |
|
38 | + $this->_bool = $bool; |
|
39 | + } |
|
40 | 40 | |
41 | - /** |
|
42 | - * Get the value. |
|
43 | - * |
|
44 | - * @return bool |
|
45 | - */ |
|
46 | - public function value(): bool |
|
47 | - { |
|
48 | - return $this->_bool; |
|
49 | - } |
|
41 | + /** |
|
42 | + * Get the value. |
|
43 | + * |
|
44 | + * @return bool |
|
45 | + */ |
|
46 | + public function value(): bool |
|
47 | + { |
|
48 | + return $this->_bool; |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * {@inheritdoc} |
|
53 | - */ |
|
54 | - protected function _encodedContentDER(): string |
|
55 | - { |
|
56 | - return $this->_bool ? chr(0xff) : chr(0); |
|
57 | - } |
|
51 | + /** |
|
52 | + * {@inheritdoc} |
|
53 | + */ |
|
54 | + protected function _encodedContentDER(): string |
|
55 | + { |
|
56 | + return $this->_bool ? chr(0xff) : chr(0); |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * {@inheritdoc} |
|
61 | - */ |
|
62 | - protected static function _decodeFromDER(Identifier $identifier, |
|
63 | - string $data, int &$offset): ElementBase |
|
64 | - { |
|
65 | - $idx = $offset; |
|
66 | - Length::expectFromDER($data, $idx, 1); |
|
67 | - $byte = ord($data[$idx++]); |
|
68 | - if (0 !== $byte) { |
|
69 | - if (0xff !== $byte) { |
|
70 | - throw new DecodeException( |
|
71 | - 'DER encoded boolean true must have all bits set to 1.'); |
|
72 | - } |
|
73 | - } |
|
74 | - $offset = $idx; |
|
75 | - return new self(0 !== $byte); |
|
76 | - } |
|
59 | + /** |
|
60 | + * {@inheritdoc} |
|
61 | + */ |
|
62 | + protected static function _decodeFromDER(Identifier $identifier, |
|
63 | + string $data, int &$offset): ElementBase |
|
64 | + { |
|
65 | + $idx = $offset; |
|
66 | + Length::expectFromDER($data, $idx, 1); |
|
67 | + $byte = ord($data[$idx++]); |
|
68 | + if (0 !== $byte) { |
|
69 | + if (0xff !== $byte) { |
|
70 | + throw new DecodeException( |
|
71 | + 'DER encoded boolean true must have all bits set to 1.'); |
|
72 | + } |
|
73 | + } |
|
74 | + $offset = $idx; |
|
75 | + return new self(0 !== $byte); |
|
76 | + } |
|
77 | 77 | } |