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