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 — php70 ( ea86b1...e15417 )
by Joni
02:02
created

UnspecifiedType::asBoolean()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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