| 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 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
final class PolicyNode implements IteratorAggregate, Countable |
| 23 |
{ |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
private array $children; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
private PolicyNode|null $parent = null; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 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 |
|
| 50 |
|
| 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 |
|
| 59 |
|
| 60 |
public static function anyPolicyNode(): self |
| 61 |
{ |
| 62 |
return self::create(PolicyInformation::OID_ANY_POLICY, [], [PolicyInformation::OID_ANY_POLICY]); |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
public function validPolicy(): string |
| 69 |
{ |
| 70 |
return $this->validPolicy; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public function isAnyPolicy(): bool |
| 77 |
{ |
| 78 |
return $this->validPolicy === PolicyInformation::OID_ANY_POLICY; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
public function qualifiers(): array |
| 87 |
{ |
| 88 |
return $this->qualifiers; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
public function hasExpectedPolicy(string $oid): bool |
| 95 |
{ |
| 96 |
return in_array($oid, $this->expectedPolicies, true); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public function expectedPolicies(): array |
| 105 |
{ |
| 106 |
return $this->expectedPolicies; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public function setExpectedPolicies(string ...$oids): void |
| 115 |
{ |
| 116 |
$this->expectedPolicies = $oids; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 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 |
|
| 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 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public function children(): array |
| 149 |
{ |
| 150 |
return array_values($this->children); |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 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 |
|
| 169 |
|
| 170 |
public function hasParent(): bool |
| 171 |
{ |
| 172 |
return isset($this->parent); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
public function parent(): ?self |
| 179 |
{ |
| 180 |
return $this->parent; |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 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 |
|
| 200 |
|
| 201 |
|
| 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 |
|
| 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 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
public function count(): int |
| 229 |
{ |
| 230 |
return count($this->children); |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
public function getIterator(): ArrayIterator |
| 239 |
{ |
| 240 |
return new ArrayIterator($this->children); |
| 241 |
} |
| 242 |
} |