<?php
 declare(strict_types=1); namespace SpomkyLabs\Pki\ASN1\Type; use SpomkyLabs\Pki\ASN1\Component\Identifier; use SpomkyLabs\Pki\ASN1\Element; use SpomkyLabs\Pki\ASN1\Type\Tagged\ExplicitTagging; use SpomkyLabs\Pki\ASN1\Type\Tagged\ImplicitTagging; use UnexpectedValueException; abstract class TaggedType extends Element { public function expectExplicit(?int $expectedTag = null): ExplicitTagging { $el = $this; if (! $el instanceof ExplicitTagging) { throw new UnexpectedValueException("Element doesn't implement explicit tagging."); } if (isset($expectedTag)) { $el->expectTagged($expectedTag); } return $el; } public function asExplicit(?int $expectedTag = null): UnspecifiedType { return $this->expectExplicit($expectedTag) ->explicit(); } public function expectImplicit(?int $expectedTag = null): ImplicitTagging { $el = $this; if (! $el instanceof ImplicitTagging) { throw new UnexpectedValueException("Element doesn't implement implicit tagging."); } if (isset($expectedTag)) { $el->expectTagged($expectedTag); } return $el; } public function asImplicit( int $tag, ?int $expectedTag = null, int $expectedClass = Identifier::CLASS_UNIVERSAL ): UnspecifiedType { return $this->expectImplicit($expectedTag) ->implicit($tag, $expectedClass); } } 