-
-
Notifications
You must be signed in to change notification settings - Fork 53
Add a new ref()->domElementNamespace() function
#2516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
stloyd
wants to merge
3
commits into
flow-php:1.x
Choose a base branch
from
stloyd:feature/task-2514
base: 1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+166
−1
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
src/core/etl/src/Flow/ETL/Function/DOMElementNamespaceValue.php
This file contains hidden or 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,90 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Flow\ETL\Function; | ||
|
|
||
| use Dom\HTMLElement; | ||
| use Dom\XPath; | ||
| use DOMDocument; | ||
| use DOMNode; | ||
| use DOMXPath; | ||
| use Flow\ETL\Exception\InvalidArgumentException; | ||
| use Flow\ETL\FlowContext; | ||
| use Flow\ETL\Row; | ||
|
|
||
| use function class_exists; | ||
| use function count; | ||
| use function Flow\Types\DSL\type_instance_of; | ||
| use function Flow\Types\DSL\type_list; | ||
| use function is_array; | ||
| use function reset; | ||
|
|
||
| final class DOMElementNamespaceValue extends ScalarFunctionChain | ||
| { | ||
| public function __construct( | ||
| private readonly ScalarFunction|DOMNode|HTMLElement $domElement, | ||
| private readonly ScalarFunction|string|null $attribute, | ||
| ) {} | ||
|
|
||
| public function eval(Row $row, FlowContext $context): ?string | ||
| { | ||
| $types = [ | ||
| type_instance_of(DOMNode::class), | ||
| type_list(type_instance_of(DOMNode::class)), | ||
| ]; | ||
|
|
||
| if (class_exists('\Dom\HTMLElement')) { | ||
| $types[] = type_instance_of(HTMLElement::class); | ||
| $types[] = type_list(type_instance_of(HTMLElement::class)); | ||
| } | ||
|
|
||
| $node = (new Parameter($this->domElement))->as($row, $context, ...$types); | ||
|
|
||
| if ($node instanceof DOMDocument) { | ||
| $node = $node->documentElement; | ||
| } | ||
|
|
||
| if (is_array($node) && count($node)) { | ||
| $node = reset($node); | ||
| } | ||
|
|
||
| if ($node === null) { | ||
| return $context | ||
| ->functions() | ||
| ->invalidResult(new InvalidArgumentException('DOMElementNamespaceValue requires non-null DOMNode')); | ||
| } | ||
|
|
||
| if (!$node instanceof DOMNode && !$node instanceof HTMLElement) { | ||
| return null; | ||
| } | ||
|
|
||
| $attributeName = (new Parameter($this->attribute))->asString($row, $context); | ||
|
|
||
| if ($attributeName === null) { | ||
| return $node->namespaceURI; | ||
| } | ||
|
|
||
| if (null === $node->ownerDocument) { | ||
| return null; | ||
| } | ||
|
|
||
| if (class_exists('\Dom\XPath')) { | ||
| // @mago-ignore analysis:possibly-invalid-argument | ||
| $xpath = new XPath($node->ownerDocument); | ||
| } else { | ||
| // @mago-ignore analysis:possibly-invalid-argument | ||
| $xpath = new DOMXPath($node->ownerDocument); | ||
| } | ||
|
|
||
| /** @var \DOMNameSpaceNode $nsNode */ | ||
| // @mago-ignore analysis:invalid-iterator | ||
| foreach ($xpath->query('namespace::*') ?: [] as $nsNode) { | ||
| if ($nsNode->nodeName === $attributeName || str_starts_with($nsNode->nodeName, $attributeName . ':')) { | ||
| return $nsNode->nodeValue; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
This file contains hidden or 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
70 changes: 70 additions & 0 deletions
70
src/core/etl/tests/Flow/ETL/Tests/Unit/Function/DOMElementNamespaceTest.php
This file contains hidden or 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,70 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Flow\ETL\Tests\Unit\Function; | ||
|
|
||
| use Dom\HTMLDocument; | ||
| use Dom\HTMLElement; | ||
| use DOMDocument; | ||
| use DOMElement; | ||
| use PHPUnit\Framework\Attributes\RequiresPhp; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| use function Flow\ETL\DSL\config; | ||
| use function Flow\ETL\DSL\flow_context; | ||
| use function Flow\ETL\DSL\ref; | ||
| use function Flow\ETL\DSL\row; | ||
|
|
||
| use const LIBXML_HTML_NOIMPLIED; | ||
| use const LIBXML_NOERROR; | ||
|
|
||
| final class DOMElementNamespaceTest extends TestCase | ||
| { | ||
| private const string XML = <<<'XML' | ||
| <?xml version='1.0' encoding='UTF-8'?> | ||
| <Invoice xmlns:cec="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" | ||
| xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" | ||
| xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" | ||
| xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" | ||
| xmlns:sh="http://www.unece.org/cefact/nodes/StandardBusinessDocumentHeader"> | ||
| </Invoice> | ||
| XML; | ||
|
|
||
| #[RequiresPhp('>= 8.4')] | ||
| public function test_html_getting_element_namespace(): void | ||
| { | ||
| // @mago-ignore analysis:unavailable-method | ||
| $element = HTMLDocument::createFromString(self::XML, LIBXML_HTML_NOIMPLIED | LIBXML_NOERROR); | ||
|
|
||
| static::assertInstanceOf(HTMLElement::class, $element->documentElement); | ||
| static::assertSame('urn:oasis:names:specification:ubl:schema:xsd:Invoice-2', ref('node') | ||
| ->domElementNamespace() | ||
| ->eval( | ||
| row(flow_context(config())->entryFactory()->create('node', $element->documentElement)), | ||
| flow_context(), | ||
| )); | ||
| } | ||
|
|
||
| public function test_xml_getting_element_namespace(): void | ||
| { | ||
| $xml = new DOMDocument(); | ||
| $xml->loadXML(self::XML); | ||
|
|
||
| static::assertInstanceOf(DOMElement::class, $xml->documentElement); | ||
| static::assertSame('urn:oasis:names:specification:ubl:schema:xsd:Invoice-2', ref('node') | ||
| ->domElementNamespace() | ||
| ->eval(row(flow_context(config())->entryFactory()->create('node', $xml->documentElement)), flow_context())); | ||
| } | ||
|
|
||
| public function test_xml_getting_element_non_default_namespace(): void | ||
| { | ||
| $xml = new DOMDocument(); | ||
| $xml->loadXML(self::XML); | ||
|
|
||
| static::assertInstanceOf(DOMElement::class, $xml->documentElement); | ||
| static::assertSame('http://www.unece.org/cefact/nodes/StandardBusinessDocumentHeader', ref('node') | ||
| ->domElementNamespace('xmlns:sh') | ||
| ->eval(row(flow_context(config())->entryFactory()->create('node', $xml->documentElement)), flow_context())); | ||
| } | ||
| } |
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
New version always returns a traversable object, old returned array or
false, and Mago has a problem detecting which is correct.