@@ -17,692 +17,692 @@ |
||
17 | 17 | */ |
18 | 18 | class UnspecifiedType implements ElementBase |
19 | 19 | { |
20 | - /** |
|
21 | - * The wrapped element. |
|
22 | - * |
|
23 | - * @var Element |
|
24 | - */ |
|
25 | - private $_element; |
|
26 | - |
|
27 | - /** |
|
28 | - * Constructor. |
|
29 | - * |
|
30 | - * @param Element $el |
|
31 | - */ |
|
32 | - public function __construct(Element $el) |
|
33 | - { |
|
34 | - $this->_element = $el; |
|
35 | - } |
|
36 | - |
|
37 | - /** |
|
38 | - * Compatibility method to dispatch calls to the wrapped element. |
|
39 | - * |
|
40 | - * @todo Remove |
|
41 | - * |
|
42 | - * @deprecated Use <code>as*</code> accessor methods to ensure strict type |
|
43 | - * |
|
44 | - * @param string $mtd Method name |
|
45 | - * @param array $args Arguments |
|
46 | - * |
|
47 | - * @return mixed |
|
48 | - */ |
|
49 | - public function __call($mtd, array $args) |
|
50 | - { |
|
51 | - return call_user_func_array([$this->_element, $mtd], $args); |
|
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * Initialize from DER data. |
|
56 | - * |
|
57 | - * @param string $data DER encoded data |
|
58 | - * |
|
59 | - * @return self |
|
60 | - */ |
|
61 | - public static function fromDER(string $data): self |
|
62 | - { |
|
63 | - return Element::fromDER($data)->asUnspecified(); |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * Initialize from ElementBase interface. |
|
68 | - * |
|
69 | - * @param ElementBase $el |
|
70 | - * |
|
71 | - * @return self |
|
72 | - */ |
|
73 | - public static function fromElementBase(ElementBase $el): self |
|
74 | - { |
|
75 | - // if element is already wrapped |
|
76 | - if ($el instanceof self) { |
|
77 | - return $el; |
|
78 | - } |
|
79 | - return new self($el->asElement()); |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * Get the wrapped element as a context specific tagged type. |
|
84 | - * |
|
85 | - * @throws \UnexpectedValueException If the element is not tagged |
|
86 | - * |
|
87 | - * @return TaggedType |
|
88 | - */ |
|
89 | - public function asTagged(): TaggedType |
|
90 | - { |
|
91 | - if (!$this->_element instanceof TaggedType) { |
|
92 | - throw new \UnexpectedValueException( |
|
93 | - 'Tagged element expected, got ' . $this->_typeDescriptorString()); |
|
94 | - } |
|
95 | - return $this->_element; |
|
96 | - } |
|
97 | - |
|
98 | - /** |
|
99 | - * Get the wrapped element as an application specific type. |
|
100 | - * |
|
101 | - * @throws \UnexpectedValueException If element is not application specific |
|
102 | - * |
|
103 | - * @return \Sop\ASN1\Type\Tagged\ApplicationType |
|
104 | - */ |
|
105 | - public function asApplication(): Tagged\ApplicationType |
|
106 | - { |
|
107 | - if (!$this->_element instanceof Tagged\ApplicationType) { |
|
108 | - throw new \UnexpectedValueException( |
|
109 | - 'Application type expected, got ' . |
|
110 | - $this->_typeDescriptorString()); |
|
111 | - } |
|
112 | - return $this->_element; |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * Get the wrapped element as a private tagged type. |
|
117 | - * |
|
118 | - * @throws \UnexpectedValueException If element is not using private tagging |
|
119 | - * |
|
120 | - * @return \Sop\ASN1\Type\Tagged\PrivateType |
|
121 | - */ |
|
122 | - public function asPrivate(): Tagged\PrivateType |
|
123 | - { |
|
124 | - if (!$this->_element instanceof Tagged\PrivateType) { |
|
125 | - throw new \UnexpectedValueException( |
|
126 | - 'Private type expected, got ' . $this->_typeDescriptorString()); |
|
127 | - } |
|
128 | - return $this->_element; |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * Get the wrapped element as a boolean type. |
|
133 | - * |
|
134 | - * @throws \UnexpectedValueException If the element is not a boolean |
|
135 | - * |
|
136 | - * @return \Sop\ASN1\Type\Primitive\Boolean |
|
137 | - */ |
|
138 | - public function asBoolean(): Primitive\Boolean |
|
139 | - { |
|
140 | - if (!$this->_element instanceof Primitive\Boolean) { |
|
141 | - throw new \UnexpectedValueException( |
|
142 | - $this->_generateExceptionMessage(Element::TYPE_BOOLEAN)); |
|
143 | - } |
|
144 | - return $this->_element; |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * Get the wrapped element as an integer type. |
|
149 | - * |
|
150 | - * @throws \UnexpectedValueException If the element is not an integer |
|
151 | - * |
|
152 | - * @return \Sop\ASN1\Type\Primitive\Integer |
|
153 | - */ |
|
154 | - public function asInteger(): Primitive\Integer |
|
155 | - { |
|
156 | - if (!$this->_element instanceof Primitive\Integer) { |
|
157 | - throw new \UnexpectedValueException( |
|
158 | - $this->_generateExceptionMessage(Element::TYPE_INTEGER)); |
|
159 | - } |
|
160 | - return $this->_element; |
|
161 | - } |
|
162 | - |
|
163 | - /** |
|
164 | - * Get the wrapped element as a bit string type. |
|
165 | - * |
|
166 | - * @throws \UnexpectedValueException If the element is not a bit string |
|
167 | - * |
|
168 | - * @return \Sop\ASN1\Type\Primitive\BitString |
|
169 | - */ |
|
170 | - public function asBitString(): Primitive\BitString |
|
171 | - { |
|
172 | - if (!$this->_element instanceof Primitive\BitString) { |
|
173 | - throw new \UnexpectedValueException( |
|
174 | - $this->_generateExceptionMessage(Element::TYPE_BIT_STRING)); |
|
175 | - } |
|
176 | - return $this->_element; |
|
177 | - } |
|
178 | - |
|
179 | - /** |
|
180 | - * Get the wrapped element as an octet string type. |
|
181 | - * |
|
182 | - * @throws \UnexpectedValueException If the element is not an octet string |
|
183 | - * |
|
184 | - * @return \Sop\ASN1\Type\Primitive\OctetString |
|
185 | - */ |
|
186 | - public function asOctetString(): Primitive\OctetString |
|
187 | - { |
|
188 | - if (!$this->_element instanceof Primitive\OctetString) { |
|
189 | - throw new \UnexpectedValueException( |
|
190 | - $this->_generateExceptionMessage(Element::TYPE_OCTET_STRING)); |
|
191 | - } |
|
192 | - return $this->_element; |
|
193 | - } |
|
194 | - |
|
195 | - /** |
|
196 | - * Get the wrapped element as a null type. |
|
197 | - * |
|
198 | - * @throws \UnexpectedValueException If the element is not a null |
|
199 | - * |
|
200 | - * @return \Sop\ASN1\Type\Primitive\NullType |
|
201 | - */ |
|
202 | - public function asNull(): Primitive\NullType |
|
203 | - { |
|
204 | - if (!$this->_element instanceof Primitive\NullType) { |
|
205 | - throw new \UnexpectedValueException( |
|
206 | - $this->_generateExceptionMessage(Element::TYPE_NULL)); |
|
207 | - } |
|
208 | - return $this->_element; |
|
209 | - } |
|
210 | - |
|
211 | - /** |
|
212 | - * Get the wrapped element as an object identifier type. |
|
213 | - * |
|
214 | - * @throws \UnexpectedValueException If the element is not an object |
|
215 | - * identifier |
|
216 | - * |
|
217 | - * @return \Sop\ASN1\Type\Primitive\ObjectIdentifier |
|
218 | - */ |
|
219 | - public function asObjectIdentifier(): Primitive\ObjectIdentifier |
|
220 | - { |
|
221 | - if (!$this->_element instanceof Primitive\ObjectIdentifier) { |
|
222 | - throw new \UnexpectedValueException( |
|
223 | - $this->_generateExceptionMessage( |
|
224 | - Element::TYPE_OBJECT_IDENTIFIER)); |
|
225 | - } |
|
226 | - return $this->_element; |
|
227 | - } |
|
228 | - |
|
229 | - /** |
|
230 | - * Get the wrapped element as an object descriptor type. |
|
231 | - * |
|
232 | - * @throws \UnexpectedValueException If the element is not an object |
|
233 | - * descriptor |
|
234 | - * |
|
235 | - * @return \Sop\ASN1\Type\Primitive\ObjectDescriptor |
|
236 | - */ |
|
237 | - public function asObjectDescriptor(): Primitive\ObjectDescriptor |
|
238 | - { |
|
239 | - if (!$this->_element instanceof Primitive\ObjectDescriptor) { |
|
240 | - throw new \UnexpectedValueException( |
|
241 | - $this->_generateExceptionMessage( |
|
242 | - Element::TYPE_OBJECT_DESCRIPTOR)); |
|
243 | - } |
|
244 | - return $this->_element; |
|
245 | - } |
|
246 | - |
|
247 | - /** |
|
248 | - * Get the wrapped element as a real type. |
|
249 | - * |
|
250 | - * @throws \UnexpectedValueException If the element is not a real |
|
251 | - * |
|
252 | - * @return \Sop\ASN1\Type\Primitive\Real |
|
253 | - */ |
|
254 | - public function asReal(): Primitive\Real |
|
255 | - { |
|
256 | - if (!$this->_element instanceof Primitive\Real) { |
|
257 | - throw new \UnexpectedValueException( |
|
258 | - $this->_generateExceptionMessage(Element::TYPE_REAL)); |
|
259 | - } |
|
260 | - return $this->_element; |
|
261 | - } |
|
262 | - |
|
263 | - /** |
|
264 | - * Get the wrapped element as an enumerated type. |
|
265 | - * |
|
266 | - * @throws \UnexpectedValueException If the element is not an enumerated |
|
267 | - * |
|
268 | - * @return \Sop\ASN1\Type\Primitive\Enumerated |
|
269 | - */ |
|
270 | - public function asEnumerated(): Primitive\Enumerated |
|
271 | - { |
|
272 | - if (!$this->_element instanceof Primitive\Enumerated) { |
|
273 | - throw new \UnexpectedValueException( |
|
274 | - $this->_generateExceptionMessage(Element::TYPE_ENUMERATED)); |
|
275 | - } |
|
276 | - return $this->_element; |
|
277 | - } |
|
278 | - |
|
279 | - /** |
|
280 | - * Get the wrapped element as a UTF8 string type. |
|
281 | - * |
|
282 | - * @throws \UnexpectedValueException If the element is not a UTF8 string |
|
283 | - * |
|
284 | - * @return \Sop\ASN1\Type\Primitive\UTF8String |
|
285 | - */ |
|
286 | - public function asUTF8String(): Primitive\UTF8String |
|
287 | - { |
|
288 | - if (!$this->_element instanceof Primitive\UTF8String) { |
|
289 | - throw new \UnexpectedValueException( |
|
290 | - $this->_generateExceptionMessage(Element::TYPE_UTF8_STRING)); |
|
291 | - } |
|
292 | - return $this->_element; |
|
293 | - } |
|
294 | - |
|
295 | - /** |
|
296 | - * Get the wrapped element as a relative OID type. |
|
297 | - * |
|
298 | - * @throws \UnexpectedValueException If the element is not a relative OID |
|
299 | - * |
|
300 | - * @return \Sop\ASN1\Type\Primitive\RelativeOID |
|
301 | - */ |
|
302 | - public function asRelativeOID(): Primitive\RelativeOID |
|
303 | - { |
|
304 | - if (!$this->_element instanceof Primitive\RelativeOID) { |
|
305 | - throw new \UnexpectedValueException( |
|
306 | - $this->_generateExceptionMessage(Element::TYPE_RELATIVE_OID)); |
|
307 | - } |
|
308 | - return $this->_element; |
|
309 | - } |
|
310 | - |
|
311 | - /** |
|
312 | - * Get the wrapped element as a sequence type. |
|
313 | - * |
|
314 | - * @throws \UnexpectedValueException If the element is not a sequence |
|
315 | - * |
|
316 | - * @return \Sop\ASN1\Type\Constructed\Sequence |
|
317 | - */ |
|
318 | - public function asSequence(): Constructed\Sequence |
|
319 | - { |
|
320 | - if (!$this->_element instanceof Constructed\Sequence) { |
|
321 | - throw new \UnexpectedValueException( |
|
322 | - $this->_generateExceptionMessage(Element::TYPE_SEQUENCE)); |
|
323 | - } |
|
324 | - return $this->_element; |
|
325 | - } |
|
326 | - |
|
327 | - /** |
|
328 | - * Get the wrapped element as a set type. |
|
329 | - * |
|
330 | - * @throws \UnexpectedValueException If the element is not a set |
|
331 | - * |
|
332 | - * @return \Sop\ASN1\Type\Constructed\Set |
|
333 | - */ |
|
334 | - public function asSet(): Constructed\Set |
|
335 | - { |
|
336 | - if (!$this->_element instanceof Constructed\Set) { |
|
337 | - throw new \UnexpectedValueException( |
|
338 | - $this->_generateExceptionMessage(Element::TYPE_SET)); |
|
339 | - } |
|
340 | - return $this->_element; |
|
341 | - } |
|
342 | - |
|
343 | - /** |
|
344 | - * Get the wrapped element as a numeric string type. |
|
345 | - * |
|
346 | - * @throws \UnexpectedValueException If the element is not a numeric string |
|
347 | - * |
|
348 | - * @return \Sop\ASN1\Type\Primitive\NumericString |
|
349 | - */ |
|
350 | - public function asNumericString(): Primitive\NumericString |
|
351 | - { |
|
352 | - if (!$this->_element instanceof Primitive\NumericString) { |
|
353 | - throw new \UnexpectedValueException( |
|
354 | - $this->_generateExceptionMessage(Element::TYPE_NUMERIC_STRING)); |
|
355 | - } |
|
356 | - return $this->_element; |
|
357 | - } |
|
358 | - |
|
359 | - /** |
|
360 | - * Get the wrapped element as a printable string type. |
|
361 | - * |
|
362 | - * @throws \UnexpectedValueException If the element is not a printable |
|
363 | - * string |
|
364 | - * |
|
365 | - * @return \Sop\ASN1\Type\Primitive\PrintableString |
|
366 | - */ |
|
367 | - public function asPrintableString(): Primitive\PrintableString |
|
368 | - { |
|
369 | - if (!$this->_element instanceof Primitive\PrintableString) { |
|
370 | - throw new \UnexpectedValueException( |
|
371 | - $this->_generateExceptionMessage(Element::TYPE_PRINTABLE_STRING)); |
|
372 | - } |
|
373 | - return $this->_element; |
|
374 | - } |
|
375 | - |
|
376 | - /** |
|
377 | - * Get the wrapped element as a T61 string type. |
|
378 | - * |
|
379 | - * @throws \UnexpectedValueException If the element is not a T61 string |
|
380 | - * |
|
381 | - * @return \Sop\ASN1\Type\Primitive\T61String |
|
382 | - */ |
|
383 | - public function asT61String(): Primitive\T61String |
|
384 | - { |
|
385 | - if (!$this->_element instanceof Primitive\T61String) { |
|
386 | - throw new \UnexpectedValueException( |
|
387 | - $this->_generateExceptionMessage(Element::TYPE_T61_STRING)); |
|
388 | - } |
|
389 | - return $this->_element; |
|
390 | - } |
|
391 | - |
|
392 | - /** |
|
393 | - * Get the wrapped element as a videotex string type. |
|
394 | - * |
|
395 | - * @throws \UnexpectedValueException If the element is not a videotex string |
|
396 | - * |
|
397 | - * @return \Sop\ASN1\Type\Primitive\VideotexString |
|
398 | - */ |
|
399 | - public function asVideotexString(): Primitive\VideotexString |
|
400 | - { |
|
401 | - if (!$this->_element instanceof Primitive\VideotexString) { |
|
402 | - throw new \UnexpectedValueException( |
|
403 | - $this->_generateExceptionMessage(Element::TYPE_VIDEOTEX_STRING)); |
|
404 | - } |
|
405 | - return $this->_element; |
|
406 | - } |
|
407 | - |
|
408 | - /** |
|
409 | - * Get the wrapped element as a IA5 string type. |
|
410 | - * |
|
411 | - * @throws \UnexpectedValueException If the element is not a IA5 string |
|
412 | - * |
|
413 | - * @return \Sop\ASN1\Type\Primitive\IA5String |
|
414 | - */ |
|
415 | - public function asIA5String(): Primitive\IA5String |
|
416 | - { |
|
417 | - if (!$this->_element instanceof Primitive\IA5String) { |
|
418 | - throw new \UnexpectedValueException( |
|
419 | - $this->_generateExceptionMessage(Element::TYPE_IA5_STRING)); |
|
420 | - } |
|
421 | - return $this->_element; |
|
422 | - } |
|
423 | - |
|
424 | - /** |
|
425 | - * Get the wrapped element as an UTC time type. |
|
426 | - * |
|
427 | - * @throws \UnexpectedValueException If the element is not a UTC time |
|
428 | - * |
|
429 | - * @return \Sop\ASN1\Type\Primitive\UTCTime |
|
430 | - */ |
|
431 | - public function asUTCTime(): Primitive\UTCTime |
|
432 | - { |
|
433 | - if (!$this->_element instanceof Primitive\UTCTime) { |
|
434 | - throw new \UnexpectedValueException( |
|
435 | - $this->_generateExceptionMessage(Element::TYPE_UTC_TIME)); |
|
436 | - } |
|
437 | - return $this->_element; |
|
438 | - } |
|
439 | - |
|
440 | - /** |
|
441 | - * Get the wrapped element as a generalized time type. |
|
442 | - * |
|
443 | - * @throws \UnexpectedValueException If the element is not a generalized |
|
444 | - * time |
|
445 | - * |
|
446 | - * @return \Sop\ASN1\Type\Primitive\GeneralizedTime |
|
447 | - */ |
|
448 | - public function asGeneralizedTime(): Primitive\GeneralizedTime |
|
449 | - { |
|
450 | - if (!$this->_element instanceof Primitive\GeneralizedTime) { |
|
451 | - throw new \UnexpectedValueException( |
|
452 | - $this->_generateExceptionMessage(Element::TYPE_GENERALIZED_TIME)); |
|
453 | - } |
|
454 | - return $this->_element; |
|
455 | - } |
|
456 | - |
|
457 | - /** |
|
458 | - * Get the wrapped element as a graphic string type. |
|
459 | - * |
|
460 | - * @throws \UnexpectedValueException If the element is not a graphic string |
|
461 | - * |
|
462 | - * @return \Sop\ASN1\Type\Primitive\GraphicString |
|
463 | - */ |
|
464 | - public function asGraphicString(): Primitive\GraphicString |
|
465 | - { |
|
466 | - if (!$this->_element instanceof Primitive\GraphicString) { |
|
467 | - throw new \UnexpectedValueException( |
|
468 | - $this->_generateExceptionMessage(Element::TYPE_GRAPHIC_STRING)); |
|
469 | - } |
|
470 | - return $this->_element; |
|
471 | - } |
|
472 | - |
|
473 | - /** |
|
474 | - * Get the wrapped element as a visible string type. |
|
475 | - * |
|
476 | - * @throws \UnexpectedValueException If the element is not a visible string |
|
477 | - * |
|
478 | - * @return \Sop\ASN1\Type\Primitive\VisibleString |
|
479 | - */ |
|
480 | - public function asVisibleString(): Primitive\VisibleString |
|
481 | - { |
|
482 | - if (!$this->_element instanceof Primitive\VisibleString) { |
|
483 | - throw new \UnexpectedValueException( |
|
484 | - $this->_generateExceptionMessage(Element::TYPE_VISIBLE_STRING)); |
|
485 | - } |
|
486 | - return $this->_element; |
|
487 | - } |
|
488 | - |
|
489 | - /** |
|
490 | - * Get the wrapped element as a general string type. |
|
491 | - * |
|
492 | - * @throws \UnexpectedValueException If the element is not general string |
|
493 | - * |
|
494 | - * @return \Sop\ASN1\Type\Primitive\GeneralString |
|
495 | - */ |
|
496 | - public function asGeneralString(): Primitive\GeneralString |
|
497 | - { |
|
498 | - if (!$this->_element instanceof Primitive\GeneralString) { |
|
499 | - throw new \UnexpectedValueException( |
|
500 | - $this->_generateExceptionMessage(Element::TYPE_GENERAL_STRING)); |
|
501 | - } |
|
502 | - return $this->_element; |
|
503 | - } |
|
504 | - |
|
505 | - /** |
|
506 | - * Get the wrapped element as a universal string type. |
|
507 | - * |
|
508 | - * @throws \UnexpectedValueException If the element is not a universal |
|
509 | - * string |
|
510 | - * |
|
511 | - * @return \Sop\ASN1\Type\Primitive\UniversalString |
|
512 | - */ |
|
513 | - public function asUniversalString(): Primitive\UniversalString |
|
514 | - { |
|
515 | - if (!$this->_element instanceof Primitive\UniversalString) { |
|
516 | - throw new \UnexpectedValueException( |
|
517 | - $this->_generateExceptionMessage(Element::TYPE_UNIVERSAL_STRING)); |
|
518 | - } |
|
519 | - return $this->_element; |
|
520 | - } |
|
521 | - |
|
522 | - /** |
|
523 | - * Get the wrapped element as a character string type. |
|
524 | - * |
|
525 | - * @throws \UnexpectedValueException If the element is not a character |
|
526 | - * string |
|
527 | - * |
|
528 | - * @return \Sop\ASN1\Type\Primitive\CharacterString |
|
529 | - */ |
|
530 | - public function asCharacterString(): Primitive\CharacterString |
|
531 | - { |
|
532 | - if (!$this->_element instanceof Primitive\CharacterString) { |
|
533 | - throw new \UnexpectedValueException( |
|
534 | - $this->_generateExceptionMessage(Element::TYPE_CHARACTER_STRING)); |
|
535 | - } |
|
536 | - return $this->_element; |
|
537 | - } |
|
538 | - |
|
539 | - /** |
|
540 | - * Get the wrapped element as a BMP string type. |
|
541 | - * |
|
542 | - * @throws \UnexpectedValueException If the element is not a bmp string |
|
543 | - * |
|
544 | - * @return \Sop\ASN1\Type\Primitive\BMPString |
|
545 | - */ |
|
546 | - public function asBMPString(): Primitive\BMPString |
|
547 | - { |
|
548 | - if (!$this->_element instanceof Primitive\BMPString) { |
|
549 | - throw new \UnexpectedValueException( |
|
550 | - $this->_generateExceptionMessage(Element::TYPE_BMP_STRING)); |
|
551 | - } |
|
552 | - return $this->_element; |
|
553 | - } |
|
554 | - |
|
555 | - /** |
|
556 | - * Get the wrapped element as any string type. |
|
557 | - * |
|
558 | - * @throws \UnexpectedValueException If the element is not a string |
|
559 | - * |
|
560 | - * @return StringType |
|
561 | - */ |
|
562 | - public function asString(): StringType |
|
563 | - { |
|
564 | - if (!$this->_element instanceof StringType) { |
|
565 | - throw new \UnexpectedValueException( |
|
566 | - $this->_generateExceptionMessage(Element::TYPE_STRING)); |
|
567 | - } |
|
568 | - return $this->_element; |
|
569 | - } |
|
570 | - |
|
571 | - /** |
|
572 | - * Get the wrapped element as any time type. |
|
573 | - * |
|
574 | - * @throws \UnexpectedValueException If the element is not a time |
|
575 | - * |
|
576 | - * @return TimeType |
|
577 | - */ |
|
578 | - public function asTime(): TimeType |
|
579 | - { |
|
580 | - if (!$this->_element instanceof TimeType) { |
|
581 | - throw new \UnexpectedValueException( |
|
582 | - $this->_generateExceptionMessage(Element::TYPE_TIME)); |
|
583 | - } |
|
584 | - return $this->_element; |
|
585 | - } |
|
586 | - |
|
587 | - /** |
|
588 | - * {@inheritdoc} |
|
589 | - */ |
|
590 | - public function toDER(): string |
|
591 | - { |
|
592 | - return $this->_element->toDER(); |
|
593 | - } |
|
594 | - |
|
595 | - /** |
|
596 | - * {@inheritdoc} |
|
597 | - */ |
|
598 | - public function typeClass(): int |
|
599 | - { |
|
600 | - return $this->_element->typeClass(); |
|
601 | - } |
|
602 | - |
|
603 | - /** |
|
604 | - * {@inheritdoc} |
|
605 | - */ |
|
606 | - public function isConstructed(): bool |
|
607 | - { |
|
608 | - return $this->_element->isConstructed(); |
|
609 | - } |
|
610 | - |
|
611 | - /** |
|
612 | - * {@inheritdoc} |
|
613 | - */ |
|
614 | - public function tag(): int |
|
615 | - { |
|
616 | - return $this->_element->tag(); |
|
617 | - } |
|
618 | - |
|
619 | - /** |
|
620 | - * {@inheritdoc} |
|
621 | - */ |
|
622 | - public function isType(int $tag): bool |
|
623 | - { |
|
624 | - return $this->_element->isType($tag); |
|
625 | - } |
|
626 | - |
|
627 | - /** |
|
628 | - * @todo Remove |
|
629 | - * |
|
630 | - * @deprecated use any <code>as*</code> accessor method first to ensure |
|
631 | - * type strictness |
|
632 | - * @see \Sop\ASN1\Feature\ElementBase::expectType() |
|
633 | - * |
|
634 | - * @return ElementBase |
|
635 | - */ |
|
636 | - public function expectType(int $tag): ElementBase |
|
637 | - { |
|
638 | - return $this->_element->expectType($tag); |
|
639 | - } |
|
640 | - |
|
641 | - /** |
|
642 | - * {@inheritdoc} |
|
643 | - */ |
|
644 | - public function isTagged(): bool |
|
645 | - { |
|
646 | - return $this->_element->isTagged(); |
|
647 | - } |
|
648 | - |
|
649 | - /** |
|
650 | - * @todo Remove |
|
651 | - * |
|
652 | - * @deprecated use any <code>as*</code> accessor method first to ensure |
|
653 | - * type strictness |
|
654 | - * @see \Sop\ASN1\Feature\ElementBase::expectTagged() |
|
655 | - * |
|
656 | - * @param null|mixed $tag |
|
657 | - * |
|
658 | - * @return TaggedType |
|
659 | - */ |
|
660 | - public function expectTagged(?int $tag = null): TaggedType |
|
661 | - { |
|
662 | - return $this->_element->expectTagged($tag); |
|
663 | - } |
|
664 | - |
|
665 | - /** |
|
666 | - * {@inheritdoc} |
|
667 | - */ |
|
668 | - public function asElement(): Element |
|
669 | - { |
|
670 | - return $this->_element; |
|
671 | - } |
|
672 | - |
|
673 | - /** |
|
674 | - * {@inheritdoc} |
|
675 | - */ |
|
676 | - public function asUnspecified(): UnspecifiedType |
|
677 | - { |
|
678 | - return $this; |
|
679 | - } |
|
680 | - |
|
681 | - /** |
|
682 | - * Generate message for exceptions thrown by <code>as*</code> methods. |
|
683 | - * |
|
684 | - * @param int $tag Type tag of the expected element |
|
685 | - * |
|
686 | - * @return string |
|
687 | - */ |
|
688 | - private function _generateExceptionMessage(int $tag): string |
|
689 | - { |
|
690 | - return sprintf('%s expected, got %s.', Element::tagToName($tag), |
|
691 | - $this->_typeDescriptorString()); |
|
692 | - } |
|
693 | - |
|
694 | - /** |
|
695 | - * Get textual description of the wrapped element for debugging purposes. |
|
696 | - * |
|
697 | - * @return string |
|
698 | - */ |
|
699 | - private function _typeDescriptorString(): string |
|
700 | - { |
|
701 | - $type_cls = $this->_element->typeClass(); |
|
702 | - $tag = $this->_element->tag(); |
|
703 | - if (Identifier::CLASS_UNIVERSAL == $type_cls) { |
|
704 | - return Element::tagToName($tag); |
|
705 | - } |
|
706 | - return Identifier::classToName($type_cls) . " TAG ${tag}"; |
|
707 | - } |
|
20 | + /** |
|
21 | + * The wrapped element. |
|
22 | + * |
|
23 | + * @var Element |
|
24 | + */ |
|
25 | + private $_element; |
|
26 | + |
|
27 | + /** |
|
28 | + * Constructor. |
|
29 | + * |
|
30 | + * @param Element $el |
|
31 | + */ |
|
32 | + public function __construct(Element $el) |
|
33 | + { |
|
34 | + $this->_element = $el; |
|
35 | + } |
|
36 | + |
|
37 | + /** |
|
38 | + * Compatibility method to dispatch calls to the wrapped element. |
|
39 | + * |
|
40 | + * @todo Remove |
|
41 | + * |
|
42 | + * @deprecated Use <code>as*</code> accessor methods to ensure strict type |
|
43 | + * |
|
44 | + * @param string $mtd Method name |
|
45 | + * @param array $args Arguments |
|
46 | + * |
|
47 | + * @return mixed |
|
48 | + */ |
|
49 | + public function __call($mtd, array $args) |
|
50 | + { |
|
51 | + return call_user_func_array([$this->_element, $mtd], $args); |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * Initialize from DER data. |
|
56 | + * |
|
57 | + * @param string $data DER encoded data |
|
58 | + * |
|
59 | + * @return self |
|
60 | + */ |
|
61 | + public static function fromDER(string $data): self |
|
62 | + { |
|
63 | + return Element::fromDER($data)->asUnspecified(); |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * Initialize from ElementBase interface. |
|
68 | + * |
|
69 | + * @param ElementBase $el |
|
70 | + * |
|
71 | + * @return self |
|
72 | + */ |
|
73 | + public static function fromElementBase(ElementBase $el): self |
|
74 | + { |
|
75 | + // if element is already wrapped |
|
76 | + if ($el instanceof self) { |
|
77 | + return $el; |
|
78 | + } |
|
79 | + return new self($el->asElement()); |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * Get the wrapped element as a context specific tagged type. |
|
84 | + * |
|
85 | + * @throws \UnexpectedValueException If the element is not tagged |
|
86 | + * |
|
87 | + * @return TaggedType |
|
88 | + */ |
|
89 | + public function asTagged(): TaggedType |
|
90 | + { |
|
91 | + if (!$this->_element instanceof TaggedType) { |
|
92 | + throw new \UnexpectedValueException( |
|
93 | + 'Tagged element expected, got ' . $this->_typeDescriptorString()); |
|
94 | + } |
|
95 | + return $this->_element; |
|
96 | + } |
|
97 | + |
|
98 | + /** |
|
99 | + * Get the wrapped element as an application specific type. |
|
100 | + * |
|
101 | + * @throws \UnexpectedValueException If element is not application specific |
|
102 | + * |
|
103 | + * @return \Sop\ASN1\Type\Tagged\ApplicationType |
|
104 | + */ |
|
105 | + public function asApplication(): Tagged\ApplicationType |
|
106 | + { |
|
107 | + if (!$this->_element instanceof Tagged\ApplicationType) { |
|
108 | + throw new \UnexpectedValueException( |
|
109 | + 'Application type expected, got ' . |
|
110 | + $this->_typeDescriptorString()); |
|
111 | + } |
|
112 | + return $this->_element; |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * Get the wrapped element as a private tagged type. |
|
117 | + * |
|
118 | + * @throws \UnexpectedValueException If element is not using private tagging |
|
119 | + * |
|
120 | + * @return \Sop\ASN1\Type\Tagged\PrivateType |
|
121 | + */ |
|
122 | + public function asPrivate(): Tagged\PrivateType |
|
123 | + { |
|
124 | + if (!$this->_element instanceof Tagged\PrivateType) { |
|
125 | + throw new \UnexpectedValueException( |
|
126 | + 'Private type expected, got ' . $this->_typeDescriptorString()); |
|
127 | + } |
|
128 | + return $this->_element; |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * Get the wrapped element as a boolean type. |
|
133 | + * |
|
134 | + * @throws \UnexpectedValueException If the element is not a boolean |
|
135 | + * |
|
136 | + * @return \Sop\ASN1\Type\Primitive\Boolean |
|
137 | + */ |
|
138 | + public function asBoolean(): Primitive\Boolean |
|
139 | + { |
|
140 | + if (!$this->_element instanceof Primitive\Boolean) { |
|
141 | + throw new \UnexpectedValueException( |
|
142 | + $this->_generateExceptionMessage(Element::TYPE_BOOLEAN)); |
|
143 | + } |
|
144 | + return $this->_element; |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * Get the wrapped element as an integer type. |
|
149 | + * |
|
150 | + * @throws \UnexpectedValueException If the element is not an integer |
|
151 | + * |
|
152 | + * @return \Sop\ASN1\Type\Primitive\Integer |
|
153 | + */ |
|
154 | + public function asInteger(): Primitive\Integer |
|
155 | + { |
|
156 | + if (!$this->_element instanceof Primitive\Integer) { |
|
157 | + throw new \UnexpectedValueException( |
|
158 | + $this->_generateExceptionMessage(Element::TYPE_INTEGER)); |
|
159 | + } |
|
160 | + return $this->_element; |
|
161 | + } |
|
162 | + |
|
163 | + /** |
|
164 | + * Get the wrapped element as a bit string type. |
|
165 | + * |
|
166 | + * @throws \UnexpectedValueException If the element is not a bit string |
|
167 | + * |
|
168 | + * @return \Sop\ASN1\Type\Primitive\BitString |
|
169 | + */ |
|
170 | + public function asBitString(): Primitive\BitString |
|
171 | + { |
|
172 | + if (!$this->_element instanceof Primitive\BitString) { |
|
173 | + throw new \UnexpectedValueException( |
|
174 | + $this->_generateExceptionMessage(Element::TYPE_BIT_STRING)); |
|
175 | + } |
|
176 | + return $this->_element; |
|
177 | + } |
|
178 | + |
|
179 | + /** |
|
180 | + * Get the wrapped element as an octet string type. |
|
181 | + * |
|
182 | + * @throws \UnexpectedValueException If the element is not an octet string |
|
183 | + * |
|
184 | + * @return \Sop\ASN1\Type\Primitive\OctetString |
|
185 | + */ |
|
186 | + public function asOctetString(): Primitive\OctetString |
|
187 | + { |
|
188 | + if (!$this->_element instanceof Primitive\OctetString) { |
|
189 | + throw new \UnexpectedValueException( |
|
190 | + $this->_generateExceptionMessage(Element::TYPE_OCTET_STRING)); |
|
191 | + } |
|
192 | + return $this->_element; |
|
193 | + } |
|
194 | + |
|
195 | + /** |
|
196 | + * Get the wrapped element as a null type. |
|
197 | + * |
|
198 | + * @throws \UnexpectedValueException If the element is not a null |
|
199 | + * |
|
200 | + * @return \Sop\ASN1\Type\Primitive\NullType |
|
201 | + */ |
|
202 | + public function asNull(): Primitive\NullType |
|
203 | + { |
|
204 | + if (!$this->_element instanceof Primitive\NullType) { |
|
205 | + throw new \UnexpectedValueException( |
|
206 | + $this->_generateExceptionMessage(Element::TYPE_NULL)); |
|
207 | + } |
|
208 | + return $this->_element; |
|
209 | + } |
|
210 | + |
|
211 | + /** |
|
212 | + * Get the wrapped element as an object identifier type. |
|
213 | + * |
|
214 | + * @throws \UnexpectedValueException If the element is not an object |
|
215 | + * identifier |
|
216 | + * |
|
217 | + * @return \Sop\ASN1\Type\Primitive\ObjectIdentifier |
|
218 | + */ |
|
219 | + public function asObjectIdentifier(): Primitive\ObjectIdentifier |
|
220 | + { |
|
221 | + if (!$this->_element instanceof Primitive\ObjectIdentifier) { |
|
222 | + throw new \UnexpectedValueException( |
|
223 | + $this->_generateExceptionMessage( |
|
224 | + Element::TYPE_OBJECT_IDENTIFIER)); |
|
225 | + } |
|
226 | + return $this->_element; |
|
227 | + } |
|
228 | + |
|
229 | + /** |
|
230 | + * Get the wrapped element as an object descriptor type. |
|
231 | + * |
|
232 | + * @throws \UnexpectedValueException If the element is not an object |
|
233 | + * descriptor |
|
234 | + * |
|
235 | + * @return \Sop\ASN1\Type\Primitive\ObjectDescriptor |
|
236 | + */ |
|
237 | + public function asObjectDescriptor(): Primitive\ObjectDescriptor |
|
238 | + { |
|
239 | + if (!$this->_element instanceof Primitive\ObjectDescriptor) { |
|
240 | + throw new \UnexpectedValueException( |
|
241 | + $this->_generateExceptionMessage( |
|
242 | + Element::TYPE_OBJECT_DESCRIPTOR)); |
|
243 | + } |
|
244 | + return $this->_element; |
|
245 | + } |
|
246 | + |
|
247 | + /** |
|
248 | + * Get the wrapped element as a real type. |
|
249 | + * |
|
250 | + * @throws \UnexpectedValueException If the element is not a real |
|
251 | + * |
|
252 | + * @return \Sop\ASN1\Type\Primitive\Real |
|
253 | + */ |
|
254 | + public function asReal(): Primitive\Real |
|
255 | + { |
|
256 | + if (!$this->_element instanceof Primitive\Real) { |
|
257 | + throw new \UnexpectedValueException( |
|
258 | + $this->_generateExceptionMessage(Element::TYPE_REAL)); |
|
259 | + } |
|
260 | + return $this->_element; |
|
261 | + } |
|
262 | + |
|
263 | + /** |
|
264 | + * Get the wrapped element as an enumerated type. |
|
265 | + * |
|
266 | + * @throws \UnexpectedValueException If the element is not an enumerated |
|
267 | + * |
|
268 | + * @return \Sop\ASN1\Type\Primitive\Enumerated |
|
269 | + */ |
|
270 | + public function asEnumerated(): Primitive\Enumerated |
|
271 | + { |
|
272 | + if (!$this->_element instanceof Primitive\Enumerated) { |
|
273 | + throw new \UnexpectedValueException( |
|
274 | + $this->_generateExceptionMessage(Element::TYPE_ENUMERATED)); |
|
275 | + } |
|
276 | + return $this->_element; |
|
277 | + } |
|
278 | + |
|
279 | + /** |
|
280 | + * Get the wrapped element as a UTF8 string type. |
|
281 | + * |
|
282 | + * @throws \UnexpectedValueException If the element is not a UTF8 string |
|
283 | + * |
|
284 | + * @return \Sop\ASN1\Type\Primitive\UTF8String |
|
285 | + */ |
|
286 | + public function asUTF8String(): Primitive\UTF8String |
|
287 | + { |
|
288 | + if (!$this->_element instanceof Primitive\UTF8String) { |
|
289 | + throw new \UnexpectedValueException( |
|
290 | + $this->_generateExceptionMessage(Element::TYPE_UTF8_STRING)); |
|
291 | + } |
|
292 | + return $this->_element; |
|
293 | + } |
|
294 | + |
|
295 | + /** |
|
296 | + * Get the wrapped element as a relative OID type. |
|
297 | + * |
|
298 | + * @throws \UnexpectedValueException If the element is not a relative OID |
|
299 | + * |
|
300 | + * @return \Sop\ASN1\Type\Primitive\RelativeOID |
|
301 | + */ |
|
302 | + public function asRelativeOID(): Primitive\RelativeOID |
|
303 | + { |
|
304 | + if (!$this->_element instanceof Primitive\RelativeOID) { |
|
305 | + throw new \UnexpectedValueException( |
|
306 | + $this->_generateExceptionMessage(Element::TYPE_RELATIVE_OID)); |
|
307 | + } |
|
308 | + return $this->_element; |
|
309 | + } |
|
310 | + |
|
311 | + /** |
|
312 | + * Get the wrapped element as a sequence type. |
|
313 | + * |
|
314 | + * @throws \UnexpectedValueException If the element is not a sequence |
|
315 | + * |
|
316 | + * @return \Sop\ASN1\Type\Constructed\Sequence |
|
317 | + */ |
|
318 | + public function asSequence(): Constructed\Sequence |
|
319 | + { |
|
320 | + if (!$this->_element instanceof Constructed\Sequence) { |
|
321 | + throw new \UnexpectedValueException( |
|
322 | + $this->_generateExceptionMessage(Element::TYPE_SEQUENCE)); |
|
323 | + } |
|
324 | + return $this->_element; |
|
325 | + } |
|
326 | + |
|
327 | + /** |
|
328 | + * Get the wrapped element as a set type. |
|
329 | + * |
|
330 | + * @throws \UnexpectedValueException If the element is not a set |
|
331 | + * |
|
332 | + * @return \Sop\ASN1\Type\Constructed\Set |
|
333 | + */ |
|
334 | + public function asSet(): Constructed\Set |
|
335 | + { |
|
336 | + if (!$this->_element instanceof Constructed\Set) { |
|
337 | + throw new \UnexpectedValueException( |
|
338 | + $this->_generateExceptionMessage(Element::TYPE_SET)); |
|
339 | + } |
|
340 | + return $this->_element; |
|
341 | + } |
|
342 | + |
|
343 | + /** |
|
344 | + * Get the wrapped element as a numeric string type. |
|
345 | + * |
|
346 | + * @throws \UnexpectedValueException If the element is not a numeric string |
|
347 | + * |
|
348 | + * @return \Sop\ASN1\Type\Primitive\NumericString |
|
349 | + */ |
|
350 | + public function asNumericString(): Primitive\NumericString |
|
351 | + { |
|
352 | + if (!$this->_element instanceof Primitive\NumericString) { |
|
353 | + throw new \UnexpectedValueException( |
|
354 | + $this->_generateExceptionMessage(Element::TYPE_NUMERIC_STRING)); |
|
355 | + } |
|
356 | + return $this->_element; |
|
357 | + } |
|
358 | + |
|
359 | + /** |
|
360 | + * Get the wrapped element as a printable string type. |
|
361 | + * |
|
362 | + * @throws \UnexpectedValueException If the element is not a printable |
|
363 | + * string |
|
364 | + * |
|
365 | + * @return \Sop\ASN1\Type\Primitive\PrintableString |
|
366 | + */ |
|
367 | + public function asPrintableString(): Primitive\PrintableString |
|
368 | + { |
|
369 | + if (!$this->_element instanceof Primitive\PrintableString) { |
|
370 | + throw new \UnexpectedValueException( |
|
371 | + $this->_generateExceptionMessage(Element::TYPE_PRINTABLE_STRING)); |
|
372 | + } |
|
373 | + return $this->_element; |
|
374 | + } |
|
375 | + |
|
376 | + /** |
|
377 | + * Get the wrapped element as a T61 string type. |
|
378 | + * |
|
379 | + * @throws \UnexpectedValueException If the element is not a T61 string |
|
380 | + * |
|
381 | + * @return \Sop\ASN1\Type\Primitive\T61String |
|
382 | + */ |
|
383 | + public function asT61String(): Primitive\T61String |
|
384 | + { |
|
385 | + if (!$this->_element instanceof Primitive\T61String) { |
|
386 | + throw new \UnexpectedValueException( |
|
387 | + $this->_generateExceptionMessage(Element::TYPE_T61_STRING)); |
|
388 | + } |
|
389 | + return $this->_element; |
|
390 | + } |
|
391 | + |
|
392 | + /** |
|
393 | + * Get the wrapped element as a videotex string type. |
|
394 | + * |
|
395 | + * @throws \UnexpectedValueException If the element is not a videotex string |
|
396 | + * |
|
397 | + * @return \Sop\ASN1\Type\Primitive\VideotexString |
|
398 | + */ |
|
399 | + public function asVideotexString(): Primitive\VideotexString |
|
400 | + { |
|
401 | + if (!$this->_element instanceof Primitive\VideotexString) { |
|
402 | + throw new \UnexpectedValueException( |
|
403 | + $this->_generateExceptionMessage(Element::TYPE_VIDEOTEX_STRING)); |
|
404 | + } |
|
405 | + return $this->_element; |
|
406 | + } |
|
407 | + |
|
408 | + /** |
|
409 | + * Get the wrapped element as a IA5 string type. |
|
410 | + * |
|
411 | + * @throws \UnexpectedValueException If the element is not a IA5 string |
|
412 | + * |
|
413 | + * @return \Sop\ASN1\Type\Primitive\IA5String |
|
414 | + */ |
|
415 | + public function asIA5String(): Primitive\IA5String |
|
416 | + { |
|
417 | + if (!$this->_element instanceof Primitive\IA5String) { |
|
418 | + throw new \UnexpectedValueException( |
|
419 | + $this->_generateExceptionMessage(Element::TYPE_IA5_STRING)); |
|
420 | + } |
|
421 | + return $this->_element; |
|
422 | + } |
|
423 | + |
|
424 | + /** |
|
425 | + * Get the wrapped element as an UTC time type. |
|
426 | + * |
|
427 | + * @throws \UnexpectedValueException If the element is not a UTC time |
|
428 | + * |
|
429 | + * @return \Sop\ASN1\Type\Primitive\UTCTime |
|
430 | + */ |
|
431 | + public function asUTCTime(): Primitive\UTCTime |
|
432 | + { |
|
433 | + if (!$this->_element instanceof Primitive\UTCTime) { |
|
434 | + throw new \UnexpectedValueException( |
|
435 | + $this->_generateExceptionMessage(Element::TYPE_UTC_TIME)); |
|
436 | + } |
|
437 | + return $this->_element; |
|
438 | + } |
|
439 | + |
|
440 | + /** |
|
441 | + * Get the wrapped element as a generalized time type. |
|
442 | + * |
|
443 | + * @throws \UnexpectedValueException If the element is not a generalized |
|
444 | + * time |
|
445 | + * |
|
446 | + * @return \Sop\ASN1\Type\Primitive\GeneralizedTime |
|
447 | + */ |
|
448 | + public function asGeneralizedTime(): Primitive\GeneralizedTime |
|
449 | + { |
|
450 | + if (!$this->_element instanceof Primitive\GeneralizedTime) { |
|
451 | + throw new \UnexpectedValueException( |
|
452 | + $this->_generateExceptionMessage(Element::TYPE_GENERALIZED_TIME)); |
|
453 | + } |
|
454 | + return $this->_element; |
|
455 | + } |
|
456 | + |
|
457 | + /** |
|
458 | + * Get the wrapped element as a graphic string type. |
|
459 | + * |
|
460 | + * @throws \UnexpectedValueException If the element is not a graphic string |
|
461 | + * |
|
462 | + * @return \Sop\ASN1\Type\Primitive\GraphicString |
|
463 | + */ |
|
464 | + public function asGraphicString(): Primitive\GraphicString |
|
465 | + { |
|
466 | + if (!$this->_element instanceof Primitive\GraphicString) { |
|
467 | + throw new \UnexpectedValueException( |
|
468 | + $this->_generateExceptionMessage(Element::TYPE_GRAPHIC_STRING)); |
|
469 | + } |
|
470 | + return $this->_element; |
|
471 | + } |
|
472 | + |
|
473 | + /** |
|
474 | + * Get the wrapped element as a visible string type. |
|
475 | + * |
|
476 | + * @throws \UnexpectedValueException If the element is not a visible string |
|
477 | + * |
|
478 | + * @return \Sop\ASN1\Type\Primitive\VisibleString |
|
479 | + */ |
|
480 | + public function asVisibleString(): Primitive\VisibleString |
|
481 | + { |
|
482 | + if (!$this->_element instanceof Primitive\VisibleString) { |
|
483 | + throw new \UnexpectedValueException( |
|
484 | + $this->_generateExceptionMessage(Element::TYPE_VISIBLE_STRING)); |
|
485 | + } |
|
486 | + return $this->_element; |
|
487 | + } |
|
488 | + |
|
489 | + /** |
|
490 | + * Get the wrapped element as a general string type. |
|
491 | + * |
|
492 | + * @throws \UnexpectedValueException If the element is not general string |
|
493 | + * |
|
494 | + * @return \Sop\ASN1\Type\Primitive\GeneralString |
|
495 | + */ |
|
496 | + public function asGeneralString(): Primitive\GeneralString |
|
497 | + { |
|
498 | + if (!$this->_element instanceof Primitive\GeneralString) { |
|
499 | + throw new \UnexpectedValueException( |
|
500 | + $this->_generateExceptionMessage(Element::TYPE_GENERAL_STRING)); |
|
501 | + } |
|
502 | + return $this->_element; |
|
503 | + } |
|
504 | + |
|
505 | + /** |
|
506 | + * Get the wrapped element as a universal string type. |
|
507 | + * |
|
508 | + * @throws \UnexpectedValueException If the element is not a universal |
|
509 | + * string |
|
510 | + * |
|
511 | + * @return \Sop\ASN1\Type\Primitive\UniversalString |
|
512 | + */ |
|
513 | + public function asUniversalString(): Primitive\UniversalString |
|
514 | + { |
|
515 | + if (!$this->_element instanceof Primitive\UniversalString) { |
|
516 | + throw new \UnexpectedValueException( |
|
517 | + $this->_generateExceptionMessage(Element::TYPE_UNIVERSAL_STRING)); |
|
518 | + } |
|
519 | + return $this->_element; |
|
520 | + } |
|
521 | + |
|
522 | + /** |
|
523 | + * Get the wrapped element as a character string type. |
|
524 | + * |
|
525 | + * @throws \UnexpectedValueException If the element is not a character |
|
526 | + * string |
|
527 | + * |
|
528 | + * @return \Sop\ASN1\Type\Primitive\CharacterString |
|
529 | + */ |
|
530 | + public function asCharacterString(): Primitive\CharacterString |
|
531 | + { |
|
532 | + if (!$this->_element instanceof Primitive\CharacterString) { |
|
533 | + throw new \UnexpectedValueException( |
|
534 | + $this->_generateExceptionMessage(Element::TYPE_CHARACTER_STRING)); |
|
535 | + } |
|
536 | + return $this->_element; |
|
537 | + } |
|
538 | + |
|
539 | + /** |
|
540 | + * Get the wrapped element as a BMP string type. |
|
541 | + * |
|
542 | + * @throws \UnexpectedValueException If the element is not a bmp string |
|
543 | + * |
|
544 | + * @return \Sop\ASN1\Type\Primitive\BMPString |
|
545 | + */ |
|
546 | + public function asBMPString(): Primitive\BMPString |
|
547 | + { |
|
548 | + if (!$this->_element instanceof Primitive\BMPString) { |
|
549 | + throw new \UnexpectedValueException( |
|
550 | + $this->_generateExceptionMessage(Element::TYPE_BMP_STRING)); |
|
551 | + } |
|
552 | + return $this->_element; |
|
553 | + } |
|
554 | + |
|
555 | + /** |
|
556 | + * Get the wrapped element as any string type. |
|
557 | + * |
|
558 | + * @throws \UnexpectedValueException If the element is not a string |
|
559 | + * |
|
560 | + * @return StringType |
|
561 | + */ |
|
562 | + public function asString(): StringType |
|
563 | + { |
|
564 | + if (!$this->_element instanceof StringType) { |
|
565 | + throw new \UnexpectedValueException( |
|
566 | + $this->_generateExceptionMessage(Element::TYPE_STRING)); |
|
567 | + } |
|
568 | + return $this->_element; |
|
569 | + } |
|
570 | + |
|
571 | + /** |
|
572 | + * Get the wrapped element as any time type. |
|
573 | + * |
|
574 | + * @throws \UnexpectedValueException If the element is not a time |
|
575 | + * |
|
576 | + * @return TimeType |
|
577 | + */ |
|
578 | + public function asTime(): TimeType |
|
579 | + { |
|
580 | + if (!$this->_element instanceof TimeType) { |
|
581 | + throw new \UnexpectedValueException( |
|
582 | + $this->_generateExceptionMessage(Element::TYPE_TIME)); |
|
583 | + } |
|
584 | + return $this->_element; |
|
585 | + } |
|
586 | + |
|
587 | + /** |
|
588 | + * {@inheritdoc} |
|
589 | + */ |
|
590 | + public function toDER(): string |
|
591 | + { |
|
592 | + return $this->_element->toDER(); |
|
593 | + } |
|
594 | + |
|
595 | + /** |
|
596 | + * {@inheritdoc} |
|
597 | + */ |
|
598 | + public function typeClass(): int |
|
599 | + { |
|
600 | + return $this->_element->typeClass(); |
|
601 | + } |
|
602 | + |
|
603 | + /** |
|
604 | + * {@inheritdoc} |
|
605 | + */ |
|
606 | + public function isConstructed(): bool |
|
607 | + { |
|
608 | + return $this->_element->isConstructed(); |
|
609 | + } |
|
610 | + |
|
611 | + /** |
|
612 | + * {@inheritdoc} |
|
613 | + */ |
|
614 | + public function tag(): int |
|
615 | + { |
|
616 | + return $this->_element->tag(); |
|
617 | + } |
|
618 | + |
|
619 | + /** |
|
620 | + * {@inheritdoc} |
|
621 | + */ |
|
622 | + public function isType(int $tag): bool |
|
623 | + { |
|
624 | + return $this->_element->isType($tag); |
|
625 | + } |
|
626 | + |
|
627 | + /** |
|
628 | + * @todo Remove |
|
629 | + * |
|
630 | + * @deprecated use any <code>as*</code> accessor method first to ensure |
|
631 | + * type strictness |
|
632 | + * @see \Sop\ASN1\Feature\ElementBase::expectType() |
|
633 | + * |
|
634 | + * @return ElementBase |
|
635 | + */ |
|
636 | + public function expectType(int $tag): ElementBase |
|
637 | + { |
|
638 | + return $this->_element->expectType($tag); |
|
639 | + } |
|
640 | + |
|
641 | + /** |
|
642 | + * {@inheritdoc} |
|
643 | + */ |
|
644 | + public function isTagged(): bool |
|
645 | + { |
|
646 | + return $this->_element->isTagged(); |
|
647 | + } |
|
648 | + |
|
649 | + /** |
|
650 | + * @todo Remove |
|
651 | + * |
|
652 | + * @deprecated use any <code>as*</code> accessor method first to ensure |
|
653 | + * type strictness |
|
654 | + * @see \Sop\ASN1\Feature\ElementBase::expectTagged() |
|
655 | + * |
|
656 | + * @param null|mixed $tag |
|
657 | + * |
|
658 | + * @return TaggedType |
|
659 | + */ |
|
660 | + public function expectTagged(?int $tag = null): TaggedType |
|
661 | + { |
|
662 | + return $this->_element->expectTagged($tag); |
|
663 | + } |
|
664 | + |
|
665 | + /** |
|
666 | + * {@inheritdoc} |
|
667 | + */ |
|
668 | + public function asElement(): Element |
|
669 | + { |
|
670 | + return $this->_element; |
|
671 | + } |
|
672 | + |
|
673 | + /** |
|
674 | + * {@inheritdoc} |
|
675 | + */ |
|
676 | + public function asUnspecified(): UnspecifiedType |
|
677 | + { |
|
678 | + return $this; |
|
679 | + } |
|
680 | + |
|
681 | + /** |
|
682 | + * Generate message for exceptions thrown by <code>as*</code> methods. |
|
683 | + * |
|
684 | + * @param int $tag Type tag of the expected element |
|
685 | + * |
|
686 | + * @return string |
|
687 | + */ |
|
688 | + private function _generateExceptionMessage(int $tag): string |
|
689 | + { |
|
690 | + return sprintf('%s expected, got %s.', Element::tagToName($tag), |
|
691 | + $this->_typeDescriptorString()); |
|
692 | + } |
|
693 | + |
|
694 | + /** |
|
695 | + * Get textual description of the wrapped element for debugging purposes. |
|
696 | + * |
|
697 | + * @return string |
|
698 | + */ |
|
699 | + private function _typeDescriptorString(): string |
|
700 | + { |
|
701 | + $type_cls = $this->_element->typeClass(); |
|
702 | + $tag = $this->_element->tag(); |
|
703 | + if (Identifier::CLASS_UNIVERSAL == $type_cls) { |
|
704 | + return Element::tagToName($tag); |
|
705 | + } |
|
706 | + return Identifier::classToName($type_cls) . " TAG ${tag}"; |
|
707 | + } |
|
708 | 708 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\ASN1\Type; |
6 | 6 |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\ASN1\Type\Primitive; |
6 | 6 |
@@ -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,38 +17,38 @@ |
||
17 | 17 | */ |
18 | 18 | class NullType extends Element |
19 | 19 | { |
20 | - use UniversalClass; |
|
21 | - use PrimitiveType; |
|
20 | + use UniversalClass; |
|
21 | + use PrimitiveType; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Constructor. |
|
25 | - */ |
|
26 | - public function __construct() |
|
27 | - { |
|
28 | - $this->_typeTag = self::TYPE_NULL; |
|
29 | - } |
|
23 | + /** |
|
24 | + * Constructor. |
|
25 | + */ |
|
26 | + public function __construct() |
|
27 | + { |
|
28 | + $this->_typeTag = self::TYPE_NULL; |
|
29 | + } |
|
30 | 30 | |
31 | - /** |
|
32 | - * {@inheritdoc} |
|
33 | - */ |
|
34 | - protected function _encodedContentDER(): string |
|
35 | - { |
|
36 | - return ''; |
|
37 | - } |
|
31 | + /** |
|
32 | + * {@inheritdoc} |
|
33 | + */ |
|
34 | + protected function _encodedContentDER(): string |
|
35 | + { |
|
36 | + return ''; |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * {@inheritdoc} |
|
41 | - */ |
|
42 | - protected static function _decodeFromDER(Identifier $identifier, |
|
43 | - string $data, int &$offset): ElementBase |
|
44 | - { |
|
45 | - $idx = $offset; |
|
46 | - if (!$identifier->isPrimitive()) { |
|
47 | - throw new DecodeException('Null value must be primitive.'); |
|
48 | - } |
|
49 | - // null type has always zero length |
|
50 | - Length::expectFromDER($data, $idx, 0); |
|
51 | - $offset = $idx; |
|
52 | - return new self(); |
|
53 | - } |
|
39 | + /** |
|
40 | + * {@inheritdoc} |
|
41 | + */ |
|
42 | + protected static function _decodeFromDER(Identifier $identifier, |
|
43 | + string $data, int &$offset): ElementBase |
|
44 | + { |
|
45 | + $idx = $offset; |
|
46 | + if (!$identifier->isPrimitive()) { |
|
47 | + throw new DecodeException('Null value must be primitive.'); |
|
48 | + } |
|
49 | + // null type has always zero length |
|
50 | + Length::expectFromDER($data, $idx, 0); |
|
51 | + $offset = $idx; |
|
52 | + return new self(); |
|
53 | + } |
|
54 | 54 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\ASN1\Type\Primitive; |
6 | 6 |
@@ -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 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\ASN1\Type\Primitive; |
6 | 6 |
@@ -12,26 +12,26 @@ |
||
12 | 12 | */ |
13 | 13 | class T61String extends PrimitiveString |
14 | 14 | { |
15 | - use UniversalClass; |
|
15 | + use UniversalClass; |
|
16 | 16 | |
17 | - /** |
|
18 | - * Constructor. |
|
19 | - * |
|
20 | - * @param string $string |
|
21 | - */ |
|
22 | - public function __construct(string $string) |
|
23 | - { |
|
24 | - $this->_typeTag = self::TYPE_T61_STRING; |
|
25 | - parent::__construct($string); |
|
26 | - } |
|
17 | + /** |
|
18 | + * Constructor. |
|
19 | + * |
|
20 | + * @param string $string |
|
21 | + */ |
|
22 | + public function __construct(string $string) |
|
23 | + { |
|
24 | + $this->_typeTag = self::TYPE_T61_STRING; |
|
25 | + parent::__construct($string); |
|
26 | + } |
|
27 | 27 | |
28 | - /** |
|
29 | - * {@inheritdoc} |
|
30 | - */ |
|
31 | - protected function _validateString(string $string): bool |
|
32 | - { |
|
33 | - // allow everything since there's literally |
|
34 | - // thousands of allowed characters (16 bit composed characters) |
|
35 | - return true; |
|
36 | - } |
|
28 | + /** |
|
29 | + * {@inheritdoc} |
|
30 | + */ |
|
31 | + protected function _validateString(string $string): bool |
|
32 | + { |
|
33 | + // allow everything since there's literally |
|
34 | + // thousands of allowed characters (16 bit composed characters) |
|
35 | + return true; |
|
36 | + } |
|
37 | 37 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\ASN1\Type\Primitive; |
6 | 6 |
@@ -17,110 +17,110 @@ |
||
17 | 17 | */ |
18 | 18 | class GeneralizedTime extends TimeType |
19 | 19 | { |
20 | - use UniversalClass; |
|
21 | - use PrimitiveType; |
|
20 | + use UniversalClass; |
|
21 | + use PrimitiveType; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Regular expression to parse date. |
|
25 | - * |
|
26 | - * DER restricts format to UTC timezone (Z suffix). |
|
27 | - * |
|
28 | - * @var string |
|
29 | - */ |
|
30 | - const REGEX = '#^' . |
|
31 | - '(\d\d\d\d)' . // YYYY |
|
32 | - '(\d\d)' . // MM |
|
33 | - '(\d\d)' . // DD |
|
34 | - '(\d\d)' . // hh |
|
35 | - '(\d\d)' . // mm |
|
36 | - '(\d\d)' . // ss |
|
37 | - '(?:\.(\d+))?' . // frac |
|
38 | - 'Z' . // TZ |
|
39 | - '$#'; |
|
23 | + /** |
|
24 | + * Regular expression to parse date. |
|
25 | + * |
|
26 | + * DER restricts format to UTC timezone (Z suffix). |
|
27 | + * |
|
28 | + * @var string |
|
29 | + */ |
|
30 | + const REGEX = '#^' . |
|
31 | + '(\d\d\d\d)' . // YYYY |
|
32 | + '(\d\d)' . // MM |
|
33 | + '(\d\d)' . // DD |
|
34 | + '(\d\d)' . // hh |
|
35 | + '(\d\d)' . // mm |
|
36 | + '(\d\d)' . // ss |
|
37 | + '(?:\.(\d+))?' . // frac |
|
38 | + 'Z' . // TZ |
|
39 | + '$#'; |
|
40 | 40 | |
41 | - /** |
|
42 | - * Cached formatted date. |
|
43 | - * |
|
44 | - * @var null|string |
|
45 | - */ |
|
46 | - private $_formatted; |
|
41 | + /** |
|
42 | + * Cached formatted date. |
|
43 | + * |
|
44 | + * @var null|string |
|
45 | + */ |
|
46 | + private $_formatted; |
|
47 | 47 | |
48 | - /** |
|
49 | - * Constructor. |
|
50 | - * |
|
51 | - * @param \DateTimeImmutable $dt |
|
52 | - */ |
|
53 | - public function __construct(\DateTimeImmutable $dt) |
|
54 | - { |
|
55 | - $this->_typeTag = self::TYPE_GENERALIZED_TIME; |
|
56 | - parent::__construct($dt); |
|
57 | - } |
|
48 | + /** |
|
49 | + * Constructor. |
|
50 | + * |
|
51 | + * @param \DateTimeImmutable $dt |
|
52 | + */ |
|
53 | + public function __construct(\DateTimeImmutable $dt) |
|
54 | + { |
|
55 | + $this->_typeTag = self::TYPE_GENERALIZED_TIME; |
|
56 | + parent::__construct($dt); |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * Clear cached variables on clone. |
|
61 | - */ |
|
62 | - public function __clone() |
|
63 | - { |
|
64 | - $this->_formatted = null; |
|
65 | - } |
|
59 | + /** |
|
60 | + * Clear cached variables on clone. |
|
61 | + */ |
|
62 | + public function __clone() |
|
63 | + { |
|
64 | + $this->_formatted = null; |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * {@inheritdoc} |
|
69 | - */ |
|
70 | - protected function _encodedContentDER(): string |
|
71 | - { |
|
72 | - if (!isset($this->_formatted)) { |
|
73 | - $dt = $this->_dateTime->setTimezone( |
|
74 | - self::_createTimeZone(self::TZ_UTC)); |
|
75 | - $this->_formatted = $dt->format('YmdHis'); |
|
76 | - // if fractions were used |
|
77 | - $frac = $dt->format('u'); |
|
78 | - if (0 != $frac) { |
|
79 | - $frac = rtrim($frac, '0'); |
|
80 | - $this->_formatted .= ".${frac}"; |
|
81 | - } |
|
82 | - // timezone |
|
83 | - $this->_formatted .= 'Z'; |
|
84 | - } |
|
85 | - return $this->_formatted; |
|
86 | - } |
|
67 | + /** |
|
68 | + * {@inheritdoc} |
|
69 | + */ |
|
70 | + protected function _encodedContentDER(): string |
|
71 | + { |
|
72 | + if (!isset($this->_formatted)) { |
|
73 | + $dt = $this->_dateTime->setTimezone( |
|
74 | + self::_createTimeZone(self::TZ_UTC)); |
|
75 | + $this->_formatted = $dt->format('YmdHis'); |
|
76 | + // if fractions were used |
|
77 | + $frac = $dt->format('u'); |
|
78 | + if (0 != $frac) { |
|
79 | + $frac = rtrim($frac, '0'); |
|
80 | + $this->_formatted .= ".${frac}"; |
|
81 | + } |
|
82 | + // timezone |
|
83 | + $this->_formatted .= 'Z'; |
|
84 | + } |
|
85 | + return $this->_formatted; |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * {@inheritdoc} |
|
90 | - */ |
|
91 | - protected static function _decodeFromDER(Identifier $identifier, |
|
92 | - string $data, int &$offset): ElementBase |
|
93 | - { |
|
94 | - $idx = $offset; |
|
95 | - $length = Length::expectFromDER($data, $idx)->intLength(); |
|
96 | - $str = substr($data, $idx, $length); |
|
97 | - $idx += $length; |
|
98 | - /** @var string[] $match */ |
|
99 | - if (!preg_match(self::REGEX, $str, $match)) { |
|
100 | - throw new DecodeException('Invalid GeneralizedTime format.'); |
|
101 | - } |
|
102 | - [, $year, $month, $day, $hour, $minute, $second] = $match; |
|
103 | - if (isset($match[7])) { |
|
104 | - $frac = $match[7]; |
|
105 | - // DER restricts trailing zeroes in fractional seconds component |
|
106 | - if ('0' === $frac[strlen($frac) - 1]) { |
|
107 | - throw new DecodeException( |
|
108 | - 'Fractional seconds must omit trailing zeroes.'); |
|
109 | - } |
|
110 | - $frac = (int) $frac; |
|
111 | - } else { |
|
112 | - $frac = 0; |
|
113 | - } |
|
114 | - $time = $year . $month . $day . $hour . $minute . $second . '.' . $frac . |
|
115 | - self::TZ_UTC; |
|
116 | - $dt = \DateTimeImmutable::createFromFormat('!YmdHis.uT', $time, |
|
117 | - self::_createTimeZone(self::TZ_UTC)); |
|
118 | - if (!$dt) { |
|
119 | - throw new DecodeException( |
|
120 | - 'Failed to decode GeneralizedTime: ' . |
|
121 | - self::_getLastDateTimeImmutableErrorsStr()); |
|
122 | - } |
|
123 | - $offset = $idx; |
|
124 | - return new self($dt); |
|
125 | - } |
|
88 | + /** |
|
89 | + * {@inheritdoc} |
|
90 | + */ |
|
91 | + protected static function _decodeFromDER(Identifier $identifier, |
|
92 | + string $data, int &$offset): ElementBase |
|
93 | + { |
|
94 | + $idx = $offset; |
|
95 | + $length = Length::expectFromDER($data, $idx)->intLength(); |
|
96 | + $str = substr($data, $idx, $length); |
|
97 | + $idx += $length; |
|
98 | + /** @var string[] $match */ |
|
99 | + if (!preg_match(self::REGEX, $str, $match)) { |
|
100 | + throw new DecodeException('Invalid GeneralizedTime format.'); |
|
101 | + } |
|
102 | + [, $year, $month, $day, $hour, $minute, $second] = $match; |
|
103 | + if (isset($match[7])) { |
|
104 | + $frac = $match[7]; |
|
105 | + // DER restricts trailing zeroes in fractional seconds component |
|
106 | + if ('0' === $frac[strlen($frac) - 1]) { |
|
107 | + throw new DecodeException( |
|
108 | + 'Fractional seconds must omit trailing zeroes.'); |
|
109 | + } |
|
110 | + $frac = (int) $frac; |
|
111 | + } else { |
|
112 | + $frac = 0; |
|
113 | + } |
|
114 | + $time = $year . $month . $day . $hour . $minute . $second . '.' . $frac . |
|
115 | + self::TZ_UTC; |
|
116 | + $dt = \DateTimeImmutable::createFromFormat('!YmdHis.uT', $time, |
|
117 | + self::_createTimeZone(self::TZ_UTC)); |
|
118 | + if (!$dt) { |
|
119 | + throw new DecodeException( |
|
120 | + 'Failed to decode GeneralizedTime: ' . |
|
121 | + self::_getLastDateTimeImmutableErrorsStr()); |
|
122 | + } |
|
123 | + $offset = $idx; |
|
124 | + return new self($dt); |
|
125 | + } |
|
126 | 126 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\ASN1\Type\Primitive; |
6 | 6 |
@@ -14,24 +14,24 @@ |
||
14 | 14 | */ |
15 | 15 | class UTF8String extends PrimitiveString |
16 | 16 | { |
17 | - use UniversalClass; |
|
17 | + use UniversalClass; |
|
18 | 18 | |
19 | - /** |
|
20 | - * Constructor. |
|
21 | - * |
|
22 | - * @param string $string |
|
23 | - */ |
|
24 | - public function __construct(string $string) |
|
25 | - { |
|
26 | - $this->_typeTag = self::TYPE_UTF8_STRING; |
|
27 | - parent::__construct($string); |
|
28 | - } |
|
19 | + /** |
|
20 | + * Constructor. |
|
21 | + * |
|
22 | + * @param string $string |
|
23 | + */ |
|
24 | + public function __construct(string $string) |
|
25 | + { |
|
26 | + $this->_typeTag = self::TYPE_UTF8_STRING; |
|
27 | + parent::__construct($string); |
|
28 | + } |
|
29 | 29 | |
30 | - /** |
|
31 | - * {@inheritdoc} |
|
32 | - */ |
|
33 | - protected function _validateString(string $string): bool |
|
34 | - { |
|
35 | - return mb_check_encoding($string, 'UTF-8'); |
|
36 | - } |
|
30 | + /** |
|
31 | + * {@inheritdoc} |
|
32 | + */ |
|
33 | + protected function _validateString(string $string): bool |
|
34 | + { |
|
35 | + return mb_check_encoding($string, 'UTF-8'); |
|
36 | + } |
|
37 | 37 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\ASN1\Type\Primitive; |
6 | 6 |
@@ -14,28 +14,28 @@ |
||
14 | 14 | */ |
15 | 15 | class UniversalString extends PrimitiveString |
16 | 16 | { |
17 | - use UniversalClass; |
|
17 | + use UniversalClass; |
|
18 | 18 | |
19 | - /** |
|
20 | - * Constructor. |
|
21 | - * |
|
22 | - * @param string $string |
|
23 | - */ |
|
24 | - public function __construct(string $string) |
|
25 | - { |
|
26 | - $this->_typeTag = self::TYPE_UNIVERSAL_STRING; |
|
27 | - parent::__construct($string); |
|
28 | - } |
|
19 | + /** |
|
20 | + * Constructor. |
|
21 | + * |
|
22 | + * @param string $string |
|
23 | + */ |
|
24 | + public function __construct(string $string) |
|
25 | + { |
|
26 | + $this->_typeTag = self::TYPE_UNIVERSAL_STRING; |
|
27 | + parent::__construct($string); |
|
28 | + } |
|
29 | 29 | |
30 | - /** |
|
31 | - * {@inheritdoc} |
|
32 | - */ |
|
33 | - protected function _validateString(string $string): bool |
|
34 | - { |
|
35 | - // UCS-4 has fixed with of 4 octets (32 bits) |
|
36 | - if (0 != strlen($string) % 4) { |
|
37 | - return false; |
|
38 | - } |
|
39 | - return true; |
|
40 | - } |
|
30 | + /** |
|
31 | + * {@inheritdoc} |
|
32 | + */ |
|
33 | + protected function _validateString(string $string): bool |
|
34 | + { |
|
35 | + // UCS-4 has fixed with of 4 octets (32 bits) |
|
36 | + if (0 != strlen($string) % 4) { |
|
37 | + return false; |
|
38 | + } |
|
39 | + return true; |
|
40 | + } |
|
41 | 41 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\ASN1\Type\Primitive; |
6 | 6 |
@@ -17,195 +17,195 @@ |
||
17 | 17 | */ |
18 | 18 | class BitString extends StringType |
19 | 19 | { |
20 | - use UniversalClass; |
|
21 | - use PrimitiveType; |
|
20 | + use UniversalClass; |
|
21 | + use PrimitiveType; |
|
22 | 22 | |
23 | - /** |
|
24 | - * Number of unused bits in the last octet. |
|
25 | - * |
|
26 | - * @var int |
|
27 | - */ |
|
28 | - protected $_unusedBits; |
|
23 | + /** |
|
24 | + * Number of unused bits in the last octet. |
|
25 | + * |
|
26 | + * @var int |
|
27 | + */ |
|
28 | + protected $_unusedBits; |
|
29 | 29 | |
30 | - /** |
|
31 | - * Constructor. |
|
32 | - * |
|
33 | - * @param string $string Content octets |
|
34 | - * @param int $unused_bits Number of unused bits in the last octet |
|
35 | - */ |
|
36 | - public function __construct(string $string, int $unused_bits = 0) |
|
37 | - { |
|
38 | - $this->_typeTag = self::TYPE_BIT_STRING; |
|
39 | - parent::__construct($string); |
|
40 | - $this->_unusedBits = $unused_bits; |
|
41 | - } |
|
30 | + /** |
|
31 | + * Constructor. |
|
32 | + * |
|
33 | + * @param string $string Content octets |
|
34 | + * @param int $unused_bits Number of unused bits in the last octet |
|
35 | + */ |
|
36 | + public function __construct(string $string, int $unused_bits = 0) |
|
37 | + { |
|
38 | + $this->_typeTag = self::TYPE_BIT_STRING; |
|
39 | + parent::__construct($string); |
|
40 | + $this->_unusedBits = $unused_bits; |
|
41 | + } |
|
42 | 42 | |
43 | - /** |
|
44 | - * Get the number of bits in the string. |
|
45 | - * |
|
46 | - * @return int |
|
47 | - */ |
|
48 | - public function numBits(): int |
|
49 | - { |
|
50 | - return strlen($this->_string) * 8 - $this->_unusedBits; |
|
51 | - } |
|
43 | + /** |
|
44 | + * Get the number of bits in the string. |
|
45 | + * |
|
46 | + * @return int |
|
47 | + */ |
|
48 | + public function numBits(): int |
|
49 | + { |
|
50 | + return strlen($this->_string) * 8 - $this->_unusedBits; |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * Get the number of unused bits in the last octet of the string. |
|
55 | - * |
|
56 | - * @return int |
|
57 | - */ |
|
58 | - public function unusedBits(): int |
|
59 | - { |
|
60 | - return $this->_unusedBits; |
|
61 | - } |
|
53 | + /** |
|
54 | + * Get the number of unused bits in the last octet of the string. |
|
55 | + * |
|
56 | + * @return int |
|
57 | + */ |
|
58 | + public function unusedBits(): int |
|
59 | + { |
|
60 | + return $this->_unusedBits; |
|
61 | + } |
|
62 | 62 | |
63 | - /** |
|
64 | - * Test whether bit is set. |
|
65 | - * |
|
66 | - * @param int $idx Bit index. |
|
67 | - * Most significant bit of the first octet is index 0. |
|
68 | - * |
|
69 | - * @return bool |
|
70 | - */ |
|
71 | - public function testBit(int $idx): bool |
|
72 | - { |
|
73 | - // octet index |
|
74 | - $oi = (int) floor($idx / 8); |
|
75 | - // if octet is outside range |
|
76 | - if ($oi < 0 || $oi >= strlen($this->_string)) { |
|
77 | - throw new \OutOfBoundsException('Index is out of bounds.'); |
|
78 | - } |
|
79 | - // bit index |
|
80 | - $bi = $idx % 8; |
|
81 | - // if tested bit is last octet's unused bit |
|
82 | - if ($oi == strlen($this->_string) - 1) { |
|
83 | - if ($bi >= 8 - $this->_unusedBits) { |
|
84 | - throw new \OutOfBoundsException( |
|
85 | - 'Index refers to an unused bit.'); |
|
86 | - } |
|
87 | - } |
|
88 | - $byte = $this->_string[$oi]; |
|
89 | - // index 0 is the most significant bit in byte |
|
90 | - $mask = 0x01 << (7 - $bi); |
|
91 | - return (ord($byte) & $mask) > 0; |
|
92 | - } |
|
63 | + /** |
|
64 | + * Test whether bit is set. |
|
65 | + * |
|
66 | + * @param int $idx Bit index. |
|
67 | + * Most significant bit of the first octet is index 0. |
|
68 | + * |
|
69 | + * @return bool |
|
70 | + */ |
|
71 | + public function testBit(int $idx): bool |
|
72 | + { |
|
73 | + // octet index |
|
74 | + $oi = (int) floor($idx / 8); |
|
75 | + // if octet is outside range |
|
76 | + if ($oi < 0 || $oi >= strlen($this->_string)) { |
|
77 | + throw new \OutOfBoundsException('Index is out of bounds.'); |
|
78 | + } |
|
79 | + // bit index |
|
80 | + $bi = $idx % 8; |
|
81 | + // if tested bit is last octet's unused bit |
|
82 | + if ($oi == strlen($this->_string) - 1) { |
|
83 | + if ($bi >= 8 - $this->_unusedBits) { |
|
84 | + throw new \OutOfBoundsException( |
|
85 | + 'Index refers to an unused bit.'); |
|
86 | + } |
|
87 | + } |
|
88 | + $byte = $this->_string[$oi]; |
|
89 | + // index 0 is the most significant bit in byte |
|
90 | + $mask = 0x01 << (7 - $bi); |
|
91 | + return (ord($byte) & $mask) > 0; |
|
92 | + } |
|
93 | 93 | |
94 | - /** |
|
95 | - * Get range of bits. |
|
96 | - * |
|
97 | - * @param int $start Index of first bit |
|
98 | - * @param int $length Number of bits in range |
|
99 | - * |
|
100 | - * @throws \OutOfBoundsException |
|
101 | - * |
|
102 | - * @return string Integer of $length bits |
|
103 | - */ |
|
104 | - public function range(int $start, int $length): string |
|
105 | - { |
|
106 | - if (!$length) { |
|
107 | - return '0'; |
|
108 | - } |
|
109 | - if ($start + $length > $this->numBits()) { |
|
110 | - throw new \OutOfBoundsException('Not enough bits.'); |
|
111 | - } |
|
112 | - $bits = gmp_init(0); |
|
113 | - $idx = $start; |
|
114 | - $end = $start + $length; |
|
115 | - while (true) { |
|
116 | - $bit = $this->testBit($idx) ? 1 : 0; |
|
117 | - $bits |= $bit; |
|
118 | - if (++$idx >= $end) { |
|
119 | - break; |
|
120 | - } |
|
121 | - $bits <<= 1; |
|
122 | - } |
|
123 | - return gmp_strval($bits, 10); |
|
124 | - } |
|
94 | + /** |
|
95 | + * Get range of bits. |
|
96 | + * |
|
97 | + * @param int $start Index of first bit |
|
98 | + * @param int $length Number of bits in range |
|
99 | + * |
|
100 | + * @throws \OutOfBoundsException |
|
101 | + * |
|
102 | + * @return string Integer of $length bits |
|
103 | + */ |
|
104 | + public function range(int $start, int $length): string |
|
105 | + { |
|
106 | + if (!$length) { |
|
107 | + return '0'; |
|
108 | + } |
|
109 | + if ($start + $length > $this->numBits()) { |
|
110 | + throw new \OutOfBoundsException('Not enough bits.'); |
|
111 | + } |
|
112 | + $bits = gmp_init(0); |
|
113 | + $idx = $start; |
|
114 | + $end = $start + $length; |
|
115 | + while (true) { |
|
116 | + $bit = $this->testBit($idx) ? 1 : 0; |
|
117 | + $bits |= $bit; |
|
118 | + if (++$idx >= $end) { |
|
119 | + break; |
|
120 | + } |
|
121 | + $bits <<= 1; |
|
122 | + } |
|
123 | + return gmp_strval($bits, 10); |
|
124 | + } |
|
125 | 125 | |
126 | - /** |
|
127 | - * Get a copy of the bit string with trailing zeroes removed. |
|
128 | - * |
|
129 | - * @return self |
|
130 | - */ |
|
131 | - public function withoutTrailingZeroes(): self |
|
132 | - { |
|
133 | - // if bit string was empty |
|
134 | - if (!strlen($this->_string)) { |
|
135 | - return new self(''); |
|
136 | - } |
|
137 | - $bits = $this->_string; |
|
138 | - // count number of empty trailing octets |
|
139 | - $unused_octets = 0; |
|
140 | - for ($idx = strlen($bits) - 1; $idx >= 0; --$idx, ++$unused_octets) { |
|
141 | - if ("\x0" != $bits[$idx]) { |
|
142 | - break; |
|
143 | - } |
|
144 | - } |
|
145 | - // strip trailing octets |
|
146 | - if ($unused_octets) { |
|
147 | - $bits = substr($bits, 0, -$unused_octets); |
|
148 | - } |
|
149 | - // if bit string was full of zeroes |
|
150 | - if (!strlen($bits)) { |
|
151 | - return new self(''); |
|
152 | - } |
|
153 | - // count number of trailing zeroes in the last octet |
|
154 | - $unused_bits = 0; |
|
155 | - $byte = ord($bits[strlen($bits) - 1]); |
|
156 | - while (!($byte & 0x01)) { |
|
157 | - ++$unused_bits; |
|
158 | - $byte >>= 1; |
|
159 | - } |
|
160 | - return new self($bits, $unused_bits); |
|
161 | - } |
|
126 | + /** |
|
127 | + * Get a copy of the bit string with trailing zeroes removed. |
|
128 | + * |
|
129 | + * @return self |
|
130 | + */ |
|
131 | + public function withoutTrailingZeroes(): self |
|
132 | + { |
|
133 | + // if bit string was empty |
|
134 | + if (!strlen($this->_string)) { |
|
135 | + return new self(''); |
|
136 | + } |
|
137 | + $bits = $this->_string; |
|
138 | + // count number of empty trailing octets |
|
139 | + $unused_octets = 0; |
|
140 | + for ($idx = strlen($bits) - 1; $idx >= 0; --$idx, ++$unused_octets) { |
|
141 | + if ("\x0" != $bits[$idx]) { |
|
142 | + break; |
|
143 | + } |
|
144 | + } |
|
145 | + // strip trailing octets |
|
146 | + if ($unused_octets) { |
|
147 | + $bits = substr($bits, 0, -$unused_octets); |
|
148 | + } |
|
149 | + // if bit string was full of zeroes |
|
150 | + if (!strlen($bits)) { |
|
151 | + return new self(''); |
|
152 | + } |
|
153 | + // count number of trailing zeroes in the last octet |
|
154 | + $unused_bits = 0; |
|
155 | + $byte = ord($bits[strlen($bits) - 1]); |
|
156 | + while (!($byte & 0x01)) { |
|
157 | + ++$unused_bits; |
|
158 | + $byte >>= 1; |
|
159 | + } |
|
160 | + return new self($bits, $unused_bits); |
|
161 | + } |
|
162 | 162 | |
163 | - /** |
|
164 | - * {@inheritdoc} |
|
165 | - */ |
|
166 | - protected function _encodedContentDER(): string |
|
167 | - { |
|
168 | - $der = chr($this->_unusedBits); |
|
169 | - $der .= $this->_string; |
|
170 | - if ($this->_unusedBits) { |
|
171 | - $octet = $der[strlen($der) - 1]; |
|
172 | - // set unused bits to zero |
|
173 | - $octet &= chr(0xff & ~((1 << $this->_unusedBits) - 1)); |
|
174 | - $der[strlen($der) - 1] = $octet; |
|
175 | - } |
|
176 | - return $der; |
|
177 | - } |
|
163 | + /** |
|
164 | + * {@inheritdoc} |
|
165 | + */ |
|
166 | + protected function _encodedContentDER(): string |
|
167 | + { |
|
168 | + $der = chr($this->_unusedBits); |
|
169 | + $der .= $this->_string; |
|
170 | + if ($this->_unusedBits) { |
|
171 | + $octet = $der[strlen($der) - 1]; |
|
172 | + // set unused bits to zero |
|
173 | + $octet &= chr(0xff & ~((1 << $this->_unusedBits) - 1)); |
|
174 | + $der[strlen($der) - 1] = $octet; |
|
175 | + } |
|
176 | + return $der; |
|
177 | + } |
|
178 | 178 | |
179 | - /** |
|
180 | - * {@inheritdoc} |
|
181 | - */ |
|
182 | - protected static function _decodeFromDER(Identifier $identifier, |
|
183 | - string $data, int &$offset): ElementBase |
|
184 | - { |
|
185 | - $idx = $offset; |
|
186 | - $length = Length::expectFromDER($data, $idx); |
|
187 | - if ($length->intLength() < 1) { |
|
188 | - throw new DecodeException('Bit string length must be at least 1.'); |
|
189 | - } |
|
190 | - $unused_bits = ord($data[$idx++]); |
|
191 | - if ($unused_bits > 7) { |
|
192 | - throw new DecodeException( |
|
193 | - 'Unused bits in a bit string must be less than 8.'); |
|
194 | - } |
|
195 | - $str_len = $length->intLength() - 1; |
|
196 | - if ($str_len) { |
|
197 | - $str = substr($data, $idx, $str_len); |
|
198 | - if ($unused_bits) { |
|
199 | - $mask = (1 << $unused_bits) - 1; |
|
200 | - if (ord($str[strlen($str) - 1]) & $mask) { |
|
201 | - throw new DecodeException( |
|
202 | - 'DER encoded bit string must have zero padding.'); |
|
203 | - } |
|
204 | - } |
|
205 | - } else { |
|
206 | - $str = ''; |
|
207 | - } |
|
208 | - $offset = $idx + $str_len; |
|
209 | - return new self($str, $unused_bits); |
|
210 | - } |
|
179 | + /** |
|
180 | + * {@inheritdoc} |
|
181 | + */ |
|
182 | + protected static function _decodeFromDER(Identifier $identifier, |
|
183 | + string $data, int &$offset): ElementBase |
|
184 | + { |
|
185 | + $idx = $offset; |
|
186 | + $length = Length::expectFromDER($data, $idx); |
|
187 | + if ($length->intLength() < 1) { |
|
188 | + throw new DecodeException('Bit string length must be at least 1.'); |
|
189 | + } |
|
190 | + $unused_bits = ord($data[$idx++]); |
|
191 | + if ($unused_bits > 7) { |
|
192 | + throw new DecodeException( |
|
193 | + 'Unused bits in a bit string must be less than 8.'); |
|
194 | + } |
|
195 | + $str_len = $length->intLength() - 1; |
|
196 | + if ($str_len) { |
|
197 | + $str = substr($data, $idx, $str_len); |
|
198 | + if ($unused_bits) { |
|
199 | + $mask = (1 << $unused_bits) - 1; |
|
200 | + if (ord($str[strlen($str) - 1]) & $mask) { |
|
201 | + throw new DecodeException( |
|
202 | + 'DER encoded bit string must have zero padding.'); |
|
203 | + } |
|
204 | + } |
|
205 | + } else { |
|
206 | + $str = ''; |
|
207 | + } |
|
208 | + $offset = $idx + $str_len; |
|
209 | + return new self($str, $unused_bits); |
|
210 | + } |
|
211 | 211 | } |
@@ -1,6 +1,6 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | -declare(strict_types = 1); |
|
3 | +declare(strict_types=1); |
|
4 | 4 | |
5 | 5 | namespace Sop\ASN1\Type\Primitive; |
6 | 6 |