| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace SpomkyLabs\Pki\X509\Certificate\Extension\Target; |
| 6 |
|
| 7 |
use RuntimeException; |
| 8 |
use SpomkyLabs\Pki\ASN1\Element; |
| 9 |
use SpomkyLabs\Pki\ASN1\Type\TaggedType; |
| 10 |
use UnexpectedValueException; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
abstract class Target |
| 18 |
{ |
| 19 |
public const TYPE_NAME = 0; |
| 20 |
|
| 21 |
public const TYPE_GROUP = 1; |
| 22 |
|
| 23 |
public const TYPE_CERT = 2; |
| 24 |
|
| 25 |
protected function __construct( |
| 26 |
protected int $type |
| 27 |
) { |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
abstract public function toASN1(): Element; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
abstract public function string(): string; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
abstract public static function fromChosenASN1(TaggedType $el): self; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public static function fromASN1(TaggedType $el): self |
| 49 |
{ |
| 50 |
return match ($el->tag()) { |
| 51 |
self::TYPE_NAME => TargetName::fromChosenASN1($el->asExplicit()->asTagged()), |
| 52 |
self::TYPE_GROUP => TargetGroup::fromChosenASN1($el->asExplicit()->asTagged()), |
| 53 |
self::TYPE_CERT => throw new RuntimeException('targetCert not supported.'), |
| 54 |
default => throw new UnexpectedValueException('Target type ' . $el->tag() . ' not supported.'), |
| 55 |
}; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public function type(): int |
| 62 |
{ |
| 63 |
return $this->type; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public function equals(self $other): bool |
| 70 |
{ |
| 71 |
if ($this->type !== $other->type) { |
| 72 |
return false; |
| 73 |
} |
| 74 |
if ($this->toASN1()->toDER() !== $other->toASN1()->toDER()) { |
| 75 |
return false; |
| 76 |
} |
| 77 |
return true; |
| 78 |
} |
| 79 |
} |