@@ -14,336 +14,336 @@ |
||
| 14 | 14 | * Base class for the constructed types. |
| 15 | 15 | */ |
| 16 | 16 | abstract class Structure extends Element implements |
| 17 | - \Countable, |
|
| 18 | - \IteratorAggregate |
|
| 17 | + \Countable, |
|
| 18 | + \IteratorAggregate |
|
| 19 | 19 | { |
| 20 | - use UniversalClass; |
|
| 20 | + use UniversalClass; |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * Array of elements in the structure. |
|
| 24 | - * |
|
| 25 | - * @var Element[] $_elements |
|
| 26 | - */ |
|
| 27 | - protected $_elements; |
|
| 22 | + /** |
|
| 23 | + * Array of elements in the structure. |
|
| 24 | + * |
|
| 25 | + * @var Element[] $_elements |
|
| 26 | + */ |
|
| 27 | + protected $_elements; |
|
| 28 | 28 | |
| 29 | - /** |
|
| 30 | - * Lookup table for the tagged elements. |
|
| 31 | - * |
|
| 32 | - * @var TaggedType[]|null $_taggedMap |
|
| 33 | - */ |
|
| 34 | - private $_taggedMap; |
|
| 29 | + /** |
|
| 30 | + * Lookup table for the tagged elements. |
|
| 31 | + * |
|
| 32 | + * @var TaggedType[]|null $_taggedMap |
|
| 33 | + */ |
|
| 34 | + private $_taggedMap; |
|
| 35 | 35 | |
| 36 | - /** |
|
| 37 | - * Cache variable of elements wrapped into UnspecifiedType objects. |
|
| 38 | - * |
|
| 39 | - * @var UnspecifiedType[]|null $_unspecifiedTypes |
|
| 40 | - */ |
|
| 41 | - private $_unspecifiedTypes; |
|
| 36 | + /** |
|
| 37 | + * Cache variable of elements wrapped into UnspecifiedType objects. |
|
| 38 | + * |
|
| 39 | + * @var UnspecifiedType[]|null $_unspecifiedTypes |
|
| 40 | + */ |
|
| 41 | + private $_unspecifiedTypes; |
|
| 42 | 42 | |
| 43 | - /** |
|
| 44 | - * Constructor. |
|
| 45 | - * |
|
| 46 | - * @param Element ...$elements Any number of elements |
|
| 47 | - */ |
|
| 48 | - public function __construct(Element ...$elements) |
|
| 49 | - { |
|
| 50 | - $this->_elements = $elements; |
|
| 51 | - } |
|
| 43 | + /** |
|
| 44 | + * Constructor. |
|
| 45 | + * |
|
| 46 | + * @param Element ...$elements Any number of elements |
|
| 47 | + */ |
|
| 48 | + public function __construct(Element ...$elements) |
|
| 49 | + { |
|
| 50 | + $this->_elements = $elements; |
|
| 51 | + } |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * Clone magic method. |
|
| 55 | - */ |
|
| 56 | - public function __clone() |
|
| 57 | - { |
|
| 58 | - // clear cache-variables |
|
| 59 | - $this->_taggedMap = null; |
|
| 60 | - $this->_unspecifiedTypes = null; |
|
| 61 | - } |
|
| 53 | + /** |
|
| 54 | + * Clone magic method. |
|
| 55 | + */ |
|
| 56 | + public function __clone() |
|
| 57 | + { |
|
| 58 | + // clear cache-variables |
|
| 59 | + $this->_taggedMap = null; |
|
| 60 | + $this->_unspecifiedTypes = null; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - /** |
|
| 64 | - * |
|
| 65 | - * @see \ASN1\Element::isConstructed() |
|
| 66 | - * @return bool |
|
| 67 | - */ |
|
| 68 | - public function isConstructed(): bool |
|
| 69 | - { |
|
| 70 | - return true; |
|
| 71 | - } |
|
| 63 | + /** |
|
| 64 | + * |
|
| 65 | + * @see \ASN1\Element::isConstructed() |
|
| 66 | + * @return bool |
|
| 67 | + */ |
|
| 68 | + public function isConstructed(): bool |
|
| 69 | + { |
|
| 70 | + return true; |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - /** |
|
| 74 | - * |
|
| 75 | - * @see \ASN1\Element::_encodedContentDER() |
|
| 76 | - * @return string |
|
| 77 | - */ |
|
| 78 | - protected function _encodedContentDER(): string |
|
| 79 | - { |
|
| 80 | - $data = ""; |
|
| 81 | - foreach ($this->_elements as $element) { |
|
| 82 | - $data .= $element->toDER(); |
|
| 83 | - } |
|
| 84 | - return $data; |
|
| 85 | - } |
|
| 73 | + /** |
|
| 74 | + * |
|
| 75 | + * @see \ASN1\Element::_encodedContentDER() |
|
| 76 | + * @return string |
|
| 77 | + */ |
|
| 78 | + protected function _encodedContentDER(): string |
|
| 79 | + { |
|
| 80 | + $data = ""; |
|
| 81 | + foreach ($this->_elements as $element) { |
|
| 82 | + $data .= $element->toDER(); |
|
| 83 | + } |
|
| 84 | + return $data; |
|
| 85 | + } |
|
| 86 | 86 | |
| 87 | - /** |
|
| 88 | - * |
|
| 89 | - * {@inheritdoc} |
|
| 90 | - * @see \ASN1\Element::_decodeFromDER() |
|
| 91 | - * @return self |
|
| 92 | - */ |
|
| 93 | - protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 94 | - int &$offset): ElementBase |
|
| 95 | - { |
|
| 96 | - $idx = $offset; |
|
| 97 | - if (!$identifier->isConstructed()) { |
|
| 98 | - throw new DecodeException( |
|
| 99 | - "Structured element must have constructed bit set."); |
|
| 100 | - } |
|
| 101 | - $length = Length::expectFromDER($data, $idx); |
|
| 102 | - $end = $idx + $length->length(); |
|
| 103 | - $elements = []; |
|
| 104 | - while ($idx < $end) { |
|
| 105 | - $elements[] = Element::fromDER($data, $idx); |
|
| 106 | - // check that element didn't overflow length |
|
| 107 | - if ($idx > $end) { |
|
| 108 | - throw new DecodeException( |
|
| 109 | - "Structure's content overflows length."); |
|
| 110 | - } |
|
| 111 | - } |
|
| 112 | - $offset = $idx; |
|
| 113 | - // return instance by static late binding |
|
| 114 | - return new static(...$elements); |
|
| 115 | - } |
|
| 87 | + /** |
|
| 88 | + * |
|
| 89 | + * {@inheritdoc} |
|
| 90 | + * @see \ASN1\Element::_decodeFromDER() |
|
| 91 | + * @return self |
|
| 92 | + */ |
|
| 93 | + protected static function _decodeFromDER(Identifier $identifier, string $data, |
|
| 94 | + int &$offset): ElementBase |
|
| 95 | + { |
|
| 96 | + $idx = $offset; |
|
| 97 | + if (!$identifier->isConstructed()) { |
|
| 98 | + throw new DecodeException( |
|
| 99 | + "Structured element must have constructed bit set."); |
|
| 100 | + } |
|
| 101 | + $length = Length::expectFromDER($data, $idx); |
|
| 102 | + $end = $idx + $length->length(); |
|
| 103 | + $elements = []; |
|
| 104 | + while ($idx < $end) { |
|
| 105 | + $elements[] = Element::fromDER($data, $idx); |
|
| 106 | + // check that element didn't overflow length |
|
| 107 | + if ($idx > $end) { |
|
| 108 | + throw new DecodeException( |
|
| 109 | + "Structure's content overflows length."); |
|
| 110 | + } |
|
| 111 | + } |
|
| 112 | + $offset = $idx; |
|
| 113 | + // return instance by static late binding |
|
| 114 | + return new static(...$elements); |
|
| 115 | + } |
|
| 116 | 116 | |
| 117 | - /** |
|
| 118 | - * Explode DER structure to DER encoded components that it contains. |
|
| 119 | - * |
|
| 120 | - * @param string $data |
|
| 121 | - * @throws DecodeException |
|
| 122 | - * @return string[] |
|
| 123 | - */ |
|
| 124 | - public static function explodeDER(string $data): array |
|
| 125 | - { |
|
| 126 | - $offset = 0; |
|
| 127 | - $identifier = Identifier::fromDER($data, $offset); |
|
| 128 | - if (!$identifier->isConstructed()) { |
|
| 129 | - throw new DecodeException("Element is not constructed."); |
|
| 130 | - } |
|
| 131 | - $length = Length::expectFromDER($data, $offset); |
|
| 132 | - $end = $offset + $length->length(); |
|
| 133 | - $parts = []; |
|
| 134 | - while ($offset < $end) { |
|
| 135 | - // start of the element |
|
| 136 | - $idx = $offset; |
|
| 137 | - // skip identifier |
|
| 138 | - Identifier::fromDER($data, $offset); |
|
| 139 | - // decode element length |
|
| 140 | - $length = Length::expectFromDER($data, $offset); |
|
| 141 | - // extract der encoding of the element |
|
| 142 | - $parts[] = substr($data, $idx, $offset - $idx + $length->length()); |
|
| 143 | - // update offset over content |
|
| 144 | - $offset += $length->length(); |
|
| 145 | - } |
|
| 146 | - return $parts; |
|
| 147 | - } |
|
| 117 | + /** |
|
| 118 | + * Explode DER structure to DER encoded components that it contains. |
|
| 119 | + * |
|
| 120 | + * @param string $data |
|
| 121 | + * @throws DecodeException |
|
| 122 | + * @return string[] |
|
| 123 | + */ |
|
| 124 | + public static function explodeDER(string $data): array |
|
| 125 | + { |
|
| 126 | + $offset = 0; |
|
| 127 | + $identifier = Identifier::fromDER($data, $offset); |
|
| 128 | + if (!$identifier->isConstructed()) { |
|
| 129 | + throw new DecodeException("Element is not constructed."); |
|
| 130 | + } |
|
| 131 | + $length = Length::expectFromDER($data, $offset); |
|
| 132 | + $end = $offset + $length->length(); |
|
| 133 | + $parts = []; |
|
| 134 | + while ($offset < $end) { |
|
| 135 | + // start of the element |
|
| 136 | + $idx = $offset; |
|
| 137 | + // skip identifier |
|
| 138 | + Identifier::fromDER($data, $offset); |
|
| 139 | + // decode element length |
|
| 140 | + $length = Length::expectFromDER($data, $offset); |
|
| 141 | + // extract der encoding of the element |
|
| 142 | + $parts[] = substr($data, $idx, $offset - $idx + $length->length()); |
|
| 143 | + // update offset over content |
|
| 144 | + $offset += $length->length(); |
|
| 145 | + } |
|
| 146 | + return $parts; |
|
| 147 | + } |
|
| 148 | 148 | |
| 149 | - /** |
|
| 150 | - * Get self with an element at the given index replaced by another. |
|
| 151 | - * |
|
| 152 | - * @param int $idx Element index |
|
| 153 | - * @param Element $el New element to insert into the structure |
|
| 154 | - * @throws \OutOfBoundsException |
|
| 155 | - * @return self |
|
| 156 | - */ |
|
| 157 | - public function withReplaced(int $idx, Element $el): self |
|
| 158 | - { |
|
| 159 | - if (!isset($this->_elements[$idx])) { |
|
| 160 | - throw new \OutOfBoundsException( |
|
| 161 | - "Structure doesn't have element at index $idx."); |
|
| 162 | - } |
|
| 163 | - $obj = clone $this; |
|
| 164 | - $obj->_elements[$idx] = $el; |
|
| 165 | - return $obj; |
|
| 166 | - } |
|
| 149 | + /** |
|
| 150 | + * Get self with an element at the given index replaced by another. |
|
| 151 | + * |
|
| 152 | + * @param int $idx Element index |
|
| 153 | + * @param Element $el New element to insert into the structure |
|
| 154 | + * @throws \OutOfBoundsException |
|
| 155 | + * @return self |
|
| 156 | + */ |
|
| 157 | + public function withReplaced(int $idx, Element $el): self |
|
| 158 | + { |
|
| 159 | + if (!isset($this->_elements[$idx])) { |
|
| 160 | + throw new \OutOfBoundsException( |
|
| 161 | + "Structure doesn't have element at index $idx."); |
|
| 162 | + } |
|
| 163 | + $obj = clone $this; |
|
| 164 | + $obj->_elements[$idx] = $el; |
|
| 165 | + return $obj; |
|
| 166 | + } |
|
| 167 | 167 | |
| 168 | - /** |
|
| 169 | - * Get self with an element inserted before the given index. |
|
| 170 | - * |
|
| 171 | - * @param int $idx Element index |
|
| 172 | - * @param Element $el New element to insert into the structure |
|
| 173 | - * @throws \OutOfBoundsException |
|
| 174 | - * @return self |
|
| 175 | - */ |
|
| 176 | - public function withInserted(int $idx, Element $el): self |
|
| 177 | - { |
|
| 178 | - if (count($this->_elements) < $idx || $idx < 0) { |
|
| 179 | - throw new \OutOfBoundsException("Index $idx is out of bounds."); |
|
| 180 | - } |
|
| 181 | - $obj = clone $this; |
|
| 182 | - array_splice($obj->_elements, $idx, 0, [$el]); |
|
| 183 | - return $obj; |
|
| 184 | - } |
|
| 168 | + /** |
|
| 169 | + * Get self with an element inserted before the given index. |
|
| 170 | + * |
|
| 171 | + * @param int $idx Element index |
|
| 172 | + * @param Element $el New element to insert into the structure |
|
| 173 | + * @throws \OutOfBoundsException |
|
| 174 | + * @return self |
|
| 175 | + */ |
|
| 176 | + public function withInserted(int $idx, Element $el): self |
|
| 177 | + { |
|
| 178 | + if (count($this->_elements) < $idx || $idx < 0) { |
|
| 179 | + throw new \OutOfBoundsException("Index $idx is out of bounds."); |
|
| 180 | + } |
|
| 181 | + $obj = clone $this; |
|
| 182 | + array_splice($obj->_elements, $idx, 0, [$el]); |
|
| 183 | + return $obj; |
|
| 184 | + } |
|
| 185 | 185 | |
| 186 | - /** |
|
| 187 | - * Get self with an element appended to the end. |
|
| 188 | - * |
|
| 189 | - * @param Element $el Element to insert into the structure |
|
| 190 | - * @return self |
|
| 191 | - */ |
|
| 192 | - public function withAppended(Element $el): self |
|
| 193 | - { |
|
| 194 | - $obj = clone $this; |
|
| 195 | - array_push($obj->_elements, $el); |
|
| 196 | - return $obj; |
|
| 197 | - } |
|
| 186 | + /** |
|
| 187 | + * Get self with an element appended to the end. |
|
| 188 | + * |
|
| 189 | + * @param Element $el Element to insert into the structure |
|
| 190 | + * @return self |
|
| 191 | + */ |
|
| 192 | + public function withAppended(Element $el): self |
|
| 193 | + { |
|
| 194 | + $obj = clone $this; |
|
| 195 | + array_push($obj->_elements, $el); |
|
| 196 | + return $obj; |
|
| 197 | + } |
|
| 198 | 198 | |
| 199 | - /** |
|
| 200 | - * Get self with an element prepended in the beginning. |
|
| 201 | - * |
|
| 202 | - * @param Element $el Element to insert into the structure |
|
| 203 | - * @return self |
|
| 204 | - */ |
|
| 205 | - public function withPrepended(Element $el): self |
|
| 206 | - { |
|
| 207 | - $obj = clone $this; |
|
| 208 | - array_unshift($obj->_elements, $el); |
|
| 209 | - return $obj; |
|
| 210 | - } |
|
| 199 | + /** |
|
| 200 | + * Get self with an element prepended in the beginning. |
|
| 201 | + * |
|
| 202 | + * @param Element $el Element to insert into the structure |
|
| 203 | + * @return self |
|
| 204 | + */ |
|
| 205 | + public function withPrepended(Element $el): self |
|
| 206 | + { |
|
| 207 | + $obj = clone $this; |
|
| 208 | + array_unshift($obj->_elements, $el); |
|
| 209 | + return $obj; |
|
| 210 | + } |
|
| 211 | 211 | |
| 212 | - /** |
|
| 213 | - * Get self with an element at the given index removed. |
|
| 214 | - * |
|
| 215 | - * @param int $idx Element index |
|
| 216 | - * @throws \OutOfBoundsException |
|
| 217 | - * @return self |
|
| 218 | - */ |
|
| 219 | - public function withoutElement($idx): self |
|
| 220 | - { |
|
| 221 | - if (!isset($this->_elements[$idx])) { |
|
| 222 | - throw new \OutOfBoundsException( |
|
| 223 | - "Structure doesn't have element at index $idx."); |
|
| 224 | - } |
|
| 225 | - $obj = clone $this; |
|
| 226 | - array_splice($obj->_elements, $idx, 1); |
|
| 227 | - return $obj; |
|
| 228 | - } |
|
| 212 | + /** |
|
| 213 | + * Get self with an element at the given index removed. |
|
| 214 | + * |
|
| 215 | + * @param int $idx Element index |
|
| 216 | + * @throws \OutOfBoundsException |
|
| 217 | + * @return self |
|
| 218 | + */ |
|
| 219 | + public function withoutElement($idx): self |
|
| 220 | + { |
|
| 221 | + if (!isset($this->_elements[$idx])) { |
|
| 222 | + throw new \OutOfBoundsException( |
|
| 223 | + "Structure doesn't have element at index $idx."); |
|
| 224 | + } |
|
| 225 | + $obj = clone $this; |
|
| 226 | + array_splice($obj->_elements, $idx, 1); |
|
| 227 | + return $obj; |
|
| 228 | + } |
|
| 229 | 229 | |
| 230 | - /** |
|
| 231 | - * Get elements in the structure. |
|
| 232 | - * |
|
| 233 | - * @return UnspecifiedType[] |
|
| 234 | - */ |
|
| 235 | - public function elements(): array |
|
| 236 | - { |
|
| 237 | - if (!isset($this->_unspecifiedTypes)) { |
|
| 238 | - $this->_unspecifiedTypes = array_map( |
|
| 239 | - function (Element $el) { |
|
| 240 | - return new UnspecifiedType($el); |
|
| 241 | - }, $this->_elements); |
|
| 242 | - } |
|
| 243 | - return $this->_unspecifiedTypes; |
|
| 244 | - } |
|
| 230 | + /** |
|
| 231 | + * Get elements in the structure. |
|
| 232 | + * |
|
| 233 | + * @return UnspecifiedType[] |
|
| 234 | + */ |
|
| 235 | + public function elements(): array |
|
| 236 | + { |
|
| 237 | + if (!isset($this->_unspecifiedTypes)) { |
|
| 238 | + $this->_unspecifiedTypes = array_map( |
|
| 239 | + function (Element $el) { |
|
| 240 | + return new UnspecifiedType($el); |
|
| 241 | + }, $this->_elements); |
|
| 242 | + } |
|
| 243 | + return $this->_unspecifiedTypes; |
|
| 244 | + } |
|
| 245 | 245 | |
| 246 | - /** |
|
| 247 | - * Check whether the structure has an element at the given index, optionally |
|
| 248 | - * satisfying given tag expectation. |
|
| 249 | - * |
|
| 250 | - * @param int $idx Index 0..n |
|
| 251 | - * @param int|null $expectedTag Optional type tag expectation |
|
| 252 | - * @return bool |
|
| 253 | - */ |
|
| 254 | - public function has(int $idx, $expectedTag = null): bool |
|
| 255 | - { |
|
| 256 | - if (!isset($this->_elements[$idx])) { |
|
| 257 | - return false; |
|
| 258 | - } |
|
| 259 | - if (isset($expectedTag)) { |
|
| 260 | - if (!$this->_elements[$idx]->isType($expectedTag)) { |
|
| 261 | - return false; |
|
| 262 | - } |
|
| 263 | - } |
|
| 264 | - return true; |
|
| 265 | - } |
|
| 246 | + /** |
|
| 247 | + * Check whether the structure has an element at the given index, optionally |
|
| 248 | + * satisfying given tag expectation. |
|
| 249 | + * |
|
| 250 | + * @param int $idx Index 0..n |
|
| 251 | + * @param int|null $expectedTag Optional type tag expectation |
|
| 252 | + * @return bool |
|
| 253 | + */ |
|
| 254 | + public function has(int $idx, $expectedTag = null): bool |
|
| 255 | + { |
|
| 256 | + if (!isset($this->_elements[$idx])) { |
|
| 257 | + return false; |
|
| 258 | + } |
|
| 259 | + if (isset($expectedTag)) { |
|
| 260 | + if (!$this->_elements[$idx]->isType($expectedTag)) { |
|
| 261 | + return false; |
|
| 262 | + } |
|
| 263 | + } |
|
| 264 | + return true; |
|
| 265 | + } |
|
| 266 | 266 | |
| 267 | - /** |
|
| 268 | - * Get the element at the given index, optionally checking that the element |
|
| 269 | - * has a given tag. |
|
| 270 | - * |
|
| 271 | - * NOTE! Expectation checking is deprecated and should be done |
|
| 272 | - * with UnspecifiedType. |
|
| 273 | - * |
|
| 274 | - * @param int $idx Index 0..n |
|
| 275 | - * @param int|null $expectedTag Optional type tag expectation |
|
| 276 | - * @throws \OutOfBoundsException If element doesn't exists |
|
| 277 | - * @throws \UnexpectedValueException If expectation fails |
|
| 278 | - * @return UnspecifiedType |
|
| 279 | - */ |
|
| 280 | - public function at(int $idx, $expectedTag = null): UnspecifiedType |
|
| 281 | - { |
|
| 282 | - if (!isset($this->_elements[$idx])) { |
|
| 283 | - throw new \OutOfBoundsException( |
|
| 284 | - "Structure doesn't have an element at index $idx."); |
|
| 285 | - } |
|
| 286 | - $element = $this->_elements[$idx]; |
|
| 287 | - if (isset($expectedTag)) { |
|
| 288 | - $element->expectType($expectedTag); |
|
| 289 | - } |
|
| 290 | - return new UnspecifiedType($element); |
|
| 291 | - } |
|
| 267 | + /** |
|
| 268 | + * Get the element at the given index, optionally checking that the element |
|
| 269 | + * has a given tag. |
|
| 270 | + * |
|
| 271 | + * NOTE! Expectation checking is deprecated and should be done |
|
| 272 | + * with UnspecifiedType. |
|
| 273 | + * |
|
| 274 | + * @param int $idx Index 0..n |
|
| 275 | + * @param int|null $expectedTag Optional type tag expectation |
|
| 276 | + * @throws \OutOfBoundsException If element doesn't exists |
|
| 277 | + * @throws \UnexpectedValueException If expectation fails |
|
| 278 | + * @return UnspecifiedType |
|
| 279 | + */ |
|
| 280 | + public function at(int $idx, $expectedTag = null): UnspecifiedType |
|
| 281 | + { |
|
| 282 | + if (!isset($this->_elements[$idx])) { |
|
| 283 | + throw new \OutOfBoundsException( |
|
| 284 | + "Structure doesn't have an element at index $idx."); |
|
| 285 | + } |
|
| 286 | + $element = $this->_elements[$idx]; |
|
| 287 | + if (isset($expectedTag)) { |
|
| 288 | + $element->expectType($expectedTag); |
|
| 289 | + } |
|
| 290 | + return new UnspecifiedType($element); |
|
| 291 | + } |
|
| 292 | 292 | |
| 293 | - /** |
|
| 294 | - * Check whether the structure contains a context specific element with a |
|
| 295 | - * given tag. |
|
| 296 | - * |
|
| 297 | - * @param int $tag Tag number |
|
| 298 | - * @return boolean |
|
| 299 | - */ |
|
| 300 | - public function hasTagged($tag): bool |
|
| 301 | - { |
|
| 302 | - // lazily build lookup map |
|
| 303 | - if (!isset($this->_taggedMap)) { |
|
| 304 | - $this->_taggedMap = []; |
|
| 305 | - foreach ($this->_elements as $element) { |
|
| 306 | - if ($element->isTagged()) { |
|
| 307 | - $this->_taggedMap[$element->tag()] = $element; |
|
| 308 | - } |
|
| 309 | - } |
|
| 310 | - } |
|
| 311 | - return isset($this->_taggedMap[$tag]); |
|
| 312 | - } |
|
| 293 | + /** |
|
| 294 | + * Check whether the structure contains a context specific element with a |
|
| 295 | + * given tag. |
|
| 296 | + * |
|
| 297 | + * @param int $tag Tag number |
|
| 298 | + * @return boolean |
|
| 299 | + */ |
|
| 300 | + public function hasTagged($tag): bool |
|
| 301 | + { |
|
| 302 | + // lazily build lookup map |
|
| 303 | + if (!isset($this->_taggedMap)) { |
|
| 304 | + $this->_taggedMap = []; |
|
| 305 | + foreach ($this->_elements as $element) { |
|
| 306 | + if ($element->isTagged()) { |
|
| 307 | + $this->_taggedMap[$element->tag()] = $element; |
|
| 308 | + } |
|
| 309 | + } |
|
| 310 | + } |
|
| 311 | + return isset($this->_taggedMap[$tag]); |
|
| 312 | + } |
|
| 313 | 313 | |
| 314 | - /** |
|
| 315 | - * Get a context specific element tagged with a given tag. |
|
| 316 | - * |
|
| 317 | - * @param int $tag |
|
| 318 | - * @throws \LogicException If tag doesn't exists |
|
| 319 | - * @return TaggedType |
|
| 320 | - */ |
|
| 321 | - public function getTagged($tag): TaggedType |
|
| 322 | - { |
|
| 323 | - if (!$this->hasTagged($tag)) { |
|
| 324 | - throw new \LogicException("No tagged element for tag $tag."); |
|
| 325 | - } |
|
| 326 | - return $this->_taggedMap[$tag]; |
|
| 327 | - } |
|
| 314 | + /** |
|
| 315 | + * Get a context specific element tagged with a given tag. |
|
| 316 | + * |
|
| 317 | + * @param int $tag |
|
| 318 | + * @throws \LogicException If tag doesn't exists |
|
| 319 | + * @return TaggedType |
|
| 320 | + */ |
|
| 321 | + public function getTagged($tag): TaggedType |
|
| 322 | + { |
|
| 323 | + if (!$this->hasTagged($tag)) { |
|
| 324 | + throw new \LogicException("No tagged element for tag $tag."); |
|
| 325 | + } |
|
| 326 | + return $this->_taggedMap[$tag]; |
|
| 327 | + } |
|
| 328 | 328 | |
| 329 | - /** |
|
| 330 | - * |
|
| 331 | - * @see \Countable::count() |
|
| 332 | - * @return int |
|
| 333 | - */ |
|
| 334 | - public function count(): int |
|
| 335 | - { |
|
| 336 | - return count($this->_elements); |
|
| 337 | - } |
|
| 329 | + /** |
|
| 330 | + * |
|
| 331 | + * @see \Countable::count() |
|
| 332 | + * @return int |
|
| 333 | + */ |
|
| 334 | + public function count(): int |
|
| 335 | + { |
|
| 336 | + return count($this->_elements); |
|
| 337 | + } |
|
| 338 | 338 | |
| 339 | - /** |
|
| 340 | - * Get an iterator for the UnspecifiedElement objects. |
|
| 341 | - * |
|
| 342 | - * @see \IteratorAggregate::getIterator() |
|
| 343 | - * @return \ArrayIterator |
|
| 344 | - */ |
|
| 345 | - public function getIterator(): \ArrayIterator |
|
| 346 | - { |
|
| 347 | - return new \ArrayIterator($this->elements()); |
|
| 348 | - } |
|
| 339 | + /** |
|
| 340 | + * Get an iterator for the UnspecifiedElement objects. |
|
| 341 | + * |
|
| 342 | + * @see \IteratorAggregate::getIterator() |
|
| 343 | + * @return \ArrayIterator |
|
| 344 | + */ |
|
| 345 | + public function getIterator(): \ArrayIterator |
|
| 346 | + { |
|
| 347 | + return new \ArrayIterator($this->elements()); |
|
| 348 | + } |
|
| 349 | 349 | } |