Skip to content

feat: add toArray() and toObject() with namespace support#70

Open
guillaumedelre wants to merge 1 commit into
downforcetech:mainfrom
guillaumedelre:feature/issue-28
Open

feat: add toArray() and toObject() with namespace support#70
guillaumedelre wants to merge 1 commit into
downforcetech:mainfrom
guillaumedelre:feature/issue-28

Conversation

@guillaumedelre
Copy link
Copy Markdown

FluidXml::toArray() returns ['rootTag' => value] for the document. FluidContext::toArray() returns an array of ['tag' => value] entries for each selected node.

Conversion rules:

  • Text-only element -> plain string value
  • Empty element -> null
  • Attributes -> '@name' keys
  • Text with siblings -> '@' key
  • CDATA content -> '@:cdata' key
  • Comment -> '@:comment' key
  • Repeated child tags -> indexed sub-arrays
  • Namespace decls -> '@xmlns' / '@xmlns:prefix' keys (only those
    introduced on the element, not inherited)

The output mirrors the array syntax accepted by addChild(), enabling round-trip import/export.

FluidXml::toObject() and FluidContext::toObject() return the same structure as stdClass instead of arrays, which integrates more naturally with JSON workflows and property-access code. Associative arrays become stdClass objects; indexed arrays (repeated siblings) remain PHP arrays.

Namespace declarations are detected via the XPath namespace::* axis, comparing in-scope namespaces with the parent element to emit only the declarations local to each node.

Closes #28

FluidXml::toArray() returns ['rootTag' => value] for the document.
FluidContext::toArray() returns an array of ['tag' => value] entries
for each selected node.

Conversion rules:
  - Text-only element   -> plain string value
  - Empty element       -> null
  - Attributes          -> '@name' keys
  - Text with siblings  -> '@' key
  - CDATA content       -> '@:cdata' key
  - Comment             -> '@:comment' key
  - Repeated child tags -> indexed sub-arrays
  - Namespace decls     -> '@xmlns' / '@xmlns:prefix' keys (only those
                           introduced on the element, not inherited)

The output mirrors the array syntax accepted by addChild(), enabling
round-trip import/export.

FluidXml::toObject() and FluidContext::toObject() return the same
structure as stdClass instead of arrays, which integrates more
naturally with JSON workflows and property-access code. Associative
arrays become stdClass objects; indexed arrays (repeated siblings)
remain PHP arrays.

Namespace declarations are detected via the XPath namespace::* axis,
comparing in-scope namespaces with the parent element to emit only
the declarations local to each node.

Closes downforcetech#28

Signed-off-by: Guillaume Delré <delre.guillaume@gmail.com>
@guillaumedelre
Copy link
Copy Markdown
Author

Hey @daniele-orlando, this PR fixes #28 — happy to adjust if anything looks off.

@guillaumedelre
Copy link
Copy Markdown
Author

Hey @daniele-orlando, just checking in on this. Happy to adjust the approach if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Export XML to array() or stdClass objects

1 participant