| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace SpomkyLabs\Pki\X509\Certificate\Extension; |
| 6 |
|
| 7 |
use ArrayIterator; |
| 8 |
use function count; |
| 9 |
use Countable; |
| 10 |
use IteratorAggregate; |
| 11 |
use SpomkyLabs\Pki\ASN1\Element; |
| 12 |
use SpomkyLabs\Pki\ASN1\Type\Constructed\Sequence; |
| 13 |
use SpomkyLabs\Pki\ASN1\Type\UnspecifiedType; |
| 14 |
use SpomkyLabs\Pki\X509\Certificate\Extension\Target\Target; |
| 15 |
use SpomkyLabs\Pki\X509\Certificate\Extension\Target\Targets; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
final class TargetInformationExtension extends Extension implements Countable, IteratorAggregate |
| 26 |
{ |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
protected array $targets; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
private ?Targets $merged = null; |
| 38 |
|
| 39 |
private function __construct(bool $critical, Targets ...$targets) |
| 40 |
{ |
| 41 |
parent::__construct(self::OID_TARGET_INFORMATION, $critical); |
| 42 |
$this->targets = $targets; |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public function __clone() |
| 49 |
{ |
| 50 |
$this->merged = null; |
| 51 |
} |
| 52 |
|
| 53 |
public static function create(bool $critical, Targets ...$targets): self |
| 54 |
{ |
| 55 |
return new self($critical, ...$targets); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public static function fromTargets(Target ...$target): self |
| 64 |
{ |
| 65 |
return self::create(true, Targets::create(...$target)); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public function targets(): Targets |
| 72 |
{ |
| 73 |
if ($this->merged === null) { |
| 74 |
$a = []; |
| 75 |
foreach ($this->targets as $targets) { |
| 76 |
$a = array_merge($a, $targets->all()); |
| 77 |
} |
| 78 |
$this->merged = Targets::create(...$a); |
| 79 |
} |
| 80 |
return $this->merged; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public function names(): array |
| 89 |
{ |
| 90 |
return $this->targets() |
| 91 |
->nameTargets(); |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public function groups(): array |
| 100 |
{ |
| 101 |
return $this->targets() |
| 102 |
->groupTargets(); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
public function count(): int |
| 109 |
{ |
| 110 |
return count($this->targets()); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public function getIterator(): ArrayIterator |
| 119 |
{ |
| 120 |
return new ArrayIterator($this->targets()->all()); |
| 121 |
} |
| 122 |
|
| 123 |
protected static function fromDER(string $data, bool $critical): static |
| 124 |
{ |
| 125 |
$targets = array_map( |
| 126 |
static fn (UnspecifiedType $el) => Targets::fromASN1($el->asSequence()), |
| 127 |
UnspecifiedType::fromDER($data)->asSequence()->elements() |
| 128 |
); |
| 129 |
return self::create($critical, ...$targets); |
| 130 |
} |
| 131 |
|
| 132 |
protected function valueASN1(): Element |
| 133 |
{ |
| 134 |
$elements = array_map(static fn (Targets $targets) => $targets->toASN1(), $this->targets); |
| 135 |
return Sequence::create(...$elements); |
| 136 |
} |
| 137 |
} |