| Total Complexity | 34 |
| Total Lines | 201 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 1 | Features | 0 |
| 1 | <?php |
||
| 29 | trait ExtendableElementTrait |
||
| 30 | { |
||
| 31 | /** @var \SimpleSAML\XML\SerializableElementInterface[] */ |
||
| 32 | protected array $elements = []; |
||
| 33 | |||
| 34 | |||
| 35 | /** |
||
| 36 | * Parse an XML document and get the child elements from the specified namespace(s). |
||
| 37 | * The namespace defaults to the XS_ANY_ELT_NAMESPACE constant on the element. |
||
| 38 | * NOTE: In case the namespace is ##any, this method will also return local non-namespaced elements! |
||
| 39 | * |
||
| 40 | * @param \DOMElement $xml |
||
| 41 | * @param \SimpleSAML\XML\XsNamespace|array|null $namespace |
||
| 42 | * |
||
| 43 | * @return list<\SimpleSAML\XML\SerializableElementInterface> $elements |
||
| 44 | */ |
||
| 45 | protected static function getChildElementsFromXML(DOMElement $xml, NS|array $namespace = null): array |
||
| 46 | { |
||
| 47 | $namespace = $namespace ?? static::XS_ANY_ELT_NAMESPACE; |
||
|
1 ignored issue
–
show
|
|||
| 48 | $exclusionList = static::getElementExclusions(); |
||
| 49 | $registry = ElementRegistry::getInstance(); |
||
| 50 | $elements = []; |
||
| 51 | |||
| 52 | // Validate namespace value |
||
| 53 | if (!is_array($namespace)) { |
||
| 54 | // Must be one of the predefined values |
||
| 55 | Assert::oneOf($namespace, NS::cases()); |
||
| 56 | |||
| 57 | foreach ($xml->childNodes as $elt) { |
||
| 58 | if (!($elt instanceof DOMElement)) { |
||
| 59 | continue; |
||
| 60 | } elseif (in_array([$elt->namespaceURI, $elt->localName], $exclusionList, true)) { |
||
| 61 | continue; |
||
| 62 | } elseif ($namespace === NS::OTHER && in_array($elt->namespaceURI, [static::NS, null], true)) { |
||
|
1 ignored issue
–
show
|
|||
| 63 | continue; |
||
| 64 | } elseif ($namespace === NS::TARGET && $elt->namespaceURI !== static::NS) { |
||
| 65 | continue; |
||
| 66 | } elseif ($namespace === NS::LOCAL && $elt->namespaceURI !== null) { |
||
| 67 | continue; |
||
| 68 | } |
||
| 69 | |||
| 70 | $handler = $registry->getElementHandler($elt->namespaceURI, $elt->localName); |
||
| 71 | $elements[] = ($handler === null) ? Chunk::fromXML($elt) : $handler::fromXML($elt); |
||
| 72 | } |
||
| 73 | } else { |
||
| 74 | // Array must be non-empty and cannot contain ##any or ##other |
||
| 75 | Assert::notEmpty($namespace); |
||
| 76 | Assert::allStringNotEmpty($namespace); |
||
| 77 | Assert::allNotSame($namespace, NS::ANY); |
||
| 78 | Assert::allNotSame($namespace, NS::OTHER); |
||
| 79 | |||
| 80 | // Replace the ##targetedNamespace with the actual namespace |
||
| 81 | if (($key = array_search(NS::TARGET, $namespace)) !== false) { |
||
| 82 | $namespace[$key] = static::NS; |
||
| 83 | } |
||
| 84 | |||
| 85 | // Replace the ##local with null |
||
| 86 | if (($key = array_search(NS::LOCAL, $namespace)) !== false) { |
||
| 87 | $namespace[$key] = null; |
||
| 88 | } |
||
| 89 | |||
| 90 | foreach ($xml->childNodes as $elt) { |
||
| 91 | if (!($elt instanceof DOMElement)) { |
||
| 92 | continue; |
||
| 93 | } elseif (in_array([$elt->namespaceURI, $elt->localName], $exclusionList, true)) { |
||
| 94 | continue; |
||
| 95 | } elseif (!in_array($elt->namespaceURI, $namespace, true)) { |
||
| 96 | continue; |
||
| 97 | } |
||
| 98 | |||
| 99 | $handler = $registry->getElementHandler($elt->namespaceURI, $elt->localName); |
||
| 100 | $elements[] = ($handler === null) ? Chunk::fromXML($elt) : $handler::fromXML($elt); |
||
| 101 | } |
||
| 102 | } |
||
| 103 | |||
| 104 | return $elements; |
||
|
1 ignored issue
–
show
|
|||
| 105 | } |
||
| 106 | |||
| 107 | |||
| 108 | /** |
||
| 109 | * Set an array with all elements present. |
||
| 110 | * |
||
| 111 | * @param \SimpleSAML\XML\SerializableElementInterface[] $elements |
||
| 112 | * @return void |
||
| 113 | */ |
||
| 114 | protected function setElements(array $elements): void |
||
| 188 | } |
||
| 189 | |||
| 190 | |||
| 191 | /** |
||
| 192 | * Get an array with all elements present. |
||
| 193 | * |
||
| 194 | * @return \SimpleSAML\XML\SerializableElementInterface[] |
||
| 195 | */ |
||
| 196 | public function getElements(): array |
||
| 197 | { |
||
| 198 | return $this->elements; |
||
| 199 | } |
||
| 200 | |||
| 201 | |||
| 202 | /** |
||
| 203 | * @return array|\SimpleSAML\XML\XsNamespace |
||
| 204 | */ |
||
| 205 | public function getElementNamespace(): array|NS |
||
| 215 | } |
||
| 216 | |||
| 217 | |||
| 218 | /** |
||
| 219 | * Get the exclusions list for getChildElementsFromXML. |
||
| 220 | * |
||
| 221 | * @return array<string, string> |
||
| 222 | */ |
||
| 223 | public static function getElementExclusions(): array |
||
| 230 | } |
||
| 231 | } |
||
| 232 |