Debian Codegraph @

lib/3rdParty/composer/spomky-labs/pki-framework/src/X509/CertificationPath/Policy/PolicyNode.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace SpomkyLabs\Pki\X509\CertificationPath\Policy;
6
7 use ArrayIterator;
8 use function count;
9 use Countable;
10 use function in_array;
11 use IteratorAggregate;
12 use SpomkyLabs\Pki\X509\Certificate\Extension\CertificatePolicy\PolicyInformation;
13 use SpomkyLabs\Pki\X509\Certificate\Extension\CertificatePolicy\PolicyQualifierInfo;
14
15 /**
16 * Policy node class for certification path validation.
17 *
18 * @internal Mutable class used by PolicyTree
19 *
20 * @see https://tools.ietf.org/html/rfc5280#section-6.1.2
21 */
22 final class PolicyNode implements IteratorAggregate, Countable
23 {
24 /**
25 * List of child nodes.
26 *
27 * @var PolicyNode[]
28 */
29 private array $children;
30
31 /**
32 * Reference to the parent node.
33 */
34 private PolicyNode|null $parent = null;
35
36 /**
37 * @param PolicyQualifierInfo[] $qualifiers
38 * @param string[] $expectedPolicies
39 */
40 private function __construct(
41 private readonly string $validPolicy,
42 private readonly array $qualifiers,
43 private array $expectedPolicies
44 ) {
45 $this->children = [];
46 }
47
48 /**
49 * @param PolicyQualifierInfo[] $qualifiers
50 * @param string[] $expectedPolicies
51 */
52 public static function create(string $validPolicy, array $qualifiers, array $expectedPolicies): self
53 {
54 return new self($validPolicy, $qualifiers, $expectedPolicies);
55 }
56
57 /**
58 * Create initial node for the policy tree.
59 */
60 public static function anyPolicyNode(): self
61 {
62 return self::create(PolicyInformation::OID_ANY_POLICY, [], [PolicyInformation::OID_ANY_POLICY]);
63 }
64
65 /**
66 * Get the valid policy OID.
67 */
68 public function validPolicy(): string
69 {
70 return $this->validPolicy;
71 }
72
73 /**
74 * Check whether node has anyPolicy as a valid policy.
75 */
76 public function isAnyPolicy(): bool
77 {
78 return $this->validPolicy === PolicyInformation::OID_ANY_POLICY;
79 }
80
81 /**
82 * Get the qualifier set.
83 *
84 * @return PolicyQualifierInfo[]
85 */
86 public function qualifiers(): array
87 {
88 return $this->qualifiers;
89 }
90
91 /**
92 * Check whether node has OID as an expected policy.
93 */
94 public function hasExpectedPolicy(string $oid): bool
95 {
96 return in_array($oid, $this->expectedPolicies, true);
97 }
98
99 /**
100 * Get the expected policy set.
101 *
102 * @return string[]
103 */
104 public function expectedPolicies(): array
105 {
106 return $this->expectedPolicies;
107 }
108
109 /**
110 * Set expected policies.
111 *
112 * @param string ...$oids Policy OIDs
113 */
114 public function setExpectedPolicies(string ...$oids): void
115 {
116 $this->expectedPolicies = $oids;
117 }
118
119 /**
120 * Check whether node has a child node with given valid policy OID.
121 */
122 public function hasChildWithValidPolicy(string $oid): bool
123 {
124 foreach ($this->children as $node) {
125 if ($node->validPolicy() === $oid) {
126 return true;
127 }
128 }
129 return false;
130 }
131
132 /**
133 * Add child node.
134 */
135 public function addChild(self $node): self
136 {
137 $id = spl_object_hash($node);
138 $node->parent = $this;
139 $this->children[$id] = $node;
140 return $this;
141 }
142
143 /**
144 * Get the child nodes.
145 *
146 * @return PolicyNode[]
147 */
148 public function children(): array
149 {
150 return array_values($this->children);
151 }
152
153 /**
154 * Remove this node from the tree.
155 *
156 * @return self The removed node
157 */
158 public function remove(): self
159 {
160 if ($this->parent !== null) {
161 $id = spl_object_hash($this);
162 unset($this->parent->children[$id], $this->parent);
163 }
164 return $this;
165 }
166
167 /**
168 * Check whether node has a parent.
169 */
170 public function hasParent(): bool
171 {
172 return isset($this->parent);
173 }
174
175 /**
176 * Get the parent node.
177 */
178 public function parent(): ?self
179 {
180 return $this->parent;
181 }
182
183 /**
184 * Get chain of parent nodes from this node's parent to the root node.
185 *
186 * @return PolicyNode[]
187 */
188 public function parents(): array
189 {
190 if ($this->parent === null) {
191 return [];
192 }
193 $nodes = $this->parent->parents();
194 $nodes[] = $this->parent;
195 return array_reverse($nodes);
196 }
197
198 /**
199 * Walk tree from this node, applying a callback for each node.
200 *
201 * Nodes are traversed depth-first and callback shall be applied post-order.
202 */
203 public function walkNodes(callable $fn): void
204 {
205 foreach ($this->children as $node) {
206 $node->walkNodes($fn);
207 }
208 $fn($this);
209 }
210
211 /**
212 * Get the total number of nodes in a tree.
213 */
214 public function nodeCount(): int
215 {
216 $c = 1;
217 foreach ($this->children as $child) {
218 $c += $child->nodeCount();
219 }
220 return $c;
221 }
222
223 /**
224 * Get the number of child nodes.
225 *
226 * @see \Countable::count()
227 */
228 public function count(): int
229 {
230 return count($this->children);
231 }
232
233 /**
234 * Get iterator for the child nodes.
235 *
236 * @see \IteratorAggregate::getIterator()
237 */
238 public function getIterator(): ArrayIterator
239 {
240 return new ArrayIterator($this->children);
241 }
242 }