Skip to content

Instantly share code, notes, and snippets.

@ThomasWeinert
Last active August 29, 2015 14:05
Show Gist options
  • Save ThomasWeinert/be4e9d10649c2d31cfeb to your computer and use it in GitHub Desktop.
Save ThomasWeinert/be4e9d10649c2d31cfeb to your computer and use it in GitHub Desktop.
FluentDOM Syntax Foo
<?php
require_once(dirname(__FILE__).'/../../vendor/autoload.php');
$json = <<<JSON
{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": 10021
},
"phoneNumbers": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "fax",
"number": "646 555-4567"
}
]
}
JSON;
$read = FluentDOM($json, 'text/json')->xpath;
$write = FluentDOM::create();
$write->formatOutput = TRUE;
echo $write(
'numbers',
$write->each(
$read('//phoneNumbers/*/number'),
function (FluentDOM\Element $node) use ($write) {
return $write(
'phone',
['type' => $node('string(parent::*/type)')],
(string)$node
);
}
)
);
@ThomasWeinert
Copy link
Author

Output:

<?xml version="1.0" encoding="UTF-8"?>
<numbers>
  <phone type="home">212 555-1234</phone>
  <phone type="fax">646 555-4567</phone>
</numbers>

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