-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Inherit XMLNS during encoding like we did in v3
- Loading branch information
Showing
11 changed files
with
83 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Xml\Dom\Locator\Xmlns; | ||
|
||
use Dom\Element; | ||
|
||
function closest_unprefixed_xmlns(Element $node): ?string | ||
{ | ||
foreach (linked_namespaces($node) as $namespace) { | ||
if ($namespace->prefix === null) { | ||
return $namespace->namespaceURI; | ||
} | ||
} | ||
|
||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Xml\Dom\Predicate; | ||
|
||
use function Psl\Result\wrap; | ||
use function VeeWee\Xml\Assertion\assert_strict_prefixed_name; | ||
|
||
function is_prefixed_node_name(string $nodeName): bool | ||
{ | ||
return wrap(static fn () => assert_strict_prefixed_name($nodeName))->proceed( | ||
static fn() => true, | ||
static fn() => false, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/Xml/Encoding/Internal/Encoder/Builder/xmlns_inheriting_element.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Xml\Encoding\Internal\Encoder\Builder; | ||
|
||
use Closure; | ||
use Dom\Element; | ||
use Dom\XMLDocument; | ||
use VeeWee\Xml\Xmlns\Xmlns; | ||
use function VeeWee\Xml\Dom\Builder\element as elementBuilder; | ||
use function VeeWee\Xml\Dom\Builder\namespaced_element as namespacedElementBuilder; | ||
use function VeeWee\Xml\Dom\Locator\Xmlns\closest_unprefixed_xmlns; | ||
use function VeeWee\Xml\Dom\Predicate\is_element; | ||
use function VeeWee\Xml\Dom\Predicate\is_prefixed_node_name; | ||
|
||
/** | ||
* This function can create element nodes that inherit the local xmlns namespace of their parent if none is configured. | ||
* | ||
* @param list<Closure(Element): Element> $children | ||
* @return Closure(Element): Element | ||
*/ | ||
function xmlns_inheriting_element(string $name, array $children, ?string $localNamespace = null): Closure | ||
{ | ||
return function (XMLDocument|Element $parent) use ($localNamespace, $name, $children): Element { | ||
if ($localNamespace === null && is_element($parent) && !is_prefixed_node_name($name)) { | ||
$localNamespace = closest_unprefixed_xmlns($parent); | ||
} | ||
|
||
return $localNamespace !== null | ||
? namespacedElementBuilder($localNamespace, $name, ...$children)($parent) | ||
: elementBuilder($name, ...$children)($parent); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters