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 ( 516cbf...fabfc0 )
by Joni
03:39
created

UnspecifiedType::asApplication()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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