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 — php72 ( 0db6cf...2f43a1 )
by Joni
02:24
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   +663 added lines, -663 removed lines patch added patch discarded remove patch
@@ -17,667 +17,667 @@
 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 ' .
93
-                $this->_typeDescriptorString());
94
-        }
95
-        return $this->_element;
96
-    }
97
-
98
-    /**
99
-     * Get the wrapped element as a private tagged type.
100
-     *
101
-     * @throws \UnexpectedValueException If element is not using private tagging
102
-     *
103
-     * @return \Sop\ASN1\Type\Tagged\PrivateType
104
-     */
105
-    public function asPrivate(): Tagged\PrivateType
106
-    {
107
-        if (!$this->_element instanceof Tagged\PrivateType) {
108
-            throw new \UnexpectedValueException(
109
-                'Private type expected, got ' . $this->_typeDescriptorString());
110
-        }
111
-        return $this->_element;
112
-    }
113
-
114
-    /**
115
-     * Get the wrapped element as a boolean type.
116
-     *
117
-     * @throws \UnexpectedValueException If the element is not a boolean
118
-     *
119
-     * @return \Sop\ASN1\Type\Primitive\Boolean
120
-     */
121
-    public function asBoolean(): Primitive\Boolean
122
-    {
123
-        if (!$this->_element instanceof Primitive\Boolean) {
124
-            throw new \UnexpectedValueException(
125
-                $this->_generateExceptionMessage(Element::TYPE_BOOLEAN));
126
-        }
127
-        return $this->_element;
128
-    }
129
-
130
-    /**
131
-     * Get the wrapped element as an integer type.
132
-     *
133
-     * @throws \UnexpectedValueException If the element is not an integer
134
-     *
135
-     * @return \Sop\ASN1\Type\Primitive\Integer
136
-     */
137
-    public function asInteger(): Primitive\Integer
138
-    {
139
-        if (!$this->_element instanceof Primitive\Integer) {
140
-            throw new \UnexpectedValueException(
141
-                $this->_generateExceptionMessage(Element::TYPE_INTEGER));
142
-        }
143
-        return $this->_element;
144
-    }
145
-
146
-    /**
147
-     * Get the wrapped element as a bit string type.
148
-     *
149
-     * @throws \UnexpectedValueException If the element is not a bit string
150
-     *
151
-     * @return \Sop\ASN1\Type\Primitive\BitString
152
-     */
153
-    public function asBitString(): Primitive\BitString
154
-    {
155
-        if (!$this->_element instanceof Primitive\BitString) {
156
-            throw new \UnexpectedValueException(
157
-                $this->_generateExceptionMessage(Element::TYPE_BIT_STRING));
158
-        }
159
-        return $this->_element;
160
-    }
161
-
162
-    /**
163
-     * Get the wrapped element as an octet string type.
164
-     *
165
-     * @throws \UnexpectedValueException If the element is not an octet string
166
-     *
167
-     * @return \Sop\ASN1\Type\Primitive\OctetString
168
-     */
169
-    public function asOctetString(): Primitive\OctetString
170
-    {
171
-        if (!$this->_element instanceof Primitive\OctetString) {
172
-            throw new \UnexpectedValueException(
173
-                $this->_generateExceptionMessage(Element::TYPE_OCTET_STRING));
174
-        }
175
-        return $this->_element;
176
-    }
177
-
178
-    /**
179
-     * Get the wrapped element as a null type.
180
-     *
181
-     * @throws \UnexpectedValueException If the element is not a null
182
-     *
183
-     * @return \Sop\ASN1\Type\Primitive\NullType
184
-     */
185
-    public function asNull(): Primitive\NullType
186
-    {
187
-        if (!$this->_element instanceof Primitive\NullType) {
188
-            throw new \UnexpectedValueException(
189
-                $this->_generateExceptionMessage(Element::TYPE_NULL));
190
-        }
191
-        return $this->_element;
192
-    }
193
-
194
-    /**
195
-     * Get the wrapped element as an object identifier type.
196
-     *
197
-     * @throws \UnexpectedValueException If the element is not an object
198
-     *                                   identifier
199
-     *
200
-     * @return \Sop\ASN1\Type\Primitive\ObjectIdentifier
201
-     */
202
-    public function asObjectIdentifier(): Primitive\ObjectIdentifier
203
-    {
204
-        if (!$this->_element instanceof Primitive\ObjectIdentifier) {
205
-            throw new \UnexpectedValueException(
206
-                $this->_generateExceptionMessage(
207
-                    Element::TYPE_OBJECT_IDENTIFIER));
208
-        }
209
-        return $this->_element;
210
-    }
211
-
212
-    /**
213
-     * Get the wrapped element as an object descriptor type.
214
-     *
215
-     * @throws \UnexpectedValueException If the element is not an object
216
-     *                                   descriptor
217
-     *
218
-     * @return \Sop\ASN1\Type\Primitive\ObjectDescriptor
219
-     */
220
-    public function asObjectDescriptor(): Primitive\ObjectDescriptor
221
-    {
222
-        if (!$this->_element instanceof Primitive\ObjectDescriptor) {
223
-            throw new \UnexpectedValueException(
224
-                $this->_generateExceptionMessage(
225
-                    Element::TYPE_OBJECT_DESCRIPTOR));
226
-        }
227
-        return $this->_element;
228
-    }
229
-
230
-    /**
231
-     * Get the wrapped element as a real type.
232
-     *
233
-     * @throws \UnexpectedValueException If the element is not a real
234
-     *
235
-     * @return \Sop\ASN1\Type\Primitive\Real
236
-     */
237
-    public function asReal(): Primitive\Real
238
-    {
239
-        if (!$this->_element instanceof Primitive\Real) {
240
-            throw new \UnexpectedValueException(
241
-                $this->_generateExceptionMessage(Element::TYPE_REAL));
242
-        }
243
-        return $this->_element;
244
-    }
245
-
246
-    /**
247
-     * Get the wrapped element as an enumerated type.
248
-     *
249
-     * @throws \UnexpectedValueException If the element is not an enumerated
250
-     *
251
-     * @return \Sop\ASN1\Type\Primitive\Enumerated
252
-     */
253
-    public function asEnumerated(): Primitive\Enumerated
254
-    {
255
-        if (!$this->_element instanceof Primitive\Enumerated) {
256
-            throw new \UnexpectedValueException(
257
-                $this->_generateExceptionMessage(Element::TYPE_ENUMERATED));
258
-        }
259
-        return $this->_element;
260
-    }
261
-
262
-    /**
263
-     * Get the wrapped element as a UTF8 string type.
264
-     *
265
-     * @throws \UnexpectedValueException If the element is not a UTF8 string
266
-     *
267
-     * @return \Sop\ASN1\Type\Primitive\UTF8String
268
-     */
269
-    public function asUTF8String(): Primitive\UTF8String
270
-    {
271
-        if (!$this->_element instanceof Primitive\UTF8String) {
272
-            throw new \UnexpectedValueException(
273
-                $this->_generateExceptionMessage(Element::TYPE_UTF8_STRING));
274
-        }
275
-        return $this->_element;
276
-    }
277
-
278
-    /**
279
-     * Get the wrapped element as a relative OID type.
280
-     *
281
-     * @throws \UnexpectedValueException If the element is not a relative OID
282
-     *
283
-     * @return \Sop\ASN1\Type\Primitive\RelativeOID
284
-     */
285
-    public function asRelativeOID(): Primitive\RelativeOID
286
-    {
287
-        if (!$this->_element instanceof Primitive\RelativeOID) {
288
-            throw new \UnexpectedValueException(
289
-                $this->_generateExceptionMessage(Element::TYPE_RELATIVE_OID));
290
-        }
291
-        return $this->_element;
292
-    }
293
-
294
-    /**
295
-     * Get the wrapped element as a sequence type.
296
-     *
297
-     * @throws \UnexpectedValueException If the element is not a sequence
298
-     *
299
-     * @return \Sop\ASN1\Type\Constructed\Sequence
300
-     */
301
-    public function asSequence(): Constructed\Sequence
302
-    {
303
-        if (!$this->_element instanceof Constructed\Sequence) {
304
-            throw new \UnexpectedValueException(
305
-                $this->_generateExceptionMessage(Element::TYPE_SEQUENCE));
306
-        }
307
-        return $this->_element;
308
-    }
309
-
310
-    /**
311
-     * Get the wrapped element as a set type.
312
-     *
313
-     * @throws \UnexpectedValueException If the element is not a set
314
-     *
315
-     * @return \Sop\ASN1\Type\Constructed\Set
316
-     */
317
-    public function asSet(): Constructed\Set
318
-    {
319
-        if (!$this->_element instanceof Constructed\Set) {
320
-            throw new \UnexpectedValueException(
321
-                $this->_generateExceptionMessage(Element::TYPE_SET));
322
-        }
323
-        return $this->_element;
324
-    }
325
-
326
-    /**
327
-     * Get the wrapped element as a numeric string type.
328
-     *
329
-     * @throws \UnexpectedValueException If the element is not a numeric string
330
-     *
331
-     * @return \Sop\ASN1\Type\Primitive\NumericString
332
-     */
333
-    public function asNumericString(): Primitive\NumericString
334
-    {
335
-        if (!$this->_element instanceof Primitive\NumericString) {
336
-            throw new \UnexpectedValueException(
337
-                $this->_generateExceptionMessage(Element::TYPE_NUMERIC_STRING));
338
-        }
339
-        return $this->_element;
340
-    }
341
-
342
-    /**
343
-     * Get the wrapped element as a printable string type.
344
-     *
345
-     * @throws \UnexpectedValueException If the element is not a printable
346
-     *                                   string
347
-     *
348
-     * @return \Sop\ASN1\Type\Primitive\PrintableString
349
-     */
350
-    public function asPrintableString(): Primitive\PrintableString
351
-    {
352
-        if (!$this->_element instanceof Primitive\PrintableString) {
353
-            throw new \UnexpectedValueException(
354
-                $this->_generateExceptionMessage(Element::TYPE_PRINTABLE_STRING));
355
-        }
356
-        return $this->_element;
357
-    }
358
-
359
-    /**
360
-     * Get the wrapped element as a T61 string type.
361
-     *
362
-     * @throws \UnexpectedValueException If the element is not a T61 string
363
-     *
364
-     * @return \Sop\ASN1\Type\Primitive\T61String
365
-     */
366
-    public function asT61String(): Primitive\T61String
367
-    {
368
-        if (!$this->_element instanceof Primitive\T61String) {
369
-            throw new \UnexpectedValueException(
370
-                $this->_generateExceptionMessage(Element::TYPE_T61_STRING));
371
-        }
372
-        return $this->_element;
373
-    }
374
-
375
-    /**
376
-     * Get the wrapped element as a videotex string type.
377
-     *
378
-     * @throws \UnexpectedValueException If the element is not a videotex string
379
-     *
380
-     * @return \Sop\ASN1\Type\Primitive\VideotexString
381
-     */
382
-    public function asVideotexString(): Primitive\VideotexString
383
-    {
384
-        if (!$this->_element instanceof Primitive\VideotexString) {
385
-            throw new \UnexpectedValueException(
386
-                $this->_generateExceptionMessage(Element::TYPE_VIDEOTEX_STRING));
387
-        }
388
-        return $this->_element;
389
-    }
390
-
391
-    /**
392
-     * Get the wrapped element as a IA5 string type.
393
-     *
394
-     * @throws \UnexpectedValueException If the element is not a IA5 string
395
-     *
396
-     * @return \Sop\ASN1\Type\Primitive\IA5String
397
-     */
398
-    public function asIA5String(): Primitive\IA5String
399
-    {
400
-        if (!$this->_element instanceof Primitive\IA5String) {
401
-            throw new \UnexpectedValueException(
402
-                $this->_generateExceptionMessage(Element::TYPE_IA5_STRING));
403
-        }
404
-        return $this->_element;
405
-    }
406
-
407
-    /**
408
-     * Get the wrapped element as an UTC time type.
409
-     *
410
-     * @throws \UnexpectedValueException If the element is not a UTC time
411
-     *
412
-     * @return \Sop\ASN1\Type\Primitive\UTCTime
413
-     */
414
-    public function asUTCTime(): Primitive\UTCTime
415
-    {
416
-        if (!$this->_element instanceof Primitive\UTCTime) {
417
-            throw new \UnexpectedValueException(
418
-                $this->_generateExceptionMessage(Element::TYPE_UTC_TIME));
419
-        }
420
-        return $this->_element;
421
-    }
422
-
423
-    /**
424
-     * Get the wrapped element as a generalized time type.
425
-     *
426
-     * @throws \UnexpectedValueException If the element is not a generalized
427
-     *                                   time
428
-     *
429
-     * @return \Sop\ASN1\Type\Primitive\GeneralizedTime
430
-     */
431
-    public function asGeneralizedTime(): Primitive\GeneralizedTime
432
-    {
433
-        if (!$this->_element instanceof Primitive\GeneralizedTime) {
434
-            throw new \UnexpectedValueException(
435
-                $this->_generateExceptionMessage(Element::TYPE_GENERALIZED_TIME));
436
-        }
437
-        return $this->_element;
438
-    }
439
-
440
-    /**
441
-     * Get the wrapped element as a graphic string type.
442
-     *
443
-     * @throws \UnexpectedValueException If the element is not a graphic string
444
-     *
445
-     * @return \Sop\ASN1\Type\Primitive\GraphicString
446
-     */
447
-    public function asGraphicString(): Primitive\GraphicString
448
-    {
449
-        if (!$this->_element instanceof Primitive\GraphicString) {
450
-            throw new \UnexpectedValueException(
451
-                $this->_generateExceptionMessage(Element::TYPE_GRAPHIC_STRING));
452
-        }
453
-        return $this->_element;
454
-    }
455
-
456
-    /**
457
-     * Get the wrapped element as a visible string type.
458
-     *
459
-     * @throws \UnexpectedValueException If the element is not a visible string
460
-     *
461
-     * @return \Sop\ASN1\Type\Primitive\VisibleString
462
-     */
463
-    public function asVisibleString(): Primitive\VisibleString
464
-    {
465
-        if (!$this->_element instanceof Primitive\VisibleString) {
466
-            throw new \UnexpectedValueException(
467
-                $this->_generateExceptionMessage(Element::TYPE_VISIBLE_STRING));
468
-        }
469
-        return $this->_element;
470
-    }
471
-
472
-    /**
473
-     * Get the wrapped element as a general string type.
474
-     *
475
-     * @throws \UnexpectedValueException If the element is not general string
476
-     *
477
-     * @return \Sop\ASN1\Type\Primitive\GeneralString
478
-     */
479
-    public function asGeneralString(): Primitive\GeneralString
480
-    {
481
-        if (!$this->_element instanceof Primitive\GeneralString) {
482
-            throw new \UnexpectedValueException(
483
-                $this->_generateExceptionMessage(Element::TYPE_GENERAL_STRING));
484
-        }
485
-        return $this->_element;
486
-    }
487
-
488
-    /**
489
-     * Get the wrapped element as a universal string type.
490
-     *
491
-     * @throws \UnexpectedValueException If the element is not a universal
492
-     *                                   string
493
-     *
494
-     * @return \Sop\ASN1\Type\Primitive\UniversalString
495
-     */
496
-    public function asUniversalString(): Primitive\UniversalString
497
-    {
498
-        if (!$this->_element instanceof Primitive\UniversalString) {
499
-            throw new \UnexpectedValueException(
500
-                $this->_generateExceptionMessage(Element::TYPE_UNIVERSAL_STRING));
501
-        }
502
-        return $this->_element;
503
-    }
504
-
505
-    /**
506
-     * Get the wrapped element as a character string type.
507
-     *
508
-     * @throws \UnexpectedValueException If the element is not a character
509
-     *                                   string
510
-     *
511
-     * @return \Sop\ASN1\Type\Primitive\CharacterString
512
-     */
513
-    public function asCharacterString(): Primitive\CharacterString
514
-    {
515
-        if (!$this->_element instanceof Primitive\CharacterString) {
516
-            throw new \UnexpectedValueException(
517
-                $this->_generateExceptionMessage(Element::TYPE_CHARACTER_STRING));
518
-        }
519
-        return $this->_element;
520
-    }
521
-
522
-    /**
523
-     * Get the wrapped element as a BMP string type.
524
-     *
525
-     * @throws \UnexpectedValueException If the element is not a bmp string
526
-     *
527
-     * @return \Sop\ASN1\Type\Primitive\BMPString
528
-     */
529
-    public function asBMPString(): Primitive\BMPString
530
-    {
531
-        if (!$this->_element instanceof Primitive\BMPString) {
532
-            throw new \UnexpectedValueException(
533
-                $this->_generateExceptionMessage(Element::TYPE_BMP_STRING));
534
-        }
535
-        return $this->_element;
536
-    }
537
-
538
-    /**
539
-     * Get the wrapped element as any string type.
540
-     *
541
-     * @throws \UnexpectedValueException If the element is not a string
542
-     *
543
-     * @return StringType
544
-     */
545
-    public function asString(): StringType
546
-    {
547
-        if (!$this->_element instanceof StringType) {
548
-            throw new \UnexpectedValueException(
549
-                $this->_generateExceptionMessage(Element::TYPE_STRING));
550
-        }
551
-        return $this->_element;
552
-    }
553
-
554
-    /**
555
-     * Get the wrapped element as any time type.
556
-     *
557
-     * @throws \UnexpectedValueException If the element is not a time
558
-     *
559
-     * @return TimeType
560
-     */
561
-    public function asTime(): TimeType
562
-    {
563
-        if (!$this->_element instanceof TimeType) {
564
-            throw new \UnexpectedValueException(
565
-                $this->_generateExceptionMessage(Element::TYPE_TIME));
566
-        }
567
-        return $this->_element;
568
-    }
569
-
570
-    /**
571
-     * {@inheritdoc}
572
-     */
573
-    public function toDER(): string
574
-    {
575
-        return $this->_element->toDER();
576
-    }
577
-
578
-    /**
579
-     * {@inheritdoc}
580
-     */
581
-    public function typeClass(): int
582
-    {
583
-        return $this->_element->typeClass();
584
-    }
585
-
586
-    /**
587
-     * {@inheritdoc}
588
-     */
589
-    public function isConstructed(): bool
590
-    {
591
-        return $this->_element->isConstructed();
592
-    }
593
-
594
-    /**
595
-     * {@inheritdoc}
596
-     */
597
-    public function tag(): int
598
-    {
599
-        return $this->_element->tag();
600
-    }
601
-
602
-    /**
603
-     * {@inheritdoc}
604
-     */
605
-    public function isType(int $tag): bool
606
-    {
607
-        return $this->_element->isType($tag);
608
-    }
609
-
610
-    /**
611
-     * {@inheritdoc}
612
-     *
613
-     * Consider using any of the <code>as*</code> accessor methods instead.
614
-     */
615
-    public function expectType(int $tag): ElementBase
616
-    {
617
-        return $this->_element->expectType($tag);
618
-    }
619
-
620
-    /**
621
-     * {@inheritdoc}
622
-     */
623
-    public function isTagged(): bool
624
-    {
625
-        return $this->_element->isTagged();
626
-    }
627
-
628
-    /**
629
-     * {@inheritdoc}
630
-     *
631
-     * Consider using <code>asTagged()</code> method instead and chaining
632
-     * with <code>TaggedType::asExplicit()</code> or
633
-     * <code>TaggedType::asImplicit()</code>.
634
-     */
635
-    public function expectTagged(?int $tag = null): TaggedType
636
-    {
637
-        return $this->_element->expectTagged($tag);
638
-    }
639
-
640
-    /**
641
-     * {@inheritdoc}
642
-     */
643
-    public function asElement(): Element
644
-    {
645
-        return $this->_element;
646
-    }
647
-
648
-    /**
649
-     * {@inheritdoc}
650
-     */
651
-    public function asUnspecified(): UnspecifiedType
652
-    {
653
-        return $this;
654
-    }
655
-
656
-    /**
657
-     * Generate message for exceptions thrown by <code>as*</code> methods.
658
-     *
659
-     * @param int $tag Type tag of the expected element
660
-     *
661
-     * @return string
662
-     */
663
-    private function _generateExceptionMessage(int $tag): string
664
-    {
665
-        return sprintf('%s expected, got %s.', Element::tagToName($tag),
666
-            $this->_typeDescriptorString());
667
-    }
668
-
669
-    /**
670
-     * Get textual description of the wrapped element for debugging purposes.
671
-     *
672
-     * @return string
673
-     */
674
-    private function _typeDescriptorString(): string
675
-    {
676
-        $type_cls = $this->_element->typeClass();
677
-        $tag = $this->_element->tag();
678
-        if (Identifier::CLASS_UNIVERSAL == $type_cls) {
679
-            return Element::tagToName($tag);
680
-        }
681
-        return Identifier::classToName($type_cls) . " TAG {$tag}";
682
-    }
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 ' .
93
+				$this->_typeDescriptorString());
94
+		}
95
+		return $this->_element;
96
+	}
97
+
98
+	/**
99
+	 * Get the wrapped element as a private tagged type.
100
+	 *
101
+	 * @throws \UnexpectedValueException If element is not using private tagging
102
+	 *
103
+	 * @return \Sop\ASN1\Type\Tagged\PrivateType
104
+	 */
105
+	public function asPrivate(): Tagged\PrivateType
106
+	{
107
+		if (!$this->_element instanceof Tagged\PrivateType) {
108
+			throw new \UnexpectedValueException(
109
+				'Private type expected, got ' . $this->_typeDescriptorString());
110
+		}
111
+		return $this->_element;
112
+	}
113
+
114
+	/**
115
+	 * Get the wrapped element as a boolean type.
116
+	 *
117
+	 * @throws \UnexpectedValueException If the element is not a boolean
118
+	 *
119
+	 * @return \Sop\ASN1\Type\Primitive\Boolean
120
+	 */
121
+	public function asBoolean(): Primitive\Boolean
122
+	{
123
+		if (!$this->_element instanceof Primitive\Boolean) {
124
+			throw new \UnexpectedValueException(
125
+				$this->_generateExceptionMessage(Element::TYPE_BOOLEAN));
126
+		}
127
+		return $this->_element;
128
+	}
129
+
130
+	/**
131
+	 * Get the wrapped element as an integer type.
132
+	 *
133
+	 * @throws \UnexpectedValueException If the element is not an integer
134
+	 *
135
+	 * @return \Sop\ASN1\Type\Primitive\Integer
136
+	 */
137
+	public function asInteger(): Primitive\Integer
138
+	{
139
+		if (!$this->_element instanceof Primitive\Integer) {
140
+			throw new \UnexpectedValueException(
141
+				$this->_generateExceptionMessage(Element::TYPE_INTEGER));
142
+		}
143
+		return $this->_element;
144
+	}
145
+
146
+	/**
147
+	 * Get the wrapped element as a bit string type.
148
+	 *
149
+	 * @throws \UnexpectedValueException If the element is not a bit string
150
+	 *
151
+	 * @return \Sop\ASN1\Type\Primitive\BitString
152
+	 */
153
+	public function asBitString(): Primitive\BitString
154
+	{
155
+		if (!$this->_element instanceof Primitive\BitString) {
156
+			throw new \UnexpectedValueException(
157
+				$this->_generateExceptionMessage(Element::TYPE_BIT_STRING));
158
+		}
159
+		return $this->_element;
160
+	}
161
+
162
+	/**
163
+	 * Get the wrapped element as an octet string type.
164
+	 *
165
+	 * @throws \UnexpectedValueException If the element is not an octet string
166
+	 *
167
+	 * @return \Sop\ASN1\Type\Primitive\OctetString
168
+	 */
169
+	public function asOctetString(): Primitive\OctetString
170
+	{
171
+		if (!$this->_element instanceof Primitive\OctetString) {
172
+			throw new \UnexpectedValueException(
173
+				$this->_generateExceptionMessage(Element::TYPE_OCTET_STRING));
174
+		}
175
+		return $this->_element;
176
+	}
177
+
178
+	/**
179
+	 * Get the wrapped element as a null type.
180
+	 *
181
+	 * @throws \UnexpectedValueException If the element is not a null
182
+	 *
183
+	 * @return \Sop\ASN1\Type\Primitive\NullType
184
+	 */
185
+	public function asNull(): Primitive\NullType
186
+	{
187
+		if (!$this->_element instanceof Primitive\NullType) {
188
+			throw new \UnexpectedValueException(
189
+				$this->_generateExceptionMessage(Element::TYPE_NULL));
190
+		}
191
+		return $this->_element;
192
+	}
193
+
194
+	/**
195
+	 * Get the wrapped element as an object identifier type.
196
+	 *
197
+	 * @throws \UnexpectedValueException If the element is not an object
198
+	 *                                   identifier
199
+	 *
200
+	 * @return \Sop\ASN1\Type\Primitive\ObjectIdentifier
201
+	 */
202
+	public function asObjectIdentifier(): Primitive\ObjectIdentifier
203
+	{
204
+		if (!$this->_element instanceof Primitive\ObjectIdentifier) {
205
+			throw new \UnexpectedValueException(
206
+				$this->_generateExceptionMessage(
207
+					Element::TYPE_OBJECT_IDENTIFIER));
208
+		}
209
+		return $this->_element;
210
+	}
211
+
212
+	/**
213
+	 * Get the wrapped element as an object descriptor type.
214
+	 *
215
+	 * @throws \UnexpectedValueException If the element is not an object
216
+	 *                                   descriptor
217
+	 *
218
+	 * @return \Sop\ASN1\Type\Primitive\ObjectDescriptor
219
+	 */
220
+	public function asObjectDescriptor(): Primitive\ObjectDescriptor
221
+	{
222
+		if (!$this->_element instanceof Primitive\ObjectDescriptor) {
223
+			throw new \UnexpectedValueException(
224
+				$this->_generateExceptionMessage(
225
+					Element::TYPE_OBJECT_DESCRIPTOR));
226
+		}
227
+		return $this->_element;
228
+	}
229
+
230
+	/**
231
+	 * Get the wrapped element as a real type.
232
+	 *
233
+	 * @throws \UnexpectedValueException If the element is not a real
234
+	 *
235
+	 * @return \Sop\ASN1\Type\Primitive\Real
236
+	 */
237
+	public function asReal(): Primitive\Real
238
+	{
239
+		if (!$this->_element instanceof Primitive\Real) {
240
+			throw new \UnexpectedValueException(
241
+				$this->_generateExceptionMessage(Element::TYPE_REAL));
242
+		}
243
+		return $this->_element;
244
+	}
245
+
246
+	/**
247
+	 * Get the wrapped element as an enumerated type.
248
+	 *
249
+	 * @throws \UnexpectedValueException If the element is not an enumerated
250
+	 *
251
+	 * @return \Sop\ASN1\Type\Primitive\Enumerated
252
+	 */
253
+	public function asEnumerated(): Primitive\Enumerated
254
+	{
255
+		if (!$this->_element instanceof Primitive\Enumerated) {
256
+			throw new \UnexpectedValueException(
257
+				$this->_generateExceptionMessage(Element::TYPE_ENUMERATED));
258
+		}
259
+		return $this->_element;
260
+	}
261
+
262
+	/**
263
+	 * Get the wrapped element as a UTF8 string type.
264
+	 *
265
+	 * @throws \UnexpectedValueException If the element is not a UTF8 string
266
+	 *
267
+	 * @return \Sop\ASN1\Type\Primitive\UTF8String
268
+	 */
269
+	public function asUTF8String(): Primitive\UTF8String
270
+	{
271
+		if (!$this->_element instanceof Primitive\UTF8String) {
272
+			throw new \UnexpectedValueException(
273
+				$this->_generateExceptionMessage(Element::TYPE_UTF8_STRING));
274
+		}
275
+		return $this->_element;
276
+	}
277
+
278
+	/**
279
+	 * Get the wrapped element as a relative OID type.
280
+	 *
281
+	 * @throws \UnexpectedValueException If the element is not a relative OID
282
+	 *
283
+	 * @return \Sop\ASN1\Type\Primitive\RelativeOID
284
+	 */
285
+	public function asRelativeOID(): Primitive\RelativeOID
286
+	{
287
+		if (!$this->_element instanceof Primitive\RelativeOID) {
288
+			throw new \UnexpectedValueException(
289
+				$this->_generateExceptionMessage(Element::TYPE_RELATIVE_OID));
290
+		}
291
+		return $this->_element;
292
+	}
293
+
294
+	/**
295
+	 * Get the wrapped element as a sequence type.
296
+	 *
297
+	 * @throws \UnexpectedValueException If the element is not a sequence
298
+	 *
299
+	 * @return \Sop\ASN1\Type\Constructed\Sequence
300
+	 */
301
+	public function asSequence(): Constructed\Sequence
302
+	{
303
+		if (!$this->_element instanceof Constructed\Sequence) {
304
+			throw new \UnexpectedValueException(
305
+				$this->_generateExceptionMessage(Element::TYPE_SEQUENCE));
306
+		}
307
+		return $this->_element;
308
+	}
309
+
310
+	/**
311
+	 * Get the wrapped element as a set type.
312
+	 *
313
+	 * @throws \UnexpectedValueException If the element is not a set
314
+	 *
315
+	 * @return \Sop\ASN1\Type\Constructed\Set
316
+	 */
317
+	public function asSet(): Constructed\Set
318
+	{
319
+		if (!$this->_element instanceof Constructed\Set) {
320
+			throw new \UnexpectedValueException(
321
+				$this->_generateExceptionMessage(Element::TYPE_SET));
322
+		}
323
+		return $this->_element;
324
+	}
325
+
326
+	/**
327
+	 * Get the wrapped element as a numeric string type.
328
+	 *
329
+	 * @throws \UnexpectedValueException If the element is not a numeric string
330
+	 *
331
+	 * @return \Sop\ASN1\Type\Primitive\NumericString
332
+	 */
333
+	public function asNumericString(): Primitive\NumericString
334
+	{
335
+		if (!$this->_element instanceof Primitive\NumericString) {
336
+			throw new \UnexpectedValueException(
337
+				$this->_generateExceptionMessage(Element::TYPE_NUMERIC_STRING));
338
+		}
339
+		return $this->_element;
340
+	}
341
+
342
+	/**
343
+	 * Get the wrapped element as a printable string type.
344
+	 *
345
+	 * @throws \UnexpectedValueException If the element is not a printable
346
+	 *                                   string
347
+	 *
348
+	 * @return \Sop\ASN1\Type\Primitive\PrintableString
349
+	 */
350
+	public function asPrintableString(): Primitive\PrintableString
351
+	{
352
+		if (!$this->_element instanceof Primitive\PrintableString) {
353
+			throw new \UnexpectedValueException(
354
+				$this->_generateExceptionMessage(Element::TYPE_PRINTABLE_STRING));
355
+		}
356
+		return $this->_element;
357
+	}
358
+
359
+	/**
360
+	 * Get the wrapped element as a T61 string type.
361
+	 *
362
+	 * @throws \UnexpectedValueException If the element is not a T61 string
363
+	 *
364
+	 * @return \Sop\ASN1\Type\Primitive\T61String
365
+	 */
366
+	public function asT61String(): Primitive\T61String
367
+	{
368
+		if (!$this->_element instanceof Primitive\T61String) {
369
+			throw new \UnexpectedValueException(
370
+				$this->_generateExceptionMessage(Element::TYPE_T61_STRING));
371
+		}
372
+		return $this->_element;
373
+	}
374
+
375
+	/**
376
+	 * Get the wrapped element as a videotex string type.
377
+	 *
378
+	 * @throws \UnexpectedValueException If the element is not a videotex string
379
+	 *
380
+	 * @return \Sop\ASN1\Type\Primitive\VideotexString
381
+	 */
382
+	public function asVideotexString(): Primitive\VideotexString
383
+	{
384
+		if (!$this->_element instanceof Primitive\VideotexString) {
385
+			throw new \UnexpectedValueException(
386
+				$this->_generateExceptionMessage(Element::TYPE_VIDEOTEX_STRING));
387
+		}
388
+		return $this->_element;
389
+	}
390
+
391
+	/**
392
+	 * Get the wrapped element as a IA5 string type.
393
+	 *
394
+	 * @throws \UnexpectedValueException If the element is not a IA5 string
395
+	 *
396
+	 * @return \Sop\ASN1\Type\Primitive\IA5String
397
+	 */
398
+	public function asIA5String(): Primitive\IA5String
399
+	{
400
+		if (!$this->_element instanceof Primitive\IA5String) {
401
+			throw new \UnexpectedValueException(
402
+				$this->_generateExceptionMessage(Element::TYPE_IA5_STRING));
403
+		}
404
+		return $this->_element;
405
+	}
406
+
407
+	/**
408
+	 * Get the wrapped element as an UTC time type.
409
+	 *
410
+	 * @throws \UnexpectedValueException If the element is not a UTC time
411
+	 *
412
+	 * @return \Sop\ASN1\Type\Primitive\UTCTime
413
+	 */
414
+	public function asUTCTime(): Primitive\UTCTime
415
+	{
416
+		if (!$this->_element instanceof Primitive\UTCTime) {
417
+			throw new \UnexpectedValueException(
418
+				$this->_generateExceptionMessage(Element::TYPE_UTC_TIME));
419
+		}
420
+		return $this->_element;
421
+	}
422
+
423
+	/**
424
+	 * Get the wrapped element as a generalized time type.
425
+	 *
426
+	 * @throws \UnexpectedValueException If the element is not a generalized
427
+	 *                                   time
428
+	 *
429
+	 * @return \Sop\ASN1\Type\Primitive\GeneralizedTime
430
+	 */
431
+	public function asGeneralizedTime(): Primitive\GeneralizedTime
432
+	{
433
+		if (!$this->_element instanceof Primitive\GeneralizedTime) {
434
+			throw new \UnexpectedValueException(
435
+				$this->_generateExceptionMessage(Element::TYPE_GENERALIZED_TIME));
436
+		}
437
+		return $this->_element;
438
+	}
439
+
440
+	/**
441
+	 * Get the wrapped element as a graphic string type.
442
+	 *
443
+	 * @throws \UnexpectedValueException If the element is not a graphic string
444
+	 *
445
+	 * @return \Sop\ASN1\Type\Primitive\GraphicString
446
+	 */
447
+	public function asGraphicString(): Primitive\GraphicString
448
+	{
449
+		if (!$this->_element instanceof Primitive\GraphicString) {
450
+			throw new \UnexpectedValueException(
451
+				$this->_generateExceptionMessage(Element::TYPE_GRAPHIC_STRING));
452
+		}
453
+		return $this->_element;
454
+	}
455
+
456
+	/**
457
+	 * Get the wrapped element as a visible string type.
458
+	 *
459
+	 * @throws \UnexpectedValueException If the element is not a visible string
460
+	 *
461
+	 * @return \Sop\ASN1\Type\Primitive\VisibleString
462
+	 */
463
+	public function asVisibleString(): Primitive\VisibleString
464
+	{
465
+		if (!$this->_element instanceof Primitive\VisibleString) {
466
+			throw new \UnexpectedValueException(
467
+				$this->_generateExceptionMessage(Element::TYPE_VISIBLE_STRING));
468
+		}
469
+		return $this->_element;
470
+	}
471
+
472
+	/**
473
+	 * Get the wrapped element as a general string type.
474
+	 *
475
+	 * @throws \UnexpectedValueException If the element is not general string
476
+	 *
477
+	 * @return \Sop\ASN1\Type\Primitive\GeneralString
478
+	 */
479
+	public function asGeneralString(): Primitive\GeneralString
480
+	{
481
+		if (!$this->_element instanceof Primitive\GeneralString) {
482
+			throw new \UnexpectedValueException(
483
+				$this->_generateExceptionMessage(Element::TYPE_GENERAL_STRING));
484
+		}
485
+		return $this->_element;
486
+	}
487
+
488
+	/**
489
+	 * Get the wrapped element as a universal string type.
490
+	 *
491
+	 * @throws \UnexpectedValueException If the element is not a universal
492
+	 *                                   string
493
+	 *
494
+	 * @return \Sop\ASN1\Type\Primitive\UniversalString
495
+	 */
496
+	public function asUniversalString(): Primitive\UniversalString
497
+	{
498
+		if (!$this->_element instanceof Primitive\UniversalString) {
499
+			throw new \UnexpectedValueException(
500
+				$this->_generateExceptionMessage(Element::TYPE_UNIVERSAL_STRING));
501
+		}
502
+		return $this->_element;
503
+	}
504
+
505
+	/**
506
+	 * Get the wrapped element as a character string type.
507
+	 *
508
+	 * @throws \UnexpectedValueException If the element is not a character
509
+	 *                                   string
510
+	 *
511
+	 * @return \Sop\ASN1\Type\Primitive\CharacterString
512
+	 */
513
+	public function asCharacterString(): Primitive\CharacterString
514
+	{
515
+		if (!$this->_element instanceof Primitive\CharacterString) {
516
+			throw new \UnexpectedValueException(
517
+				$this->_generateExceptionMessage(Element::TYPE_CHARACTER_STRING));
518
+		}
519
+		return $this->_element;
520
+	}
521
+
522
+	/**
523
+	 * Get the wrapped element as a BMP string type.
524
+	 *
525
+	 * @throws \UnexpectedValueException If the element is not a bmp string
526
+	 *
527
+	 * @return \Sop\ASN1\Type\Primitive\BMPString
528
+	 */
529
+	public function asBMPString(): Primitive\BMPString
530
+	{
531
+		if (!$this->_element instanceof Primitive\BMPString) {
532
+			throw new \UnexpectedValueException(
533
+				$this->_generateExceptionMessage(Element::TYPE_BMP_STRING));
534
+		}
535
+		return $this->_element;
536
+	}
537
+
538
+	/**
539
+	 * Get the wrapped element as any string type.
540
+	 *
541
+	 * @throws \UnexpectedValueException If the element is not a string
542
+	 *
543
+	 * @return StringType
544
+	 */
545
+	public function asString(): StringType
546
+	{
547
+		if (!$this->_element instanceof StringType) {
548
+			throw new \UnexpectedValueException(
549
+				$this->_generateExceptionMessage(Element::TYPE_STRING));
550
+		}
551
+		return $this->_element;
552
+	}
553
+
554
+	/**
555
+	 * Get the wrapped element as any time type.
556
+	 *
557
+	 * @throws \UnexpectedValueException If the element is not a time
558
+	 *
559
+	 * @return TimeType
560
+	 */
561
+	public function asTime(): TimeType
562
+	{
563
+		if (!$this->_element instanceof TimeType) {
564
+			throw new \UnexpectedValueException(
565
+				$this->_generateExceptionMessage(Element::TYPE_TIME));
566
+		}
567
+		return $this->_element;
568
+	}
569
+
570
+	/**
571
+	 * {@inheritdoc}
572
+	 */
573
+	public function toDER(): string
574
+	{
575
+		return $this->_element->toDER();
576
+	}
577
+
578
+	/**
579
+	 * {@inheritdoc}
580
+	 */
581
+	public function typeClass(): int
582
+	{
583
+		return $this->_element->typeClass();
584
+	}
585
+
586
+	/**
587
+	 * {@inheritdoc}
588
+	 */
589
+	public function isConstructed(): bool
590
+	{
591
+		return $this->_element->isConstructed();
592
+	}
593
+
594
+	/**
595
+	 * {@inheritdoc}
596
+	 */
597
+	public function tag(): int
598
+	{
599
+		return $this->_element->tag();
600
+	}
601
+
602
+	/**
603
+	 * {@inheritdoc}
604
+	 */
605
+	public function isType(int $tag): bool
606
+	{
607
+		return $this->_element->isType($tag);
608
+	}
609
+
610
+	/**
611
+	 * {@inheritdoc}
612
+	 *
613
+	 * Consider using any of the <code>as*</code> accessor methods instead.
614
+	 */
615
+	public function expectType(int $tag): ElementBase
616
+	{
617
+		return $this->_element->expectType($tag);
618
+	}
619
+
620
+	/**
621
+	 * {@inheritdoc}
622
+	 */
623
+	public function isTagged(): bool
624
+	{
625
+		return $this->_element->isTagged();
626
+	}
627
+
628
+	/**
629
+	 * {@inheritdoc}
630
+	 *
631
+	 * Consider using <code>asTagged()</code> method instead and chaining
632
+	 * with <code>TaggedType::asExplicit()</code> or
633
+	 * <code>TaggedType::asImplicit()</code>.
634
+	 */
635
+	public function expectTagged(?int $tag = null): TaggedType
636
+	{
637
+		return $this->_element->expectTagged($tag);
638
+	}
639
+
640
+	/**
641
+	 * {@inheritdoc}
642
+	 */
643
+	public function asElement(): Element
644
+	{
645
+		return $this->_element;
646
+	}
647
+
648
+	/**
649
+	 * {@inheritdoc}
650
+	 */
651
+	public function asUnspecified(): UnspecifiedType
652
+	{
653
+		return $this;
654
+	}
655
+
656
+	/**
657
+	 * Generate message for exceptions thrown by <code>as*</code> methods.
658
+	 *
659
+	 * @param int $tag Type tag of the expected element
660
+	 *
661
+	 * @return string
662
+	 */
663
+	private function _generateExceptionMessage(int $tag): string
664
+	{
665
+		return sprintf('%s expected, got %s.', Element::tagToName($tag),
666
+			$this->_typeDescriptorString());
667
+	}
668
+
669
+	/**
670
+	 * Get textual description of the wrapped element for debugging purposes.
671
+	 *
672
+	 * @return string
673
+	 */
674
+	private function _typeDescriptorString(): string
675
+	{
676
+		$type_cls = $this->_element->typeClass();
677
+		$tag = $this->_element->tag();
678
+		if (Identifier::CLASS_UNIVERSAL == $type_cls) {
679
+			return Element::tagToName($tag);
680
+		}
681
+		return Identifier::classToName($type_cls) . " TAG {$tag}";
682
+	}
683 683
 }
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   +100 added lines, -100 removed lines patch added patch discarded remove patch
@@ -17,110 +17,110 @@
 block discarded – undo
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
 }
Please login to merge, or discard this patch.
lib/ASN1/Util/Flags.php 2 patches
Indentation   +137 added lines, -137 removed lines patch added patch discarded remove patch
@@ -11,149 +11,149 @@
 block discarded – undo
11 11
  */
12 12
 class Flags
13 13
 {
14
-    /**
15
-     * Flag octets.
16
-     *
17
-     * @var string
18
-     */
19
-    protected $_flags;
14
+	/**
15
+	 * Flag octets.
16
+	 *
17
+	 * @var string
18
+	 */
19
+	protected $_flags;
20 20
 
21
-    /**
22
-     * Number of flags.
23
-     *
24
-     * @var int
25
-     */
26
-    protected $_width;
21
+	/**
22
+	 * Number of flags.
23
+	 *
24
+	 * @var int
25
+	 */
26
+	protected $_width;
27 27
 
28
-    /**
29
-     * Constructor.
30
-     *
31
-     * @param int|string $flags Flags
32
-     * @param int        $width The number of flags. If width is larger than
33
-     *                          number of bits in $flags, zeroes are prepended
34
-     *                          to flag field.
35
-     */
36
-    public function __construct($flags, int $width)
37
-    {
38
-        if (!$width) {
39
-            $this->_flags = '';
40
-        } else {
41
-            // calculate number of unused bits in last octet
42
-            $last_octet_bits = $width % 8;
43
-            $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0;
44
-            $num = gmp_init($flags);
45
-            // mask bits outside bitfield width
46
-            $mask = gmp_sub(gmp_init(1) << $width, 1);
47
-            $num &= $mask;
48
-            // shift towards MSB if needed
49
-            $data = gmp_export($num << $unused_bits, 1,
50
-                GMP_MSW_FIRST | GMP_BIG_ENDIAN);
51
-            $octets = unpack('C*', $data);
52
-            assert(is_array($octets), new \RuntimeException('unpack() failed'));
53
-            $bits = count($octets) * 8;
54
-            // pad with zeroes
55
-            while ($bits < $width) {
56
-                array_unshift($octets, 0);
57
-                $bits += 8;
58
-            }
59
-            $this->_flags = pack('C*', ...$octets);
60
-        }
61
-        $this->_width = $width;
62
-    }
28
+	/**
29
+	 * Constructor.
30
+	 *
31
+	 * @param int|string $flags Flags
32
+	 * @param int        $width The number of flags. If width is larger than
33
+	 *                          number of bits in $flags, zeroes are prepended
34
+	 *                          to flag field.
35
+	 */
36
+	public function __construct($flags, int $width)
37
+	{
38
+		if (!$width) {
39
+			$this->_flags = '';
40
+		} else {
41
+			// calculate number of unused bits in last octet
42
+			$last_octet_bits = $width % 8;
43
+			$unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0;
44
+			$num = gmp_init($flags);
45
+			// mask bits outside bitfield width
46
+			$mask = gmp_sub(gmp_init(1) << $width, 1);
47
+			$num &= $mask;
48
+			// shift towards MSB if needed
49
+			$data = gmp_export($num << $unused_bits, 1,
50
+				GMP_MSW_FIRST | GMP_BIG_ENDIAN);
51
+			$octets = unpack('C*', $data);
52
+			assert(is_array($octets), new \RuntimeException('unpack() failed'));
53
+			$bits = count($octets) * 8;
54
+			// pad with zeroes
55
+			while ($bits < $width) {
56
+				array_unshift($octets, 0);
57
+				$bits += 8;
58
+			}
59
+			$this->_flags = pack('C*', ...$octets);
60
+		}
61
+		$this->_width = $width;
62
+	}
63 63
 
64
-    /**
65
-     * Initialize from BitString.
66
-     *
67
-     * @param BitString $bs
68
-     * @param int       $width
69
-     *
70
-     * @return self
71
-     */
72
-    public static function fromBitString(BitString $bs, int $width): self
73
-    {
74
-        $num_bits = $bs->numBits();
75
-        $num = gmp_import($bs->string(), 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN);
76
-        $num >>= $bs->unusedBits();
77
-        if ($num_bits < $width) {
78
-            $num <<= ($width - $num_bits);
79
-        }
80
-        return new self(gmp_strval($num, 10), $width);
81
-    }
64
+	/**
65
+	 * Initialize from BitString.
66
+	 *
67
+	 * @param BitString $bs
68
+	 * @param int       $width
69
+	 *
70
+	 * @return self
71
+	 */
72
+	public static function fromBitString(BitString $bs, int $width): self
73
+	{
74
+		$num_bits = $bs->numBits();
75
+		$num = gmp_import($bs->string(), 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN);
76
+		$num >>= $bs->unusedBits();
77
+		if ($num_bits < $width) {
78
+			$num <<= ($width - $num_bits);
79
+		}
80
+		return new self(gmp_strval($num, 10), $width);
81
+	}
82 82
 
83
-    /**
84
-     * Check whether a bit at given index is set.
85
-     *
86
-     * Index 0 is the leftmost bit.
87
-     *
88
-     * @param int $idx
89
-     *
90
-     * @throws \OutOfBoundsException
91
-     *
92
-     * @return bool
93
-     */
94
-    public function test(int $idx): bool
95
-    {
96
-        if ($idx >= $this->_width) {
97
-            throw new \OutOfBoundsException('Index is out of bounds.');
98
-        }
99
-        // octet index
100
-        $oi = (int) floor($idx / 8);
101
-        $byte = $this->_flags[$oi];
102
-        // bit index
103
-        $bi = $idx % 8;
104
-        // index 0 is the most significant bit in byte
105
-        $mask = 0x01 << (7 - $bi);
106
-        return (ord($byte) & $mask) > 0;
107
-    }
83
+	/**
84
+	 * Check whether a bit at given index is set.
85
+	 *
86
+	 * Index 0 is the leftmost bit.
87
+	 *
88
+	 * @param int $idx
89
+	 *
90
+	 * @throws \OutOfBoundsException
91
+	 *
92
+	 * @return bool
93
+	 */
94
+	public function test(int $idx): bool
95
+	{
96
+		if ($idx >= $this->_width) {
97
+			throw new \OutOfBoundsException('Index is out of bounds.');
98
+		}
99
+		// octet index
100
+		$oi = (int) floor($idx / 8);
101
+		$byte = $this->_flags[$oi];
102
+		// bit index
103
+		$bi = $idx % 8;
104
+		// index 0 is the most significant bit in byte
105
+		$mask = 0x01 << (7 - $bi);
106
+		return (ord($byte) & $mask) > 0;
107
+	}
108 108
 
109
-    /**
110
-     * Get flags as an octet string.
111
-     *
112
-     * Zeroes are appended to the last octet if width is not divisible by 8.
113
-     *
114
-     * @return string
115
-     */
116
-    public function string(): string
117
-    {
118
-        return $this->_flags;
119
-    }
109
+	/**
110
+	 * Get flags as an octet string.
111
+	 *
112
+	 * Zeroes are appended to the last octet if width is not divisible by 8.
113
+	 *
114
+	 * @return string
115
+	 */
116
+	public function string(): string
117
+	{
118
+		return $this->_flags;
119
+	}
120 120
 
121
-    /**
122
-     * Get flags as a base 10 integer.
123
-     *
124
-     * @return string Integer as a string
125
-     */
126
-    public function number(): string
127
-    {
128
-        $num = gmp_import($this->_flags, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN);
129
-        $last_octet_bits = $this->_width % 8;
130
-        $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0;
131
-        $num >>= $unused_bits;
132
-        return gmp_strval($num, 10);
133
-    }
121
+	/**
122
+	 * Get flags as a base 10 integer.
123
+	 *
124
+	 * @return string Integer as a string
125
+	 */
126
+	public function number(): string
127
+	{
128
+		$num = gmp_import($this->_flags, 1, GMP_MSW_FIRST | GMP_BIG_ENDIAN);
129
+		$last_octet_bits = $this->_width % 8;
130
+		$unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0;
131
+		$num >>= $unused_bits;
132
+		return gmp_strval($num, 10);
133
+	}
134 134
 
135
-    /**
136
-     * Get flags as an integer.
137
-     *
138
-     * @return int
139
-     */
140
-    public function intNumber(): int
141
-    {
142
-        $num = new BigInt($this->number());
143
-        return $num->intVal();
144
-    }
135
+	/**
136
+	 * Get flags as an integer.
137
+	 *
138
+	 * @return int
139
+	 */
140
+	public function intNumber(): int
141
+	{
142
+		$num = new BigInt($this->number());
143
+		return $num->intVal();
144
+	}
145 145
 
146
-    /**
147
-     * Get flags as a BitString.
148
-     *
149
-     * Unused bits are set accordingly. Trailing zeroes are not stripped.
150
-     *
151
-     * @return BitString
152
-     */
153
-    public function bitString(): BitString
154
-    {
155
-        $last_octet_bits = $this->_width % 8;
156
-        $unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0;
157
-        return new BitString($this->_flags, $unused_bits);
158
-    }
146
+	/**
147
+	 * Get flags as a BitString.
148
+	 *
149
+	 * Unused bits are set accordingly. Trailing zeroes are not stripped.
150
+	 *
151
+	 * @return BitString
152
+	 */
153
+	public function bitString(): BitString
154
+	{
155
+		$last_octet_bits = $this->_width % 8;
156
+		$unused_bits = $last_octet_bits ? 8 - $last_octet_bits : 0;
157
+		return new BitString($this->_flags, $unused_bits);
158
+	}
159 159
 }
Please login to merge, or discard this 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\Util;
6 6
 
Please login to merge, or discard this patch.