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