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