Last active
August 29, 2015 14:05
-
-
Save ThomasWeinert/be4e9d10649c2d31cfeb to your computer and use it in GitHub Desktop.
FluentDOM Syntax Foo
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
<?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 | |
); | |
} | |
) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: