|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace VeeWee\Xml\Encoding\Internal\Encoder\Builder; |
| 6 | + |
| 7 | +use Closure; |
| 8 | +use Dom\Element; |
| 9 | +use Dom\XMLDocument; |
| 10 | +use Webmozart\Assert\Assert; |
| 11 | +use function VeeWee\Xml\Dom\Builder\default_xmlns_attribute; |
| 12 | +use function VeeWee\Xml\Dom\Builder\element as elementBuilder; |
| 13 | +use function VeeWee\Xml\Dom\Builder\namespaced_element as namespacedElementBuilder; |
| 14 | +use function VeeWee\Xml\Dom\Predicate\is_element; |
| 15 | +use function VeeWee\Xml\Dom\Predicate\is_prefixed_node_name; |
| 16 | + |
| 17 | +/** |
| 18 | + * This function can create element nodes that inherit the local xmlns namespace of their parent if none is configured. |
| 19 | + * |
| 20 | + * @param list<Closure(Element): Element> $children |
| 21 | + * @param array<string, string> $namespaces |
| 22 | + * |
| 23 | + * @return Closure(Element): Element |
| 24 | + */ |
| 25 | +function xmlns_inheriting_element(string $name, array $children, ?array $namespaces = []): Closure |
| 26 | +{ |
| 27 | + return static function (XMLDocument|Element $parent) use ($namespaces, $name, $children): Element { |
| 28 | + |
| 29 | + $defaultNamespace = $namespaces[''] ?? null; |
| 30 | + |
| 31 | + // These rules apply for non prefixed elements only: |
| 32 | + // If no local namespace has been defined: lookup the default local namespace of the closest parent element. |
| 33 | + // Use that specific local namespace to create the element if one could be found. |
| 34 | + // Otherwise, just create a non-namespaced element. |
| 35 | + if (!is_prefixed_node_name($name)) { |
| 36 | + // Try to find the inherited default XMLNS for non prefixed elements without a desired local namespace. |
| 37 | + if ($defaultNamespace === null && is_element($parent)) { |
| 38 | + $defaultNamespace = $parent->lookupNamespaceURI(''); |
| 39 | + } |
| 40 | + |
| 41 | + return $defaultNamespace !== null |
| 42 | + ? namespacedElementBuilder($defaultNamespace, $name, ...$children)($parent) |
| 43 | + : elementBuilder($name, ...$children)($parent); |
| 44 | + } |
| 45 | + |
| 46 | + // Prefixed elements can be created as regular elements: |
| 47 | + // The configured xmlns attributes will be added by the $children. |
| 48 | + // If a local namespace is configured, make sure to register it on the node manually. |
| 49 | + [$prefix] = explode(':', $name); |
| 50 | + $prefixedNamespace = $namespaces[$prefix] ?? (is_element($parent) ? $parent->lookupNamespaceURI($prefix) : null); |
| 51 | + |
| 52 | + Assert::notNull($prefixedNamespace, 'No namespace URI could be found for prefix: '.$prefix); |
| 53 | + |
| 54 | + $defaultXmlns = $defaultNamespace !== null ? [default_xmlns_attribute($defaultNamespace)] : []; |
| 55 | + return namespacedElementBuilder( |
| 56 | + $prefixedNamespace, |
| 57 | + $name, |
| 58 | + ...$defaultXmlns, |
| 59 | + ...$children, |
| 60 | + )($parent); |
| 61 | + }; |
| 62 | +} |
0 commit comments