Debian Codegraph @

lib/3rdParty/composer/spomky-labs/pki-framework/src/X509/Certificate/Extension/NameConstraints/GeneralSubtrees.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace SpomkyLabs\Pki\X509\Certificate\Extension\NameConstraints;
6
7 use ArrayIterator;
8 use function count;
9 use Countable;
10 use IteratorAggregate;
11 use LogicException;
12 use SpomkyLabs\Pki\ASN1\Type\Constructed\Sequence;
13 use SpomkyLabs\Pki\ASN1\Type\UnspecifiedType;
14 use UnexpectedValueException;
15
16 /**
17 * Implements *GeneralSubtrees* ASN.1 type used by 'Name Constraints' certificate extension.
18 *
19 * @see @link https://tools.ietf.org/html/rfc5280#section-4.2.1.10
20 */
21 final class GeneralSubtrees implements Countable, IteratorAggregate
22 {
23 /**
24 * Subtrees.
25 *
26 * @var GeneralSubtree[]
27 */
28 private readonly array $subtrees;
29
30 private function __construct(GeneralSubtree ...$subtrees)
31 {
32 $this->subtrees = $subtrees;
33 }
34
35 public static function create(GeneralSubtree ...$subtrees): self
36 {
37 return new self(...$subtrees);
38 }
39
40 /**
41 * Initialize from ASN.1.
42 */
43 public static function fromASN1(Sequence $seq): self
44 {
45 $subtrees = array_map(
46 static fn (UnspecifiedType $el) => GeneralSubtree::fromASN1($el->asSequence()),
47 $seq->elements()
48 );
49 if (count($subtrees) === 0) {
50 throw new UnexpectedValueException('GeneralSubtrees must contain at least one GeneralSubtree.');
51 }
52 return self::create(...$subtrees);
53 }
54
55 /**
56 * Get all subtrees.
57 *
58 * @return GeneralSubtree[]
59 */
60 public function all(): array
61 {
62 return $this->subtrees;
63 }
64
65 /**
66 * Generate ASN.1 structure.
67 */
68 public function toASN1(): Sequence
69 {
70 if (count($this->subtrees) === 0) {
71 throw new LogicException('No subtrees.');
72 }
73 $elements = array_map(static fn (GeneralSubtree $gs) => $gs->toASN1(), $this->subtrees);
74 return Sequence::create(...$elements);
75 }
76
77 /**
78 * @see \Countable::count()
79 */
80 public function count(): int
81 {
82 return count($this->subtrees);
83 }
84
85 /**
86 * Get iterator for subtrees.
87 *
88 * @see \IteratorAggregate::getIterator()
89 */
90 public function getIterator(): ArrayIterator
91 {
92 return new ArrayIterator($this->subtrees);
93 }
94 }