This class transmogrifies arrays into DOMDocument XML objects, and vice-versa.
Here's how you use it:
require_once 'Transmogrifier.php';
use Skyzyx\Components\Transmogrifier;
$data = array(
'book' => array(
array(
'author' => 'Author0',
'title' => 'Title0',
'publisher' => 'Publisher0',
'__attributes__' => array(
'isbn' => '978-3-16-148410-0'
)
),
array(
'author' => array('Author1', 'Author2'),
'title' => 'Title1',
'publisher' => 'Publisher1'
),
array(
'__attributes__' => array(
'isbn' => '978-3-16-148410-0'
),
'__content__' => 'Title2'
)
)
);
echo Transmogrifier::array2xml($data);
Which gives you the following:
<?xml version="1.0"?>
<root>
<book isbn="978-3-16-148410-0">
<author><![CDATA[Author0]]></author>
<title><![CDATA[Title0]]></title>
<publisher><![CDATA[Publisher0]]></publisher>
</book>
<book>
<author><![CDATA[Author1]]></author>
<author><![CDATA[Author2]]></author>
<title><![CDATA[Title1]]></title>
<publisher><![CDATA[Publisher1]]></publisher>
</book>
<book isbn="978-3-16-148410-0"><![CDATA[Title2]]></book>
</root>
Check out the tests to get a sense of how it works.
To install the source code:
git clone git://github.com/skyzyx/transmogrifier.git
And include it in your scripts:
require_once '/path/to/transmogrifier/Transmogrifier.php';
If you're using Composer to manage dependencies, you can add Transmogrifier with it.
{
"require": {
"skyzyx/transmogrifier": "*"
}
}
If you're using a class loader (e.g., Symfony Class Loader):
$loader->registerNamespace('Skyzyx\\Components\\Transmogrifier', 'path/to/vendor/transmogrifier');
Tests are written in PHPT format. You can run them with either the PEAR Test Runner or with PHPUnit 3.6+.
cd tests/
pear run-tests .
...or...
cd tests/
phpunit .
Copyright (c) 2010-2011 Omer Hassan. Copyright (c) 2012 Ryan Parman. Licensed for use under the terms of the MIT license.