<?php
use Thapp\XmlBuilder\XmlBuilder;
use Thapp\XmlBuilder\Normalizer;
$data = array(
'foo' => 'bar',
'node' => array(
'@attributes' => array(
'date' => '2013-06-06'
), 'some string'
);
);
$xmlBuilder = new XmlBuilder('data');
$xmlBuilder->load($data);
// createXML accepts a boolean value weather to return a string or a DOMDocument
// Set it to `false` if you want to retreive a DOMDocument instead.
echo $xmlBuilder->createXML(true);
prints:
<data>
<foo>bar</foo>
<node date="2013-06-06">some string</node>
</data>
XmlBuilder by defaul will create attributes on a dom node by itself as long as your key name statrts with '@',
however @attributes
expects an array of key value pairs wereas a key like @key
would accept only scalar values (string, int, float, or boolean).
$data = array('foo' => 'bar', 'bar' => 'baz');
$xmlBuilder = new XmlBuilder('response');
$XmlBuilder->load($data);
$XmlBuilder->setAttributeMapp(array('response' => array('foo')));
echo $XmlBuilder->createXML();
Prints:
<response foo="bar">
<bar>baz</bar>
</response>
<?php
class DataObject
{
protected $foo = 'bar';
public $bar = 'baz';
public function getFoo()
{
return $this->foo;
}
}
XmlBuilder's Normalizer Object is aware of the getter methods of an object
$object = new DataObject('data');
$xmlBuilder->load($object);
echo $xmlBuilder->createXML(true);
prints:
<data>
<foo>bar</foo>
<bar>baz</bar>
</data>
$xmlBuilder->setSingularizer(function ($name) {
if ('entries' === $name) {
return 'entry';
}
return $name;
});
$entries = array(
'entries' => array(
'foo',
'bar',
'baz',
)
);
$xmlBuilder->load($entries);
echo $xmlBuilder->createXML();
prints:
<data>
<entries>
<entry>foo</entry>
<entry>bar</entry>
<entry>baz</entry>
</entries>
</data>