Skip to content
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

Feature: element_encode() #31

Closed
lykciv opened this issue Apr 29, 2022 · 1 comment
Closed

Feature: element_encode() #31

lykciv opened this issue Apr 29, 2022 · 1 comment

Comments

@lykciv
Copy link
Contributor

lykciv commented Apr 29, 2022

Feature Request

Q A
New Feature yes
RFC no
BC Break no

Summary

I have come across a scenario where I needed to generated a part of an xml document instead of the full document. The available helper functions only allow for generating full documents. It might be useful to have a helper function like element_encode().

Example full doc

$doc = [
    'Data' => [
        'Hello' => 'World',
        'Extra' => [
            'Lorem' => 'Ipsum',
        ],
    ],
];

xml_encode($doc);

Output

<?xml version="1.0"?>
<Data>
  <Hello>World</Hello>
  <Extra>
    <Lorem>Ipsum</Lorem>
  </Extra>
</Data>

Example partial doc

$element = [
    'Lorem' => 'Ipsum',
];

element_encode($element);

Output

<Lorem>Ipsum</Lorem>
@veewee
Copy link
Owner

veewee commented Apr 29, 2022

It is possible by using document_encode($data) and format the document_element() through to_xml().

$document = document_encode($data, ...$configurators);
$result = xml_string()($document->locate(document_element()));

But this sure sounds like a lovely shortcut function to have in the toolbelt.
Feel free to provide a PR.

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

No branches or pull requests

2 participants