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