Debian Codegraph @

lib/3rdParty/composer/spomky-labs/pki-framework/src/X509/GeneralName/GeneralNames.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace SpomkyLabs\Pki\X509\GeneralName;
6
7 use ArrayIterator;
8 use function count;
9 use Countable;
10 use IteratorAggregate;
11 use LogicException;
12 use RuntimeException;
13 use SpomkyLabs\Pki\ASN1\Type\Constructed\Sequence;
14 use SpomkyLabs\Pki\ASN1\Type\UnspecifiedType;
15 use SpomkyLabs\Pki\X501\ASN1\Name;
16 use UnexpectedValueException;
17
18 /**
19 * Implements *GeneralNames* ASN.1 type.
20 *
21 * Provides convenience methods to retrieve the first value of commonly used CHOICE types.
22 *
23 * @see https://tools.ietf.org/html/rfc5280#section-4.2.1.6
24 */
25 final class GeneralNames implements Countable, IteratorAggregate
26 {
27 /**
28 * GeneralName objects.
29 *
30 * @var GeneralName[]
31 */
32 private readonly array $_names;
33
34 /**
35 * @param GeneralName ...$names One or more GeneralName objects
36 */
37 private function __construct(GeneralName ...$names)
38 {
39 $this->_names = $names;
40 }
41
42 public static function create(GeneralName ...$names): self
43 {
44 return new self(...$names);
45 }
46
47 /**
48 * Initialize from ASN.1.
49 */
50 public static function fromASN1(Sequence $seq): self
51 {
52 if (count($seq) === 0) {
53 throw new UnexpectedValueException('GeneralNames must have at least one GeneralName.');
54 }
55 $names = array_map(static fn (UnspecifiedType $el) => GeneralName::fromASN1($el->asTagged()), $seq->elements());
56 return self::create(...$names);
57 }
58
59 /**
60 * Check whether GeneralNames contains a GeneralName of given type.
61 *
62 * @param int $tag One of `GeneralName::TAG_*` enumerations
63 */
64 public function has(int $tag): bool
65 {
66 return $this->findFirst($tag) !== null;
67 }
68
69 /**
70 * Get first GeneralName of given type.
71 *
72 * @param int $tag One of `GeneralName::TAG_*` enumerations
73 */
74 public function firstOf(int $tag): GeneralName
75 {
76 $name = $this->findFirst($tag);
77 if ($name === null) {
78 throw new UnexpectedValueException("No GeneralName by tag {$tag}.");
79 }
80 return $name;
81 }
82
83 /**
84 * Get all GeneralName objects of given type.
85 *
86 * @param int $tag One of `GeneralName::TAG_*` enumerations
87 *
88 * @return GeneralName[]
89 */
90 public function allOf(int $tag): array
91 {
92 $names = array_filter($this->_names, fn (GeneralName $name) => $name->tag() === $tag);
93 return array_values($names);
94 }
95
96 /**
97 * Get value of the first 'dNSName' type.
98 */
99 public function firstDNS(): string
100 {
101 $gn = $this->firstOf(GeneralName::TAG_DNS_NAME);
102 if (! $gn instanceof DNSName) {
103 throw new RuntimeException(DNSName::class . ' expected, got ' . $gn::class);
104 }
105 return $gn->name();
106 }
107
108 /**
109 * Get value of the first 'directoryName' type.
110 */
111 public function firstDN(): Name
112 {
113 $gn = $this->firstOf(GeneralName::TAG_DIRECTORY_NAME);
114 if (! $gn instanceof DirectoryName) {
115 throw new RuntimeException(DirectoryName::class . ' expected, got ' . $gn::class);
116 }
117 return $gn->dn();
118 }
119
120 /**
121 * Get value of the first 'uniformResourceIdentifier' type.
122 */
123 public function firstURI(): string
124 {
125 $gn = $this->firstOf(GeneralName::TAG_URI);
126 if (! $gn instanceof UniformResourceIdentifier) {
127 throw new RuntimeException(UniformResourceIdentifier::class . ' expected, got ' . $gn::class);
128 }
129 return $gn->uri();
130 }
131
132 /**
133 * Generate ASN.1 structure.
134 */
135 public function toASN1(): Sequence
136 {
137 if (count($this->_names) === 0) {
138 throw new LogicException('GeneralNames must have at least one GeneralName.');
139 }
140 $elements = array_map(static fn (GeneralName $name) => $name->toASN1(), $this->_names);
141 return Sequence::create(...$elements);
142 }
143
144 /**
145 * @see \Countable::count()
146 */
147 public function count(): int
148 {
149 return count($this->_names);
150 }
151
152 /**
153 * Get iterator for GeneralName objects.
154 *
155 * @see \IteratorAggregate::getIterator()
156 */
157 public function getIterator(): ArrayIterator
158 {
159 return new ArrayIterator($this->_names);
160 }
161
162 /**
163 * Find first GeneralName by given tag.
164 */
165 private function findFirst(int $tag): ?GeneralName
166 {
167 foreach ($this->_names as $name) {
168 if ($name->tag() === $tag) {
169 return $name;
170 }
171 }
172 return null;
173 }
174 }