GitHub Access Token became invalid

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