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