Debian Codegraph @

lib/3rdParty/composer/spomky-labs/pki-framework/src/X501/ASN1/Collection/AttributeCollection.php

raw symbols SCIP
1 <?php
2
3 declare(strict_types=1);
4
5 namespace SpomkyLabs\Pki\X501\ASN1\Collection;
6
7 use ArrayIterator;
8 use function count;
9 use Countable;
10 use IteratorAggregate;
11 use SpomkyLabs\Pki\ASN1\Type\Structure;
12 use SpomkyLabs\Pki\ASN1\Type\UnspecifiedType;
13 use SpomkyLabs\Pki\X501\ASN1\Attribute;
14 use SpomkyLabs\Pki\X501\ASN1\AttributeType;
15 use UnexpectedValueException;
16
17 /**
18 * Base class for X.501 attribute containers.
19 *
20 * Implements methods for Countable and IteratorAggregate interfaces.
21 */
22 abstract class AttributeCollection implements Countable, IteratorAggregate
23 {
24 /**
25 * Array of attributes.
26 *
27 * Always with consecutive indices.
28 *
29 * @var Attribute[]
30 */
31 protected array $_attributes;
32
33 /**
34 * @param Attribute ...$attribs List of attributes
35 */
36 final private function __construct(Attribute ...$attribs)
37 {
38 $this->_attributes = $attribs;
39 }
40
41 public static function create(Attribute ...$attribs): static
42 {
43 return new static(...$attribs);
44 }
45
46 /**
47 * Check whether attribute is present.
48 *
49 * @param string $name OID or attribute name
50 */
51 public function has(string $name): bool
52 {
53 return $this->_findFirst($name) !== null;
54 }
55
56 /**
57 * Get first attribute by OID or attribute name.
58 *
59 * @param string $name OID or attribute name
60 */
61 public function firstOf(string $name): Attribute
62 {
63 $attr = $this->_findFirst($name);
64 if ($attr === null) {
65 throw new UnexpectedValueException("No {$name} attribute.");
66 }
67 return $attr;
68 }
69
70 /**
71 * Get all attributes of given name.
72 *
73 * @param string $name OID or attribute name
74 *
75 * @return Attribute[]
76 */
77 public function allOf(string $name): array
78 {
79 $oid = AttributeType::attrNameToOID($name);
80 return array_values(array_filter($this->_attributes, fn (Attribute $attr) => $attr->oid() === $oid));
81 }
82
83 /**
84 * Get all attributes.
85 *
86 * @return Attribute[]
87 */
88 public function all(): array
89 {
90 return $this->_attributes;
91 }
92
93 /**
94 * Get self with additional attributes added.
95 *
96 * @param Attribute ...$attribs List of attributes to add
97 */
98 public function withAdditional(Attribute ...$attribs): self
99 {
100 $obj = clone $this;
101 foreach ($attribs as $attr) {
102 $obj->_attributes[] = $attr;
103 }
104 return $obj;
105 }
106
107 /**
108 * Get self with single unique attribute added.
109 *
110 * All previous attributes of the same type are removed.
111 *
112 * @param Attribute $attr Attribute to add
113 */
114 public function withUnique(Attribute $attr): static
115 {
116 $attribs = array_values(array_filter($this->_attributes, fn (Attribute $a) => $a->oid() !== $attr->oid()));
117 $attribs[] = $attr;
118 $obj = clone $this;
119 $obj->_attributes = $attribs;
120 return $obj;
121 }
122
123 /**
124 * Get number of attributes.
125 *
126 * @see \Countable::count()
127 */
128 public function count(): int
129 {
130 return count($this->_attributes);
131 }
132
133 /**
134 * Get iterator for attributes.
135 *
136 * @return ArrayIterator|Attribute[]
137 * @see \IteratorAggregate::getIterator()
138 */
139 public function getIterator(): ArrayIterator
140 {
141 return new ArrayIterator($this->_attributes);
142 }
143
144 /**
145 * Find first attribute of given name or OID.
146 *
147 * @param string $name OID or attribute name
148 */
149 protected function _findFirst(string $name): ?Attribute
150 {
151 $oid = AttributeType::attrNameToOID($name);
152 foreach ($this->_attributes as $attr) {
153 if ($attr->oid() === $oid) {
154 return $attr;
155 }
156 }
157 return null;
158 }
159
160 /**
161 * Initialize from ASN.1 constructed element.
162 *
163 * @param Structure $struct ASN.1 structure
164 */
165 protected static function _fromASN1Structure(Structure $struct): static
166 {
167 return static::create(...array_map(
168 static fn (UnspecifiedType $el) => static::_castAttributeValues(
169 Attribute::fromASN1($el->asSequence())
170 ),
171 $struct->elements()
172 ));
173 }
174
175 /**
176 * Cast Attribute's AttributeValues to implementation specific objects.
177 *
178 * Overridden in derived classes.
179 *
180 * @param Attribute $attribute Attribute to cast
181 */
182 protected static function _castAttributeValues(Attribute $attribute): Attribute
183 {
184 // pass through by default
185 return $attribute;
186 }
187 }