@@ -16,682 +16,682 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class UnspecifiedType implements ElementBase |
| 18 | 18 | { |
| 19 | - /** |
|
| 20 | - * The wrapped element. |
|
| 21 | - * |
|
| 22 | - * @var Element |
|
| 23 | - */ |
|
| 24 | - private $_element; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * Constructor. |
|
| 28 | - * |
|
| 29 | - * @param Element $el |
|
| 30 | - */ |
|
| 31 | - public function __construct(Element $el) |
|
| 32 | - { |
|
| 33 | - $this->_element = $el; |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * Initialize from DER data. |
|
| 38 | - * |
|
| 39 | - * @param string $data DER encoded data |
|
| 40 | - * @return self |
|
| 41 | - */ |
|
| 42 | - public static function fromDER(string $data): self |
|
| 43 | - { |
|
| 44 | - return Element::fromDER($data)->asUnspecified(); |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Initialize from ElementBase interface. |
|
| 49 | - * |
|
| 50 | - * @param ElementBase $el |
|
| 51 | - * @return self |
|
| 52 | - */ |
|
| 53 | - public static function fromElementBase(ElementBase $el): self |
|
| 54 | - { |
|
| 55 | - // if element is already wrapped |
|
| 56 | - if ($el instanceof self) { |
|
| 57 | - return $el; |
|
| 58 | - } |
|
| 59 | - return new self($el->asElement()); |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Compatibility method to dispatch calls to the wrapped element. |
|
| 64 | - * |
|
| 65 | - * @deprecated Use <code>as*</code> accessor methods to ensure strict type |
|
| 66 | - * @param string $mtd Method name |
|
| 67 | - * @param array $args Arguments |
|
| 68 | - * @return mixed |
|
| 69 | - */ |
|
| 70 | - public function __call($mtd, array $args) |
|
| 71 | - { |
|
| 72 | - return call_user_func_array([$this->_element, $mtd], $args); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - /** |
|
| 76 | - * Get the wrapped element as a context specific tagged type. |
|
| 77 | - * |
|
| 78 | - * @throws \UnexpectedValueException If the element is not tagged |
|
| 79 | - * @return TaggedType |
|
| 80 | - */ |
|
| 81 | - public function asTagged(): TaggedType |
|
| 82 | - { |
|
| 83 | - if (!$this->_element instanceof TaggedType) { |
|
| 84 | - throw new \UnexpectedValueException( |
|
| 85 | - "Tagged element expected, got " . $this->_typeDescriptorString()); |
|
| 86 | - } |
|
| 87 | - return $this->_element; |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - /** |
|
| 91 | - * Get the wrapped element as an application specific type. |
|
| 92 | - * |
|
| 93 | - * @throws \UnexpectedValueException If element is not application specific |
|
| 94 | - * @return \ASN1\Type\Tagged\ApplicationType |
|
| 95 | - */ |
|
| 96 | - public function asApplication(): Tagged\ApplicationType |
|
| 97 | - { |
|
| 98 | - if (!$this->_element instanceof Tagged\ApplicationType) { |
|
| 99 | - throw new \UnexpectedValueException( |
|
| 100 | - "Application type expected, got " . |
|
| 101 | - $this->_typeDescriptorString()); |
|
| 102 | - } |
|
| 103 | - return $this->_element; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * Get the wrapped element as a private tagged type. |
|
| 108 | - * |
|
| 109 | - * @throws \UnexpectedValueException If element is not using private tagging |
|
| 110 | - * @return \ASN1\Type\Tagged\PrivateType |
|
| 111 | - */ |
|
| 112 | - public function asPrivate(): Tagged\PrivateType |
|
| 113 | - { |
|
| 114 | - if (!$this->_element instanceof Tagged\PrivateType) { |
|
| 115 | - throw new \UnexpectedValueException( |
|
| 116 | - "Private type expected, got " . $this->_typeDescriptorString()); |
|
| 117 | - } |
|
| 118 | - return $this->_element; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Get the wrapped element as a boolean type. |
|
| 123 | - * |
|
| 124 | - * @throws \UnexpectedValueException If the element is not a boolean |
|
| 125 | - * @return \ASN1\Type\Primitive\Boolean |
|
| 126 | - */ |
|
| 127 | - public function asBoolean(): Primitive\Boolean |
|
| 128 | - { |
|
| 129 | - if (!$this->_element instanceof Primitive\Boolean) { |
|
| 130 | - throw new \UnexpectedValueException( |
|
| 131 | - $this->_generateExceptionMessage(Element::TYPE_BOOLEAN)); |
|
| 132 | - } |
|
| 133 | - return $this->_element; |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - /** |
|
| 137 | - * Get the wrapped element as an integer type. |
|
| 138 | - * |
|
| 139 | - * @throws \UnexpectedValueException If the element is not an integer |
|
| 140 | - * @return \ASN1\Type\Primitive\Integer |
|
| 141 | - */ |
|
| 142 | - public function asInteger(): Primitive\Integer |
|
| 143 | - { |
|
| 144 | - if (!$this->_element instanceof Primitive\Integer) { |
|
| 145 | - throw new \UnexpectedValueException( |
|
| 146 | - $this->_generateExceptionMessage(Element::TYPE_INTEGER)); |
|
| 147 | - } |
|
| 148 | - return $this->_element; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - /** |
|
| 152 | - * Get the wrapped element as a bit string type. |
|
| 153 | - * |
|
| 154 | - * @throws \UnexpectedValueException If the element is not a bit string |
|
| 155 | - * @return \ASN1\Type\Primitive\BitString |
|
| 156 | - */ |
|
| 157 | - public function asBitString(): Primitive\BitString |
|
| 158 | - { |
|
| 159 | - if (!$this->_element instanceof Primitive\BitString) { |
|
| 160 | - throw new \UnexpectedValueException( |
|
| 161 | - $this->_generateExceptionMessage(Element::TYPE_BIT_STRING)); |
|
| 162 | - } |
|
| 163 | - return $this->_element; |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * Get the wrapped element as an octet string type. |
|
| 168 | - * |
|
| 169 | - * @throws \UnexpectedValueException If the element is not an octet string |
|
| 170 | - * @return \ASN1\Type\Primitive\OctetString |
|
| 171 | - */ |
|
| 172 | - public function asOctetString(): Primitive\OctetString |
|
| 173 | - { |
|
| 174 | - if (!$this->_element instanceof Primitive\OctetString) { |
|
| 175 | - throw new \UnexpectedValueException( |
|
| 176 | - $this->_generateExceptionMessage(Element::TYPE_OCTET_STRING)); |
|
| 177 | - } |
|
| 178 | - return $this->_element; |
|
| 179 | - } |
|
| 180 | - |
|
| 181 | - /** |
|
| 182 | - * Get the wrapped element as a null type. |
|
| 183 | - * |
|
| 184 | - * @throws \UnexpectedValueException If the element is not a null |
|
| 185 | - * @return \ASN1\Type\Primitive\NullType |
|
| 186 | - */ |
|
| 187 | - public function asNull(): Primitive\NullType |
|
| 188 | - { |
|
| 189 | - if (!$this->_element instanceof Primitive\NullType) { |
|
| 190 | - throw new \UnexpectedValueException( |
|
| 191 | - $this->_generateExceptionMessage(Element::TYPE_NULL)); |
|
| 192 | - } |
|
| 193 | - return $this->_element; |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - /** |
|
| 197 | - * Get the wrapped element as an object identifier type. |
|
| 198 | - * |
|
| 199 | - * @throws \UnexpectedValueException If the element is not an object |
|
| 200 | - * identifier |
|
| 201 | - * @return \ASN1\Type\Primitive\ObjectIdentifier |
|
| 202 | - */ |
|
| 203 | - public function asObjectIdentifier(): Primitive\ObjectIdentifier |
|
| 204 | - { |
|
| 205 | - if (!$this->_element instanceof Primitive\ObjectIdentifier) { |
|
| 206 | - throw new \UnexpectedValueException( |
|
| 207 | - $this->_generateExceptionMessage( |
|
| 208 | - Element::TYPE_OBJECT_IDENTIFIER)); |
|
| 209 | - } |
|
| 210 | - return $this->_element; |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - /** |
|
| 214 | - * Get the wrapped element as an object descriptor type. |
|
| 215 | - * |
|
| 216 | - * @throws \UnexpectedValueException If the element is not an object |
|
| 217 | - * descriptor |
|
| 218 | - * @return \ASN1\Type\Primitive\ObjectDescriptor |
|
| 219 | - */ |
|
| 220 | - public function asObjectDescriptor(): Primitive\ObjectDescriptor |
|
| 221 | - { |
|
| 222 | - if (!$this->_element instanceof Primitive\ObjectDescriptor) { |
|
| 223 | - throw new \UnexpectedValueException( |
|
| 224 | - $this->_generateExceptionMessage( |
|
| 225 | - Element::TYPE_OBJECT_DESCRIPTOR)); |
|
| 226 | - } |
|
| 227 | - return $this->_element; |
|
| 228 | - } |
|
| 229 | - |
|
| 230 | - /** |
|
| 231 | - * Get the wrapped element as a real type. |
|
| 232 | - * |
|
| 233 | - * @throws \UnexpectedValueException If the element is not a real |
|
| 234 | - * @return \ASN1\Type\Primitive\Real |
|
| 235 | - */ |
|
| 236 | - public function asReal(): Primitive\Real |
|
| 237 | - { |
|
| 238 | - if (!$this->_element instanceof Primitive\Real) { |
|
| 239 | - throw new \UnexpectedValueException( |
|
| 240 | - $this->_generateExceptionMessage(Element::TYPE_REAL)); |
|
| 241 | - } |
|
| 242 | - return $this->_element; |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - /** |
|
| 246 | - * Get the wrapped element as an enumerated type. |
|
| 247 | - * |
|
| 248 | - * @throws \UnexpectedValueException If the element is not an enumerated |
|
| 249 | - * @return \ASN1\Type\Primitive\Enumerated |
|
| 250 | - */ |
|
| 251 | - public function asEnumerated(): Primitive\Enumerated |
|
| 252 | - { |
|
| 253 | - if (!$this->_element instanceof Primitive\Enumerated) { |
|
| 254 | - throw new \UnexpectedValueException( |
|
| 255 | - $this->_generateExceptionMessage(Element::TYPE_ENUMERATED)); |
|
| 256 | - } |
|
| 257 | - return $this->_element; |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - /** |
|
| 261 | - * Get the wrapped element as a UTF8 string type. |
|
| 262 | - * |
|
| 263 | - * @throws \UnexpectedValueException If the element is not a UTF8 string |
|
| 264 | - * @return \ASN1\Type\Primitive\UTF8String |
|
| 265 | - */ |
|
| 266 | - public function asUTF8String(): Primitive\UTF8String |
|
| 267 | - { |
|
| 268 | - if (!$this->_element instanceof Primitive\UTF8String) { |
|
| 269 | - throw new \UnexpectedValueException( |
|
| 270 | - $this->_generateExceptionMessage(Element::TYPE_UTF8_STRING)); |
|
| 271 | - } |
|
| 272 | - return $this->_element; |
|
| 273 | - } |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * Get the wrapped element as a relative OID type. |
|
| 277 | - * |
|
| 278 | - * @throws \UnexpectedValueException If the element is not a relative OID |
|
| 279 | - * @return \ASN1\Type\Primitive\RelativeOID |
|
| 280 | - */ |
|
| 281 | - public function asRelativeOID(): Primitive\RelativeOID |
|
| 282 | - { |
|
| 283 | - if (!$this->_element instanceof Primitive\RelativeOID) { |
|
| 284 | - throw new \UnexpectedValueException( |
|
| 285 | - $this->_generateExceptionMessage(Element::TYPE_RELATIVE_OID)); |
|
| 286 | - } |
|
| 287 | - return $this->_element; |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * Get the wrapped element as a sequence type. |
|
| 292 | - * |
|
| 293 | - * @throws \UnexpectedValueException If the element is not a sequence |
|
| 294 | - * @return \ASN1\Type\Constructed\Sequence |
|
| 295 | - */ |
|
| 296 | - public function asSequence(): Constructed\Sequence |
|
| 297 | - { |
|
| 298 | - if (!$this->_element instanceof Constructed\Sequence) { |
|
| 299 | - throw new \UnexpectedValueException( |
|
| 300 | - $this->_generateExceptionMessage(Element::TYPE_SEQUENCE)); |
|
| 301 | - } |
|
| 302 | - return $this->_element; |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - /** |
|
| 306 | - * Get the wrapped element as a set type. |
|
| 307 | - * |
|
| 308 | - * @throws \UnexpectedValueException If the element is not a set |
|
| 309 | - * @return \ASN1\Type\Constructed\Set |
|
| 310 | - */ |
|
| 311 | - public function asSet(): Constructed\Set |
|
| 312 | - { |
|
| 313 | - if (!$this->_element instanceof Constructed\Set) { |
|
| 314 | - throw new \UnexpectedValueException( |
|
| 315 | - $this->_generateExceptionMessage(Element::TYPE_SET)); |
|
| 316 | - } |
|
| 317 | - return $this->_element; |
|
| 318 | - } |
|
| 319 | - |
|
| 320 | - /** |
|
| 321 | - * Get the wrapped element as a numeric string type. |
|
| 322 | - * |
|
| 323 | - * @throws \UnexpectedValueException If the element is not a numeric string |
|
| 324 | - * @return \ASN1\Type\Primitive\NumericString |
|
| 325 | - */ |
|
| 326 | - public function asNumericString(): Primitive\NumericString |
|
| 327 | - { |
|
| 328 | - if (!$this->_element instanceof Primitive\NumericString) { |
|
| 329 | - throw new \UnexpectedValueException( |
|
| 330 | - $this->_generateExceptionMessage(Element::TYPE_NUMERIC_STRING)); |
|
| 331 | - } |
|
| 332 | - return $this->_element; |
|
| 333 | - } |
|
| 334 | - |
|
| 335 | - /** |
|
| 336 | - * Get the wrapped element as a printable string type. |
|
| 337 | - * |
|
| 338 | - * @throws \UnexpectedValueException If the element is not a printable |
|
| 339 | - * string |
|
| 340 | - * @return \ASN1\Type\Primitive\PrintableString |
|
| 341 | - */ |
|
| 342 | - public function asPrintableString(): Primitive\PrintableString |
|
| 343 | - { |
|
| 344 | - if (!$this->_element instanceof Primitive\PrintableString) { |
|
| 345 | - throw new \UnexpectedValueException( |
|
| 346 | - $this->_generateExceptionMessage(Element::TYPE_PRINTABLE_STRING)); |
|
| 347 | - } |
|
| 348 | - return $this->_element; |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - /** |
|
| 352 | - * Get the wrapped element as a T61 string type. |
|
| 353 | - * |
|
| 354 | - * @throws \UnexpectedValueException If the element is not a T61 string |
|
| 355 | - * @return \ASN1\Type\Primitive\T61String |
|
| 356 | - */ |
|
| 357 | - public function asT61String(): Primitive\T61String |
|
| 358 | - { |
|
| 359 | - if (!$this->_element instanceof Primitive\T61String) { |
|
| 360 | - throw new \UnexpectedValueException( |
|
| 361 | - $this->_generateExceptionMessage(Element::TYPE_T61_STRING)); |
|
| 362 | - } |
|
| 363 | - return $this->_element; |
|
| 364 | - } |
|
| 365 | - |
|
| 366 | - /** |
|
| 367 | - * Get the wrapped element as a videotex string type. |
|
| 368 | - * |
|
| 369 | - * @throws \UnexpectedValueException If the element is not a videotex string |
|
| 370 | - * @return \ASN1\Type\Primitive\VideotexString |
|
| 371 | - */ |
|
| 372 | - public function asVideotexString(): Primitive\VideotexString |
|
| 373 | - { |
|
| 374 | - if (!$this->_element instanceof Primitive\VideotexString) { |
|
| 375 | - throw new \UnexpectedValueException( |
|
| 376 | - $this->_generateExceptionMessage(Element::TYPE_VIDEOTEX_STRING)); |
|
| 377 | - } |
|
| 378 | - return $this->_element; |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - /** |
|
| 382 | - * Get the wrapped element as a IA5 string type. |
|
| 383 | - * |
|
| 384 | - * @throws \UnexpectedValueException If the element is not a IA5 string |
|
| 385 | - * @return \ASN1\Type\Primitive\IA5String |
|
| 386 | - */ |
|
| 387 | - public function asIA5String(): Primitive\IA5String |
|
| 388 | - { |
|
| 389 | - if (!$this->_element instanceof Primitive\IA5String) { |
|
| 390 | - throw new \UnexpectedValueException( |
|
| 391 | - $this->_generateExceptionMessage(Element::TYPE_IA5_STRING)); |
|
| 392 | - } |
|
| 393 | - return $this->_element; |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - /** |
|
| 397 | - * Get the wrapped element as an UTC time type. |
|
| 398 | - * |
|
| 399 | - * @throws \UnexpectedValueException If the element is not a UTC time |
|
| 400 | - * @return \ASN1\Type\Primitive\UTCTime |
|
| 401 | - */ |
|
| 402 | - public function asUTCTime(): Primitive\UTCTime |
|
| 403 | - { |
|
| 404 | - if (!$this->_element instanceof Primitive\UTCTime) { |
|
| 405 | - throw new \UnexpectedValueException( |
|
| 406 | - $this->_generateExceptionMessage(Element::TYPE_UTC_TIME)); |
|
| 407 | - } |
|
| 408 | - return $this->_element; |
|
| 409 | - } |
|
| 410 | - |
|
| 411 | - /** |
|
| 412 | - * Get the wrapped element as a generalized time type. |
|
| 413 | - * |
|
| 414 | - * @throws \UnexpectedValueException If the element is not a generalized |
|
| 415 | - * time |
|
| 416 | - * @return \ASN1\Type\Primitive\GeneralizedTime |
|
| 417 | - */ |
|
| 418 | - public function asGeneralizedTime(): Primitive\GeneralizedTime |
|
| 419 | - { |
|
| 420 | - if (!$this->_element instanceof Primitive\GeneralizedTime) { |
|
| 421 | - throw new \UnexpectedValueException( |
|
| 422 | - $this->_generateExceptionMessage(Element::TYPE_GENERALIZED_TIME)); |
|
| 423 | - } |
|
| 424 | - return $this->_element; |
|
| 425 | - } |
|
| 426 | - |
|
| 427 | - /** |
|
| 428 | - * Get the wrapped element as a graphic string type. |
|
| 429 | - * |
|
| 430 | - * @throws \UnexpectedValueException If the element is not a graphic string |
|
| 431 | - * @return \ASN1\Type\Primitive\GraphicString |
|
| 432 | - */ |
|
| 433 | - public function asGraphicString(): Primitive\GraphicString |
|
| 434 | - { |
|
| 435 | - if (!$this->_element instanceof Primitive\GraphicString) { |
|
| 436 | - throw new \UnexpectedValueException( |
|
| 437 | - $this->_generateExceptionMessage(Element::TYPE_GRAPHIC_STRING)); |
|
| 438 | - } |
|
| 439 | - return $this->_element; |
|
| 440 | - } |
|
| 441 | - |
|
| 442 | - /** |
|
| 443 | - * Get the wrapped element as a visible string type. |
|
| 444 | - * |
|
| 445 | - * @throws \UnexpectedValueException If the element is not a visible string |
|
| 446 | - * @return \ASN1\Type\Primitive\VisibleString |
|
| 447 | - */ |
|
| 448 | - public function asVisibleString(): Primitive\VisibleString |
|
| 449 | - { |
|
| 450 | - if (!$this->_element instanceof Primitive\VisibleString) { |
|
| 451 | - throw new \UnexpectedValueException( |
|
| 452 | - $this->_generateExceptionMessage(Element::TYPE_VISIBLE_STRING)); |
|
| 453 | - } |
|
| 454 | - return $this->_element; |
|
| 455 | - } |
|
| 456 | - |
|
| 457 | - /** |
|
| 458 | - * Get the wrapped element as a general string type. |
|
| 459 | - * |
|
| 460 | - * @throws \UnexpectedValueException If the element is not general string |
|
| 461 | - * @return \ASN1\Type\Primitive\GeneralString |
|
| 462 | - */ |
|
| 463 | - public function asGeneralString(): Primitive\GeneralString |
|
| 464 | - { |
|
| 465 | - if (!$this->_element instanceof Primitive\GeneralString) { |
|
| 466 | - throw new \UnexpectedValueException( |
|
| 467 | - $this->_generateExceptionMessage(Element::TYPE_GENERAL_STRING)); |
|
| 468 | - } |
|
| 469 | - return $this->_element; |
|
| 470 | - } |
|
| 471 | - |
|
| 472 | - /** |
|
| 473 | - * Get the wrapped element as a universal string type. |
|
| 474 | - * |
|
| 475 | - * @throws \UnexpectedValueException If the element is not a universal |
|
| 476 | - * string |
|
| 477 | - * @return \ASN1\Type\Primitive\UniversalString |
|
| 478 | - */ |
|
| 479 | - public function asUniversalString(): Primitive\UniversalString |
|
| 480 | - { |
|
| 481 | - if (!$this->_element instanceof Primitive\UniversalString) { |
|
| 482 | - throw new \UnexpectedValueException( |
|
| 483 | - $this->_generateExceptionMessage(Element::TYPE_UNIVERSAL_STRING)); |
|
| 484 | - } |
|
| 485 | - return $this->_element; |
|
| 486 | - } |
|
| 487 | - |
|
| 488 | - /** |
|
| 489 | - * Get the wrapped element as a character string type. |
|
| 490 | - * |
|
| 491 | - * @throws \UnexpectedValueException If the element is not a character |
|
| 492 | - * string |
|
| 493 | - * @return \ASN1\Type\Primitive\CharacterString |
|
| 494 | - */ |
|
| 495 | - public function asCharacterString(): Primitive\CharacterString |
|
| 496 | - { |
|
| 497 | - if (!$this->_element instanceof Primitive\CharacterString) { |
|
| 498 | - throw new \UnexpectedValueException( |
|
| 499 | - $this->_generateExceptionMessage(Element::TYPE_CHARACTER_STRING)); |
|
| 500 | - } |
|
| 501 | - return $this->_element; |
|
| 502 | - } |
|
| 503 | - |
|
| 504 | - /** |
|
| 505 | - * Get the wrapped element as a BMP string type. |
|
| 506 | - * |
|
| 507 | - * @throws \UnexpectedValueException If the element is not a bmp string |
|
| 508 | - * @return \ASN1\Type\Primitive\BMPString |
|
| 509 | - */ |
|
| 510 | - public function asBMPString(): Primitive\BMPString |
|
| 511 | - { |
|
| 512 | - if (!$this->_element instanceof Primitive\BMPString) { |
|
| 513 | - throw new \UnexpectedValueException( |
|
| 514 | - $this->_generateExceptionMessage(Element::TYPE_BMP_STRING)); |
|
| 515 | - } |
|
| 516 | - 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 |
|
| 523 | - * string |
|
| 524 | - * @return Constructed\ConstructedString |
|
| 525 | - */ |
|
| 526 | - public function asConstructedString(): Constructed\ConstructedString |
|
| 527 | - { |
|
| 528 | - if (!$this->_element instanceof Constructed\ConstructedString) { |
|
| 529 | - throw new \UnexpectedValueException( |
|
| 530 | - $this->_generateExceptionMessage( |
|
| 531 | - Element::TYPE_CONSTRUCTED_STRING)); |
|
| 532 | - } |
|
| 533 | - return $this->_element; |
|
| 534 | - } |
|
| 535 | - |
|
| 536 | - /** |
|
| 537 | - * Get the wrapped element as any string type. |
|
| 538 | - * |
|
| 539 | - * @throws \UnexpectedValueException If the element is not a string |
|
| 540 | - * @return StringType |
|
| 541 | - */ |
|
| 542 | - public function asString(): StringType |
|
| 543 | - { |
|
| 544 | - if (!$this->_element instanceof StringType) { |
|
| 545 | - throw new \UnexpectedValueException( |
|
| 546 | - $this->_generateExceptionMessage(Element::TYPE_STRING)); |
|
| 547 | - } |
|
| 548 | - return $this->_element; |
|
| 549 | - } |
|
| 550 | - |
|
| 551 | - /** |
|
| 552 | - * Get the wrapped element as any time type. |
|
| 553 | - * |
|
| 554 | - * @throws \UnexpectedValueException If the element is not a time |
|
| 555 | - * @return TimeType |
|
| 556 | - */ |
|
| 557 | - public function asTime(): TimeType |
|
| 558 | - { |
|
| 559 | - if (!$this->_element instanceof TimeType) { |
|
| 560 | - throw new \UnexpectedValueException( |
|
| 561 | - $this->_generateExceptionMessage(Element::TYPE_TIME)); |
|
| 562 | - } |
|
| 563 | - return $this->_element; |
|
| 564 | - } |
|
| 565 | - |
|
| 566 | - /** |
|
| 567 | - * Generate message for exceptions thrown by <code>as*</code> methods. |
|
| 568 | - * |
|
| 569 | - * @param int $tag Type tag of the expected element |
|
| 570 | - * @return string |
|
| 571 | - */ |
|
| 572 | - private function _generateExceptionMessage(int $tag): string |
|
| 573 | - { |
|
| 574 | - return sprintf("%s expected, got %s.", Element::tagToName($tag), |
|
| 575 | - $this->_typeDescriptorString()); |
|
| 576 | - } |
|
| 577 | - |
|
| 578 | - /** |
|
| 579 | - * Get textual description of the wrapped element for debugging purposes. |
|
| 580 | - * |
|
| 581 | - * @return string |
|
| 582 | - */ |
|
| 583 | - private function _typeDescriptorString(): string |
|
| 584 | - { |
|
| 585 | - $type_cls = $this->_element->typeClass(); |
|
| 586 | - $tag = $this->_element->tag(); |
|
| 587 | - if ($type_cls == Identifier::CLASS_UNIVERSAL) { |
|
| 588 | - return Element::tagToName($tag); |
|
| 589 | - } |
|
| 590 | - return Identifier::classToName($type_cls) . " TAG $tag"; |
|
| 591 | - } |
|
| 592 | - |
|
| 593 | - /** |
|
| 594 | - * |
|
| 595 | - * @see \ASN1\Feature\Encodable::toDER() |
|
| 596 | - * @return string |
|
| 597 | - */ |
|
| 598 | - public function toDER(): string |
|
| 599 | - { |
|
| 600 | - return $this->_element->toDER(); |
|
| 601 | - } |
|
| 602 | - |
|
| 603 | - /** |
|
| 604 | - * |
|
| 605 | - * @see \ASN1\Feature\ElementBase::typeClass() |
|
| 606 | - * @return int |
|
| 607 | - */ |
|
| 608 | - public function typeClass(): int |
|
| 609 | - { |
|
| 610 | - return $this->_element->typeClass(); |
|
| 611 | - } |
|
| 612 | - |
|
| 613 | - /** |
|
| 614 | - * |
|
| 615 | - * @see \ASN1\Feature\ElementBase::isConstructed() |
|
| 616 | - * @return bool |
|
| 617 | - */ |
|
| 618 | - public function isConstructed(): bool |
|
| 619 | - { |
|
| 620 | - return $this->_element->isConstructed(); |
|
| 621 | - } |
|
| 622 | - |
|
| 623 | - /** |
|
| 624 | - * |
|
| 625 | - * @see \ASN1\Feature\ElementBase::tag() |
|
| 626 | - * @return int |
|
| 627 | - */ |
|
| 628 | - public function tag(): int |
|
| 629 | - { |
|
| 630 | - return $this->_element->tag(); |
|
| 631 | - } |
|
| 632 | - |
|
| 633 | - /** |
|
| 634 | - * |
|
| 635 | - * {@inheritdoc} |
|
| 636 | - * @see \ASN1\Feature\ElementBase::isType() |
|
| 637 | - * @return bool |
|
| 638 | - */ |
|
| 639 | - public function isType(int $tag): bool |
|
| 640 | - { |
|
| 641 | - return $this->_element->isType($tag); |
|
| 642 | - } |
|
| 643 | - |
|
| 644 | - /** |
|
| 645 | - * |
|
| 646 | - * @deprecated Use any <code>as*</code> accessor method first to ensure |
|
| 647 | - * type strictness. |
|
| 648 | - * @see \ASN1\Feature\ElementBase::expectType() |
|
| 649 | - * @return ElementBase |
|
| 650 | - */ |
|
| 651 | - public function expectType(int $tag): ElementBase |
|
| 652 | - { |
|
| 653 | - return $this->_element->expectType($tag); |
|
| 654 | - } |
|
| 655 | - |
|
| 656 | - /** |
|
| 657 | - * |
|
| 658 | - * @see \ASN1\Feature\ElementBase::isTagged() |
|
| 659 | - * @return bool |
|
| 660 | - */ |
|
| 661 | - public function isTagged(): bool |
|
| 662 | - { |
|
| 663 | - return $this->_element->isTagged(); |
|
| 664 | - } |
|
| 665 | - |
|
| 666 | - /** |
|
| 667 | - * |
|
| 668 | - * @deprecated Use any <code>as*</code> accessor method first to ensure |
|
| 669 | - * type strictness. |
|
| 670 | - * @see \ASN1\Feature\ElementBase::expectTagged() |
|
| 671 | - * @return TaggedType |
|
| 672 | - */ |
|
| 673 | - public function expectTagged($tag = null): TaggedType |
|
| 674 | - { |
|
| 675 | - return $this->_element->expectTagged($tag); |
|
| 676 | - } |
|
| 677 | - |
|
| 678 | - /** |
|
| 679 | - * |
|
| 680 | - * @see \ASN1\Feature\ElementBase::asElement() |
|
| 681 | - * @return Element |
|
| 682 | - */ |
|
| 683 | - public function asElement(): Element |
|
| 684 | - { |
|
| 685 | - return $this->_element; |
|
| 686 | - } |
|
| 687 | - |
|
| 688 | - /** |
|
| 689 | - * |
|
| 690 | - * {@inheritdoc} |
|
| 691 | - * @return UnspecifiedType |
|
| 692 | - */ |
|
| 693 | - public function asUnspecified(): UnspecifiedType |
|
| 694 | - { |
|
| 695 | - return $this; |
|
| 696 | - } |
|
| 19 | + /** |
|
| 20 | + * The wrapped element. |
|
| 21 | + * |
|
| 22 | + * @var Element |
|
| 23 | + */ |
|
| 24 | + private $_element; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * Constructor. |
|
| 28 | + * |
|
| 29 | + * @param Element $el |
|
| 30 | + */ |
|
| 31 | + public function __construct(Element $el) |
|
| 32 | + { |
|
| 33 | + $this->_element = $el; |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * Initialize from DER data. |
|
| 38 | + * |
|
| 39 | + * @param string $data DER encoded data |
|
| 40 | + * @return self |
|
| 41 | + */ |
|
| 42 | + public static function fromDER(string $data): self |
|
| 43 | + { |
|
| 44 | + return Element::fromDER($data)->asUnspecified(); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Initialize from ElementBase interface. |
|
| 49 | + * |
|
| 50 | + * @param ElementBase $el |
|
| 51 | + * @return self |
|
| 52 | + */ |
|
| 53 | + public static function fromElementBase(ElementBase $el): self |
|
| 54 | + { |
|
| 55 | + // if element is already wrapped |
|
| 56 | + if ($el instanceof self) { |
|
| 57 | + return $el; |
|
| 58 | + } |
|
| 59 | + return new self($el->asElement()); |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Compatibility method to dispatch calls to the wrapped element. |
|
| 64 | + * |
|
| 65 | + * @deprecated Use <code>as*</code> accessor methods to ensure strict type |
|
| 66 | + * @param string $mtd Method name |
|
| 67 | + * @param array $args Arguments |
|
| 68 | + * @return mixed |
|
| 69 | + */ |
|
| 70 | + public function __call($mtd, array $args) |
|
| 71 | + { |
|
| 72 | + return call_user_func_array([$this->_element, $mtd], $args); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + /** |
|
| 76 | + * Get the wrapped element as a context specific tagged type. |
|
| 77 | + * |
|
| 78 | + * @throws \UnexpectedValueException If the element is not tagged |
|
| 79 | + * @return TaggedType |
|
| 80 | + */ |
|
| 81 | + public function asTagged(): TaggedType |
|
| 82 | + { |
|
| 83 | + if (!$this->_element instanceof TaggedType) { |
|
| 84 | + throw new \UnexpectedValueException( |
|
| 85 | + "Tagged element expected, got " . $this->_typeDescriptorString()); |
|
| 86 | + } |
|
| 87 | + return $this->_element; |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + /** |
|
| 91 | + * Get the wrapped element as an application specific type. |
|
| 92 | + * |
|
| 93 | + * @throws \UnexpectedValueException If element is not application specific |
|
| 94 | + * @return \ASN1\Type\Tagged\ApplicationType |
|
| 95 | + */ |
|
| 96 | + public function asApplication(): Tagged\ApplicationType |
|
| 97 | + { |
|
| 98 | + if (!$this->_element instanceof Tagged\ApplicationType) { |
|
| 99 | + throw new \UnexpectedValueException( |
|
| 100 | + "Application type expected, got " . |
|
| 101 | + $this->_typeDescriptorString()); |
|
| 102 | + } |
|
| 103 | + return $this->_element; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * Get the wrapped element as a private tagged type. |
|
| 108 | + * |
|
| 109 | + * @throws \UnexpectedValueException If element is not using private tagging |
|
| 110 | + * @return \ASN1\Type\Tagged\PrivateType |
|
| 111 | + */ |
|
| 112 | + public function asPrivate(): Tagged\PrivateType |
|
| 113 | + { |
|
| 114 | + if (!$this->_element instanceof Tagged\PrivateType) { |
|
| 115 | + throw new \UnexpectedValueException( |
|
| 116 | + "Private type expected, got " . $this->_typeDescriptorString()); |
|
| 117 | + } |
|
| 118 | + return $this->_element; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Get the wrapped element as a boolean type. |
|
| 123 | + * |
|
| 124 | + * @throws \UnexpectedValueException If the element is not a boolean |
|
| 125 | + * @return \ASN1\Type\Primitive\Boolean |
|
| 126 | + */ |
|
| 127 | + public function asBoolean(): Primitive\Boolean |
|
| 128 | + { |
|
| 129 | + if (!$this->_element instanceof Primitive\Boolean) { |
|
| 130 | + throw new \UnexpectedValueException( |
|
| 131 | + $this->_generateExceptionMessage(Element::TYPE_BOOLEAN)); |
|
| 132 | + } |
|
| 133 | + return $this->_element; |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + /** |
|
| 137 | + * Get the wrapped element as an integer type. |
|
| 138 | + * |
|
| 139 | + * @throws \UnexpectedValueException If the element is not an integer |
|
| 140 | + * @return \ASN1\Type\Primitive\Integer |
|
| 141 | + */ |
|
| 142 | + public function asInteger(): Primitive\Integer |
|
| 143 | + { |
|
| 144 | + if (!$this->_element instanceof Primitive\Integer) { |
|
| 145 | + throw new \UnexpectedValueException( |
|
| 146 | + $this->_generateExceptionMessage(Element::TYPE_INTEGER)); |
|
| 147 | + } |
|
| 148 | + return $this->_element; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + /** |
|
| 152 | + * Get the wrapped element as a bit string type. |
|
| 153 | + * |
|
| 154 | + * @throws \UnexpectedValueException If the element is not a bit string |
|
| 155 | + * @return \ASN1\Type\Primitive\BitString |
|
| 156 | + */ |
|
| 157 | + public function asBitString(): Primitive\BitString |
|
| 158 | + { |
|
| 159 | + if (!$this->_element instanceof Primitive\BitString) { |
|
| 160 | + throw new \UnexpectedValueException( |
|
| 161 | + $this->_generateExceptionMessage(Element::TYPE_BIT_STRING)); |
|
| 162 | + } |
|
| 163 | + return $this->_element; |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * Get the wrapped element as an octet string type. |
|
| 168 | + * |
|
| 169 | + * @throws \UnexpectedValueException If the element is not an octet string |
|
| 170 | + * @return \ASN1\Type\Primitive\OctetString |
|
| 171 | + */ |
|
| 172 | + public function asOctetString(): Primitive\OctetString |
|
| 173 | + { |
|
| 174 | + if (!$this->_element instanceof Primitive\OctetString) { |
|
| 175 | + throw new \UnexpectedValueException( |
|
| 176 | + $this->_generateExceptionMessage(Element::TYPE_OCTET_STRING)); |
|
| 177 | + } |
|
| 178 | + return $this->_element; |
|
| 179 | + } |
|
| 180 | + |
|
| 181 | + /** |
|
| 182 | + * Get the wrapped element as a null type. |
|
| 183 | + * |
|
| 184 | + * @throws \UnexpectedValueException If the element is not a null |
|
| 185 | + * @return \ASN1\Type\Primitive\NullType |
|
| 186 | + */ |
|
| 187 | + public function asNull(): Primitive\NullType |
|
| 188 | + { |
|
| 189 | + if (!$this->_element instanceof Primitive\NullType) { |
|
| 190 | + throw new \UnexpectedValueException( |
|
| 191 | + $this->_generateExceptionMessage(Element::TYPE_NULL)); |
|
| 192 | + } |
|
| 193 | + return $this->_element; |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + /** |
|
| 197 | + * Get the wrapped element as an object identifier type. |
|
| 198 | + * |
|
| 199 | + * @throws \UnexpectedValueException If the element is not an object |
|
| 200 | + * identifier |
|
| 201 | + * @return \ASN1\Type\Primitive\ObjectIdentifier |
|
| 202 | + */ |
|
| 203 | + public function asObjectIdentifier(): Primitive\ObjectIdentifier |
|
| 204 | + { |
|
| 205 | + if (!$this->_element instanceof Primitive\ObjectIdentifier) { |
|
| 206 | + throw new \UnexpectedValueException( |
|
| 207 | + $this->_generateExceptionMessage( |
|
| 208 | + Element::TYPE_OBJECT_IDENTIFIER)); |
|
| 209 | + } |
|
| 210 | + return $this->_element; |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + /** |
|
| 214 | + * Get the wrapped element as an object descriptor type. |
|
| 215 | + * |
|
| 216 | + * @throws \UnexpectedValueException If the element is not an object |
|
| 217 | + * descriptor |
|
| 218 | + * @return \ASN1\Type\Primitive\ObjectDescriptor |
|
| 219 | + */ |
|
| 220 | + public function asObjectDescriptor(): Primitive\ObjectDescriptor |
|
| 221 | + { |
|
| 222 | + if (!$this->_element instanceof Primitive\ObjectDescriptor) { |
|
| 223 | + throw new \UnexpectedValueException( |
|
| 224 | + $this->_generateExceptionMessage( |
|
| 225 | + Element::TYPE_OBJECT_DESCRIPTOR)); |
|
| 226 | + } |
|
| 227 | + return $this->_element; |
|
| 228 | + } |
|
| 229 | + |
|
| 230 | + /** |
|
| 231 | + * Get the wrapped element as a real type. |
|
| 232 | + * |
|
| 233 | + * @throws \UnexpectedValueException If the element is not a real |
|
| 234 | + * @return \ASN1\Type\Primitive\Real |
|
| 235 | + */ |
|
| 236 | + public function asReal(): Primitive\Real |
|
| 237 | + { |
|
| 238 | + if (!$this->_element instanceof Primitive\Real) { |
|
| 239 | + throw new \UnexpectedValueException( |
|
| 240 | + $this->_generateExceptionMessage(Element::TYPE_REAL)); |
|
| 241 | + } |
|
| 242 | + return $this->_element; |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + /** |
|
| 246 | + * Get the wrapped element as an enumerated type. |
|
| 247 | + * |
|
| 248 | + * @throws \UnexpectedValueException If the element is not an enumerated |
|
| 249 | + * @return \ASN1\Type\Primitive\Enumerated |
|
| 250 | + */ |
|
| 251 | + public function asEnumerated(): Primitive\Enumerated |
|
| 252 | + { |
|
| 253 | + if (!$this->_element instanceof Primitive\Enumerated) { |
|
| 254 | + throw new \UnexpectedValueException( |
|
| 255 | + $this->_generateExceptionMessage(Element::TYPE_ENUMERATED)); |
|
| 256 | + } |
|
| 257 | + return $this->_element; |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + /** |
|
| 261 | + * Get the wrapped element as a UTF8 string type. |
|
| 262 | + * |
|
| 263 | + * @throws \UnexpectedValueException If the element is not a UTF8 string |
|
| 264 | + * @return \ASN1\Type\Primitive\UTF8String |
|
| 265 | + */ |
|
| 266 | + public function asUTF8String(): Primitive\UTF8String |
|
| 267 | + { |
|
| 268 | + if (!$this->_element instanceof Primitive\UTF8String) { |
|
| 269 | + throw new \UnexpectedValueException( |
|
| 270 | + $this->_generateExceptionMessage(Element::TYPE_UTF8_STRING)); |
|
| 271 | + } |
|
| 272 | + return $this->_element; |
|
| 273 | + } |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * Get the wrapped element as a relative OID type. |
|
| 277 | + * |
|
| 278 | + * @throws \UnexpectedValueException If the element is not a relative OID |
|
| 279 | + * @return \ASN1\Type\Primitive\RelativeOID |
|
| 280 | + */ |
|
| 281 | + public function asRelativeOID(): Primitive\RelativeOID |
|
| 282 | + { |
|
| 283 | + if (!$this->_element instanceof Primitive\RelativeOID) { |
|
| 284 | + throw new \UnexpectedValueException( |
|
| 285 | + $this->_generateExceptionMessage(Element::TYPE_RELATIVE_OID)); |
|
| 286 | + } |
|
| 287 | + return $this->_element; |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * Get the wrapped element as a sequence type. |
|
| 292 | + * |
|
| 293 | + * @throws \UnexpectedValueException If the element is not a sequence |
|
| 294 | + * @return \ASN1\Type\Constructed\Sequence |
|
| 295 | + */ |
|
| 296 | + public function asSequence(): Constructed\Sequence |
|
| 297 | + { |
|
| 298 | + if (!$this->_element instanceof Constructed\Sequence) { |
|
| 299 | + throw new \UnexpectedValueException( |
|
| 300 | + $this->_generateExceptionMessage(Element::TYPE_SEQUENCE)); |
|
| 301 | + } |
|
| 302 | + return $this->_element; |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + /** |
|
| 306 | + * Get the wrapped element as a set type. |
|
| 307 | + * |
|
| 308 | + * @throws \UnexpectedValueException If the element is not a set |
|
| 309 | + * @return \ASN1\Type\Constructed\Set |
|
| 310 | + */ |
|
| 311 | + public function asSet(): Constructed\Set |
|
| 312 | + { |
|
| 313 | + if (!$this->_element instanceof Constructed\Set) { |
|
| 314 | + throw new \UnexpectedValueException( |
|
| 315 | + $this->_generateExceptionMessage(Element::TYPE_SET)); |
|
| 316 | + } |
|
| 317 | + return $this->_element; |
|
| 318 | + } |
|
| 319 | + |
|
| 320 | + /** |
|
| 321 | + * Get the wrapped element as a numeric string type. |
|
| 322 | + * |
|
| 323 | + * @throws \UnexpectedValueException If the element is not a numeric string |
|
| 324 | + * @return \ASN1\Type\Primitive\NumericString |
|
| 325 | + */ |
|
| 326 | + public function asNumericString(): Primitive\NumericString |
|
| 327 | + { |
|
| 328 | + if (!$this->_element instanceof Primitive\NumericString) { |
|
| 329 | + throw new \UnexpectedValueException( |
|
| 330 | + $this->_generateExceptionMessage(Element::TYPE_NUMERIC_STRING)); |
|
| 331 | + } |
|
| 332 | + return $this->_element; |
|
| 333 | + } |
|
| 334 | + |
|
| 335 | + /** |
|
| 336 | + * Get the wrapped element as a printable string type. |
|
| 337 | + * |
|
| 338 | + * @throws \UnexpectedValueException If the element is not a printable |
|
| 339 | + * string |
|
| 340 | + * @return \ASN1\Type\Primitive\PrintableString |
|
| 341 | + */ |
|
| 342 | + public function asPrintableString(): Primitive\PrintableString |
|
| 343 | + { |
|
| 344 | + if (!$this->_element instanceof Primitive\PrintableString) { |
|
| 345 | + throw new \UnexpectedValueException( |
|
| 346 | + $this->_generateExceptionMessage(Element::TYPE_PRINTABLE_STRING)); |
|
| 347 | + } |
|
| 348 | + return $this->_element; |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + /** |
|
| 352 | + * Get the wrapped element as a T61 string type. |
|
| 353 | + * |
|
| 354 | + * @throws \UnexpectedValueException If the element is not a T61 string |
|
| 355 | + * @return \ASN1\Type\Primitive\T61String |
|
| 356 | + */ |
|
| 357 | + public function asT61String(): Primitive\T61String |
|
| 358 | + { |
|
| 359 | + if (!$this->_element instanceof Primitive\T61String) { |
|
| 360 | + throw new \UnexpectedValueException( |
|
| 361 | + $this->_generateExceptionMessage(Element::TYPE_T61_STRING)); |
|
| 362 | + } |
|
| 363 | + return $this->_element; |
|
| 364 | + } |
|
| 365 | + |
|
| 366 | + /** |
|
| 367 | + * Get the wrapped element as a videotex string type. |
|
| 368 | + * |
|
| 369 | + * @throws \UnexpectedValueException If the element is not a videotex string |
|
| 370 | + * @return \ASN1\Type\Primitive\VideotexString |
|
| 371 | + */ |
|
| 372 | + public function asVideotexString(): Primitive\VideotexString |
|
| 373 | + { |
|
| 374 | + if (!$this->_element instanceof Primitive\VideotexString) { |
|
| 375 | + throw new \UnexpectedValueException( |
|
| 376 | + $this->_generateExceptionMessage(Element::TYPE_VIDEOTEX_STRING)); |
|
| 377 | + } |
|
| 378 | + return $this->_element; |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + /** |
|
| 382 | + * Get the wrapped element as a IA5 string type. |
|
| 383 | + * |
|
| 384 | + * @throws \UnexpectedValueException If the element is not a IA5 string |
|
| 385 | + * @return \ASN1\Type\Primitive\IA5String |
|
| 386 | + */ |
|
| 387 | + public function asIA5String(): Primitive\IA5String |
|
| 388 | + { |
|
| 389 | + if (!$this->_element instanceof Primitive\IA5String) { |
|
| 390 | + throw new \UnexpectedValueException( |
|
| 391 | + $this->_generateExceptionMessage(Element::TYPE_IA5_STRING)); |
|
| 392 | + } |
|
| 393 | + return $this->_element; |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + /** |
|
| 397 | + * Get the wrapped element as an UTC time type. |
|
| 398 | + * |
|
| 399 | + * @throws \UnexpectedValueException If the element is not a UTC time |
|
| 400 | + * @return \ASN1\Type\Primitive\UTCTime |
|
| 401 | + */ |
|
| 402 | + public function asUTCTime(): Primitive\UTCTime |
|
| 403 | + { |
|
| 404 | + if (!$this->_element instanceof Primitive\UTCTime) { |
|
| 405 | + throw new \UnexpectedValueException( |
|
| 406 | + $this->_generateExceptionMessage(Element::TYPE_UTC_TIME)); |
|
| 407 | + } |
|
| 408 | + return $this->_element; |
|
| 409 | + } |
|
| 410 | + |
|
| 411 | + /** |
|
| 412 | + * Get the wrapped element as a generalized time type. |
|
| 413 | + * |
|
| 414 | + * @throws \UnexpectedValueException If the element is not a generalized |
|
| 415 | + * time |
|
| 416 | + * @return \ASN1\Type\Primitive\GeneralizedTime |
|
| 417 | + */ |
|
| 418 | + public function asGeneralizedTime(): Primitive\GeneralizedTime |
|
| 419 | + { |
|
| 420 | + if (!$this->_element instanceof Primitive\GeneralizedTime) { |
|
| 421 | + throw new \UnexpectedValueException( |
|
| 422 | + $this->_generateExceptionMessage(Element::TYPE_GENERALIZED_TIME)); |
|
| 423 | + } |
|
| 424 | + return $this->_element; |
|
| 425 | + } |
|
| 426 | + |
|
| 427 | + /** |
|
| 428 | + * Get the wrapped element as a graphic string type. |
|
| 429 | + * |
|
| 430 | + * @throws \UnexpectedValueException If the element is not a graphic string |
|
| 431 | + * @return \ASN1\Type\Primitive\GraphicString |
|
| 432 | + */ |
|
| 433 | + public function asGraphicString(): Primitive\GraphicString |
|
| 434 | + { |
|
| 435 | + if (!$this->_element instanceof Primitive\GraphicString) { |
|
| 436 | + throw new \UnexpectedValueException( |
|
| 437 | + $this->_generateExceptionMessage(Element::TYPE_GRAPHIC_STRING)); |
|
| 438 | + } |
|
| 439 | + return $this->_element; |
|
| 440 | + } |
|
| 441 | + |
|
| 442 | + /** |
|
| 443 | + * Get the wrapped element as a visible string type. |
|
| 444 | + * |
|
| 445 | + * @throws \UnexpectedValueException If the element is not a visible string |
|
| 446 | + * @return \ASN1\Type\Primitive\VisibleString |
|
| 447 | + */ |
|
| 448 | + public function asVisibleString(): Primitive\VisibleString |
|
| 449 | + { |
|
| 450 | + if (!$this->_element instanceof Primitive\VisibleString) { |
|
| 451 | + throw new \UnexpectedValueException( |
|
| 452 | + $this->_generateExceptionMessage(Element::TYPE_VISIBLE_STRING)); |
|
| 453 | + } |
|
| 454 | + return $this->_element; |
|
| 455 | + } |
|
| 456 | + |
|
| 457 | + /** |
|
| 458 | + * Get the wrapped element as a general string type. |
|
| 459 | + * |
|
| 460 | + * @throws \UnexpectedValueException If the element is not general string |
|
| 461 | + * @return \ASN1\Type\Primitive\GeneralString |
|
| 462 | + */ |
|
| 463 | + public function asGeneralString(): Primitive\GeneralString |
|
| 464 | + { |
|
| 465 | + if (!$this->_element instanceof Primitive\GeneralString) { |
|
| 466 | + throw new \UnexpectedValueException( |
|
| 467 | + $this->_generateExceptionMessage(Element::TYPE_GENERAL_STRING)); |
|
| 468 | + } |
|
| 469 | + return $this->_element; |
|
| 470 | + } |
|
| 471 | + |
|
| 472 | + /** |
|
| 473 | + * Get the wrapped element as a universal string type. |
|
| 474 | + * |
|
| 475 | + * @throws \UnexpectedValueException If the element is not a universal |
|
| 476 | + * string |
|
| 477 | + * @return \ASN1\Type\Primitive\UniversalString |
|
| 478 | + */ |
|
| 479 | + public function asUniversalString(): Primitive\UniversalString |
|
| 480 | + { |
|
| 481 | + if (!$this->_element instanceof Primitive\UniversalString) { |
|
| 482 | + throw new \UnexpectedValueException( |
|
| 483 | + $this->_generateExceptionMessage(Element::TYPE_UNIVERSAL_STRING)); |
|
| 484 | + } |
|
| 485 | + return $this->_element; |
|
| 486 | + } |
|
| 487 | + |
|
| 488 | + /** |
|
| 489 | + * Get the wrapped element as a character string type. |
|
| 490 | + * |
|
| 491 | + * @throws \UnexpectedValueException If the element is not a character |
|
| 492 | + * string |
|
| 493 | + * @return \ASN1\Type\Primitive\CharacterString |
|
| 494 | + */ |
|
| 495 | + public function asCharacterString(): Primitive\CharacterString |
|
| 496 | + { |
|
| 497 | + if (!$this->_element instanceof Primitive\CharacterString) { |
|
| 498 | + throw new \UnexpectedValueException( |
|
| 499 | + $this->_generateExceptionMessage(Element::TYPE_CHARACTER_STRING)); |
|
| 500 | + } |
|
| 501 | + return $this->_element; |
|
| 502 | + } |
|
| 503 | + |
|
| 504 | + /** |
|
| 505 | + * Get the wrapped element as a BMP string type. |
|
| 506 | + * |
|
| 507 | + * @throws \UnexpectedValueException If the element is not a bmp string |
|
| 508 | + * @return \ASN1\Type\Primitive\BMPString |
|
| 509 | + */ |
|
| 510 | + public function asBMPString(): Primitive\BMPString |
|
| 511 | + { |
|
| 512 | + if (!$this->_element instanceof Primitive\BMPString) { |
|
| 513 | + throw new \UnexpectedValueException( |
|
| 514 | + $this->_generateExceptionMessage(Element::TYPE_BMP_STRING)); |
|
| 515 | + } |
|
| 516 | + 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 |
|
| 523 | + * string |
|
| 524 | + * @return Constructed\ConstructedString |
|
| 525 | + */ |
|
| 526 | + public function asConstructedString(): Constructed\ConstructedString |
|
| 527 | + { |
|
| 528 | + if (!$this->_element instanceof Constructed\ConstructedString) { |
|
| 529 | + throw new \UnexpectedValueException( |
|
| 530 | + $this->_generateExceptionMessage( |
|
| 531 | + Element::TYPE_CONSTRUCTED_STRING)); |
|
| 532 | + } |
|
| 533 | + return $this->_element; |
|
| 534 | + } |
|
| 535 | + |
|
| 536 | + /** |
|
| 537 | + * Get the wrapped element as any string type. |
|
| 538 | + * |
|
| 539 | + * @throws \UnexpectedValueException If the element is not a string |
|
| 540 | + * @return StringType |
|
| 541 | + */ |
|
| 542 | + public function asString(): StringType |
|
| 543 | + { |
|
| 544 | + if (!$this->_element instanceof StringType) { |
|
| 545 | + throw new \UnexpectedValueException( |
|
| 546 | + $this->_generateExceptionMessage(Element::TYPE_STRING)); |
|
| 547 | + } |
|
| 548 | + return $this->_element; |
|
| 549 | + } |
|
| 550 | + |
|
| 551 | + /** |
|
| 552 | + * Get the wrapped element as any time type. |
|
| 553 | + * |
|
| 554 | + * @throws \UnexpectedValueException If the element is not a time |
|
| 555 | + * @return TimeType |
|
| 556 | + */ |
|
| 557 | + public function asTime(): TimeType |
|
| 558 | + { |
|
| 559 | + if (!$this->_element instanceof TimeType) { |
|
| 560 | + throw new \UnexpectedValueException( |
|
| 561 | + $this->_generateExceptionMessage(Element::TYPE_TIME)); |
|
| 562 | + } |
|
| 563 | + return $this->_element; |
|
| 564 | + } |
|
| 565 | + |
|
| 566 | + /** |
|
| 567 | + * Generate message for exceptions thrown by <code>as*</code> methods. |
|
| 568 | + * |
|
| 569 | + * @param int $tag Type tag of the expected element |
|
| 570 | + * @return string |
|
| 571 | + */ |
|
| 572 | + private function _generateExceptionMessage(int $tag): string |
|
| 573 | + { |
|
| 574 | + return sprintf("%s expected, got %s.", Element::tagToName($tag), |
|
| 575 | + $this->_typeDescriptorString()); |
|
| 576 | + } |
|
| 577 | + |
|
| 578 | + /** |
|
| 579 | + * Get textual description of the wrapped element for debugging purposes. |
|
| 580 | + * |
|
| 581 | + * @return string |
|
| 582 | + */ |
|
| 583 | + private function _typeDescriptorString(): string |
|
| 584 | + { |
|
| 585 | + $type_cls = $this->_element->typeClass(); |
|
| 586 | + $tag = $this->_element->tag(); |
|
| 587 | + if ($type_cls == Identifier::CLASS_UNIVERSAL) { |
|
| 588 | + return Element::tagToName($tag); |
|
| 589 | + } |
|
| 590 | + return Identifier::classToName($type_cls) . " TAG $tag"; |
|
| 591 | + } |
|
| 592 | + |
|
| 593 | + /** |
|
| 594 | + * |
|
| 595 | + * @see \ASN1\Feature\Encodable::toDER() |
|
| 596 | + * @return string |
|
| 597 | + */ |
|
| 598 | + public function toDER(): string |
|
| 599 | + { |
|
| 600 | + return $this->_element->toDER(); |
|
| 601 | + } |
|
| 602 | + |
|
| 603 | + /** |
|
| 604 | + * |
|
| 605 | + * @see \ASN1\Feature\ElementBase::typeClass() |
|
| 606 | + * @return int |
|
| 607 | + */ |
|
| 608 | + public function typeClass(): int |
|
| 609 | + { |
|
| 610 | + return $this->_element->typeClass(); |
|
| 611 | + } |
|
| 612 | + |
|
| 613 | + /** |
|
| 614 | + * |
|
| 615 | + * @see \ASN1\Feature\ElementBase::isConstructed() |
|
| 616 | + * @return bool |
|
| 617 | + */ |
|
| 618 | + public function isConstructed(): bool |
|
| 619 | + { |
|
| 620 | + return $this->_element->isConstructed(); |
|
| 621 | + } |
|
| 622 | + |
|
| 623 | + /** |
|
| 624 | + * |
|
| 625 | + * @see \ASN1\Feature\ElementBase::tag() |
|
| 626 | + * @return int |
|
| 627 | + */ |
|
| 628 | + public function tag(): int |
|
| 629 | + { |
|
| 630 | + return $this->_element->tag(); |
|
| 631 | + } |
|
| 632 | + |
|
| 633 | + /** |
|
| 634 | + * |
|
| 635 | + * {@inheritdoc} |
|
| 636 | + * @see \ASN1\Feature\ElementBase::isType() |
|
| 637 | + * @return bool |
|
| 638 | + */ |
|
| 639 | + public function isType(int $tag): bool |
|
| 640 | + { |
|
| 641 | + return $this->_element->isType($tag); |
|
| 642 | + } |
|
| 643 | + |
|
| 644 | + /** |
|
| 645 | + * |
|
| 646 | + * @deprecated Use any <code>as*</code> accessor method first to ensure |
|
| 647 | + * type strictness. |
|
| 648 | + * @see \ASN1\Feature\ElementBase::expectType() |
|
| 649 | + * @return ElementBase |
|
| 650 | + */ |
|
| 651 | + public function expectType(int $tag): ElementBase |
|
| 652 | + { |
|
| 653 | + return $this->_element->expectType($tag); |
|
| 654 | + } |
|
| 655 | + |
|
| 656 | + /** |
|
| 657 | + * |
|
| 658 | + * @see \ASN1\Feature\ElementBase::isTagged() |
|
| 659 | + * @return bool |
|
| 660 | + */ |
|
| 661 | + public function isTagged(): bool |
|
| 662 | + { |
|
| 663 | + return $this->_element->isTagged(); |
|
| 664 | + } |
|
| 665 | + |
|
| 666 | + /** |
|
| 667 | + * |
|
| 668 | + * @deprecated Use any <code>as*</code> accessor method first to ensure |
|
| 669 | + * type strictness. |
|
| 670 | + * @see \ASN1\Feature\ElementBase::expectTagged() |
|
| 671 | + * @return TaggedType |
|
| 672 | + */ |
|
| 673 | + public function expectTagged($tag = null): TaggedType |
|
| 674 | + { |
|
| 675 | + return $this->_element->expectTagged($tag); |
|
| 676 | + } |
|
| 677 | + |
|
| 678 | + /** |
|
| 679 | + * |
|
| 680 | + * @see \ASN1\Feature\ElementBase::asElement() |
|
| 681 | + * @return Element |
|
| 682 | + */ |
|
| 683 | + public function asElement(): Element |
|
| 684 | + { |
|
| 685 | + return $this->_element; |
|
| 686 | + } |
|
| 687 | + |
|
| 688 | + /** |
|
| 689 | + * |
|
| 690 | + * {@inheritdoc} |
|
| 691 | + * @return UnspecifiedType |
|
| 692 | + */ |
|
| 693 | + public function asUnspecified(): UnspecifiedType |
|
| 694 | + { |
|
| 695 | + return $this; |
|
| 696 | + } |
|
| 697 | 697 | } |
@@ -15,112 +15,112 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class ConstructedString extends Structure implements Stringable |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * Constructor. |
|
| 20 | - * |
|
| 21 | - * @internal Use create() |
|
| 22 | - * |
|
| 23 | - * @param Element ...$elements Any number of elements |
|
| 24 | - */ |
|
| 25 | - public function __construct(Element ...$elements) |
|
| 26 | - { |
|
| 27 | - parent::__construct(...$elements); |
|
| 28 | - } |
|
| 18 | + /** |
|
| 19 | + * Constructor. |
|
| 20 | + * |
|
| 21 | + * @internal Use create() |
|
| 22 | + * |
|
| 23 | + * @param Element ...$elements Any number of elements |
|
| 24 | + */ |
|
| 25 | + public function __construct(Element ...$elements) |
|
| 26 | + { |
|
| 27 | + parent::__construct(...$elements); |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - /** |
|
| 31 | - * Create from a list of string type elements |
|
| 32 | - * |
|
| 33 | - * All strings must have the same type. |
|
| 34 | - * |
|
| 35 | - * @param StringType ...$elements |
|
| 36 | - * @throws \LogicException |
|
| 37 | - * @return self |
|
| 38 | - */ |
|
| 39 | - public static function create(StringType ...$elements): self |
|
| 40 | - { |
|
| 41 | - if (!count($elements)) { |
|
| 42 | - throw new \LogicException( |
|
| 43 | - 'No elements, unable to determine type tag.'); |
|
| 44 | - } |
|
| 45 | - $tag = $elements[0]->tag(); |
|
| 46 | - foreach ($elements as $el) { |
|
| 47 | - if ($el->tag() !== $tag) { |
|
| 48 | - throw new \LogicException( |
|
| 49 | - 'All elements in constructed string must have the same type.'); |
|
| 50 | - } |
|
| 51 | - } |
|
| 52 | - return self::createWithTag($tag, ...$elements); |
|
| 53 | - } |
|
| 30 | + /** |
|
| 31 | + * Create from a list of string type elements |
|
| 32 | + * |
|
| 33 | + * All strings must have the same type. |
|
| 34 | + * |
|
| 35 | + * @param StringType ...$elements |
|
| 36 | + * @throws \LogicException |
|
| 37 | + * @return self |
|
| 38 | + */ |
|
| 39 | + public static function create(StringType ...$elements): self |
|
| 40 | + { |
|
| 41 | + if (!count($elements)) { |
|
| 42 | + throw new \LogicException( |
|
| 43 | + 'No elements, unable to determine type tag.'); |
|
| 44 | + } |
|
| 45 | + $tag = $elements[0]->tag(); |
|
| 46 | + foreach ($elements as $el) { |
|
| 47 | + if ($el->tag() !== $tag) { |
|
| 48 | + throw new \LogicException( |
|
| 49 | + 'All elements in constructed string must have the same type.'); |
|
| 50 | + } |
|
| 51 | + } |
|
| 52 | + return self::createWithTag($tag, ...$elements); |
|
| 53 | + } |
|
| 54 | 54 | |
| 55 | - /** |
|
| 56 | - * Create from strings with a given type tag. |
|
| 57 | - * |
|
| 58 | - * @param int $tag Type tag for the constructed string element |
|
| 59 | - * @param StringType ...$elements Any number of elements |
|
| 60 | - * @return self |
|
| 61 | - */ |
|
| 62 | - public static function createWithTag(int $tag, StringType ...$elements) |
|
| 63 | - { |
|
| 64 | - $el = new self(...$elements); |
|
| 65 | - $el->_typeTag = $tag; |
|
| 66 | - return $el; |
|
| 67 | - } |
|
| 55 | + /** |
|
| 56 | + * Create from strings with a given type tag. |
|
| 57 | + * |
|
| 58 | + * @param int $tag Type tag for the constructed string element |
|
| 59 | + * @param StringType ...$elements Any number of elements |
|
| 60 | + * @return self |
|
| 61 | + */ |
|
| 62 | + public static function createWithTag(int $tag, StringType ...$elements) |
|
| 63 | + { |
|
| 64 | + $el = new self(...$elements); |
|
| 65 | + $el->_typeTag = $tag; |
|
| 66 | + return $el; |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * Get a list of strings in this structure. |
|
| 71 | - * |
|
| 72 | - * @return string[] |
|
| 73 | - */ |
|
| 74 | - public function strings(): array |
|
| 75 | - { |
|
| 76 | - return array_map(function (StringType $el) { |
|
| 77 | - return $el->string(); |
|
| 78 | - }, $this->_elements); |
|
| 79 | - } |
|
| 69 | + /** |
|
| 70 | + * Get a list of strings in this structure. |
|
| 71 | + * |
|
| 72 | + * @return string[] |
|
| 73 | + */ |
|
| 74 | + public function strings(): array |
|
| 75 | + { |
|
| 76 | + return array_map(function (StringType $el) { |
|
| 77 | + return $el->string(); |
|
| 78 | + }, $this->_elements); |
|
| 79 | + } |
|
| 80 | 80 | |
| 81 | - /** |
|
| 82 | - * Get the contained strings concatenated together. |
|
| 83 | - * |
|
| 84 | - * @return string |
|
| 85 | - */ |
|
| 86 | - public function concatenated(): string |
|
| 87 | - { |
|
| 88 | - return implode('', $this->strings()); |
|
| 89 | - } |
|
| 81 | + /** |
|
| 82 | + * Get the contained strings concatenated together. |
|
| 83 | + * |
|
| 84 | + * @return string |
|
| 85 | + */ |
|
| 86 | + public function concatenated(): string |
|
| 87 | + { |
|
| 88 | + return implode('', $this->strings()); |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - /** |
|
| 92 | - * Get the contained strings concatenated together. |
|
| 93 | - * |
|
| 94 | - * @return string |
|
| 95 | - */ |
|
| 96 | - public function string(): string |
|
| 97 | - { |
|
| 98 | - return $this->concatenated(); |
|
| 99 | - } |
|
| 91 | + /** |
|
| 92 | + * Get the contained strings concatenated together. |
|
| 93 | + * |
|
| 94 | + * @return string |
|
| 95 | + */ |
|
| 96 | + public function string(): string |
|
| 97 | + { |
|
| 98 | + return $this->concatenated(); |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - /** |
|
| 102 | - * |
|
| 103 | - * @inheritdoc |
|
| 104 | - * @return string |
|
| 105 | - */ |
|
| 106 | - public function __toString(): string |
|
| 107 | - { |
|
| 108 | - return $this->concatenated(); |
|
| 109 | - } |
|
| 101 | + /** |
|
| 102 | + * |
|
| 103 | + * @inheritdoc |
|
| 104 | + * @return string |
|
| 105 | + */ |
|
| 106 | + public function __toString(): string |
|
| 107 | + { |
|
| 108 | + return $this->concatenated(); |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - /** |
|
| 112 | - * |
|
| 113 | - * {@inheritdoc} |
|
| 114 | - * |
|
| 115 | - * @return self |
|
| 116 | - */ |
|
| 117 | - protected static function _decodeFromDER(Identifier $identifier, |
|
| 118 | - string $data, int &$offset): ElementBase |
|
| 119 | - { |
|
| 120 | - /** @var ConstructedString $type */ |
|
| 121 | - $type = forward_static_call_array([parent::class, __FUNCTION__], |
|
| 122 | - [$identifier, $data, &$offset]); |
|
| 123 | - $type->_typeTag = $identifier->intTag(); |
|
| 124 | - return $type; |
|
| 125 | - } |
|
| 111 | + /** |
|
| 112 | + * |
|
| 113 | + * {@inheritdoc} |
|
| 114 | + * |
|
| 115 | + * @return self |
|
| 116 | + */ |
|
| 117 | + protected static function _decodeFromDER(Identifier $identifier, |
|
| 118 | + string $data, int &$offset): ElementBase |
|
| 119 | + { |
|
| 120 | + /** @var ConstructedString $type */ |
|
| 121 | + $type = forward_static_call_array([parent::class, __FUNCTION__], |
|
| 122 | + [$identifier, $data, &$offset]); |
|
| 123 | + $type->_typeTag = $identifier->intTag(); |
|
| 124 | + return $type; |
|
| 125 | + } |
|
| 126 | 126 | } |
@@ -1,5 +1,5 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | -declare(strict_types = 1); |
|
| 2 | +declare(strict_types=1); |
|
| 3 | 3 | |
| 4 | 4 | namespace ASN1\Type\Constructed; |
| 5 | 5 | |
@@ -73,7 +73,7 @@ discard block |
||
| 73 | 73 | */ |
| 74 | 74 | public function strings(): array |
| 75 | 75 | { |
| 76 | - return array_map(function (StringType $el) { |
|
| 76 | + return array_map(function(StringType $el) { |
|
| 77 | 77 | return $el->string(); |
| 78 | 78 | }, $this->_elements); |
| 79 | 79 | } |
@@ -11,58 +11,58 @@ |
||
| 11 | 11 | */ |
| 12 | 12 | abstract class StringType extends Element implements Stringable |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * String value. |
|
| 16 | - * |
|
| 17 | - * @var string $_string |
|
| 18 | - */ |
|
| 19 | - protected $_string; |
|
| 14 | + /** |
|
| 15 | + * String value. |
|
| 16 | + * |
|
| 17 | + * @var string $_string |
|
| 18 | + */ |
|
| 19 | + protected $_string; |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Constructor. |
|
| 23 | - * |
|
| 24 | - * @param string $string |
|
| 25 | - * @throws \InvalidArgumentException |
|
| 26 | - */ |
|
| 27 | - public function __construct(string $string) |
|
| 28 | - { |
|
| 29 | - if (!$this->_validateString($string)) { |
|
| 30 | - throw new \InvalidArgumentException( |
|
| 31 | - sprintf("Not a valid %s string.", |
|
| 32 | - self::tagToName($this->_typeTag))); |
|
| 33 | - } |
|
| 34 | - $this->_string = $string; |
|
| 35 | - } |
|
| 21 | + /** |
|
| 22 | + * Constructor. |
|
| 23 | + * |
|
| 24 | + * @param string $string |
|
| 25 | + * @throws \InvalidArgumentException |
|
| 26 | + */ |
|
| 27 | + public function __construct(string $string) |
|
| 28 | + { |
|
| 29 | + if (!$this->_validateString($string)) { |
|
| 30 | + throw new \InvalidArgumentException( |
|
| 31 | + sprintf("Not a valid %s string.", |
|
| 32 | + self::tagToName($this->_typeTag))); |
|
| 33 | + } |
|
| 34 | + $this->_string = $string; |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * Get the string value. |
|
| 39 | - * |
|
| 40 | - * @return string |
|
| 41 | - */ |
|
| 42 | - public function string(): string |
|
| 43 | - { |
|
| 44 | - return $this->_string; |
|
| 45 | - } |
|
| 37 | + /** |
|
| 38 | + * Get the string value. |
|
| 39 | + * |
|
| 40 | + * @return string |
|
| 41 | + */ |
|
| 42 | + public function string(): string |
|
| 43 | + { |
|
| 44 | + return $this->_string; |
|
| 45 | + } |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * |
|
| 49 | - * @inheritdoc |
|
| 50 | - * @return string |
|
| 51 | - */ |
|
| 52 | - public function __toString(): string |
|
| 53 | - { |
|
| 54 | - return $this->_string; |
|
| 55 | - } |
|
| 47 | + /** |
|
| 48 | + * |
|
| 49 | + * @inheritdoc |
|
| 50 | + * @return string |
|
| 51 | + */ |
|
| 52 | + public function __toString(): string |
|
| 53 | + { |
|
| 54 | + return $this->_string; |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * Check whether string is valid for the concrete type. |
|
| 59 | - * |
|
| 60 | - * @param string $string |
|
| 61 | - * @return bool |
|
| 62 | - */ |
|
| 63 | - protected function _validateString(string $string): bool |
|
| 64 | - { |
|
| 65 | - // Override in derived classes |
|
| 66 | - return true; |
|
| 67 | - } |
|
| 57 | + /** |
|
| 58 | + * Check whether string is valid for the concrete type. |
|
| 59 | + * |
|
| 60 | + * @param string $string |
|
| 61 | + * @return bool |
|
| 62 | + */ |
|
| 63 | + protected function _validateString(string $string): bool |
|
| 64 | + { |
|
| 65 | + // Override in derived classes |
|
| 66 | + return true; |
|
| 67 | + } |
|
| 68 | 68 | } |
@@ -24,495 +24,495 @@ |
||
| 24 | 24 | */ |
| 25 | 25 | abstract class Element implements ElementBase |
| 26 | 26 | { |
| 27 | - // Universal type tags |
|
| 28 | - const TYPE_EOC = 0x00; |
|
| 29 | - const TYPE_BOOLEAN = 0x01; |
|
| 30 | - const TYPE_INTEGER = 0x02; |
|
| 31 | - const TYPE_BIT_STRING = 0x03; |
|
| 32 | - const TYPE_OCTET_STRING = 0x04; |
|
| 33 | - const TYPE_NULL = 0x05; |
|
| 34 | - const TYPE_OBJECT_IDENTIFIER = 0x06; |
|
| 35 | - const TYPE_OBJECT_DESCRIPTOR = 0x07; |
|
| 36 | - const TYPE_EXTERNAL = 0x08; |
|
| 37 | - const TYPE_REAL = 0x09; |
|
| 38 | - const TYPE_ENUMERATED = 0x0a; |
|
| 39 | - const TYPE_EMBEDDED_PDV = 0x0b; |
|
| 40 | - const TYPE_UTF8_STRING = 0x0c; |
|
| 41 | - const TYPE_RELATIVE_OID = 0x0d; |
|
| 42 | - const TYPE_SEQUENCE = 0x10; |
|
| 43 | - const TYPE_SET = 0x11; |
|
| 44 | - const TYPE_NUMERIC_STRING = 0x12; |
|
| 45 | - const TYPE_PRINTABLE_STRING = 0x13; |
|
| 46 | - const TYPE_T61_STRING = 0x14; |
|
| 47 | - const TYPE_VIDEOTEX_STRING = 0x15; |
|
| 48 | - const TYPE_IA5_STRING = 0x16; |
|
| 49 | - const TYPE_UTC_TIME = 0x17; |
|
| 50 | - const TYPE_GENERALIZED_TIME = 0x18; |
|
| 51 | - const TYPE_GRAPHIC_STRING = 0x19; |
|
| 52 | - const TYPE_VISIBLE_STRING = 0x1a; |
|
| 53 | - const TYPE_GENERAL_STRING = 0x1b; |
|
| 54 | - const TYPE_UNIVERSAL_STRING = 0x1c; |
|
| 55 | - const TYPE_CHARACTER_STRING = 0x1d; |
|
| 56 | - const TYPE_BMP_STRING = 0x1e; |
|
| 27 | + // Universal type tags |
|
| 28 | + const TYPE_EOC = 0x00; |
|
| 29 | + const TYPE_BOOLEAN = 0x01; |
|
| 30 | + const TYPE_INTEGER = 0x02; |
|
| 31 | + const TYPE_BIT_STRING = 0x03; |
|
| 32 | + const TYPE_OCTET_STRING = 0x04; |
|
| 33 | + const TYPE_NULL = 0x05; |
|
| 34 | + const TYPE_OBJECT_IDENTIFIER = 0x06; |
|
| 35 | + const TYPE_OBJECT_DESCRIPTOR = 0x07; |
|
| 36 | + const TYPE_EXTERNAL = 0x08; |
|
| 37 | + const TYPE_REAL = 0x09; |
|
| 38 | + const TYPE_ENUMERATED = 0x0a; |
|
| 39 | + const TYPE_EMBEDDED_PDV = 0x0b; |
|
| 40 | + const TYPE_UTF8_STRING = 0x0c; |
|
| 41 | + const TYPE_RELATIVE_OID = 0x0d; |
|
| 42 | + const TYPE_SEQUENCE = 0x10; |
|
| 43 | + const TYPE_SET = 0x11; |
|
| 44 | + const TYPE_NUMERIC_STRING = 0x12; |
|
| 45 | + const TYPE_PRINTABLE_STRING = 0x13; |
|
| 46 | + const TYPE_T61_STRING = 0x14; |
|
| 47 | + const TYPE_VIDEOTEX_STRING = 0x15; |
|
| 48 | + const TYPE_IA5_STRING = 0x16; |
|
| 49 | + const TYPE_UTC_TIME = 0x17; |
|
| 50 | + const TYPE_GENERALIZED_TIME = 0x18; |
|
| 51 | + const TYPE_GRAPHIC_STRING = 0x19; |
|
| 52 | + const TYPE_VISIBLE_STRING = 0x1a; |
|
| 53 | + const TYPE_GENERAL_STRING = 0x1b; |
|
| 54 | + const TYPE_UNIVERSAL_STRING = 0x1c; |
|
| 55 | + const TYPE_CHARACTER_STRING = 0x1d; |
|
| 56 | + const TYPE_BMP_STRING = 0x1e; |
|
| 57 | 57 | |
| 58 | - /** |
|
| 59 | - * Mapping from universal type tag to implementation class name. |
|
| 60 | - * |
|
| 61 | - * @internal |
|
| 62 | - * |
|
| 63 | - * @var array |
|
| 64 | - */ |
|
| 65 | - const MAP_TAG_TO_CLASS = [ /* @formatter:off */ |
|
| 66 | - self::TYPE_EOC => Primitive\EOC::class, |
|
| 67 | - self::TYPE_BOOLEAN => Primitive\Boolean::class, |
|
| 68 | - self::TYPE_INTEGER => Primitive\Integer::class, |
|
| 69 | - self::TYPE_BIT_STRING => Primitive\BitString::class, |
|
| 70 | - self::TYPE_OCTET_STRING => Primitive\OctetString::class, |
|
| 71 | - self::TYPE_NULL => Primitive\NullType::class, |
|
| 72 | - self::TYPE_OBJECT_IDENTIFIER => Primitive\ObjectIdentifier::class, |
|
| 73 | - self::TYPE_OBJECT_DESCRIPTOR => Primitive\ObjectDescriptor::class, |
|
| 74 | - self::TYPE_REAL => Primitive\Real::class, |
|
| 75 | - self::TYPE_ENUMERATED => Primitive\Enumerated::class, |
|
| 76 | - self::TYPE_UTF8_STRING => Primitive\UTF8String::class, |
|
| 77 | - self::TYPE_RELATIVE_OID => Primitive\RelativeOID::class, |
|
| 78 | - self::TYPE_SEQUENCE => Constructed\Sequence::class, |
|
| 79 | - self::TYPE_SET => Constructed\Set::class, |
|
| 80 | - self::TYPE_NUMERIC_STRING => Primitive\NumericString::class, |
|
| 81 | - self::TYPE_PRINTABLE_STRING => Primitive\PrintableString::class, |
|
| 82 | - self::TYPE_T61_STRING => Primitive\T61String::class, |
|
| 83 | - self::TYPE_VIDEOTEX_STRING => Primitive\VideotexString::class, |
|
| 84 | - self::TYPE_IA5_STRING => Primitive\IA5String::class, |
|
| 85 | - self::TYPE_UTC_TIME => Primitive\UTCTime::class, |
|
| 86 | - self::TYPE_GENERALIZED_TIME => Primitive\GeneralizedTime::class, |
|
| 87 | - self::TYPE_GRAPHIC_STRING => Primitive\GraphicString::class, |
|
| 88 | - self::TYPE_VISIBLE_STRING => Primitive\VisibleString::class, |
|
| 89 | - self::TYPE_GENERAL_STRING => Primitive\GeneralString::class, |
|
| 90 | - self::TYPE_UNIVERSAL_STRING => Primitive\UniversalString::class, |
|
| 91 | - self::TYPE_CHARACTER_STRING => Primitive\CharacterString::class, |
|
| 92 | - self::TYPE_BMP_STRING => Primitive\BMPString::class |
|
| 93 | - /* @formatter:on */ |
|
| 94 | - ]; |
|
| 58 | + /** |
|
| 59 | + * Mapping from universal type tag to implementation class name. |
|
| 60 | + * |
|
| 61 | + * @internal |
|
| 62 | + * |
|
| 63 | + * @var array |
|
| 64 | + */ |
|
| 65 | + const MAP_TAG_TO_CLASS = [ /* @formatter:off */ |
|
| 66 | + self::TYPE_EOC => Primitive\EOC::class, |
|
| 67 | + self::TYPE_BOOLEAN => Primitive\Boolean::class, |
|
| 68 | + self::TYPE_INTEGER => Primitive\Integer::class, |
|
| 69 | + self::TYPE_BIT_STRING => Primitive\BitString::class, |
|
| 70 | + self::TYPE_OCTET_STRING => Primitive\OctetString::class, |
|
| 71 | + self::TYPE_NULL => Primitive\NullType::class, |
|
| 72 | + self::TYPE_OBJECT_IDENTIFIER => Primitive\ObjectIdentifier::class, |
|
| 73 | + self::TYPE_OBJECT_DESCRIPTOR => Primitive\ObjectDescriptor::class, |
|
| 74 | + self::TYPE_REAL => Primitive\Real::class, |
|
| 75 | + self::TYPE_ENUMERATED => Primitive\Enumerated::class, |
|
| 76 | + self::TYPE_UTF8_STRING => Primitive\UTF8String::class, |
|
| 77 | + self::TYPE_RELATIVE_OID => Primitive\RelativeOID::class, |
|
| 78 | + self::TYPE_SEQUENCE => Constructed\Sequence::class, |
|
| 79 | + self::TYPE_SET => Constructed\Set::class, |
|
| 80 | + self::TYPE_NUMERIC_STRING => Primitive\NumericString::class, |
|
| 81 | + self::TYPE_PRINTABLE_STRING => Primitive\PrintableString::class, |
|
| 82 | + self::TYPE_T61_STRING => Primitive\T61String::class, |
|
| 83 | + self::TYPE_VIDEOTEX_STRING => Primitive\VideotexString::class, |
|
| 84 | + self::TYPE_IA5_STRING => Primitive\IA5String::class, |
|
| 85 | + self::TYPE_UTC_TIME => Primitive\UTCTime::class, |
|
| 86 | + self::TYPE_GENERALIZED_TIME => Primitive\GeneralizedTime::class, |
|
| 87 | + self::TYPE_GRAPHIC_STRING => Primitive\GraphicString::class, |
|
| 88 | + self::TYPE_VISIBLE_STRING => Primitive\VisibleString::class, |
|
| 89 | + self::TYPE_GENERAL_STRING => Primitive\GeneralString::class, |
|
| 90 | + self::TYPE_UNIVERSAL_STRING => Primitive\UniversalString::class, |
|
| 91 | + self::TYPE_CHARACTER_STRING => Primitive\CharacterString::class, |
|
| 92 | + self::TYPE_BMP_STRING => Primitive\BMPString::class |
|
| 93 | + /* @formatter:on */ |
|
| 94 | + ]; |
|
| 95 | 95 | |
| 96 | - /** |
|
| 97 | - * Pseudotype for all string types. |
|
| 98 | - * |
|
| 99 | - * May be used as an expectation parameter. |
|
| 100 | - * |
|
| 101 | - * @var int |
|
| 102 | - */ |
|
| 103 | - const TYPE_STRING = -1; |
|
| 96 | + /** |
|
| 97 | + * Pseudotype for all string types. |
|
| 98 | + * |
|
| 99 | + * May be used as an expectation parameter. |
|
| 100 | + * |
|
| 101 | + * @var int |
|
| 102 | + */ |
|
| 103 | + const TYPE_STRING = -1; |
|
| 104 | 104 | |
| 105 | - /** |
|
| 106 | - * Pseudotype for all time types. |
|
| 107 | - * |
|
| 108 | - * May be used as an expectation parameter. |
|
| 109 | - * |
|
| 110 | - * @var int |
|
| 111 | - */ |
|
| 112 | - const TYPE_TIME = -2; |
|
| 105 | + /** |
|
| 106 | + * Pseudotype for all time types. |
|
| 107 | + * |
|
| 108 | + * May be used as an expectation parameter. |
|
| 109 | + * |
|
| 110 | + * @var int |
|
| 111 | + */ |
|
| 112 | + const TYPE_TIME = -2; |
|
| 113 | 113 | |
| 114 | - /** |
|
| 115 | - * Pseudotype for constructed strings. |
|
| 116 | - * |
|
| 117 | - * May be used as an expectation parameter. |
|
| 118 | - * |
|
| 119 | - * @var int |
|
| 120 | - */ |
|
| 121 | - const TYPE_CONSTRUCTED_STRING = -3; |
|
| 114 | + /** |
|
| 115 | + * Pseudotype for constructed strings. |
|
| 116 | + * |
|
| 117 | + * May be used as an expectation parameter. |
|
| 118 | + * |
|
| 119 | + * @var int |
|
| 120 | + */ |
|
| 121 | + const TYPE_CONSTRUCTED_STRING = -3; |
|
| 122 | 122 | |
| 123 | - /** |
|
| 124 | - * Mapping from universal type tag to human readable name. |
|
| 125 | - * |
|
| 126 | - * @internal |
|
| 127 | - * |
|
| 128 | - * @var array |
|
| 129 | - */ |
|
| 130 | - const MAP_TYPE_TO_NAME = [ /* @formatter:off */ |
|
| 131 | - self::TYPE_EOC => "EOC", |
|
| 132 | - self::TYPE_BOOLEAN => "BOOLEAN", |
|
| 133 | - self::TYPE_INTEGER => "INTEGER", |
|
| 134 | - self::TYPE_BIT_STRING => "BIT STRING", |
|
| 135 | - self::TYPE_OCTET_STRING => "OCTET STRING", |
|
| 136 | - self::TYPE_NULL => "NULL", |
|
| 137 | - self::TYPE_OBJECT_IDENTIFIER => "OBJECT IDENTIFIER", |
|
| 138 | - self::TYPE_OBJECT_DESCRIPTOR => "ObjectDescriptor", |
|
| 139 | - self::TYPE_EXTERNAL => "EXTERNAL", |
|
| 140 | - self::TYPE_REAL => "REAL", |
|
| 141 | - self::TYPE_ENUMERATED => "ENUMERATED", |
|
| 142 | - self::TYPE_EMBEDDED_PDV => "EMBEDDED PDV", |
|
| 143 | - self::TYPE_UTF8_STRING => "UTF8String", |
|
| 144 | - self::TYPE_RELATIVE_OID => "RELATIVE-OID", |
|
| 145 | - self::TYPE_SEQUENCE => "SEQUENCE", |
|
| 146 | - self::TYPE_SET => "SET", |
|
| 147 | - self::TYPE_NUMERIC_STRING => "NumericString", |
|
| 148 | - self::TYPE_PRINTABLE_STRING => "PrintableString", |
|
| 149 | - self::TYPE_T61_STRING => "T61String", |
|
| 150 | - self::TYPE_VIDEOTEX_STRING => "VideotexString", |
|
| 151 | - self::TYPE_IA5_STRING => "IA5String", |
|
| 152 | - self::TYPE_UTC_TIME => "UTCTime", |
|
| 153 | - self::TYPE_GENERALIZED_TIME => "GeneralizedTime", |
|
| 154 | - self::TYPE_GRAPHIC_STRING => "GraphicString", |
|
| 155 | - self::TYPE_VISIBLE_STRING => "VisibleString", |
|
| 156 | - self::TYPE_GENERAL_STRING => "GeneralString", |
|
| 157 | - self::TYPE_UNIVERSAL_STRING => "UniversalString", |
|
| 158 | - self::TYPE_CHARACTER_STRING => "CHARACTER STRING", |
|
| 159 | - self::TYPE_BMP_STRING => "BMPString", |
|
| 160 | - self::TYPE_STRING => "Any String", |
|
| 161 | - self::TYPE_TIME => "Any Time", |
|
| 162 | - self::TYPE_CONSTRUCTED_STRING => "Constructed String" |
|
| 163 | - /* @formatter:on */ |
|
| 164 | - ]; |
|
| 123 | + /** |
|
| 124 | + * Mapping from universal type tag to human readable name. |
|
| 125 | + * |
|
| 126 | + * @internal |
|
| 127 | + * |
|
| 128 | + * @var array |
|
| 129 | + */ |
|
| 130 | + const MAP_TYPE_TO_NAME = [ /* @formatter:off */ |
|
| 131 | + self::TYPE_EOC => "EOC", |
|
| 132 | + self::TYPE_BOOLEAN => "BOOLEAN", |
|
| 133 | + self::TYPE_INTEGER => "INTEGER", |
|
| 134 | + self::TYPE_BIT_STRING => "BIT STRING", |
|
| 135 | + self::TYPE_OCTET_STRING => "OCTET STRING", |
|
| 136 | + self::TYPE_NULL => "NULL", |
|
| 137 | + self::TYPE_OBJECT_IDENTIFIER => "OBJECT IDENTIFIER", |
|
| 138 | + self::TYPE_OBJECT_DESCRIPTOR => "ObjectDescriptor", |
|
| 139 | + self::TYPE_EXTERNAL => "EXTERNAL", |
|
| 140 | + self::TYPE_REAL => "REAL", |
|
| 141 | + self::TYPE_ENUMERATED => "ENUMERATED", |
|
| 142 | + self::TYPE_EMBEDDED_PDV => "EMBEDDED PDV", |
|
| 143 | + self::TYPE_UTF8_STRING => "UTF8String", |
|
| 144 | + self::TYPE_RELATIVE_OID => "RELATIVE-OID", |
|
| 145 | + self::TYPE_SEQUENCE => "SEQUENCE", |
|
| 146 | + self::TYPE_SET => "SET", |
|
| 147 | + self::TYPE_NUMERIC_STRING => "NumericString", |
|
| 148 | + self::TYPE_PRINTABLE_STRING => "PrintableString", |
|
| 149 | + self::TYPE_T61_STRING => "T61String", |
|
| 150 | + self::TYPE_VIDEOTEX_STRING => "VideotexString", |
|
| 151 | + self::TYPE_IA5_STRING => "IA5String", |
|
| 152 | + self::TYPE_UTC_TIME => "UTCTime", |
|
| 153 | + self::TYPE_GENERALIZED_TIME => "GeneralizedTime", |
|
| 154 | + self::TYPE_GRAPHIC_STRING => "GraphicString", |
|
| 155 | + self::TYPE_VISIBLE_STRING => "VisibleString", |
|
| 156 | + self::TYPE_GENERAL_STRING => "GeneralString", |
|
| 157 | + self::TYPE_UNIVERSAL_STRING => "UniversalString", |
|
| 158 | + self::TYPE_CHARACTER_STRING => "CHARACTER STRING", |
|
| 159 | + self::TYPE_BMP_STRING => "BMPString", |
|
| 160 | + self::TYPE_STRING => "Any String", |
|
| 161 | + self::TYPE_TIME => "Any Time", |
|
| 162 | + self::TYPE_CONSTRUCTED_STRING => "Constructed String" |
|
| 163 | + /* @formatter:on */ |
|
| 164 | + ]; |
|
| 165 | 165 | |
| 166 | - /** |
|
| 167 | - * Element's type tag. |
|
| 168 | - * |
|
| 169 | - * @var int |
|
| 170 | - */ |
|
| 171 | - protected $_typeTag; |
|
| 166 | + /** |
|
| 167 | + * Element's type tag. |
|
| 168 | + * |
|
| 169 | + * @var int |
|
| 170 | + */ |
|
| 171 | + protected $_typeTag; |
|
| 172 | 172 | |
| 173 | - /** |
|
| 174 | - * Whether type shall be encoded with indefinite length. |
|
| 175 | - * |
|
| 176 | - * @var bool |
|
| 177 | - */ |
|
| 178 | - protected $_indefiniteLength = false; |
|
| 173 | + /** |
|
| 174 | + * Whether type shall be encoded with indefinite length. |
|
| 175 | + * |
|
| 176 | + * @var bool |
|
| 177 | + */ |
|
| 178 | + protected $_indefiniteLength = false; |
|
| 179 | 179 | |
| 180 | - /** |
|
| 181 | - * |
|
| 182 | - * @see \ASN1\Feature\ElementBase::typeClass() |
|
| 183 | - * @return int |
|
| 184 | - */ |
|
| 185 | - abstract public function typeClass(): int; |
|
| 180 | + /** |
|
| 181 | + * |
|
| 182 | + * @see \ASN1\Feature\ElementBase::typeClass() |
|
| 183 | + * @return int |
|
| 184 | + */ |
|
| 185 | + abstract public function typeClass(): int; |
|
| 186 | 186 | |
| 187 | - /** |
|
| 188 | - * |
|
| 189 | - * @see \ASN1\Feature\ElementBase::isConstructed() |
|
| 190 | - * @return bool |
|
| 191 | - */ |
|
| 192 | - abstract public function isConstructed(): bool; |
|
| 187 | + /** |
|
| 188 | + * |
|
| 189 | + * @see \ASN1\Feature\ElementBase::isConstructed() |
|
| 190 | + * @return bool |
|
| 191 | + */ |
|
| 192 | + abstract public function isConstructed(): bool; |
|
| 193 | 193 | |
| 194 | - /** |
|
| 195 | - * Get the content encoded in DER. |
|
| 196 | - * |
|
| 197 | - * Returns the DER encoded content without identifier and length header |
|
| 198 | - * octets. |
|
| 199 | - * |
|
| 200 | - * @return string |
|
| 201 | - */ |
|
| 202 | - abstract protected function _encodedContentDER(): string; |
|
| 194 | + /** |
|
| 195 | + * Get the content encoded in DER. |
|
| 196 | + * |
|
| 197 | + * Returns the DER encoded content without identifier and length header |
|
| 198 | + * octets. |
|
| 199 | + * |
|
| 200 | + * @return string |
|
| 201 | + */ |
|
| 202 | + abstract protected function _encodedContentDER(): string; |
|
| 203 | 203 | |
| 204 | - /** |
|
| 205 | - * Decode type-specific element from DER. |
|
| 206 | - * |
|
| 207 | - * @param Identifier $identifier Pre-parsed identifier |
|
| 208 | - * @param string $data DER data |
|
| 209 | - * @param int $offset Offset in data to the next byte after identifier |
|
| 210 | - * @throws DecodeException If decoding fails |
|
| 211 | - * @return ElementBase |
|
| 212 | - */ |
|
| 213 | - protected static function _decodeFromDER(Identifier $identifier, |
|
| 214 | - string $data, int &$offset): ElementBase |
|
| 215 | - { |
|
| 216 | - throw new \BadMethodCallException( |
|
| 217 | - __METHOD__ . " must be implemented in derived class."); |
|
| 218 | - } |
|
| 204 | + /** |
|
| 205 | + * Decode type-specific element from DER. |
|
| 206 | + * |
|
| 207 | + * @param Identifier $identifier Pre-parsed identifier |
|
| 208 | + * @param string $data DER data |
|
| 209 | + * @param int $offset Offset in data to the next byte after identifier |
|
| 210 | + * @throws DecodeException If decoding fails |
|
| 211 | + * @return ElementBase |
|
| 212 | + */ |
|
| 213 | + protected static function _decodeFromDER(Identifier $identifier, |
|
| 214 | + string $data, int &$offset): ElementBase |
|
| 215 | + { |
|
| 216 | + throw new \BadMethodCallException( |
|
| 217 | + __METHOD__ . " must be implemented in derived class."); |
|
| 218 | + } |
|
| 219 | 219 | |
| 220 | - /** |
|
| 221 | - * Decode element from DER data. |
|
| 222 | - * |
|
| 223 | - * @param string $data DER encoded data |
|
| 224 | - * @param int|null $offset Reference to the variable that contains offset |
|
| 225 | - * into the data where to start parsing. Variable is updated to |
|
| 226 | - * the offset next to the parsed element. If null, start from offset |
|
| 227 | - * 0. |
|
| 228 | - * @throws DecodeException If decoding fails |
|
| 229 | - * @throws \UnexpectedValueException If called in the context of an expected |
|
| 230 | - * type, but decoding yields another type |
|
| 231 | - * @return ElementBase |
|
| 232 | - */ |
|
| 233 | - public static function fromDER(string $data, int &$offset = null): ElementBase |
|
| 234 | - { |
|
| 235 | - $idx = $offset ?? 0; |
|
| 236 | - // decode identifier |
|
| 237 | - $identifier = Identifier::fromDER($data, $idx); |
|
| 238 | - // determine class that implements type specific decoding |
|
| 239 | - $cls = self::_determineImplClass($identifier); |
|
| 240 | - try { |
|
| 241 | - // decode remaining element |
|
| 242 | - $element = $cls::_decodeFromDER($identifier, $data, $idx); |
|
| 243 | - } catch (\LogicException $e) { |
|
| 244 | - // rethrow as a RuntimeException for unified exception handling |
|
| 245 | - throw new DecodeException( |
|
| 246 | - sprintf("Error while decoding %s.", |
|
| 247 | - self::tagToName($identifier->intTag())), 0, $e); |
|
| 248 | - } |
|
| 249 | - // if called in the context of a concrete class, check |
|
| 250 | - // that decoded type matches the type of a calling class |
|
| 251 | - $called_class = get_called_class(); |
|
| 252 | - if (self::class != $called_class) { |
|
| 253 | - if (!$element instanceof $called_class) { |
|
| 254 | - throw new \UnexpectedValueException( |
|
| 255 | - sprintf("%s expected, got %s.", $called_class, |
|
| 256 | - get_class($element))); |
|
| 257 | - } |
|
| 258 | - } |
|
| 259 | - // update offset for the caller |
|
| 260 | - if (isset($offset)) { |
|
| 261 | - $offset = $idx; |
|
| 262 | - } |
|
| 263 | - return $element; |
|
| 264 | - } |
|
| 220 | + /** |
|
| 221 | + * Decode element from DER data. |
|
| 222 | + * |
|
| 223 | + * @param string $data DER encoded data |
|
| 224 | + * @param int|null $offset Reference to the variable that contains offset |
|
| 225 | + * into the data where to start parsing. Variable is updated to |
|
| 226 | + * the offset next to the parsed element. If null, start from offset |
|
| 227 | + * 0. |
|
| 228 | + * @throws DecodeException If decoding fails |
|
| 229 | + * @throws \UnexpectedValueException If called in the context of an expected |
|
| 230 | + * type, but decoding yields another type |
|
| 231 | + * @return ElementBase |
|
| 232 | + */ |
|
| 233 | + public static function fromDER(string $data, int &$offset = null): ElementBase |
|
| 234 | + { |
|
| 235 | + $idx = $offset ?? 0; |
|
| 236 | + // decode identifier |
|
| 237 | + $identifier = Identifier::fromDER($data, $idx); |
|
| 238 | + // determine class that implements type specific decoding |
|
| 239 | + $cls = self::_determineImplClass($identifier); |
|
| 240 | + try { |
|
| 241 | + // decode remaining element |
|
| 242 | + $element = $cls::_decodeFromDER($identifier, $data, $idx); |
|
| 243 | + } catch (\LogicException $e) { |
|
| 244 | + // rethrow as a RuntimeException for unified exception handling |
|
| 245 | + throw new DecodeException( |
|
| 246 | + sprintf("Error while decoding %s.", |
|
| 247 | + self::tagToName($identifier->intTag())), 0, $e); |
|
| 248 | + } |
|
| 249 | + // if called in the context of a concrete class, check |
|
| 250 | + // that decoded type matches the type of a calling class |
|
| 251 | + $called_class = get_called_class(); |
|
| 252 | + if (self::class != $called_class) { |
|
| 253 | + if (!$element instanceof $called_class) { |
|
| 254 | + throw new \UnexpectedValueException( |
|
| 255 | + sprintf("%s expected, got %s.", $called_class, |
|
| 256 | + get_class($element))); |
|
| 257 | + } |
|
| 258 | + } |
|
| 259 | + // update offset for the caller |
|
| 260 | + if (isset($offset)) { |
|
| 261 | + $offset = $idx; |
|
| 262 | + } |
|
| 263 | + return $element; |
|
| 264 | + } |
|
| 265 | 265 | |
| 266 | - /** |
|
| 267 | - * |
|
| 268 | - * @see \ASN1\Feature\Encodable::toDER() |
|
| 269 | - * @return string |
|
| 270 | - */ |
|
| 271 | - public function toDER(): string |
|
| 272 | - { |
|
| 273 | - $identifier = new Identifier($this->typeClass(), |
|
| 274 | - $this->isConstructed() ? Identifier::CONSTRUCTED : Identifier::PRIMITIVE, |
|
| 275 | - $this->_typeTag); |
|
| 276 | - $content = $this->_encodedContentDER(); |
|
| 277 | - if ($this->_indefiniteLength) { |
|
| 278 | - $length = new Length(0, true); |
|
| 279 | - $eoc = new Primitive\EOC(); |
|
| 280 | - return $identifier->toDER() . $length->toDER() . $content . |
|
| 281 | - $eoc->toDER(); |
|
| 282 | - } else { |
|
| 283 | - $length = new Length(strlen($content)); |
|
| 284 | - return $identifier->toDER() . $length->toDER() . $content; |
|
| 285 | - } |
|
| 286 | - } |
|
| 266 | + /** |
|
| 267 | + * |
|
| 268 | + * @see \ASN1\Feature\Encodable::toDER() |
|
| 269 | + * @return string |
|
| 270 | + */ |
|
| 271 | + public function toDER(): string |
|
| 272 | + { |
|
| 273 | + $identifier = new Identifier($this->typeClass(), |
|
| 274 | + $this->isConstructed() ? Identifier::CONSTRUCTED : Identifier::PRIMITIVE, |
|
| 275 | + $this->_typeTag); |
|
| 276 | + $content = $this->_encodedContentDER(); |
|
| 277 | + if ($this->_indefiniteLength) { |
|
| 278 | + $length = new Length(0, true); |
|
| 279 | + $eoc = new Primitive\EOC(); |
|
| 280 | + return $identifier->toDER() . $length->toDER() . $content . |
|
| 281 | + $eoc->toDER(); |
|
| 282 | + } else { |
|
| 283 | + $length = new Length(strlen($content)); |
|
| 284 | + return $identifier->toDER() . $length->toDER() . $content; |
|
| 285 | + } |
|
| 286 | + } |
|
| 287 | 287 | |
| 288 | - /** |
|
| 289 | - * |
|
| 290 | - * @see \ASN1\Feature\ElementBase::tag() |
|
| 291 | - * @return int |
|
| 292 | - */ |
|
| 293 | - public function tag(): int |
|
| 294 | - { |
|
| 295 | - return $this->_typeTag; |
|
| 296 | - } |
|
| 288 | + /** |
|
| 289 | + * |
|
| 290 | + * @see \ASN1\Feature\ElementBase::tag() |
|
| 291 | + * @return int |
|
| 292 | + */ |
|
| 293 | + public function tag(): int |
|
| 294 | + { |
|
| 295 | + return $this->_typeTag; |
|
| 296 | + } |
|
| 297 | 297 | |
| 298 | - /** |
|
| 299 | - * |
|
| 300 | - * @see \ASN1\Feature\ElementBase::isType() |
|
| 301 | - * @return bool |
|
| 302 | - */ |
|
| 303 | - public function isType(int $tag): bool |
|
| 304 | - { |
|
| 305 | - // if element is context specific |
|
| 306 | - if ($this->typeClass() == Identifier::CLASS_CONTEXT_SPECIFIC) { |
|
| 307 | - return false; |
|
| 308 | - } |
|
| 309 | - // negative tags identify an abstract pseudotype |
|
| 310 | - if ($tag < 0) { |
|
| 311 | - return $this->_isPseudoType($tag); |
|
| 312 | - } |
|
| 313 | - return $this->_isConcreteType($tag); |
|
| 314 | - } |
|
| 298 | + /** |
|
| 299 | + * |
|
| 300 | + * @see \ASN1\Feature\ElementBase::isType() |
|
| 301 | + * @return bool |
|
| 302 | + */ |
|
| 303 | + public function isType(int $tag): bool |
|
| 304 | + { |
|
| 305 | + // if element is context specific |
|
| 306 | + if ($this->typeClass() == Identifier::CLASS_CONTEXT_SPECIFIC) { |
|
| 307 | + return false; |
|
| 308 | + } |
|
| 309 | + // negative tags identify an abstract pseudotype |
|
| 310 | + if ($tag < 0) { |
|
| 311 | + return $this->_isPseudoType($tag); |
|
| 312 | + } |
|
| 313 | + return $this->_isConcreteType($tag); |
|
| 314 | + } |
|
| 315 | 315 | |
| 316 | - /** |
|
| 317 | - * |
|
| 318 | - * @see \ASN1\Feature\ElementBase::expectType() |
|
| 319 | - * @return ElementBase |
|
| 320 | - */ |
|
| 321 | - public function expectType(int $tag): ElementBase |
|
| 322 | - { |
|
| 323 | - if (!$this->isType($tag)) { |
|
| 324 | - throw new \UnexpectedValueException( |
|
| 325 | - sprintf("%s expected, got %s.", self::tagToName($tag), |
|
| 326 | - $this->_typeDescriptorString())); |
|
| 327 | - } |
|
| 328 | - return $this; |
|
| 329 | - } |
|
| 316 | + /** |
|
| 317 | + * |
|
| 318 | + * @see \ASN1\Feature\ElementBase::expectType() |
|
| 319 | + * @return ElementBase |
|
| 320 | + */ |
|
| 321 | + public function expectType(int $tag): ElementBase |
|
| 322 | + { |
|
| 323 | + if (!$this->isType($tag)) { |
|
| 324 | + throw new \UnexpectedValueException( |
|
| 325 | + sprintf("%s expected, got %s.", self::tagToName($tag), |
|
| 326 | + $this->_typeDescriptorString())); |
|
| 327 | + } |
|
| 328 | + return $this; |
|
| 329 | + } |
|
| 330 | 330 | |
| 331 | - /** |
|
| 332 | - * Check whether the element is a concrete type of a given tag. |
|
| 333 | - * |
|
| 334 | - * @param int $tag |
|
| 335 | - * @return bool |
|
| 336 | - */ |
|
| 337 | - private function _isConcreteType(int $tag): bool |
|
| 338 | - { |
|
| 339 | - // if tag doesn't match |
|
| 340 | - if ($this->tag() != $tag) { |
|
| 341 | - return false; |
|
| 342 | - } |
|
| 343 | - // if type is universal check that instance is of a correct class |
|
| 344 | - if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) { |
|
| 345 | - $cls = self::_determineUniversalImplClass($tag); |
|
| 346 | - if (!$this instanceof $cls) { |
|
| 347 | - return false; |
|
| 348 | - } |
|
| 349 | - } |
|
| 350 | - return true; |
|
| 351 | - } |
|
| 331 | + /** |
|
| 332 | + * Check whether the element is a concrete type of a given tag. |
|
| 333 | + * |
|
| 334 | + * @param int $tag |
|
| 335 | + * @return bool |
|
| 336 | + */ |
|
| 337 | + private function _isConcreteType(int $tag): bool |
|
| 338 | + { |
|
| 339 | + // if tag doesn't match |
|
| 340 | + if ($this->tag() != $tag) { |
|
| 341 | + return false; |
|
| 342 | + } |
|
| 343 | + // if type is universal check that instance is of a correct class |
|
| 344 | + if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) { |
|
| 345 | + $cls = self::_determineUniversalImplClass($tag); |
|
| 346 | + if (!$this instanceof $cls) { |
|
| 347 | + return false; |
|
| 348 | + } |
|
| 349 | + } |
|
| 350 | + return true; |
|
| 351 | + } |
|
| 352 | 352 | |
| 353 | - /** |
|
| 354 | - * Check whether the element is a pseudotype. |
|
| 355 | - * |
|
| 356 | - * @param int $tag |
|
| 357 | - * @return bool |
|
| 358 | - */ |
|
| 359 | - private function _isPseudoType(int $tag): bool |
|
| 360 | - { |
|
| 361 | - switch ($tag) { |
|
| 362 | - case self::TYPE_STRING: |
|
| 363 | - return $this instanceof StringType; |
|
| 364 | - case self::TYPE_TIME: |
|
| 365 | - return $this instanceof TimeType; |
|
| 366 | - case self::TYPE_CONSTRUCTED_STRING: |
|
| 367 | - return $this instanceof ConstructedString; |
|
| 368 | - } |
|
| 369 | - return false; |
|
| 370 | - } |
|
| 353 | + /** |
|
| 354 | + * Check whether the element is a pseudotype. |
|
| 355 | + * |
|
| 356 | + * @param int $tag |
|
| 357 | + * @return bool |
|
| 358 | + */ |
|
| 359 | + private function _isPseudoType(int $tag): bool |
|
| 360 | + { |
|
| 361 | + switch ($tag) { |
|
| 362 | + case self::TYPE_STRING: |
|
| 363 | + return $this instanceof StringType; |
|
| 364 | + case self::TYPE_TIME: |
|
| 365 | + return $this instanceof TimeType; |
|
| 366 | + case self::TYPE_CONSTRUCTED_STRING: |
|
| 367 | + return $this instanceof ConstructedString; |
|
| 368 | + } |
|
| 369 | + return false; |
|
| 370 | + } |
|
| 371 | 371 | |
| 372 | - /** |
|
| 373 | - * |
|
| 374 | - * @see \ASN1\Feature\ElementBase::isTagged() |
|
| 375 | - * @return bool |
|
| 376 | - */ |
|
| 377 | - public function isTagged(): bool |
|
| 378 | - { |
|
| 379 | - return $this instanceof TaggedType; |
|
| 380 | - } |
|
| 372 | + /** |
|
| 373 | + * |
|
| 374 | + * @see \ASN1\Feature\ElementBase::isTagged() |
|
| 375 | + * @return bool |
|
| 376 | + */ |
|
| 377 | + public function isTagged(): bool |
|
| 378 | + { |
|
| 379 | + return $this instanceof TaggedType; |
|
| 380 | + } |
|
| 381 | 381 | |
| 382 | - /** |
|
| 383 | - * |
|
| 384 | - * @see \ASN1\Feature\ElementBase::expectTagged() |
|
| 385 | - * @return TaggedType |
|
| 386 | - */ |
|
| 387 | - public function expectTagged($tag = null): TaggedType |
|
| 388 | - { |
|
| 389 | - if (!$this->isTagged()) { |
|
| 390 | - throw new \UnexpectedValueException( |
|
| 391 | - sprintf("Context specific element expected, got %s.", |
|
| 392 | - Identifier::classToName($this->typeClass()))); |
|
| 393 | - } |
|
| 394 | - if (isset($tag) && $this->tag() != $tag) { |
|
| 395 | - throw new \UnexpectedValueException( |
|
| 396 | - sprintf("Tag %d expected, got %d.", $tag, $this->tag())); |
|
| 397 | - } |
|
| 398 | - return $this; |
|
| 399 | - } |
|
| 382 | + /** |
|
| 383 | + * |
|
| 384 | + * @see \ASN1\Feature\ElementBase::expectTagged() |
|
| 385 | + * @return TaggedType |
|
| 386 | + */ |
|
| 387 | + public function expectTagged($tag = null): TaggedType |
|
| 388 | + { |
|
| 389 | + if (!$this->isTagged()) { |
|
| 390 | + throw new \UnexpectedValueException( |
|
| 391 | + sprintf("Context specific element expected, got %s.", |
|
| 392 | + Identifier::classToName($this->typeClass()))); |
|
| 393 | + } |
|
| 394 | + if (isset($tag) && $this->tag() != $tag) { |
|
| 395 | + throw new \UnexpectedValueException( |
|
| 396 | + sprintf("Tag %d expected, got %d.", $tag, $this->tag())); |
|
| 397 | + } |
|
| 398 | + return $this; |
|
| 399 | + } |
|
| 400 | 400 | |
| 401 | - /** |
|
| 402 | - * Whether element has indefinite length. |
|
| 403 | - * |
|
| 404 | - * @return bool |
|
| 405 | - */ |
|
| 406 | - public function hasIndefiniteLength(): bool |
|
| 407 | - { |
|
| 408 | - return $this->_indefiniteLength; |
|
| 409 | - } |
|
| 401 | + /** |
|
| 402 | + * Whether element has indefinite length. |
|
| 403 | + * |
|
| 404 | + * @return bool |
|
| 405 | + */ |
|
| 406 | + public function hasIndefiniteLength(): bool |
|
| 407 | + { |
|
| 408 | + return $this->_indefiniteLength; |
|
| 409 | + } |
|
| 410 | 410 | |
| 411 | - /** |
|
| 412 | - * Get self with indefinite length encoding set. |
|
| 413 | - * |
|
| 414 | - * @param bool $indefinite True for indefinite length, false for definite |
|
| 415 | - * length |
|
| 416 | - * @return self |
|
| 417 | - */ |
|
| 418 | - public function withIndefiniteLength(bool $indefinite = true): self |
|
| 419 | - { |
|
| 420 | - $obj = clone $this; |
|
| 421 | - $obj->_indefiniteLength = $indefinite; |
|
| 422 | - return $obj; |
|
| 423 | - } |
|
| 411 | + /** |
|
| 412 | + * Get self with indefinite length encoding set. |
|
| 413 | + * |
|
| 414 | + * @param bool $indefinite True for indefinite length, false for definite |
|
| 415 | + * length |
|
| 416 | + * @return self |
|
| 417 | + */ |
|
| 418 | + public function withIndefiniteLength(bool $indefinite = true): self |
|
| 419 | + { |
|
| 420 | + $obj = clone $this; |
|
| 421 | + $obj->_indefiniteLength = $indefinite; |
|
| 422 | + return $obj; |
|
| 423 | + } |
|
| 424 | 424 | |
| 425 | - /** |
|
| 426 | - * |
|
| 427 | - * @see \ASN1\Feature\ElementBase::asElement() |
|
| 428 | - * @return Element |
|
| 429 | - */ |
|
| 430 | - final public function asElement(): Element |
|
| 431 | - { |
|
| 432 | - return $this; |
|
| 433 | - } |
|
| 425 | + /** |
|
| 426 | + * |
|
| 427 | + * @see \ASN1\Feature\ElementBase::asElement() |
|
| 428 | + * @return Element |
|
| 429 | + */ |
|
| 430 | + final public function asElement(): Element |
|
| 431 | + { |
|
| 432 | + return $this; |
|
| 433 | + } |
|
| 434 | 434 | |
| 435 | - /** |
|
| 436 | - * Get element decorated with UnspecifiedType object. |
|
| 437 | - * |
|
| 438 | - * @return UnspecifiedType |
|
| 439 | - */ |
|
| 440 | - public function asUnspecified(): UnspecifiedType |
|
| 441 | - { |
|
| 442 | - return new UnspecifiedType($this); |
|
| 443 | - } |
|
| 435 | + /** |
|
| 436 | + * Get element decorated with UnspecifiedType object. |
|
| 437 | + * |
|
| 438 | + * @return UnspecifiedType |
|
| 439 | + */ |
|
| 440 | + public function asUnspecified(): UnspecifiedType |
|
| 441 | + { |
|
| 442 | + return new UnspecifiedType($this); |
|
| 443 | + } |
|
| 444 | 444 | |
| 445 | - /** |
|
| 446 | - * Determine the class that implements the type. |
|
| 447 | - * |
|
| 448 | - * @param Identifier $identifier |
|
| 449 | - * @return string Class name |
|
| 450 | - */ |
|
| 451 | - protected static function _determineImplClass(Identifier $identifier): string |
|
| 452 | - { |
|
| 453 | - switch ($identifier->typeClass()) { |
|
| 454 | - case Identifier::CLASS_UNIVERSAL: |
|
| 455 | - $cls = self::_determineUniversalImplClass($identifier->intTag()); |
|
| 456 | - // constructed strings may be present in BER |
|
| 457 | - if ($identifier->isConstructed() && |
|
| 458 | - is_subclass_of($cls, PrimitiveString::class)) { |
|
| 459 | - $cls = ConstructedString::class; |
|
| 460 | - } |
|
| 461 | - return $cls; |
|
| 462 | - case Identifier::CLASS_CONTEXT_SPECIFIC: |
|
| 463 | - return ContextSpecificType::class; |
|
| 464 | - case Identifier::CLASS_APPLICATION: |
|
| 465 | - return ApplicationType::class; |
|
| 466 | - case Identifier::CLASS_PRIVATE: |
|
| 467 | - return PrivateType::class; |
|
| 468 | - } |
|
| 469 | - throw new \UnexpectedValueException( |
|
| 470 | - sprintf("%s %d not implemented.", |
|
| 471 | - Identifier::classToName($identifier->typeClass()), |
|
| 472 | - $identifier->tag())); |
|
| 473 | - } |
|
| 445 | + /** |
|
| 446 | + * Determine the class that implements the type. |
|
| 447 | + * |
|
| 448 | + * @param Identifier $identifier |
|
| 449 | + * @return string Class name |
|
| 450 | + */ |
|
| 451 | + protected static function _determineImplClass(Identifier $identifier): string |
|
| 452 | + { |
|
| 453 | + switch ($identifier->typeClass()) { |
|
| 454 | + case Identifier::CLASS_UNIVERSAL: |
|
| 455 | + $cls = self::_determineUniversalImplClass($identifier->intTag()); |
|
| 456 | + // constructed strings may be present in BER |
|
| 457 | + if ($identifier->isConstructed() && |
|
| 458 | + is_subclass_of($cls, PrimitiveString::class)) { |
|
| 459 | + $cls = ConstructedString::class; |
|
| 460 | + } |
|
| 461 | + return $cls; |
|
| 462 | + case Identifier::CLASS_CONTEXT_SPECIFIC: |
|
| 463 | + return ContextSpecificType::class; |
|
| 464 | + case Identifier::CLASS_APPLICATION: |
|
| 465 | + return ApplicationType::class; |
|
| 466 | + case Identifier::CLASS_PRIVATE: |
|
| 467 | + return PrivateType::class; |
|
| 468 | + } |
|
| 469 | + throw new \UnexpectedValueException( |
|
| 470 | + sprintf("%s %d not implemented.", |
|
| 471 | + Identifier::classToName($identifier->typeClass()), |
|
| 472 | + $identifier->tag())); |
|
| 473 | + } |
|
| 474 | 474 | |
| 475 | - /** |
|
| 476 | - * Determine the class that implements an universal type of the given tag. |
|
| 477 | - * |
|
| 478 | - * @param int $tag |
|
| 479 | - * @throws \UnexpectedValueException |
|
| 480 | - * @return string Class name |
|
| 481 | - */ |
|
| 482 | - protected static function _determineUniversalImplClass(int $tag): string |
|
| 483 | - { |
|
| 484 | - if (!array_key_exists($tag, self::MAP_TAG_TO_CLASS)) { |
|
| 485 | - throw new \UnexpectedValueException( |
|
| 486 | - "Universal tag $tag not implemented."); |
|
| 487 | - } |
|
| 488 | - return self::MAP_TAG_TO_CLASS[$tag]; |
|
| 489 | - } |
|
| 475 | + /** |
|
| 476 | + * Determine the class that implements an universal type of the given tag. |
|
| 477 | + * |
|
| 478 | + * @param int $tag |
|
| 479 | + * @throws \UnexpectedValueException |
|
| 480 | + * @return string Class name |
|
| 481 | + */ |
|
| 482 | + protected static function _determineUniversalImplClass(int $tag): string |
|
| 483 | + { |
|
| 484 | + if (!array_key_exists($tag, self::MAP_TAG_TO_CLASS)) { |
|
| 485 | + throw new \UnexpectedValueException( |
|
| 486 | + "Universal tag $tag not implemented."); |
|
| 487 | + } |
|
| 488 | + return self::MAP_TAG_TO_CLASS[$tag]; |
|
| 489 | + } |
|
| 490 | 490 | |
| 491 | - /** |
|
| 492 | - * Get textual description of the type for debugging purposes. |
|
| 493 | - * |
|
| 494 | - * @return string |
|
| 495 | - */ |
|
| 496 | - protected function _typeDescriptorString(): string |
|
| 497 | - { |
|
| 498 | - if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) { |
|
| 499 | - return self::tagToName($this->_typeTag); |
|
| 500 | - } |
|
| 501 | - return sprintf("%s TAG %d", Identifier::classToName($this->typeClass()), |
|
| 502 | - $this->_typeTag); |
|
| 503 | - } |
|
| 491 | + /** |
|
| 492 | + * Get textual description of the type for debugging purposes. |
|
| 493 | + * |
|
| 494 | + * @return string |
|
| 495 | + */ |
|
| 496 | + protected function _typeDescriptorString(): string |
|
| 497 | + { |
|
| 498 | + if ($this->typeClass() == Identifier::CLASS_UNIVERSAL) { |
|
| 499 | + return self::tagToName($this->_typeTag); |
|
| 500 | + } |
|
| 501 | + return sprintf("%s TAG %d", Identifier::classToName($this->typeClass()), |
|
| 502 | + $this->_typeTag); |
|
| 503 | + } |
|
| 504 | 504 | |
| 505 | - /** |
|
| 506 | - * Get human readable name for an universal tag. |
|
| 507 | - * |
|
| 508 | - * @param int $tag |
|
| 509 | - * @return string |
|
| 510 | - */ |
|
| 511 | - public static function tagToName(int $tag): string |
|
| 512 | - { |
|
| 513 | - if (!array_key_exists($tag, self::MAP_TYPE_TO_NAME)) { |
|
| 514 | - return "TAG $tag"; |
|
| 515 | - } |
|
| 516 | - return self::MAP_TYPE_TO_NAME[$tag]; |
|
| 517 | - } |
|
| 505 | + /** |
|
| 506 | + * Get human readable name for an universal tag. |
|
| 507 | + * |
|
| 508 | + * @param int $tag |
|
| 509 | + * @return string |
|
| 510 | + */ |
|
| 511 | + public static function tagToName(int $tag): string |
|
| 512 | + { |
|
| 513 | + if (!array_key_exists($tag, self::MAP_TYPE_TO_NAME)) { |
|
| 514 | + return "TAG $tag"; |
|
| 515 | + } |
|
| 516 | + return self::MAP_TYPE_TO_NAME[$tag]; |
|
| 517 | + } |
|
| 518 | 518 | } |
@@ -359,12 +359,12 @@ discard block |
||
| 359 | 359 | private function _isPseudoType(int $tag): bool |
| 360 | 360 | { |
| 361 | 361 | switch ($tag) { |
| 362 | - case self::TYPE_STRING: |
|
| 363 | - return $this instanceof StringType; |
|
| 364 | - case self::TYPE_TIME: |
|
| 365 | - return $this instanceof TimeType; |
|
| 366 | - case self::TYPE_CONSTRUCTED_STRING: |
|
| 367 | - return $this instanceof ConstructedString; |
|
| 362 | + case self::TYPE_STRING: |
|
| 363 | + return $this instanceof StringType; |
|
| 364 | + case self::TYPE_TIME: |
|
| 365 | + return $this instanceof TimeType; |
|
| 366 | + case self::TYPE_CONSTRUCTED_STRING: |
|
| 367 | + return $this instanceof ConstructedString; |
|
| 368 | 368 | } |
| 369 | 369 | return false; |
| 370 | 370 | } |
@@ -451,20 +451,20 @@ discard block |
||
| 451 | 451 | protected static function _determineImplClass(Identifier $identifier): string |
| 452 | 452 | { |
| 453 | 453 | switch ($identifier->typeClass()) { |
| 454 | - case Identifier::CLASS_UNIVERSAL: |
|
| 455 | - $cls = self::_determineUniversalImplClass($identifier->intTag()); |
|
| 456 | - // constructed strings may be present in BER |
|
| 457 | - if ($identifier->isConstructed() && |
|
| 458 | - is_subclass_of($cls, PrimitiveString::class)) { |
|
| 459 | - $cls = ConstructedString::class; |
|
| 460 | - } |
|
| 461 | - return $cls; |
|
| 462 | - case Identifier::CLASS_CONTEXT_SPECIFIC: |
|
| 463 | - return ContextSpecificType::class; |
|
| 464 | - case Identifier::CLASS_APPLICATION: |
|
| 465 | - return ApplicationType::class; |
|
| 466 | - case Identifier::CLASS_PRIVATE: |
|
| 467 | - return PrivateType::class; |
|
| 454 | + case Identifier::CLASS_UNIVERSAL: |
|
| 455 | + $cls = self::_determineUniversalImplClass($identifier->intTag()); |
|
| 456 | + // constructed strings may be present in BER |
|
| 457 | + if ($identifier->isConstructed() && |
|
| 458 | + is_subclass_of($cls, PrimitiveString::class)) { |
|
| 459 | + $cls = ConstructedString::class; |
|
| 460 | + } |
|
| 461 | + return $cls; |
|
| 462 | + case Identifier::CLASS_CONTEXT_SPECIFIC: |
|
| 463 | + return ContextSpecificType::class; |
|
| 464 | + case Identifier::CLASS_APPLICATION: |
|
| 465 | + return ApplicationType::class; |
|
| 466 | + case Identifier::CLASS_PRIVATE: |
|
| 467 | + return PrivateType::class; |
|
| 468 | 468 | } |
| 469 | 469 | throw new \UnexpectedValueException( |
| 470 | 470 | sprintf("%s %d not implemented.", |
@@ -8,17 +8,17 @@ |
||
| 8 | 8 | */ |
| 9 | 9 | interface Stringable |
| 10 | 10 | { |
| 11 | - /** |
|
| 12 | - * Get the string representation of the type. |
|
| 13 | - * |
|
| 14 | - * @return string |
|
| 15 | - */ |
|
| 16 | - public function string(): string; |
|
| 11 | + /** |
|
| 12 | + * Get the string representation of the type. |
|
| 13 | + * |
|
| 14 | + * @return string |
|
| 15 | + */ |
|
| 16 | + public function string(): string; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * |
|
| 20 | - * @inheritdoc |
|
| 21 | - * @return string |
|
| 22 | - */ |
|
| 23 | - public function __toString(): string; |
|
| 18 | + /** |
|
| 19 | + * |
|
| 20 | + * @inheritdoc |
|
| 21 | + * @return string |
|
| 22 | + */ |
|
| 23 | + public function __toString(): string; |
|
| 24 | 24 | } |
@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types = 1); |
|
| 3 | +declare(strict_types=1); |
|
| 4 | 4 | |
| 5 | 5 | namespace ASN1\Feature; |
| 6 | 6 | |