@@ -17,667 +17,667 @@ |
||
| 17 | 17 | */ |
| 18 | 18 | class UnspecifiedType implements ElementBase |
| 19 | 19 | { |
| 20 | - /** |
|
| 21 | - * The wrapped element. |
|
| 22 | - * |
|
| 23 | - * @var Element |
|
| 24 | - */ |
|
| 25 | - private $_element; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * Constructor. |
|
| 29 | - * |
|
| 30 | - * @param Element $el |
|
| 31 | - */ |
|
| 32 | - public function __construct(Element $el) |
|
| 33 | - { |
|
| 34 | - $this->_element = $el; |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Initialize from DER data. |
|
| 39 | - * |
|
| 40 | - * @param string $data DER encoded data |
|
| 41 | - * |
|
| 42 | - * @return self |
|
| 43 | - */ |
|
| 44 | - public static function fromDER(string $data): self |
|
| 45 | - { |
|
| 46 | - return Element::fromDER($data)->asUnspecified(); |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - /** |
|
| 50 | - * Initialize from ElementBase interface. |
|
| 51 | - * |
|
| 52 | - * @param ElementBase $el |
|
| 53 | - * |
|
| 54 | - * @return self |
|
| 55 | - */ |
|
| 56 | - public static function fromElementBase(ElementBase $el): self |
|
| 57 | - { |
|
| 58 | - // if element is already wrapped |
|
| 59 | - if ($el instanceof self) { |
|
| 60 | - return $el; |
|
| 61 | - } |
|
| 62 | - return new self($el->asElement()); |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * Get the wrapped element as a context specific tagged type. |
|
| 67 | - * |
|
| 68 | - * @throws \UnexpectedValueException If the element is not tagged |
|
| 69 | - * |
|
| 70 | - * @return TaggedType |
|
| 71 | - */ |
|
| 72 | - public function asTagged(): TaggedType |
|
| 73 | - { |
|
| 74 | - if (!$this->_element instanceof TaggedType) { |
|
| 75 | - throw new \UnexpectedValueException( |
|
| 76 | - 'Tagged element expected, got ' . $this->_typeDescriptorString()); |
|
| 77 | - } |
|
| 78 | - return $this->_element; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Get the wrapped element as an application specific type. |
|
| 83 | - * |
|
| 84 | - * @throws \UnexpectedValueException If element is not application specific |
|
| 85 | - * |
|
| 86 | - * @return \Sop\ASN1\Type\Tagged\ApplicationType |
|
| 87 | - */ |
|
| 88 | - public function asApplication(): Tagged\ApplicationType |
|
| 89 | - { |
|
| 90 | - if (!$this->_element instanceof Tagged\ApplicationType) { |
|
| 91 | - throw new \UnexpectedValueException( |
|
| 92 | - 'Application type expected, got ' . |
|
| 93 | - $this->_typeDescriptorString()); |
|
| 94 | - } |
|
| 95 | - return $this->_element; |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - /** |
|
| 99 | - * Get the wrapped element as a private tagged type. |
|
| 100 | - * |
|
| 101 | - * @throws \UnexpectedValueException If element is not using private tagging |
|
| 102 | - * |
|
| 103 | - * @return \Sop\ASN1\Type\Tagged\PrivateType |
|
| 104 | - */ |
|
| 105 | - public function asPrivate(): Tagged\PrivateType |
|
| 106 | - { |
|
| 107 | - if (!$this->_element instanceof Tagged\PrivateType) { |
|
| 108 | - throw new \UnexpectedValueException( |
|
| 109 | - 'Private type expected, got ' . $this->_typeDescriptorString()); |
|
| 110 | - } |
|
| 111 | - return $this->_element; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * Get the wrapped element as a boolean type. |
|
| 116 | - * |
|
| 117 | - * @throws \UnexpectedValueException If the element is not a boolean |
|
| 118 | - * |
|
| 119 | - * @return \Sop\ASN1\Type\Primitive\Boolean |
|
| 120 | - */ |
|
| 121 | - public function asBoolean(): Primitive\Boolean |
|
| 122 | - { |
|
| 123 | - if (!$this->_element instanceof Primitive\Boolean) { |
|
| 124 | - throw new \UnexpectedValueException( |
|
| 125 | - $this->_generateExceptionMessage(Element::TYPE_BOOLEAN)); |
|
| 126 | - } |
|
| 127 | - return $this->_element; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * Get the wrapped element as an integer type. |
|
| 132 | - * |
|
| 133 | - * @throws \UnexpectedValueException If the element is not an integer |
|
| 134 | - * |
|
| 135 | - * @return \Sop\ASN1\Type\Primitive\Integer |
|
| 136 | - */ |
|
| 137 | - public function asInteger(): Primitive\Integer |
|
| 138 | - { |
|
| 139 | - if (!$this->_element instanceof Primitive\Integer) { |
|
| 140 | - throw new \UnexpectedValueException( |
|
| 141 | - $this->_generateExceptionMessage(Element::TYPE_INTEGER)); |
|
| 142 | - } |
|
| 143 | - return $this->_element; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * Get the wrapped element as a bit string type. |
|
| 148 | - * |
|
| 149 | - * @throws \UnexpectedValueException If the element is not a bit string |
|
| 150 | - * |
|
| 151 | - * @return \Sop\ASN1\Type\Primitive\BitString |
|
| 152 | - */ |
|
| 153 | - public function asBitString(): Primitive\BitString |
|
| 154 | - { |
|
| 155 | - if (!$this->_element instanceof Primitive\BitString) { |
|
| 156 | - throw new \UnexpectedValueException( |
|
| 157 | - $this->_generateExceptionMessage(Element::TYPE_BIT_STRING)); |
|
| 158 | - } |
|
| 159 | - return $this->_element; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Get the wrapped element as an octet string type. |
|
| 164 | - * |
|
| 165 | - * @throws \UnexpectedValueException If the element is not an octet string |
|
| 166 | - * |
|
| 167 | - * @return \Sop\ASN1\Type\Primitive\OctetString |
|
| 168 | - */ |
|
| 169 | - public function asOctetString(): Primitive\OctetString |
|
| 170 | - { |
|
| 171 | - if (!$this->_element instanceof Primitive\OctetString) { |
|
| 172 | - throw new \UnexpectedValueException( |
|
| 173 | - $this->_generateExceptionMessage(Element::TYPE_OCTET_STRING)); |
|
| 174 | - } |
|
| 175 | - return $this->_element; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * Get the wrapped element as a null type. |
|
| 180 | - * |
|
| 181 | - * @throws \UnexpectedValueException If the element is not a null |
|
| 182 | - * |
|
| 183 | - * @return \Sop\ASN1\Type\Primitive\NullType |
|
| 184 | - */ |
|
| 185 | - public function asNull(): Primitive\NullType |
|
| 186 | - { |
|
| 187 | - if (!$this->_element instanceof Primitive\NullType) { |
|
| 188 | - throw new \UnexpectedValueException( |
|
| 189 | - $this->_generateExceptionMessage(Element::TYPE_NULL)); |
|
| 190 | - } |
|
| 191 | - return $this->_element; |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * Get the wrapped element as an object identifier type. |
|
| 196 | - * |
|
| 197 | - * @throws \UnexpectedValueException If the element is not an object |
|
| 198 | - * identifier |
|
| 199 | - * |
|
| 200 | - * @return \Sop\ASN1\Type\Primitive\ObjectIdentifier |
|
| 201 | - */ |
|
| 202 | - public function asObjectIdentifier(): Primitive\ObjectIdentifier |
|
| 203 | - { |
|
| 204 | - if (!$this->_element instanceof Primitive\ObjectIdentifier) { |
|
| 205 | - throw new \UnexpectedValueException( |
|
| 206 | - $this->_generateExceptionMessage( |
|
| 207 | - Element::TYPE_OBJECT_IDENTIFIER)); |
|
| 208 | - } |
|
| 209 | - return $this->_element; |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * Get the wrapped element as an object descriptor type. |
|
| 214 | - * |
|
| 215 | - * @throws \UnexpectedValueException If the element is not an object |
|
| 216 | - * descriptor |
|
| 217 | - * |
|
| 218 | - * @return \Sop\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 | - * |
|
| 235 | - * @return \Sop\ASN1\Type\Primitive\Real |
|
| 236 | - */ |
|
| 237 | - public function asReal(): Primitive\Real |
|
| 238 | - { |
|
| 239 | - if (!$this->_element instanceof Primitive\Real) { |
|
| 240 | - throw new \UnexpectedValueException( |
|
| 241 | - $this->_generateExceptionMessage(Element::TYPE_REAL)); |
|
| 242 | - } |
|
| 243 | - return $this->_element; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - /** |
|
| 247 | - * Get the wrapped element as an enumerated type. |
|
| 248 | - * |
|
| 249 | - * @throws \UnexpectedValueException If the element is not an enumerated |
|
| 250 | - * |
|
| 251 | - * @return \Sop\ASN1\Type\Primitive\Enumerated |
|
| 252 | - */ |
|
| 253 | - public function asEnumerated(): Primitive\Enumerated |
|
| 254 | - { |
|
| 255 | - if (!$this->_element instanceof Primitive\Enumerated) { |
|
| 256 | - throw new \UnexpectedValueException( |
|
| 257 | - $this->_generateExceptionMessage(Element::TYPE_ENUMERATED)); |
|
| 258 | - } |
|
| 259 | - return $this->_element; |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - /** |
|
| 263 | - * Get the wrapped element as a UTF8 string type. |
|
| 264 | - * |
|
| 265 | - * @throws \UnexpectedValueException If the element is not a UTF8 string |
|
| 266 | - * |
|
| 267 | - * @return \Sop\ASN1\Type\Primitive\UTF8String |
|
| 268 | - */ |
|
| 269 | - public function asUTF8String(): Primitive\UTF8String |
|
| 270 | - { |
|
| 271 | - if (!$this->_element instanceof Primitive\UTF8String) { |
|
| 272 | - throw new \UnexpectedValueException( |
|
| 273 | - $this->_generateExceptionMessage(Element::TYPE_UTF8_STRING)); |
|
| 274 | - } |
|
| 275 | - return $this->_element; |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - /** |
|
| 279 | - * Get the wrapped element as a relative OID type. |
|
| 280 | - * |
|
| 281 | - * @throws \UnexpectedValueException If the element is not a relative OID |
|
| 282 | - * |
|
| 283 | - * @return \Sop\ASN1\Type\Primitive\RelativeOID |
|
| 284 | - */ |
|
| 285 | - public function asRelativeOID(): Primitive\RelativeOID |
|
| 286 | - { |
|
| 287 | - if (!$this->_element instanceof Primitive\RelativeOID) { |
|
| 288 | - throw new \UnexpectedValueException( |
|
| 289 | - $this->_generateExceptionMessage(Element::TYPE_RELATIVE_OID)); |
|
| 290 | - } |
|
| 291 | - return $this->_element; |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - /** |
|
| 295 | - * Get the wrapped element as a sequence type. |
|
| 296 | - * |
|
| 297 | - * @throws \UnexpectedValueException If the element is not a sequence |
|
| 298 | - * |
|
| 299 | - * @return \Sop\ASN1\Type\Constructed\Sequence |
|
| 300 | - */ |
|
| 301 | - public function asSequence(): Constructed\Sequence |
|
| 302 | - { |
|
| 303 | - if (!$this->_element instanceof Constructed\Sequence) { |
|
| 304 | - throw new \UnexpectedValueException( |
|
| 305 | - $this->_generateExceptionMessage(Element::TYPE_SEQUENCE)); |
|
| 306 | - } |
|
| 307 | - return $this->_element; |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - /** |
|
| 311 | - * Get the wrapped element as a set type. |
|
| 312 | - * |
|
| 313 | - * @throws \UnexpectedValueException If the element is not a set |
|
| 314 | - * |
|
| 315 | - * @return \Sop\ASN1\Type\Constructed\Set |
|
| 316 | - */ |
|
| 317 | - public function asSet(): Constructed\Set |
|
| 318 | - { |
|
| 319 | - if (!$this->_element instanceof Constructed\Set) { |
|
| 320 | - throw new \UnexpectedValueException( |
|
| 321 | - $this->_generateExceptionMessage(Element::TYPE_SET)); |
|
| 322 | - } |
|
| 323 | - return $this->_element; |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - /** |
|
| 327 | - * Get the wrapped element as a numeric string type. |
|
| 328 | - * |
|
| 329 | - * @throws \UnexpectedValueException If the element is not a numeric string |
|
| 330 | - * |
|
| 331 | - * @return \Sop\ASN1\Type\Primitive\NumericString |
|
| 332 | - */ |
|
| 333 | - public function asNumericString(): Primitive\NumericString |
|
| 334 | - { |
|
| 335 | - if (!$this->_element instanceof Primitive\NumericString) { |
|
| 336 | - throw new \UnexpectedValueException( |
|
| 337 | - $this->_generateExceptionMessage(Element::TYPE_NUMERIC_STRING)); |
|
| 338 | - } |
|
| 339 | - return $this->_element; |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * Get the wrapped element as a printable string type. |
|
| 344 | - * |
|
| 345 | - * @throws \UnexpectedValueException If the element is not a printable |
|
| 346 | - * string |
|
| 347 | - * |
|
| 348 | - * @return \Sop\ASN1\Type\Primitive\PrintableString |
|
| 349 | - */ |
|
| 350 | - public function asPrintableString(): Primitive\PrintableString |
|
| 351 | - { |
|
| 352 | - if (!$this->_element instanceof Primitive\PrintableString) { |
|
| 353 | - throw new \UnexpectedValueException( |
|
| 354 | - $this->_generateExceptionMessage(Element::TYPE_PRINTABLE_STRING)); |
|
| 355 | - } |
|
| 356 | - return $this->_element; |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - /** |
|
| 360 | - * Get the wrapped element as a T61 string type. |
|
| 361 | - * |
|
| 362 | - * @throws \UnexpectedValueException If the element is not a T61 string |
|
| 363 | - * |
|
| 364 | - * @return \Sop\ASN1\Type\Primitive\T61String |
|
| 365 | - */ |
|
| 366 | - public function asT61String(): Primitive\T61String |
|
| 367 | - { |
|
| 368 | - if (!$this->_element instanceof Primitive\T61String) { |
|
| 369 | - throw new \UnexpectedValueException( |
|
| 370 | - $this->_generateExceptionMessage(Element::TYPE_T61_STRING)); |
|
| 371 | - } |
|
| 372 | - return $this->_element; |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - /** |
|
| 376 | - * Get the wrapped element as a videotex string type. |
|
| 377 | - * |
|
| 378 | - * @throws \UnexpectedValueException If the element is not a videotex string |
|
| 379 | - * |
|
| 380 | - * @return \Sop\ASN1\Type\Primitive\VideotexString |
|
| 381 | - */ |
|
| 382 | - public function asVideotexString(): Primitive\VideotexString |
|
| 383 | - { |
|
| 384 | - if (!$this->_element instanceof Primitive\VideotexString) { |
|
| 385 | - throw new \UnexpectedValueException( |
|
| 386 | - $this->_generateExceptionMessage(Element::TYPE_VIDEOTEX_STRING)); |
|
| 387 | - } |
|
| 388 | - return $this->_element; |
|
| 389 | - } |
|
| 390 | - |
|
| 391 | - /** |
|
| 392 | - * Get the wrapped element as a IA5 string type. |
|
| 393 | - * |
|
| 394 | - * @throws \UnexpectedValueException If the element is not a IA5 string |
|
| 395 | - * |
|
| 396 | - * @return \Sop\ASN1\Type\Primitive\IA5String |
|
| 397 | - */ |
|
| 398 | - public function asIA5String(): Primitive\IA5String |
|
| 399 | - { |
|
| 400 | - if (!$this->_element instanceof Primitive\IA5String) { |
|
| 401 | - throw new \UnexpectedValueException( |
|
| 402 | - $this->_generateExceptionMessage(Element::TYPE_IA5_STRING)); |
|
| 403 | - } |
|
| 404 | - return $this->_element; |
|
| 405 | - } |
|
| 406 | - |
|
| 407 | - /** |
|
| 408 | - * Get the wrapped element as an UTC time type. |
|
| 409 | - * |
|
| 410 | - * @throws \UnexpectedValueException If the element is not a UTC time |
|
| 411 | - * |
|
| 412 | - * @return \Sop\ASN1\Type\Primitive\UTCTime |
|
| 413 | - */ |
|
| 414 | - public function asUTCTime(): Primitive\UTCTime |
|
| 415 | - { |
|
| 416 | - if (!$this->_element instanceof Primitive\UTCTime) { |
|
| 417 | - throw new \UnexpectedValueException( |
|
| 418 | - $this->_generateExceptionMessage(Element::TYPE_UTC_TIME)); |
|
| 419 | - } |
|
| 420 | - return $this->_element; |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - /** |
|
| 424 | - * Get the wrapped element as a generalized time type. |
|
| 425 | - * |
|
| 426 | - * @throws \UnexpectedValueException If the element is not a generalized |
|
| 427 | - * time |
|
| 428 | - * |
|
| 429 | - * @return \Sop\ASN1\Type\Primitive\GeneralizedTime |
|
| 430 | - */ |
|
| 431 | - public function asGeneralizedTime(): Primitive\GeneralizedTime |
|
| 432 | - { |
|
| 433 | - if (!$this->_element instanceof Primitive\GeneralizedTime) { |
|
| 434 | - throw new \UnexpectedValueException( |
|
| 435 | - $this->_generateExceptionMessage(Element::TYPE_GENERALIZED_TIME)); |
|
| 436 | - } |
|
| 437 | - return $this->_element; |
|
| 438 | - } |
|
| 439 | - |
|
| 440 | - /** |
|
| 441 | - * Get the wrapped element as a graphic string type. |
|
| 442 | - * |
|
| 443 | - * @throws \UnexpectedValueException If the element is not a graphic string |
|
| 444 | - * |
|
| 445 | - * @return \Sop\ASN1\Type\Primitive\GraphicString |
|
| 446 | - */ |
|
| 447 | - public function asGraphicString(): Primitive\GraphicString |
|
| 448 | - { |
|
| 449 | - if (!$this->_element instanceof Primitive\GraphicString) { |
|
| 450 | - throw new \UnexpectedValueException( |
|
| 451 | - $this->_generateExceptionMessage(Element::TYPE_GRAPHIC_STRING)); |
|
| 452 | - } |
|
| 453 | - return $this->_element; |
|
| 454 | - } |
|
| 455 | - |
|
| 456 | - /** |
|
| 457 | - * Get the wrapped element as a visible string type. |
|
| 458 | - * |
|
| 459 | - * @throws \UnexpectedValueException If the element is not a visible string |
|
| 460 | - * |
|
| 461 | - * @return \Sop\ASN1\Type\Primitive\VisibleString |
|
| 462 | - */ |
|
| 463 | - public function asVisibleString(): Primitive\VisibleString |
|
| 464 | - { |
|
| 465 | - if (!$this->_element instanceof Primitive\VisibleString) { |
|
| 466 | - throw new \UnexpectedValueException( |
|
| 467 | - $this->_generateExceptionMessage(Element::TYPE_VISIBLE_STRING)); |
|
| 468 | - } |
|
| 469 | - return $this->_element; |
|
| 470 | - } |
|
| 471 | - |
|
| 472 | - /** |
|
| 473 | - * Get the wrapped element as a general string type. |
|
| 474 | - * |
|
| 475 | - * @throws \UnexpectedValueException If the element is not general string |
|
| 476 | - * |
|
| 477 | - * @return \Sop\ASN1\Type\Primitive\GeneralString |
|
| 478 | - */ |
|
| 479 | - public function asGeneralString(): Primitive\GeneralString |
|
| 480 | - { |
|
| 481 | - if (!$this->_element instanceof Primitive\GeneralString) { |
|
| 482 | - throw new \UnexpectedValueException( |
|
| 483 | - $this->_generateExceptionMessage(Element::TYPE_GENERAL_STRING)); |
|
| 484 | - } |
|
| 485 | - return $this->_element; |
|
| 486 | - } |
|
| 487 | - |
|
| 488 | - /** |
|
| 489 | - * Get the wrapped element as a universal string type. |
|
| 490 | - * |
|
| 491 | - * @throws \UnexpectedValueException If the element is not a universal |
|
| 492 | - * string |
|
| 493 | - * |
|
| 494 | - * @return \Sop\ASN1\Type\Primitive\UniversalString |
|
| 495 | - */ |
|
| 496 | - public function asUniversalString(): Primitive\UniversalString |
|
| 497 | - { |
|
| 498 | - if (!$this->_element instanceof Primitive\UniversalString) { |
|
| 499 | - throw new \UnexpectedValueException( |
|
| 500 | - $this->_generateExceptionMessage(Element::TYPE_UNIVERSAL_STRING)); |
|
| 501 | - } |
|
| 502 | - return $this->_element; |
|
| 503 | - } |
|
| 504 | - |
|
| 505 | - /** |
|
| 506 | - * Get the wrapped element as a character string type. |
|
| 507 | - * |
|
| 508 | - * @throws \UnexpectedValueException If the element is not a character |
|
| 509 | - * string |
|
| 510 | - * |
|
| 511 | - * @return \Sop\ASN1\Type\Primitive\CharacterString |
|
| 512 | - */ |
|
| 513 | - public function asCharacterString(): Primitive\CharacterString |
|
| 514 | - { |
|
| 515 | - if (!$this->_element instanceof Primitive\CharacterString) { |
|
| 516 | - throw new \UnexpectedValueException( |
|
| 517 | - $this->_generateExceptionMessage(Element::TYPE_CHARACTER_STRING)); |
|
| 518 | - } |
|
| 519 | - return $this->_element; |
|
| 520 | - } |
|
| 521 | - |
|
| 522 | - /** |
|
| 523 | - * Get the wrapped element as a BMP string type. |
|
| 524 | - * |
|
| 525 | - * @throws \UnexpectedValueException If the element is not a bmp string |
|
| 526 | - * |
|
| 527 | - * @return \Sop\ASN1\Type\Primitive\BMPString |
|
| 528 | - */ |
|
| 529 | - public function asBMPString(): Primitive\BMPString |
|
| 530 | - { |
|
| 531 | - if (!$this->_element instanceof Primitive\BMPString) { |
|
| 532 | - throw new \UnexpectedValueException( |
|
| 533 | - $this->_generateExceptionMessage(Element::TYPE_BMP_STRING)); |
|
| 534 | - } |
|
| 535 | - return $this->_element; |
|
| 536 | - } |
|
| 537 | - |
|
| 538 | - /** |
|
| 539 | - * Get the wrapped element as any string type. |
|
| 540 | - * |
|
| 541 | - * @throws \UnexpectedValueException If the element is not a string |
|
| 542 | - * |
|
| 543 | - * @return StringType |
|
| 544 | - */ |
|
| 545 | - public function asString(): StringType |
|
| 546 | - { |
|
| 547 | - if (!$this->_element instanceof StringType) { |
|
| 548 | - throw new \UnexpectedValueException( |
|
| 549 | - $this->_generateExceptionMessage(Element::TYPE_STRING)); |
|
| 550 | - } |
|
| 551 | - return $this->_element; |
|
| 552 | - } |
|
| 553 | - |
|
| 554 | - /** |
|
| 555 | - * Get the wrapped element as any time type. |
|
| 556 | - * |
|
| 557 | - * @throws \UnexpectedValueException If the element is not a time |
|
| 558 | - * |
|
| 559 | - * @return TimeType |
|
| 560 | - */ |
|
| 561 | - public function asTime(): TimeType |
|
| 562 | - { |
|
| 563 | - if (!$this->_element instanceof TimeType) { |
|
| 564 | - throw new \UnexpectedValueException( |
|
| 565 | - $this->_generateExceptionMessage(Element::TYPE_TIME)); |
|
| 566 | - } |
|
| 567 | - return $this->_element; |
|
| 568 | - } |
|
| 569 | - |
|
| 570 | - /** |
|
| 571 | - * {@inheritdoc} |
|
| 572 | - */ |
|
| 573 | - public function toDER(): string |
|
| 574 | - { |
|
| 575 | - return $this->_element->toDER(); |
|
| 576 | - } |
|
| 577 | - |
|
| 578 | - /** |
|
| 579 | - * {@inheritdoc} |
|
| 580 | - */ |
|
| 581 | - public function typeClass(): int |
|
| 582 | - { |
|
| 583 | - return $this->_element->typeClass(); |
|
| 584 | - } |
|
| 585 | - |
|
| 586 | - /** |
|
| 587 | - * {@inheritdoc} |
|
| 588 | - */ |
|
| 589 | - public function isConstructed(): bool |
|
| 590 | - { |
|
| 591 | - return $this->_element->isConstructed(); |
|
| 592 | - } |
|
| 593 | - |
|
| 594 | - /** |
|
| 595 | - * {@inheritdoc} |
|
| 596 | - */ |
|
| 597 | - public function tag(): int |
|
| 598 | - { |
|
| 599 | - return $this->_element->tag(); |
|
| 600 | - } |
|
| 601 | - |
|
| 602 | - /** |
|
| 603 | - * {@inheritdoc} |
|
| 604 | - */ |
|
| 605 | - public function isType(int $tag): bool |
|
| 606 | - { |
|
| 607 | - return $this->_element->isType($tag); |
|
| 608 | - } |
|
| 609 | - |
|
| 610 | - /** |
|
| 611 | - * {@inheritdoc} |
|
| 612 | - * |
|
| 613 | - * Consider using any of the <code>as*</code> accessor methods instead. |
|
| 614 | - */ |
|
| 615 | - public function expectType(int $tag): ElementBase |
|
| 616 | - { |
|
| 617 | - return $this->_element->expectType($tag); |
|
| 618 | - } |
|
| 619 | - |
|
| 620 | - /** |
|
| 621 | - * {@inheritdoc} |
|
| 622 | - */ |
|
| 623 | - public function isTagged(): bool |
|
| 624 | - { |
|
| 625 | - return $this->_element->isTagged(); |
|
| 626 | - } |
|
| 627 | - |
|
| 628 | - /** |
|
| 629 | - * {@inheritdoc} |
|
| 630 | - * |
|
| 631 | - * Consider using <code>asTagged()</code> method instead and chaining |
|
| 632 | - * with <code>TaggedType::asExplicit()</code> or |
|
| 633 | - * <code>TaggedType::asImplicit()</code>. |
|
| 634 | - */ |
|
| 635 | - public function expectTagged(?int $tag = null): TaggedType |
|
| 636 | - { |
|
| 637 | - return $this->_element->expectTagged($tag); |
|
| 638 | - } |
|
| 639 | - |
|
| 640 | - /** |
|
| 641 | - * {@inheritdoc} |
|
| 642 | - */ |
|
| 643 | - public function asElement(): Element |
|
| 644 | - { |
|
| 645 | - return $this->_element; |
|
| 646 | - } |
|
| 647 | - |
|
| 648 | - /** |
|
| 649 | - * {@inheritdoc} |
|
| 650 | - */ |
|
| 651 | - public function asUnspecified(): UnspecifiedType |
|
| 652 | - { |
|
| 653 | - return $this; |
|
| 654 | - } |
|
| 655 | - |
|
| 656 | - /** |
|
| 657 | - * Generate message for exceptions thrown by <code>as*</code> methods. |
|
| 658 | - * |
|
| 659 | - * @param int $tag Type tag of the expected element |
|
| 660 | - * |
|
| 661 | - * @return string |
|
| 662 | - */ |
|
| 663 | - private function _generateExceptionMessage(int $tag): string |
|
| 664 | - { |
|
| 665 | - return sprintf('%s expected, got %s.', Element::tagToName($tag), |
|
| 666 | - $this->_typeDescriptorString()); |
|
| 667 | - } |
|
| 668 | - |
|
| 669 | - /** |
|
| 670 | - * Get textual description of the wrapped element for debugging purposes. |
|
| 671 | - * |
|
| 672 | - * @return string |
|
| 673 | - */ |
|
| 674 | - private function _typeDescriptorString(): string |
|
| 675 | - { |
|
| 676 | - $type_cls = $this->_element->typeClass(); |
|
| 677 | - $tag = $this->_element->tag(); |
|
| 678 | - if (Identifier::CLASS_UNIVERSAL == $type_cls) { |
|
| 679 | - return Element::tagToName($tag); |
|
| 680 | - } |
|
| 681 | - return Identifier::classToName($type_cls) . " TAG ${tag}"; |
|
| 682 | - } |
|
| 20 | + /** |
|
| 21 | + * The wrapped element. |
|
| 22 | + * |
|
| 23 | + * @var Element |
|
| 24 | + */ |
|
| 25 | + private $_element; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * Constructor. |
|
| 29 | + * |
|
| 30 | + * @param Element $el |
|
| 31 | + */ |
|
| 32 | + public function __construct(Element $el) |
|
| 33 | + { |
|
| 34 | + $this->_element = $el; |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Initialize from DER data. |
|
| 39 | + * |
|
| 40 | + * @param string $data DER encoded data |
|
| 41 | + * |
|
| 42 | + * @return self |
|
| 43 | + */ |
|
| 44 | + public static function fromDER(string $data): self |
|
| 45 | + { |
|
| 46 | + return Element::fromDER($data)->asUnspecified(); |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + /** |
|
| 50 | + * Initialize from ElementBase interface. |
|
| 51 | + * |
|
| 52 | + * @param ElementBase $el |
|
| 53 | + * |
|
| 54 | + * @return self |
|
| 55 | + */ |
|
| 56 | + public static function fromElementBase(ElementBase $el): self |
|
| 57 | + { |
|
| 58 | + // if element is already wrapped |
|
| 59 | + if ($el instanceof self) { |
|
| 60 | + return $el; |
|
| 61 | + } |
|
| 62 | + return new self($el->asElement()); |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * Get the wrapped element as a context specific tagged type. |
|
| 67 | + * |
|
| 68 | + * @throws \UnexpectedValueException If the element is not tagged |
|
| 69 | + * |
|
| 70 | + * @return TaggedType |
|
| 71 | + */ |
|
| 72 | + public function asTagged(): TaggedType |
|
| 73 | + { |
|
| 74 | + if (!$this->_element instanceof TaggedType) { |
|
| 75 | + throw new \UnexpectedValueException( |
|
| 76 | + 'Tagged element expected, got ' . $this->_typeDescriptorString()); |
|
| 77 | + } |
|
| 78 | + return $this->_element; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Get the wrapped element as an application specific type. |
|
| 83 | + * |
|
| 84 | + * @throws \UnexpectedValueException If element is not application specific |
|
| 85 | + * |
|
| 86 | + * @return \Sop\ASN1\Type\Tagged\ApplicationType |
|
| 87 | + */ |
|
| 88 | + public function asApplication(): Tagged\ApplicationType |
|
| 89 | + { |
|
| 90 | + if (!$this->_element instanceof Tagged\ApplicationType) { |
|
| 91 | + throw new \UnexpectedValueException( |
|
| 92 | + 'Application type expected, got ' . |
|
| 93 | + $this->_typeDescriptorString()); |
|
| 94 | + } |
|
| 95 | + return $this->_element; |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + /** |
|
| 99 | + * Get the wrapped element as a private tagged type. |
|
| 100 | + * |
|
| 101 | + * @throws \UnexpectedValueException If element is not using private tagging |
|
| 102 | + * |
|
| 103 | + * @return \Sop\ASN1\Type\Tagged\PrivateType |
|
| 104 | + */ |
|
| 105 | + public function asPrivate(): Tagged\PrivateType |
|
| 106 | + { |
|
| 107 | + if (!$this->_element instanceof Tagged\PrivateType) { |
|
| 108 | + throw new \UnexpectedValueException( |
|
| 109 | + 'Private type expected, got ' . $this->_typeDescriptorString()); |
|
| 110 | + } |
|
| 111 | + return $this->_element; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * Get the wrapped element as a boolean type. |
|
| 116 | + * |
|
| 117 | + * @throws \UnexpectedValueException If the element is not a boolean |
|
| 118 | + * |
|
| 119 | + * @return \Sop\ASN1\Type\Primitive\Boolean |
|
| 120 | + */ |
|
| 121 | + public function asBoolean(): Primitive\Boolean |
|
| 122 | + { |
|
| 123 | + if (!$this->_element instanceof Primitive\Boolean) { |
|
| 124 | + throw new \UnexpectedValueException( |
|
| 125 | + $this->_generateExceptionMessage(Element::TYPE_BOOLEAN)); |
|
| 126 | + } |
|
| 127 | + return $this->_element; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * Get the wrapped element as an integer type. |
|
| 132 | + * |
|
| 133 | + * @throws \UnexpectedValueException If the element is not an integer |
|
| 134 | + * |
|
| 135 | + * @return \Sop\ASN1\Type\Primitive\Integer |
|
| 136 | + */ |
|
| 137 | + public function asInteger(): Primitive\Integer |
|
| 138 | + { |
|
| 139 | + if (!$this->_element instanceof Primitive\Integer) { |
|
| 140 | + throw new \UnexpectedValueException( |
|
| 141 | + $this->_generateExceptionMessage(Element::TYPE_INTEGER)); |
|
| 142 | + } |
|
| 143 | + return $this->_element; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * Get the wrapped element as a bit string type. |
|
| 148 | + * |
|
| 149 | + * @throws \UnexpectedValueException If the element is not a bit string |
|
| 150 | + * |
|
| 151 | + * @return \Sop\ASN1\Type\Primitive\BitString |
|
| 152 | + */ |
|
| 153 | + public function asBitString(): Primitive\BitString |
|
| 154 | + { |
|
| 155 | + if (!$this->_element instanceof Primitive\BitString) { |
|
| 156 | + throw new \UnexpectedValueException( |
|
| 157 | + $this->_generateExceptionMessage(Element::TYPE_BIT_STRING)); |
|
| 158 | + } |
|
| 159 | + return $this->_element; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Get the wrapped element as an octet string type. |
|
| 164 | + * |
|
| 165 | + * @throws \UnexpectedValueException If the element is not an octet string |
|
| 166 | + * |
|
| 167 | + * @return \Sop\ASN1\Type\Primitive\OctetString |
|
| 168 | + */ |
|
| 169 | + public function asOctetString(): Primitive\OctetString |
|
| 170 | + { |
|
| 171 | + if (!$this->_element instanceof Primitive\OctetString) { |
|
| 172 | + throw new \UnexpectedValueException( |
|
| 173 | + $this->_generateExceptionMessage(Element::TYPE_OCTET_STRING)); |
|
| 174 | + } |
|
| 175 | + return $this->_element; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * Get the wrapped element as a null type. |
|
| 180 | + * |
|
| 181 | + * @throws \UnexpectedValueException If the element is not a null |
|
| 182 | + * |
|
| 183 | + * @return \Sop\ASN1\Type\Primitive\NullType |
|
| 184 | + */ |
|
| 185 | + public function asNull(): Primitive\NullType |
|
| 186 | + { |
|
| 187 | + if (!$this->_element instanceof Primitive\NullType) { |
|
| 188 | + throw new \UnexpectedValueException( |
|
| 189 | + $this->_generateExceptionMessage(Element::TYPE_NULL)); |
|
| 190 | + } |
|
| 191 | + return $this->_element; |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * Get the wrapped element as an object identifier type. |
|
| 196 | + * |
|
| 197 | + * @throws \UnexpectedValueException If the element is not an object |
|
| 198 | + * identifier |
|
| 199 | + * |
|
| 200 | + * @return \Sop\ASN1\Type\Primitive\ObjectIdentifier |
|
| 201 | + */ |
|
| 202 | + public function asObjectIdentifier(): Primitive\ObjectIdentifier |
|
| 203 | + { |
|
| 204 | + if (!$this->_element instanceof Primitive\ObjectIdentifier) { |
|
| 205 | + throw new \UnexpectedValueException( |
|
| 206 | + $this->_generateExceptionMessage( |
|
| 207 | + Element::TYPE_OBJECT_IDENTIFIER)); |
|
| 208 | + } |
|
| 209 | + return $this->_element; |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * Get the wrapped element as an object descriptor type. |
|
| 214 | + * |
|
| 215 | + * @throws \UnexpectedValueException If the element is not an object |
|
| 216 | + * descriptor |
|
| 217 | + * |
|
| 218 | + * @return \Sop\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 | + * |
|
| 235 | + * @return \Sop\ASN1\Type\Primitive\Real |
|
| 236 | + */ |
|
| 237 | + public function asReal(): Primitive\Real |
|
| 238 | + { |
|
| 239 | + if (!$this->_element instanceof Primitive\Real) { |
|
| 240 | + throw new \UnexpectedValueException( |
|
| 241 | + $this->_generateExceptionMessage(Element::TYPE_REAL)); |
|
| 242 | + } |
|
| 243 | + return $this->_element; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + /** |
|
| 247 | + * Get the wrapped element as an enumerated type. |
|
| 248 | + * |
|
| 249 | + * @throws \UnexpectedValueException If the element is not an enumerated |
|
| 250 | + * |
|
| 251 | + * @return \Sop\ASN1\Type\Primitive\Enumerated |
|
| 252 | + */ |
|
| 253 | + public function asEnumerated(): Primitive\Enumerated |
|
| 254 | + { |
|
| 255 | + if (!$this->_element instanceof Primitive\Enumerated) { |
|
| 256 | + throw new \UnexpectedValueException( |
|
| 257 | + $this->_generateExceptionMessage(Element::TYPE_ENUMERATED)); |
|
| 258 | + } |
|
| 259 | + return $this->_element; |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + /** |
|
| 263 | + * Get the wrapped element as a UTF8 string type. |
|
| 264 | + * |
|
| 265 | + * @throws \UnexpectedValueException If the element is not a UTF8 string |
|
| 266 | + * |
|
| 267 | + * @return \Sop\ASN1\Type\Primitive\UTF8String |
|
| 268 | + */ |
|
| 269 | + public function asUTF8String(): Primitive\UTF8String |
|
| 270 | + { |
|
| 271 | + if (!$this->_element instanceof Primitive\UTF8String) { |
|
| 272 | + throw new \UnexpectedValueException( |
|
| 273 | + $this->_generateExceptionMessage(Element::TYPE_UTF8_STRING)); |
|
| 274 | + } |
|
| 275 | + return $this->_element; |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + /** |
|
| 279 | + * Get the wrapped element as a relative OID type. |
|
| 280 | + * |
|
| 281 | + * @throws \UnexpectedValueException If the element is not a relative OID |
|
| 282 | + * |
|
| 283 | + * @return \Sop\ASN1\Type\Primitive\RelativeOID |
|
| 284 | + */ |
|
| 285 | + public function asRelativeOID(): Primitive\RelativeOID |
|
| 286 | + { |
|
| 287 | + if (!$this->_element instanceof Primitive\RelativeOID) { |
|
| 288 | + throw new \UnexpectedValueException( |
|
| 289 | + $this->_generateExceptionMessage(Element::TYPE_RELATIVE_OID)); |
|
| 290 | + } |
|
| 291 | + return $this->_element; |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + /** |
|
| 295 | + * Get the wrapped element as a sequence type. |
|
| 296 | + * |
|
| 297 | + * @throws \UnexpectedValueException If the element is not a sequence |
|
| 298 | + * |
|
| 299 | + * @return \Sop\ASN1\Type\Constructed\Sequence |
|
| 300 | + */ |
|
| 301 | + public function asSequence(): Constructed\Sequence |
|
| 302 | + { |
|
| 303 | + if (!$this->_element instanceof Constructed\Sequence) { |
|
| 304 | + throw new \UnexpectedValueException( |
|
| 305 | + $this->_generateExceptionMessage(Element::TYPE_SEQUENCE)); |
|
| 306 | + } |
|
| 307 | + return $this->_element; |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + /** |
|
| 311 | + * Get the wrapped element as a set type. |
|
| 312 | + * |
|
| 313 | + * @throws \UnexpectedValueException If the element is not a set |
|
| 314 | + * |
|
| 315 | + * @return \Sop\ASN1\Type\Constructed\Set |
|
| 316 | + */ |
|
| 317 | + public function asSet(): Constructed\Set |
|
| 318 | + { |
|
| 319 | + if (!$this->_element instanceof Constructed\Set) { |
|
| 320 | + throw new \UnexpectedValueException( |
|
| 321 | + $this->_generateExceptionMessage(Element::TYPE_SET)); |
|
| 322 | + } |
|
| 323 | + return $this->_element; |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + /** |
|
| 327 | + * Get the wrapped element as a numeric string type. |
|
| 328 | + * |
|
| 329 | + * @throws \UnexpectedValueException If the element is not a numeric string |
|
| 330 | + * |
|
| 331 | + * @return \Sop\ASN1\Type\Primitive\NumericString |
|
| 332 | + */ |
|
| 333 | + public function asNumericString(): Primitive\NumericString |
|
| 334 | + { |
|
| 335 | + if (!$this->_element instanceof Primitive\NumericString) { |
|
| 336 | + throw new \UnexpectedValueException( |
|
| 337 | + $this->_generateExceptionMessage(Element::TYPE_NUMERIC_STRING)); |
|
| 338 | + } |
|
| 339 | + return $this->_element; |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * Get the wrapped element as a printable string type. |
|
| 344 | + * |
|
| 345 | + * @throws \UnexpectedValueException If the element is not a printable |
|
| 346 | + * string |
|
| 347 | + * |
|
| 348 | + * @return \Sop\ASN1\Type\Primitive\PrintableString |
|
| 349 | + */ |
|
| 350 | + public function asPrintableString(): Primitive\PrintableString |
|
| 351 | + { |
|
| 352 | + if (!$this->_element instanceof Primitive\PrintableString) { |
|
| 353 | + throw new \UnexpectedValueException( |
|
| 354 | + $this->_generateExceptionMessage(Element::TYPE_PRINTABLE_STRING)); |
|
| 355 | + } |
|
| 356 | + return $this->_element; |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + /** |
|
| 360 | + * Get the wrapped element as a T61 string type. |
|
| 361 | + * |
|
| 362 | + * @throws \UnexpectedValueException If the element is not a T61 string |
|
| 363 | + * |
|
| 364 | + * @return \Sop\ASN1\Type\Primitive\T61String |
|
| 365 | + */ |
|
| 366 | + public function asT61String(): Primitive\T61String |
|
| 367 | + { |
|
| 368 | + if (!$this->_element instanceof Primitive\T61String) { |
|
| 369 | + throw new \UnexpectedValueException( |
|
| 370 | + $this->_generateExceptionMessage(Element::TYPE_T61_STRING)); |
|
| 371 | + } |
|
| 372 | + return $this->_element; |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + /** |
|
| 376 | + * Get the wrapped element as a videotex string type. |
|
| 377 | + * |
|
| 378 | + * @throws \UnexpectedValueException If the element is not a videotex string |
|
| 379 | + * |
|
| 380 | + * @return \Sop\ASN1\Type\Primitive\VideotexString |
|
| 381 | + */ |
|
| 382 | + public function asVideotexString(): Primitive\VideotexString |
|
| 383 | + { |
|
| 384 | + if (!$this->_element instanceof Primitive\VideotexString) { |
|
| 385 | + throw new \UnexpectedValueException( |
|
| 386 | + $this->_generateExceptionMessage(Element::TYPE_VIDEOTEX_STRING)); |
|
| 387 | + } |
|
| 388 | + return $this->_element; |
|
| 389 | + } |
|
| 390 | + |
|
| 391 | + /** |
|
| 392 | + * Get the wrapped element as a IA5 string type. |
|
| 393 | + * |
|
| 394 | + * @throws \UnexpectedValueException If the element is not a IA5 string |
|
| 395 | + * |
|
| 396 | + * @return \Sop\ASN1\Type\Primitive\IA5String |
|
| 397 | + */ |
|
| 398 | + public function asIA5String(): Primitive\IA5String |
|
| 399 | + { |
|
| 400 | + if (!$this->_element instanceof Primitive\IA5String) { |
|
| 401 | + throw new \UnexpectedValueException( |
|
| 402 | + $this->_generateExceptionMessage(Element::TYPE_IA5_STRING)); |
|
| 403 | + } |
|
| 404 | + return $this->_element; |
|
| 405 | + } |
|
| 406 | + |
|
| 407 | + /** |
|
| 408 | + * Get the wrapped element as an UTC time type. |
|
| 409 | + * |
|
| 410 | + * @throws \UnexpectedValueException If the element is not a UTC time |
|
| 411 | + * |
|
| 412 | + * @return \Sop\ASN1\Type\Primitive\UTCTime |
|
| 413 | + */ |
|
| 414 | + public function asUTCTime(): Primitive\UTCTime |
|
| 415 | + { |
|
| 416 | + if (!$this->_element instanceof Primitive\UTCTime) { |
|
| 417 | + throw new \UnexpectedValueException( |
|
| 418 | + $this->_generateExceptionMessage(Element::TYPE_UTC_TIME)); |
|
| 419 | + } |
|
| 420 | + return $this->_element; |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + /** |
|
| 424 | + * Get the wrapped element as a generalized time type. |
|
| 425 | + * |
|
| 426 | + * @throws \UnexpectedValueException If the element is not a generalized |
|
| 427 | + * time |
|
| 428 | + * |
|
| 429 | + * @return \Sop\ASN1\Type\Primitive\GeneralizedTime |
|
| 430 | + */ |
|
| 431 | + public function asGeneralizedTime(): Primitive\GeneralizedTime |
|
| 432 | + { |
|
| 433 | + if (!$this->_element instanceof Primitive\GeneralizedTime) { |
|
| 434 | + throw new \UnexpectedValueException( |
|
| 435 | + $this->_generateExceptionMessage(Element::TYPE_GENERALIZED_TIME)); |
|
| 436 | + } |
|
| 437 | + return $this->_element; |
|
| 438 | + } |
|
| 439 | + |
|
| 440 | + /** |
|
| 441 | + * Get the wrapped element as a graphic string type. |
|
| 442 | + * |
|
| 443 | + * @throws \UnexpectedValueException If the element is not a graphic string |
|
| 444 | + * |
|
| 445 | + * @return \Sop\ASN1\Type\Primitive\GraphicString |
|
| 446 | + */ |
|
| 447 | + public function asGraphicString(): Primitive\GraphicString |
|
| 448 | + { |
|
| 449 | + if (!$this->_element instanceof Primitive\GraphicString) { |
|
| 450 | + throw new \UnexpectedValueException( |
|
| 451 | + $this->_generateExceptionMessage(Element::TYPE_GRAPHIC_STRING)); |
|
| 452 | + } |
|
| 453 | + return $this->_element; |
|
| 454 | + } |
|
| 455 | + |
|
| 456 | + /** |
|
| 457 | + * Get the wrapped element as a visible string type. |
|
| 458 | + * |
|
| 459 | + * @throws \UnexpectedValueException If the element is not a visible string |
|
| 460 | + * |
|
| 461 | + * @return \Sop\ASN1\Type\Primitive\VisibleString |
|
| 462 | + */ |
|
| 463 | + public function asVisibleString(): Primitive\VisibleString |
|
| 464 | + { |
|
| 465 | + if (!$this->_element instanceof Primitive\VisibleString) { |
|
| 466 | + throw new \UnexpectedValueException( |
|
| 467 | + $this->_generateExceptionMessage(Element::TYPE_VISIBLE_STRING)); |
|
| 468 | + } |
|
| 469 | + return $this->_element; |
|
| 470 | + } |
|
| 471 | + |
|
| 472 | + /** |
|
| 473 | + * Get the wrapped element as a general string type. |
|
| 474 | + * |
|
| 475 | + * @throws \UnexpectedValueException If the element is not general string |
|
| 476 | + * |
|
| 477 | + * @return \Sop\ASN1\Type\Primitive\GeneralString |
|
| 478 | + */ |
|
| 479 | + public function asGeneralString(): Primitive\GeneralString |
|
| 480 | + { |
|
| 481 | + if (!$this->_element instanceof Primitive\GeneralString) { |
|
| 482 | + throw new \UnexpectedValueException( |
|
| 483 | + $this->_generateExceptionMessage(Element::TYPE_GENERAL_STRING)); |
|
| 484 | + } |
|
| 485 | + return $this->_element; |
|
| 486 | + } |
|
| 487 | + |
|
| 488 | + /** |
|
| 489 | + * Get the wrapped element as a universal string type. |
|
| 490 | + * |
|
| 491 | + * @throws \UnexpectedValueException If the element is not a universal |
|
| 492 | + * string |
|
| 493 | + * |
|
| 494 | + * @return \Sop\ASN1\Type\Primitive\UniversalString |
|
| 495 | + */ |
|
| 496 | + public function asUniversalString(): Primitive\UniversalString |
|
| 497 | + { |
|
| 498 | + if (!$this->_element instanceof Primitive\UniversalString) { |
|
| 499 | + throw new \UnexpectedValueException( |
|
| 500 | + $this->_generateExceptionMessage(Element::TYPE_UNIVERSAL_STRING)); |
|
| 501 | + } |
|
| 502 | + return $this->_element; |
|
| 503 | + } |
|
| 504 | + |
|
| 505 | + /** |
|
| 506 | + * Get the wrapped element as a character string type. |
|
| 507 | + * |
|
| 508 | + * @throws \UnexpectedValueException If the element is not a character |
|
| 509 | + * string |
|
| 510 | + * |
|
| 511 | + * @return \Sop\ASN1\Type\Primitive\CharacterString |
|
| 512 | + */ |
|
| 513 | + public function asCharacterString(): Primitive\CharacterString |
|
| 514 | + { |
|
| 515 | + if (!$this->_element instanceof Primitive\CharacterString) { |
|
| 516 | + throw new \UnexpectedValueException( |
|
| 517 | + $this->_generateExceptionMessage(Element::TYPE_CHARACTER_STRING)); |
|
| 518 | + } |
|
| 519 | + return $this->_element; |
|
| 520 | + } |
|
| 521 | + |
|
| 522 | + /** |
|
| 523 | + * Get the wrapped element as a BMP string type. |
|
| 524 | + * |
|
| 525 | + * @throws \UnexpectedValueException If the element is not a bmp string |
|
| 526 | + * |
|
| 527 | + * @return \Sop\ASN1\Type\Primitive\BMPString |
|
| 528 | + */ |
|
| 529 | + public function asBMPString(): Primitive\BMPString |
|
| 530 | + { |
|
| 531 | + if (!$this->_element instanceof Primitive\BMPString) { |
|
| 532 | + throw new \UnexpectedValueException( |
|
| 533 | + $this->_generateExceptionMessage(Element::TYPE_BMP_STRING)); |
|
| 534 | + } |
|
| 535 | + return $this->_element; |
|
| 536 | + } |
|
| 537 | + |
|
| 538 | + /** |
|
| 539 | + * Get the wrapped element as any string type. |
|
| 540 | + * |
|
| 541 | + * @throws \UnexpectedValueException If the element is not a string |
|
| 542 | + * |
|
| 543 | + * @return StringType |
|
| 544 | + */ |
|
| 545 | + public function asString(): StringType |
|
| 546 | + { |
|
| 547 | + if (!$this->_element instanceof StringType) { |
|
| 548 | + throw new \UnexpectedValueException( |
|
| 549 | + $this->_generateExceptionMessage(Element::TYPE_STRING)); |
|
| 550 | + } |
|
| 551 | + return $this->_element; |
|
| 552 | + } |
|
| 553 | + |
|
| 554 | + /** |
|
| 555 | + * Get the wrapped element as any time type. |
|
| 556 | + * |
|
| 557 | + * @throws \UnexpectedValueException If the element is not a time |
|
| 558 | + * |
|
| 559 | + * @return TimeType |
|
| 560 | + */ |
|
| 561 | + public function asTime(): TimeType |
|
| 562 | + { |
|
| 563 | + if (!$this->_element instanceof TimeType) { |
|
| 564 | + throw new \UnexpectedValueException( |
|
| 565 | + $this->_generateExceptionMessage(Element::TYPE_TIME)); |
|
| 566 | + } |
|
| 567 | + return $this->_element; |
|
| 568 | + } |
|
| 569 | + |
|
| 570 | + /** |
|
| 571 | + * {@inheritdoc} |
|
| 572 | + */ |
|
| 573 | + public function toDER(): string |
|
| 574 | + { |
|
| 575 | + return $this->_element->toDER(); |
|
| 576 | + } |
|
| 577 | + |
|
| 578 | + /** |
|
| 579 | + * {@inheritdoc} |
|
| 580 | + */ |
|
| 581 | + public function typeClass(): int |
|
| 582 | + { |
|
| 583 | + return $this->_element->typeClass(); |
|
| 584 | + } |
|
| 585 | + |
|
| 586 | + /** |
|
| 587 | + * {@inheritdoc} |
|
| 588 | + */ |
|
| 589 | + public function isConstructed(): bool |
|
| 590 | + { |
|
| 591 | + return $this->_element->isConstructed(); |
|
| 592 | + } |
|
| 593 | + |
|
| 594 | + /** |
|
| 595 | + * {@inheritdoc} |
|
| 596 | + */ |
|
| 597 | + public function tag(): int |
|
| 598 | + { |
|
| 599 | + return $this->_element->tag(); |
|
| 600 | + } |
|
| 601 | + |
|
| 602 | + /** |
|
| 603 | + * {@inheritdoc} |
|
| 604 | + */ |
|
| 605 | + public function isType(int $tag): bool |
|
| 606 | + { |
|
| 607 | + return $this->_element->isType($tag); |
|
| 608 | + } |
|
| 609 | + |
|
| 610 | + /** |
|
| 611 | + * {@inheritdoc} |
|
| 612 | + * |
|
| 613 | + * Consider using any of the <code>as*</code> accessor methods instead. |
|
| 614 | + */ |
|
| 615 | + public function expectType(int $tag): ElementBase |
|
| 616 | + { |
|
| 617 | + return $this->_element->expectType($tag); |
|
| 618 | + } |
|
| 619 | + |
|
| 620 | + /** |
|
| 621 | + * {@inheritdoc} |
|
| 622 | + */ |
|
| 623 | + public function isTagged(): bool |
|
| 624 | + { |
|
| 625 | + return $this->_element->isTagged(); |
|
| 626 | + } |
|
| 627 | + |
|
| 628 | + /** |
|
| 629 | + * {@inheritdoc} |
|
| 630 | + * |
|
| 631 | + * Consider using <code>asTagged()</code> method instead and chaining |
|
| 632 | + * with <code>TaggedType::asExplicit()</code> or |
|
| 633 | + * <code>TaggedType::asImplicit()</code>. |
|
| 634 | + */ |
|
| 635 | + public function expectTagged(?int $tag = null): TaggedType |
|
| 636 | + { |
|
| 637 | + return $this->_element->expectTagged($tag); |
|
| 638 | + } |
|
| 639 | + |
|
| 640 | + /** |
|
| 641 | + * {@inheritdoc} |
|
| 642 | + */ |
|
| 643 | + public function asElement(): Element |
|
| 644 | + { |
|
| 645 | + return $this->_element; |
|
| 646 | + } |
|
| 647 | + |
|
| 648 | + /** |
|
| 649 | + * {@inheritdoc} |
|
| 650 | + */ |
|
| 651 | + public function asUnspecified(): UnspecifiedType |
|
| 652 | + { |
|
| 653 | + return $this; |
|
| 654 | + } |
|
| 655 | + |
|
| 656 | + /** |
|
| 657 | + * Generate message for exceptions thrown by <code>as*</code> methods. |
|
| 658 | + * |
|
| 659 | + * @param int $tag Type tag of the expected element |
|
| 660 | + * |
|
| 661 | + * @return string |
|
| 662 | + */ |
|
| 663 | + private function _generateExceptionMessage(int $tag): string |
|
| 664 | + { |
|
| 665 | + return sprintf('%s expected, got %s.', Element::tagToName($tag), |
|
| 666 | + $this->_typeDescriptorString()); |
|
| 667 | + } |
|
| 668 | + |
|
| 669 | + /** |
|
| 670 | + * Get textual description of the wrapped element for debugging purposes. |
|
| 671 | + * |
|
| 672 | + * @return string |
|
| 673 | + */ |
|
| 674 | + private function _typeDescriptorString(): string |
|
| 675 | + { |
|
| 676 | + $type_cls = $this->_element->typeClass(); |
|
| 677 | + $tag = $this->_element->tag(); |
|
| 678 | + if (Identifier::CLASS_UNIVERSAL == $type_cls) { |
|
| 679 | + return Element::tagToName($tag); |
|
| 680 | + } |
|
| 681 | + return Identifier::classToName($type_cls) . " TAG ${tag}"; |
|
| 682 | + } |
|
| 683 | 683 | } |
@@ -23,135 +23,135 @@ |
||
| 23 | 23 | */ |
| 24 | 24 | class DERTaggedType extends TaggedType implements ExplicitTagging, ImplicitTagging |
| 25 | 25 | { |
| 26 | - /** |
|
| 27 | - * Identifier. |
|
| 28 | - * |
|
| 29 | - * @var Identifier |
|
| 30 | - */ |
|
| 31 | - private $_identifier; |
|
| 26 | + /** |
|
| 27 | + * Identifier. |
|
| 28 | + * |
|
| 29 | + * @var Identifier |
|
| 30 | + */ |
|
| 31 | + private $_identifier; |
|
| 32 | 32 | |
| 33 | - /** |
|
| 34 | - * DER data. |
|
| 35 | - * |
|
| 36 | - * @var string |
|
| 37 | - */ |
|
| 38 | - private $_data; |
|
| 33 | + /** |
|
| 34 | + * DER data. |
|
| 35 | + * |
|
| 36 | + * @var string |
|
| 37 | + */ |
|
| 38 | + private $_data; |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * Offset to next byte after identifier. |
|
| 42 | - * |
|
| 43 | - * @var int |
|
| 44 | - */ |
|
| 45 | - private $_offset; |
|
| 40 | + /** |
|
| 41 | + * Offset to next byte after identifier. |
|
| 42 | + * |
|
| 43 | + * @var int |
|
| 44 | + */ |
|
| 45 | + private $_offset; |
|
| 46 | 46 | |
| 47 | - /** |
|
| 48 | - * Offset to content. |
|
| 49 | - * |
|
| 50 | - * @var int |
|
| 51 | - */ |
|
| 52 | - private $_valueOffset; |
|
| 47 | + /** |
|
| 48 | + * Offset to content. |
|
| 49 | + * |
|
| 50 | + * @var int |
|
| 51 | + */ |
|
| 52 | + private $_valueOffset; |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Length of the content. |
|
| 56 | - * |
|
| 57 | - * @var int |
|
| 58 | - */ |
|
| 59 | - private $_valueLength; |
|
| 54 | + /** |
|
| 55 | + * Length of the content. |
|
| 56 | + * |
|
| 57 | + * @var int |
|
| 58 | + */ |
|
| 59 | + private $_valueLength; |
|
| 60 | 60 | |
| 61 | - /** |
|
| 62 | - * Constructor. |
|
| 63 | - * |
|
| 64 | - * @param Identifier $identifier Pre-parsed identifier |
|
| 65 | - * @param string $data DER data |
|
| 66 | - * @param int $offset Offset to next byte after identifier |
|
| 67 | - * @param int $value_offset Offset to content |
|
| 68 | - * @param int $value_length Content length |
|
| 69 | - */ |
|
| 70 | - public function __construct(Identifier $identifier, string $data, |
|
| 71 | - int $offset, int $value_offset, int $value_length, |
|
| 72 | - bool $indefinite_length) |
|
| 73 | - { |
|
| 74 | - $this->_identifier = $identifier; |
|
| 75 | - $this->_data = $data; |
|
| 76 | - $this->_offset = $offset; |
|
| 77 | - $this->_valueOffset = $value_offset; |
|
| 78 | - $this->_valueLength = $value_length; |
|
| 79 | - $this->_indefiniteLength = $indefinite_length; |
|
| 80 | - $this->_typeTag = $identifier->intTag(); |
|
| 81 | - } |
|
| 61 | + /** |
|
| 62 | + * Constructor. |
|
| 63 | + * |
|
| 64 | + * @param Identifier $identifier Pre-parsed identifier |
|
| 65 | + * @param string $data DER data |
|
| 66 | + * @param int $offset Offset to next byte after identifier |
|
| 67 | + * @param int $value_offset Offset to content |
|
| 68 | + * @param int $value_length Content length |
|
| 69 | + */ |
|
| 70 | + public function __construct(Identifier $identifier, string $data, |
|
| 71 | + int $offset, int $value_offset, int $value_length, |
|
| 72 | + bool $indefinite_length) |
|
| 73 | + { |
|
| 74 | + $this->_identifier = $identifier; |
|
| 75 | + $this->_data = $data; |
|
| 76 | + $this->_offset = $offset; |
|
| 77 | + $this->_valueOffset = $value_offset; |
|
| 78 | + $this->_valueLength = $value_length; |
|
| 79 | + $this->_indefiniteLength = $indefinite_length; |
|
| 80 | + $this->_typeTag = $identifier->intTag(); |
|
| 81 | + } |
|
| 82 | 82 | |
| 83 | - /** |
|
| 84 | - * {@inheritdoc} |
|
| 85 | - */ |
|
| 86 | - public function typeClass(): int |
|
| 87 | - { |
|
| 88 | - return $this->_identifier->typeClass(); |
|
| 89 | - } |
|
| 83 | + /** |
|
| 84 | + * {@inheritdoc} |
|
| 85 | + */ |
|
| 86 | + public function typeClass(): int |
|
| 87 | + { |
|
| 88 | + return $this->_identifier->typeClass(); |
|
| 89 | + } |
|
| 90 | 90 | |
| 91 | - /** |
|
| 92 | - * {@inheritdoc} |
|
| 93 | - */ |
|
| 94 | - public function isConstructed(): bool |
|
| 95 | - { |
|
| 96 | - return $this->_identifier->isConstructed(); |
|
| 97 | - } |
|
| 91 | + /** |
|
| 92 | + * {@inheritdoc} |
|
| 93 | + */ |
|
| 94 | + public function isConstructed(): bool |
|
| 95 | + { |
|
| 96 | + return $this->_identifier->isConstructed(); |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - /** |
|
| 100 | - * {@inheritdoc} |
|
| 101 | - */ |
|
| 102 | - public function implicit(int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
| 103 | - { |
|
| 104 | - $identifier = $this->_identifier->withClass($class)->withTag($tag); |
|
| 105 | - $cls = self::_determineImplClass($identifier); |
|
| 106 | - $idx = $this->_offset; |
|
| 107 | - /** @var \Sop\ASN1\Feature\ElementBase $element */ |
|
| 108 | - $element = $cls::_decodeFromDER($identifier, $this->_data, $idx); |
|
| 109 | - return $element->asUnspecified(); |
|
| 110 | - } |
|
| 99 | + /** |
|
| 100 | + * {@inheritdoc} |
|
| 101 | + */ |
|
| 102 | + public function implicit(int $tag, int $class = Identifier::CLASS_UNIVERSAL): UnspecifiedType |
|
| 103 | + { |
|
| 104 | + $identifier = $this->_identifier->withClass($class)->withTag($tag); |
|
| 105 | + $cls = self::_determineImplClass($identifier); |
|
| 106 | + $idx = $this->_offset; |
|
| 107 | + /** @var \Sop\ASN1\Feature\ElementBase $element */ |
|
| 108 | + $element = $cls::_decodeFromDER($identifier, $this->_data, $idx); |
|
| 109 | + return $element->asUnspecified(); |
|
| 110 | + } |
|
| 111 | 111 | |
| 112 | - /** |
|
| 113 | - * {@inheritdoc} |
|
| 114 | - */ |
|
| 115 | - public function explicit(): UnspecifiedType |
|
| 116 | - { |
|
| 117 | - $idx = $this->_valueOffset; |
|
| 118 | - return Element::fromDER($this->_data, $idx)->asUnspecified(); |
|
| 119 | - } |
|
| 112 | + /** |
|
| 113 | + * {@inheritdoc} |
|
| 114 | + */ |
|
| 115 | + public function explicit(): UnspecifiedType |
|
| 116 | + { |
|
| 117 | + $idx = $this->_valueOffset; |
|
| 118 | + return Element::fromDER($this->_data, $idx)->asUnspecified(); |
|
| 119 | + } |
|
| 120 | 120 | |
| 121 | - /** |
|
| 122 | - * {@inheritdoc} |
|
| 123 | - */ |
|
| 124 | - protected static function _decodeFromDER(Identifier $identifier, |
|
| 125 | - string $data, int &$offset): ElementBase |
|
| 126 | - { |
|
| 127 | - $idx = $offset; |
|
| 128 | - $length = Length::expectFromDER($data, $idx); |
|
| 129 | - // offset to inner value |
|
| 130 | - $value_offset = $idx; |
|
| 131 | - if ($length->isIndefinite()) { |
|
| 132 | - if ($identifier->isPrimitive()) { |
|
| 133 | - throw new DecodeException( |
|
| 134 | - 'Primitive type with indefinite length is not supported.'); |
|
| 135 | - } |
|
| 136 | - while (!Element::fromDER($data, $idx)->isType(self::TYPE_EOC)); |
|
| 137 | - // EOC consists of two octets. |
|
| 138 | - $value_length = $idx - $value_offset - 2; |
|
| 139 | - } else { |
|
| 140 | - $value_length = $length->intLength(); |
|
| 141 | - $idx += $value_length; |
|
| 142 | - } |
|
| 143 | - // late static binding since ApplicationType and PrivateType extend this class |
|
| 144 | - $type = new static($identifier, $data, $offset, $value_offset, |
|
| 145 | - $value_length, $length->isIndefinite()); |
|
| 146 | - $offset = $idx; |
|
| 147 | - return $type; |
|
| 148 | - } |
|
| 121 | + /** |
|
| 122 | + * {@inheritdoc} |
|
| 123 | + */ |
|
| 124 | + protected static function _decodeFromDER(Identifier $identifier, |
|
| 125 | + string $data, int &$offset): ElementBase |
|
| 126 | + { |
|
| 127 | + $idx = $offset; |
|
| 128 | + $length = Length::expectFromDER($data, $idx); |
|
| 129 | + // offset to inner value |
|
| 130 | + $value_offset = $idx; |
|
| 131 | + if ($length->isIndefinite()) { |
|
| 132 | + if ($identifier->isPrimitive()) { |
|
| 133 | + throw new DecodeException( |
|
| 134 | + 'Primitive type with indefinite length is not supported.'); |
|
| 135 | + } |
|
| 136 | + while (!Element::fromDER($data, $idx)->isType(self::TYPE_EOC)); |
|
| 137 | + // EOC consists of two octets. |
|
| 138 | + $value_length = $idx - $value_offset - 2; |
|
| 139 | + } else { |
|
| 140 | + $value_length = $length->intLength(); |
|
| 141 | + $idx += $value_length; |
|
| 142 | + } |
|
| 143 | + // late static binding since ApplicationType and PrivateType extend this class |
|
| 144 | + $type = new static($identifier, $data, $offset, $value_offset, |
|
| 145 | + $value_length, $length->isIndefinite()); |
|
| 146 | + $offset = $idx; |
|
| 147 | + return $type; |
|
| 148 | + } |
|
| 149 | 149 | |
| 150 | - /** |
|
| 151 | - * {@inheritdoc} |
|
| 152 | - */ |
|
| 153 | - protected function _encodedContentDER(): string |
|
| 154 | - { |
|
| 155 | - return substr($this->_data, $this->_valueOffset, $this->_valueLength); |
|
| 156 | - } |
|
| 150 | + /** |
|
| 151 | + * {@inheritdoc} |
|
| 152 | + */ |
|
| 153 | + protected function _encodedContentDER(): string |
|
| 154 | + { |
|
| 155 | + return substr($this->_data, $this->_valueOffset, $this->_valueLength); |
|
| 156 | + } |
|
| 157 | 157 | } |
@@ -12,10 +12,10 @@ |
||
| 12 | 12 | */ |
| 13 | 13 | interface ExplicitTagging extends ElementBase |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * Get explicitly tagged wrapped element. |
|
| 17 | - * |
|
| 18 | - * @return UnspecifiedType |
|
| 19 | - */ |
|
| 20 | - public function explicit(): UnspecifiedType; |
|
| 15 | + /** |
|
| 16 | + * Get explicitly tagged wrapped element. |
|
| 17 | + * |
|
| 18 | + * @return UnspecifiedType |
|
| 19 | + */ |
|
| 20 | + public function explicit(): UnspecifiedType; |
|
| 21 | 21 | } |
@@ -16,43 +16,43 @@ |
||
| 16 | 16 | */ |
| 17 | 17 | class ExplicitlyTaggedType extends TaggedTypeWrap implements ExplicitTagging |
| 18 | 18 | { |
| 19 | - /** |
|
| 20 | - * Constructor. |
|
| 21 | - * |
|
| 22 | - * @param int $tag Tag number |
|
| 23 | - * @param Element $element Wrapped element |
|
| 24 | - * @param int $class Type class |
|
| 25 | - */ |
|
| 26 | - public function __construct(int $tag, Element $element, |
|
| 27 | - int $class = Identifier::CLASS_CONTEXT_SPECIFIC) |
|
| 28 | - { |
|
| 29 | - $this->_typeTag = $tag; |
|
| 30 | - $this->_element = $element; |
|
| 31 | - $this->_class = $class; |
|
| 32 | - } |
|
| 19 | + /** |
|
| 20 | + * Constructor. |
|
| 21 | + * |
|
| 22 | + * @param int $tag Tag number |
|
| 23 | + * @param Element $element Wrapped element |
|
| 24 | + * @param int $class Type class |
|
| 25 | + */ |
|
| 26 | + public function __construct(int $tag, Element $element, |
|
| 27 | + int $class = Identifier::CLASS_CONTEXT_SPECIFIC) |
|
| 28 | + { |
|
| 29 | + $this->_typeTag = $tag; |
|
| 30 | + $this->_element = $element; |
|
| 31 | + $this->_class = $class; |
|
| 32 | + } |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * {@inheritdoc} |
|
| 36 | - */ |
|
| 37 | - public function isConstructed(): bool |
|
| 38 | - { |
|
| 39 | - return true; |
|
| 40 | - } |
|
| 34 | + /** |
|
| 35 | + * {@inheritdoc} |
|
| 36 | + */ |
|
| 37 | + public function isConstructed(): bool |
|
| 38 | + { |
|
| 39 | + return true; |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * {@inheritdoc} |
|
| 44 | - */ |
|
| 45 | - public function explicit(): UnspecifiedType |
|
| 46 | - { |
|
| 47 | - return $this->_element->asUnspecified(); |
|
| 48 | - } |
|
| 42 | + /** |
|
| 43 | + * {@inheritdoc} |
|
| 44 | + */ |
|
| 45 | + public function explicit(): UnspecifiedType |
|
| 46 | + { |
|
| 47 | + return $this->_element->asUnspecified(); |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | - /** |
|
| 51 | - * {@inheritdoc} |
|
| 52 | - */ |
|
| 53 | - protected function _encodedContentDER(): string |
|
| 54 | - { |
|
| 55 | - // get the full encoding of the wrapped element |
|
| 56 | - return $this->_element->toDER(); |
|
| 57 | - } |
|
| 50 | + /** |
|
| 51 | + * {@inheritdoc} |
|
| 52 | + */ |
|
| 53 | + protected function _encodedContentDER(): string |
|
| 54 | + { |
|
| 55 | + // get the full encoding of the wrapped element |
|
| 56 | + return $this->_element->toDER(); |
|
| 57 | + } |
|
| 58 | 58 | } |
@@ -15,406 +15,406 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | abstract class Structure extends Element implements \Countable, \IteratorAggregate |
| 17 | 17 | { |
| 18 | - use UniversalClass; |
|
| 18 | + use UniversalClass; |
|
| 19 | 19 | |
| 20 | - /** |
|
| 21 | - * Array of elements in the structure. |
|
| 22 | - * |
|
| 23 | - * @var Element[] |
|
| 24 | - */ |
|
| 25 | - protected $_elements; |
|
| 20 | + /** |
|
| 21 | + * Array of elements in the structure. |
|
| 22 | + * |
|
| 23 | + * @var Element[] |
|
| 24 | + */ |
|
| 25 | + protected $_elements; |
|
| 26 | 26 | |
| 27 | - /** |
|
| 28 | - * Lookup table for the tagged elements. |
|
| 29 | - * |
|
| 30 | - * @var null|TaggedType[] |
|
| 31 | - */ |
|
| 32 | - private $_taggedMap; |
|
| 27 | + /** |
|
| 28 | + * Lookup table for the tagged elements. |
|
| 29 | + * |
|
| 30 | + * @var null|TaggedType[] |
|
| 31 | + */ |
|
| 32 | + private $_taggedMap; |
|
| 33 | 33 | |
| 34 | - /** |
|
| 35 | - * Cache variable of elements wrapped into UnspecifiedType objects. |
|
| 36 | - * |
|
| 37 | - * @var null|UnspecifiedType[] |
|
| 38 | - */ |
|
| 39 | - private $_unspecifiedTypes; |
|
| 34 | + /** |
|
| 35 | + * Cache variable of elements wrapped into UnspecifiedType objects. |
|
| 36 | + * |
|
| 37 | + * @var null|UnspecifiedType[] |
|
| 38 | + */ |
|
| 39 | + private $_unspecifiedTypes; |
|
| 40 | 40 | |
| 41 | - /** |
|
| 42 | - * Constructor. |
|
| 43 | - * |
|
| 44 | - * @param ElementBase ...$elements Any number of elements |
|
| 45 | - */ |
|
| 46 | - public function __construct(ElementBase ...$elements) |
|
| 47 | - { |
|
| 48 | - $this->_elements = array_map( |
|
| 49 | - function (ElementBase $el) { |
|
| 50 | - return $el->asElement(); |
|
| 51 | - }, $elements); |
|
| 52 | - } |
|
| 41 | + /** |
|
| 42 | + * Constructor. |
|
| 43 | + * |
|
| 44 | + * @param ElementBase ...$elements Any number of elements |
|
| 45 | + */ |
|
| 46 | + public function __construct(ElementBase ...$elements) |
|
| 47 | + { |
|
| 48 | + $this->_elements = array_map( |
|
| 49 | + function (ElementBase $el) { |
|
| 50 | + return $el->asElement(); |
|
| 51 | + }, $elements); |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * Clone magic method. |
|
| 56 | - */ |
|
| 57 | - public function __clone() |
|
| 58 | - { |
|
| 59 | - // clear cache-variables |
|
| 60 | - $this->_taggedMap = null; |
|
| 61 | - $this->_unspecifiedTypes = null; |
|
| 62 | - } |
|
| 54 | + /** |
|
| 55 | + * Clone magic method. |
|
| 56 | + */ |
|
| 57 | + public function __clone() |
|
| 58 | + { |
|
| 59 | + // clear cache-variables |
|
| 60 | + $this->_taggedMap = null; |
|
| 61 | + $this->_unspecifiedTypes = null; |
|
| 62 | + } |
|
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * {@inheritdoc} |
|
| 66 | - */ |
|
| 67 | - public function isConstructed(): bool |
|
| 68 | - { |
|
| 69 | - return true; |
|
| 70 | - } |
|
| 64 | + /** |
|
| 65 | + * {@inheritdoc} |
|
| 66 | + */ |
|
| 67 | + public function isConstructed(): bool |
|
| 68 | + { |
|
| 69 | + return true; |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - /** |
|
| 73 | - * Explode DER structure to DER encoded components that it contains. |
|
| 74 | - * |
|
| 75 | - * @param string $data |
|
| 76 | - * |
|
| 77 | - * @throws DecodeException |
|
| 78 | - * |
|
| 79 | - * @return string[] |
|
| 80 | - */ |
|
| 81 | - public static function explodeDER(string $data): array |
|
| 82 | - { |
|
| 83 | - $offset = 0; |
|
| 84 | - $identifier = Identifier::fromDER($data, $offset); |
|
| 85 | - if (!$identifier->isConstructed()) { |
|
| 86 | - throw new DecodeException('Element is not constructed.'); |
|
| 87 | - } |
|
| 88 | - $length = Length::expectFromDER($data, $offset); |
|
| 89 | - if ($length->isIndefinite()) { |
|
| 90 | - throw new DecodeException( |
|
| 91 | - 'Explode not implemented for indefinite length encoding.'); |
|
| 92 | - } |
|
| 93 | - $end = $offset + $length->intLength(); |
|
| 94 | - $parts = []; |
|
| 95 | - while ($offset < $end) { |
|
| 96 | - // start of the element |
|
| 97 | - $idx = $offset; |
|
| 98 | - // skip identifier |
|
| 99 | - Identifier::fromDER($data, $offset); |
|
| 100 | - // decode element length |
|
| 101 | - $length = Length::expectFromDER($data, $offset)->intLength(); |
|
| 102 | - // extract der encoding of the element |
|
| 103 | - $parts[] = substr($data, $idx, $offset - $idx + $length); |
|
| 104 | - // update offset over content |
|
| 105 | - $offset += $length; |
|
| 106 | - } |
|
| 107 | - return $parts; |
|
| 108 | - } |
|
| 72 | + /** |
|
| 73 | + * Explode DER structure to DER encoded components that it contains. |
|
| 74 | + * |
|
| 75 | + * @param string $data |
|
| 76 | + * |
|
| 77 | + * @throws DecodeException |
|
| 78 | + * |
|
| 79 | + * @return string[] |
|
| 80 | + */ |
|
| 81 | + public static function explodeDER(string $data): array |
|
| 82 | + { |
|
| 83 | + $offset = 0; |
|
| 84 | + $identifier = Identifier::fromDER($data, $offset); |
|
| 85 | + if (!$identifier->isConstructed()) { |
|
| 86 | + throw new DecodeException('Element is not constructed.'); |
|
| 87 | + } |
|
| 88 | + $length = Length::expectFromDER($data, $offset); |
|
| 89 | + if ($length->isIndefinite()) { |
|
| 90 | + throw new DecodeException( |
|
| 91 | + 'Explode not implemented for indefinite length encoding.'); |
|
| 92 | + } |
|
| 93 | + $end = $offset + $length->intLength(); |
|
| 94 | + $parts = []; |
|
| 95 | + while ($offset < $end) { |
|
| 96 | + // start of the element |
|
| 97 | + $idx = $offset; |
|
| 98 | + // skip identifier |
|
| 99 | + Identifier::fromDER($data, $offset); |
|
| 100 | + // decode element length |
|
| 101 | + $length = Length::expectFromDER($data, $offset)->intLength(); |
|
| 102 | + // extract der encoding of the element |
|
| 103 | + $parts[] = substr($data, $idx, $offset - $idx + $length); |
|
| 104 | + // update offset over content |
|
| 105 | + $offset += $length; |
|
| 106 | + } |
|
| 107 | + return $parts; |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | - /** |
|
| 111 | - * Get self with an element at the given index replaced by another. |
|
| 112 | - * |
|
| 113 | - * @param int $idx Element index |
|
| 114 | - * @param Element $el New element to insert into the structure |
|
| 115 | - * |
|
| 116 | - * @throws \OutOfBoundsException |
|
| 117 | - * |
|
| 118 | - * @return self |
|
| 119 | - */ |
|
| 120 | - public function withReplaced(int $idx, Element $el): self |
|
| 121 | - { |
|
| 122 | - if (!isset($this->_elements[$idx])) { |
|
| 123 | - throw new \OutOfBoundsException( |
|
| 124 | - "Structure doesn't have element at index ${idx}."); |
|
| 125 | - } |
|
| 126 | - $obj = clone $this; |
|
| 127 | - $obj->_elements[$idx] = $el; |
|
| 128 | - return $obj; |
|
| 129 | - } |
|
| 110 | + /** |
|
| 111 | + * Get self with an element at the given index replaced by another. |
|
| 112 | + * |
|
| 113 | + * @param int $idx Element index |
|
| 114 | + * @param Element $el New element to insert into the structure |
|
| 115 | + * |
|
| 116 | + * @throws \OutOfBoundsException |
|
| 117 | + * |
|
| 118 | + * @return self |
|
| 119 | + */ |
|
| 120 | + public function withReplaced(int $idx, Element $el): self |
|
| 121 | + { |
|
| 122 | + if (!isset($this->_elements[$idx])) { |
|
| 123 | + throw new \OutOfBoundsException( |
|
| 124 | + "Structure doesn't have element at index ${idx}."); |
|
| 125 | + } |
|
| 126 | + $obj = clone $this; |
|
| 127 | + $obj->_elements[$idx] = $el; |
|
| 128 | + return $obj; |
|
| 129 | + } |
|
| 130 | 130 | |
| 131 | - /** |
|
| 132 | - * Get self with an element inserted before the given index. |
|
| 133 | - * |
|
| 134 | - * @param int $idx Element index |
|
| 135 | - * @param Element $el New element to insert into the structure |
|
| 136 | - * |
|
| 137 | - * @throws \OutOfBoundsException |
|
| 138 | - * |
|
| 139 | - * @return self |
|
| 140 | - */ |
|
| 141 | - public function withInserted(int $idx, Element $el): self |
|
| 142 | - { |
|
| 143 | - if (count($this->_elements) < $idx || $idx < 0) { |
|
| 144 | - throw new \OutOfBoundsException("Index ${idx} is out of bounds."); |
|
| 145 | - } |
|
| 146 | - $obj = clone $this; |
|
| 147 | - array_splice($obj->_elements, $idx, 0, [$el]); |
|
| 148 | - return $obj; |
|
| 149 | - } |
|
| 131 | + /** |
|
| 132 | + * Get self with an element inserted before the given index. |
|
| 133 | + * |
|
| 134 | + * @param int $idx Element index |
|
| 135 | + * @param Element $el New element to insert into the structure |
|
| 136 | + * |
|
| 137 | + * @throws \OutOfBoundsException |
|
| 138 | + * |
|
| 139 | + * @return self |
|
| 140 | + */ |
|
| 141 | + public function withInserted(int $idx, Element $el): self |
|
| 142 | + { |
|
| 143 | + if (count($this->_elements) < $idx || $idx < 0) { |
|
| 144 | + throw new \OutOfBoundsException("Index ${idx} is out of bounds."); |
|
| 145 | + } |
|
| 146 | + $obj = clone $this; |
|
| 147 | + array_splice($obj->_elements, $idx, 0, [$el]); |
|
| 148 | + return $obj; |
|
| 149 | + } |
|
| 150 | 150 | |
| 151 | - /** |
|
| 152 | - * Get self with an element appended to the end. |
|
| 153 | - * |
|
| 154 | - * @param Element $el Element to insert into the structure |
|
| 155 | - * |
|
| 156 | - * @return self |
|
| 157 | - */ |
|
| 158 | - public function withAppended(Element $el): self |
|
| 159 | - { |
|
| 160 | - $obj = clone $this; |
|
| 161 | - array_push($obj->_elements, $el); |
|
| 162 | - return $obj; |
|
| 163 | - } |
|
| 151 | + /** |
|
| 152 | + * Get self with an element appended to the end. |
|
| 153 | + * |
|
| 154 | + * @param Element $el Element to insert into the structure |
|
| 155 | + * |
|
| 156 | + * @return self |
|
| 157 | + */ |
|
| 158 | + public function withAppended(Element $el): self |
|
| 159 | + { |
|
| 160 | + $obj = clone $this; |
|
| 161 | + array_push($obj->_elements, $el); |
|
| 162 | + return $obj; |
|
| 163 | + } |
|
| 164 | 164 | |
| 165 | - /** |
|
| 166 | - * Get self with an element prepended in the beginning. |
|
| 167 | - * |
|
| 168 | - * @param Element $el Element to insert into the structure |
|
| 169 | - * |
|
| 170 | - * @return self |
|
| 171 | - */ |
|
| 172 | - public function withPrepended(Element $el): self |
|
| 173 | - { |
|
| 174 | - $obj = clone $this; |
|
| 175 | - array_unshift($obj->_elements, $el); |
|
| 176 | - return $obj; |
|
| 177 | - } |
|
| 165 | + /** |
|
| 166 | + * Get self with an element prepended in the beginning. |
|
| 167 | + * |
|
| 168 | + * @param Element $el Element to insert into the structure |
|
| 169 | + * |
|
| 170 | + * @return self |
|
| 171 | + */ |
|
| 172 | + public function withPrepended(Element $el): self |
|
| 173 | + { |
|
| 174 | + $obj = clone $this; |
|
| 175 | + array_unshift($obj->_elements, $el); |
|
| 176 | + return $obj; |
|
| 177 | + } |
|
| 178 | 178 | |
| 179 | - /** |
|
| 180 | - * Get self with an element at the given index removed. |
|
| 181 | - * |
|
| 182 | - * @param int $idx Element index |
|
| 183 | - * |
|
| 184 | - * @throws \OutOfBoundsException |
|
| 185 | - * |
|
| 186 | - * @return self |
|
| 187 | - */ |
|
| 188 | - public function withoutElement(int $idx): self |
|
| 189 | - { |
|
| 190 | - if (!isset($this->_elements[$idx])) { |
|
| 191 | - throw new \OutOfBoundsException( |
|
| 192 | - "Structure doesn't have element at index ${idx}."); |
|
| 193 | - } |
|
| 194 | - $obj = clone $this; |
|
| 195 | - array_splice($obj->_elements, $idx, 1); |
|
| 196 | - return $obj; |
|
| 197 | - } |
|
| 179 | + /** |
|
| 180 | + * Get self with an element at the given index removed. |
|
| 181 | + * |
|
| 182 | + * @param int $idx Element index |
|
| 183 | + * |
|
| 184 | + * @throws \OutOfBoundsException |
|
| 185 | + * |
|
| 186 | + * @return self |
|
| 187 | + */ |
|
| 188 | + public function withoutElement(int $idx): self |
|
| 189 | + { |
|
| 190 | + if (!isset($this->_elements[$idx])) { |
|
| 191 | + throw new \OutOfBoundsException( |
|
| 192 | + "Structure doesn't have element at index ${idx}."); |
|
| 193 | + } |
|
| 194 | + $obj = clone $this; |
|
| 195 | + array_splice($obj->_elements, $idx, 1); |
|
| 196 | + return $obj; |
|
| 197 | + } |
|
| 198 | 198 | |
| 199 | - /** |
|
| 200 | - * Get elements in the structure. |
|
| 201 | - * |
|
| 202 | - * @return UnspecifiedType[] |
|
| 203 | - */ |
|
| 204 | - public function elements(): array |
|
| 205 | - { |
|
| 206 | - if (!isset($this->_unspecifiedTypes)) { |
|
| 207 | - $this->_unspecifiedTypes = array_map( |
|
| 208 | - function (Element $el) { |
|
| 209 | - return new UnspecifiedType($el); |
|
| 210 | - }, $this->_elements); |
|
| 211 | - } |
|
| 212 | - return $this->_unspecifiedTypes; |
|
| 213 | - } |
|
| 199 | + /** |
|
| 200 | + * Get elements in the structure. |
|
| 201 | + * |
|
| 202 | + * @return UnspecifiedType[] |
|
| 203 | + */ |
|
| 204 | + public function elements(): array |
|
| 205 | + { |
|
| 206 | + if (!isset($this->_unspecifiedTypes)) { |
|
| 207 | + $this->_unspecifiedTypes = array_map( |
|
| 208 | + function (Element $el) { |
|
| 209 | + return new UnspecifiedType($el); |
|
| 210 | + }, $this->_elements); |
|
| 211 | + } |
|
| 212 | + return $this->_unspecifiedTypes; |
|
| 213 | + } |
|
| 214 | 214 | |
| 215 | - /** |
|
| 216 | - * Check whether the structure has an element at the given index, optionally |
|
| 217 | - * satisfying given tag expectation. |
|
| 218 | - * |
|
| 219 | - * @param int $idx Index 0..n |
|
| 220 | - * @param null|int $expectedTag Optional type tag expectation |
|
| 221 | - * |
|
| 222 | - * @return bool |
|
| 223 | - */ |
|
| 224 | - public function has(int $idx, ?int $expectedTag = null): bool |
|
| 225 | - { |
|
| 226 | - if (!isset($this->_elements[$idx])) { |
|
| 227 | - return false; |
|
| 228 | - } |
|
| 229 | - if (isset($expectedTag)) { |
|
| 230 | - if (!$this->_elements[$idx]->isType($expectedTag)) { |
|
| 231 | - return false; |
|
| 232 | - } |
|
| 233 | - } |
|
| 234 | - return true; |
|
| 235 | - } |
|
| 215 | + /** |
|
| 216 | + * Check whether the structure has an element at the given index, optionally |
|
| 217 | + * satisfying given tag expectation. |
|
| 218 | + * |
|
| 219 | + * @param int $idx Index 0..n |
|
| 220 | + * @param null|int $expectedTag Optional type tag expectation |
|
| 221 | + * |
|
| 222 | + * @return bool |
|
| 223 | + */ |
|
| 224 | + public function has(int $idx, ?int $expectedTag = null): bool |
|
| 225 | + { |
|
| 226 | + if (!isset($this->_elements[$idx])) { |
|
| 227 | + return false; |
|
| 228 | + } |
|
| 229 | + if (isset($expectedTag)) { |
|
| 230 | + if (!$this->_elements[$idx]->isType($expectedTag)) { |
|
| 231 | + return false; |
|
| 232 | + } |
|
| 233 | + } |
|
| 234 | + return true; |
|
| 235 | + } |
|
| 236 | 236 | |
| 237 | - /** |
|
| 238 | - * Get the element at the given index, optionally checking that the element |
|
| 239 | - * has a given tag. |
|
| 240 | - * |
|
| 241 | - * @param int $idx Index 0..n |
|
| 242 | - * |
|
| 243 | - * @throws \OutOfBoundsException If element doesn't exists |
|
| 244 | - * @throws \UnexpectedValueException If expectation fails |
|
| 245 | - * |
|
| 246 | - * @return UnspecifiedType |
|
| 247 | - */ |
|
| 248 | - public function at(int $idx): UnspecifiedType |
|
| 249 | - { |
|
| 250 | - if (!isset($this->_elements[$idx])) { |
|
| 251 | - throw new \OutOfBoundsException( |
|
| 252 | - "Structure doesn't have an element at index ${idx}."); |
|
| 253 | - } |
|
| 254 | - return new UnspecifiedType($this->_elements[$idx]); |
|
| 255 | - } |
|
| 237 | + /** |
|
| 238 | + * Get the element at the given index, optionally checking that the element |
|
| 239 | + * has a given tag. |
|
| 240 | + * |
|
| 241 | + * @param int $idx Index 0..n |
|
| 242 | + * |
|
| 243 | + * @throws \OutOfBoundsException If element doesn't exists |
|
| 244 | + * @throws \UnexpectedValueException If expectation fails |
|
| 245 | + * |
|
| 246 | + * @return UnspecifiedType |
|
| 247 | + */ |
|
| 248 | + public function at(int $idx): UnspecifiedType |
|
| 249 | + { |
|
| 250 | + if (!isset($this->_elements[$idx])) { |
|
| 251 | + throw new \OutOfBoundsException( |
|
| 252 | + "Structure doesn't have an element at index ${idx}."); |
|
| 253 | + } |
|
| 254 | + return new UnspecifiedType($this->_elements[$idx]); |
|
| 255 | + } |
|
| 256 | 256 | |
| 257 | - /** |
|
| 258 | - * Check whether the structure contains a context specific element with a |
|
| 259 | - * given tag. |
|
| 260 | - * |
|
| 261 | - * @param int $tag Tag number |
|
| 262 | - * |
|
| 263 | - * @return bool |
|
| 264 | - */ |
|
| 265 | - public function hasTagged(int $tag): bool |
|
| 266 | - { |
|
| 267 | - // lazily build lookup map |
|
| 268 | - if (!isset($this->_taggedMap)) { |
|
| 269 | - $this->_taggedMap = []; |
|
| 270 | - foreach ($this->_elements as $element) { |
|
| 271 | - if ($element->isTagged()) { |
|
| 272 | - $this->_taggedMap[$element->tag()] = $element; |
|
| 273 | - } |
|
| 274 | - } |
|
| 275 | - } |
|
| 276 | - return isset($this->_taggedMap[$tag]); |
|
| 277 | - } |
|
| 257 | + /** |
|
| 258 | + * Check whether the structure contains a context specific element with a |
|
| 259 | + * given tag. |
|
| 260 | + * |
|
| 261 | + * @param int $tag Tag number |
|
| 262 | + * |
|
| 263 | + * @return bool |
|
| 264 | + */ |
|
| 265 | + public function hasTagged(int $tag): bool |
|
| 266 | + { |
|
| 267 | + // lazily build lookup map |
|
| 268 | + if (!isset($this->_taggedMap)) { |
|
| 269 | + $this->_taggedMap = []; |
|
| 270 | + foreach ($this->_elements as $element) { |
|
| 271 | + if ($element->isTagged()) { |
|
| 272 | + $this->_taggedMap[$element->tag()] = $element; |
|
| 273 | + } |
|
| 274 | + } |
|
| 275 | + } |
|
| 276 | + return isset($this->_taggedMap[$tag]); |
|
| 277 | + } |
|
| 278 | 278 | |
| 279 | - /** |
|
| 280 | - * Get a context specific element tagged with a given tag. |
|
| 281 | - * |
|
| 282 | - * @param int $tag |
|
| 283 | - * |
|
| 284 | - * @throws \LogicException If tag doesn't exists |
|
| 285 | - * |
|
| 286 | - * @return TaggedType |
|
| 287 | - */ |
|
| 288 | - public function getTagged(int $tag): TaggedType |
|
| 289 | - { |
|
| 290 | - if (!$this->hasTagged($tag)) { |
|
| 291 | - throw new \LogicException("No tagged element for tag ${tag}."); |
|
| 292 | - } |
|
| 293 | - return $this->_taggedMap[$tag]; |
|
| 294 | - } |
|
| 279 | + /** |
|
| 280 | + * Get a context specific element tagged with a given tag. |
|
| 281 | + * |
|
| 282 | + * @param int $tag |
|
| 283 | + * |
|
| 284 | + * @throws \LogicException If tag doesn't exists |
|
| 285 | + * |
|
| 286 | + * @return TaggedType |
|
| 287 | + */ |
|
| 288 | + public function getTagged(int $tag): TaggedType |
|
| 289 | + { |
|
| 290 | + if (!$this->hasTagged($tag)) { |
|
| 291 | + throw new \LogicException("No tagged element for tag ${tag}."); |
|
| 292 | + } |
|
| 293 | + return $this->_taggedMap[$tag]; |
|
| 294 | + } |
|
| 295 | 295 | |
| 296 | - /** |
|
| 297 | - * @see \Countable::count() |
|
| 298 | - * |
|
| 299 | - * @return int |
|
| 300 | - */ |
|
| 301 | - public function count(): int |
|
| 302 | - { |
|
| 303 | - return count($this->_elements); |
|
| 304 | - } |
|
| 296 | + /** |
|
| 297 | + * @see \Countable::count() |
|
| 298 | + * |
|
| 299 | + * @return int |
|
| 300 | + */ |
|
| 301 | + public function count(): int |
|
| 302 | + { |
|
| 303 | + return count($this->_elements); |
|
| 304 | + } |
|
| 305 | 305 | |
| 306 | - /** |
|
| 307 | - * Get an iterator for the UnspecifiedElement objects. |
|
| 308 | - * |
|
| 309 | - * @see \IteratorAggregate::getIterator() |
|
| 310 | - * |
|
| 311 | - * @return \ArrayIterator |
|
| 312 | - */ |
|
| 313 | - public function getIterator(): \ArrayIterator |
|
| 314 | - { |
|
| 315 | - return new \ArrayIterator($this->elements()); |
|
| 316 | - } |
|
| 306 | + /** |
|
| 307 | + * Get an iterator for the UnspecifiedElement objects. |
|
| 308 | + * |
|
| 309 | + * @see \IteratorAggregate::getIterator() |
|
| 310 | + * |
|
| 311 | + * @return \ArrayIterator |
|
| 312 | + */ |
|
| 313 | + public function getIterator(): \ArrayIterator |
|
| 314 | + { |
|
| 315 | + return new \ArrayIterator($this->elements()); |
|
| 316 | + } |
|
| 317 | 317 | |
| 318 | - /** |
|
| 319 | - * @see \Sop\ASN1\Element::_encodedContentDER() |
|
| 320 | - * |
|
| 321 | - * @return string |
|
| 322 | - */ |
|
| 323 | - protected function _encodedContentDER(): string |
|
| 324 | - { |
|
| 325 | - $data = ''; |
|
| 326 | - foreach ($this->_elements as $element) { |
|
| 327 | - $data .= $element->toDER(); |
|
| 328 | - } |
|
| 329 | - return $data; |
|
| 330 | - } |
|
| 318 | + /** |
|
| 319 | + * @see \Sop\ASN1\Element::_encodedContentDER() |
|
| 320 | + * |
|
| 321 | + * @return string |
|
| 322 | + */ |
|
| 323 | + protected function _encodedContentDER(): string |
|
| 324 | + { |
|
| 325 | + $data = ''; |
|
| 326 | + foreach ($this->_elements as $element) { |
|
| 327 | + $data .= $element->toDER(); |
|
| 328 | + } |
|
| 329 | + return $data; |
|
| 330 | + } |
|
| 331 | 331 | |
| 332 | - /** |
|
| 333 | - * {@inheritdoc} |
|
| 334 | - * |
|
| 335 | - * @see \Sop\ASN1\Element::_decodeFromDER() |
|
| 336 | - * |
|
| 337 | - * @return self |
|
| 338 | - */ |
|
| 339 | - protected static function _decodeFromDER(Identifier $identifier, |
|
| 340 | - string $data, int &$offset): ElementBase |
|
| 341 | - { |
|
| 342 | - if (!$identifier->isConstructed()) { |
|
| 343 | - throw new DecodeException( |
|
| 344 | - 'Structured element must have constructed bit set.'); |
|
| 345 | - } |
|
| 346 | - $idx = $offset; |
|
| 347 | - $length = Length::expectFromDER($data, $idx); |
|
| 348 | - if ($length->isIndefinite()) { |
|
| 349 | - $type = self::_decodeIndefiniteLength($data, $idx); |
|
| 350 | - } else { |
|
| 351 | - $type = self::_decodeDefiniteLength($data, $idx, |
|
| 352 | - $length->intLength()); |
|
| 353 | - } |
|
| 354 | - $offset = $idx; |
|
| 355 | - return $type; |
|
| 356 | - } |
|
| 332 | + /** |
|
| 333 | + * {@inheritdoc} |
|
| 334 | + * |
|
| 335 | + * @see \Sop\ASN1\Element::_decodeFromDER() |
|
| 336 | + * |
|
| 337 | + * @return self |
|
| 338 | + */ |
|
| 339 | + protected static function _decodeFromDER(Identifier $identifier, |
|
| 340 | + string $data, int &$offset): ElementBase |
|
| 341 | + { |
|
| 342 | + if (!$identifier->isConstructed()) { |
|
| 343 | + throw new DecodeException( |
|
| 344 | + 'Structured element must have constructed bit set.'); |
|
| 345 | + } |
|
| 346 | + $idx = $offset; |
|
| 347 | + $length = Length::expectFromDER($data, $idx); |
|
| 348 | + if ($length->isIndefinite()) { |
|
| 349 | + $type = self::_decodeIndefiniteLength($data, $idx); |
|
| 350 | + } else { |
|
| 351 | + $type = self::_decodeDefiniteLength($data, $idx, |
|
| 352 | + $length->intLength()); |
|
| 353 | + } |
|
| 354 | + $offset = $idx; |
|
| 355 | + return $type; |
|
| 356 | + } |
|
| 357 | 357 | |
| 358 | - /** |
|
| 359 | - * Decode elements for a definite length. |
|
| 360 | - * |
|
| 361 | - * @param string $data DER data |
|
| 362 | - * @param int $offset Offset to data |
|
| 363 | - * @param int $length Number of bytes to decode |
|
| 364 | - * |
|
| 365 | - * @throws DecodeException |
|
| 366 | - * |
|
| 367 | - * @return ElementBase |
|
| 368 | - */ |
|
| 369 | - private static function _decodeDefiniteLength(string $data, int &$offset, |
|
| 370 | - int $length): ElementBase |
|
| 371 | - { |
|
| 372 | - $idx = $offset; |
|
| 373 | - $end = $idx + $length; |
|
| 374 | - $elements = []; |
|
| 375 | - while ($idx < $end) { |
|
| 376 | - $elements[] = Element::fromDER($data, $idx); |
|
| 377 | - // check that element didn't overflow length |
|
| 378 | - if ($idx > $end) { |
|
| 379 | - throw new DecodeException( |
|
| 380 | - "Structure's content overflows length."); |
|
| 381 | - } |
|
| 382 | - } |
|
| 383 | - $offset = $idx; |
|
| 384 | - // return instance by static late binding |
|
| 385 | - return new static(...$elements); |
|
| 386 | - } |
|
| 358 | + /** |
|
| 359 | + * Decode elements for a definite length. |
|
| 360 | + * |
|
| 361 | + * @param string $data DER data |
|
| 362 | + * @param int $offset Offset to data |
|
| 363 | + * @param int $length Number of bytes to decode |
|
| 364 | + * |
|
| 365 | + * @throws DecodeException |
|
| 366 | + * |
|
| 367 | + * @return ElementBase |
|
| 368 | + */ |
|
| 369 | + private static function _decodeDefiniteLength(string $data, int &$offset, |
|
| 370 | + int $length): ElementBase |
|
| 371 | + { |
|
| 372 | + $idx = $offset; |
|
| 373 | + $end = $idx + $length; |
|
| 374 | + $elements = []; |
|
| 375 | + while ($idx < $end) { |
|
| 376 | + $elements[] = Element::fromDER($data, $idx); |
|
| 377 | + // check that element didn't overflow length |
|
| 378 | + if ($idx > $end) { |
|
| 379 | + throw new DecodeException( |
|
| 380 | + "Structure's content overflows length."); |
|
| 381 | + } |
|
| 382 | + } |
|
| 383 | + $offset = $idx; |
|
| 384 | + // return instance by static late binding |
|
| 385 | + return new static(...$elements); |
|
| 386 | + } |
|
| 387 | 387 | |
| 388 | - /** |
|
| 389 | - * Decode elements for an indefinite length. |
|
| 390 | - * |
|
| 391 | - * @param string $data DER data |
|
| 392 | - * @param int $offset Offset to data |
|
| 393 | - * |
|
| 394 | - * @throws DecodeException |
|
| 395 | - * |
|
| 396 | - * @return ElementBase |
|
| 397 | - */ |
|
| 398 | - private static function _decodeIndefiniteLength( |
|
| 399 | - string $data, int &$offset): ElementBase |
|
| 400 | - { |
|
| 401 | - $idx = $offset; |
|
| 402 | - $elements = []; |
|
| 403 | - $end = strlen($data); |
|
| 404 | - while (true) { |
|
| 405 | - if ($idx >= $end) { |
|
| 406 | - throw new DecodeException( |
|
| 407 | - 'Unexpected end of data while decoding indefinite length structure.'); |
|
| 408 | - } |
|
| 409 | - $el = Element::fromDER($data, $idx); |
|
| 410 | - if ($el->isType(self::TYPE_EOC)) { |
|
| 411 | - break; |
|
| 412 | - } |
|
| 413 | - $elements[] = $el; |
|
| 414 | - } |
|
| 415 | - $offset = $idx; |
|
| 416 | - $type = new static(...$elements); |
|
| 417 | - $type->_indefiniteLength = true; |
|
| 418 | - return $type; |
|
| 419 | - } |
|
| 388 | + /** |
|
| 389 | + * Decode elements for an indefinite length. |
|
| 390 | + * |
|
| 391 | + * @param string $data DER data |
|
| 392 | + * @param int $offset Offset to data |
|
| 393 | + * |
|
| 394 | + * @throws DecodeException |
|
| 395 | + * |
|
| 396 | + * @return ElementBase |
|
| 397 | + */ |
|
| 398 | + private static function _decodeIndefiniteLength( |
|
| 399 | + string $data, int &$offset): ElementBase |
|
| 400 | + { |
|
| 401 | + $idx = $offset; |
|
| 402 | + $elements = []; |
|
| 403 | + $end = strlen($data); |
|
| 404 | + while (true) { |
|
| 405 | + if ($idx >= $end) { |
|
| 406 | + throw new DecodeException( |
|
| 407 | + 'Unexpected end of data while decoding indefinite length structure.'); |
|
| 408 | + } |
|
| 409 | + $el = Element::fromDER($data, $idx); |
|
| 410 | + if ($el->isType(self::TYPE_EOC)) { |
|
| 411 | + break; |
|
| 412 | + } |
|
| 413 | + $elements[] = $el; |
|
| 414 | + } |
|
| 415 | + $offset = $idx; |
|
| 416 | + $type = new static(...$elements); |
|
| 417 | + $type->_indefiniteLength = true; |
|
| 418 | + return $type; |
|
| 419 | + } |
|
| 420 | 420 | } |