diff --git a/source/FluidXml/FluidXml.php b/source/FluidXml/FluidXml.php
index 5e05c07..32fddf8 100644
--- a/source/FluidXml/FluidXml.php
+++ b/source/FluidXml/FluidXml.php
@@ -2,6 +2,9 @@
namespace FluidXml;
+// PEAR
+use XML_Unserializer;
+
/**
* @method array array()
* @method FluidXml namespace(...$arguments)
@@ -173,6 +176,26 @@ public function html($strip = false)
return "{$header}{$html}";
}
+
+ function exportTo($complexType, $strip = false)
+ {
+ $xml = $this->xml($strip);
+ $unserializer = $this->xmlUnserializer($complexType);
+ $unserializer->unserialize($xml);
+
+ return $unserializer->getUnserializedData();
+ }
+
+ protected function xmlUnserializer($complexType)
+ {
+ $options = array(
+ 'parseAttributes' => true,
+ 'prependAttributes' => '@',
+ 'complexType' => $complexType
+ );
+
+ return new XML_Unserializer($options);
+ }
public function namespaces()
{
diff --git a/specs/FluidXml.php b/specs/FluidXml.php
index 6cac1ef..2abf6ea 100644
--- a/specs/FluidXml.php
+++ b/specs/FluidXml.php
@@ -2318,6 +2318,106 @@ function addchild($parent, $i)
});
});
+ describe('.exportTo()', function () {
+ it('should return an array', function () {
+
+ $xmlStr = "\n"
+ . " Herbivore\n"
+ . " \n"
+ . " 4\n"
+ . " 1\n"
+ . " \n"
+ . "";
+
+ $xml = new FluidXml();
+ $xml->addChild($xmlStr);
+ $arr = $xml->exportTo("array");
+
+ $actual = is_array($arr);
+ $expected = false;
+
+ \assert($actual === $expected, __($actual, $expected));
+ });
+
+ it('should return an array in the correct format', function () {
+
+ $xmlStr = "\n"
+ . " Herbivore\n"
+ . " \n"
+ . " 4\n"
+ . " 1\n"
+ . " \n"
+ . "";
+
+ $xml = new FluidXml();
+ $xml->addChild($xmlStr);
+ $actual = $xml->exportTo('array');
+
+ $array = $xml->array();
+
+ $expected = array (
+ 'animal' =>
+ array (
+ '@xmlns:foo' => 'http://namespace.foo',
+ '@operation' => 'create',
+ 'type' => 'Herbivore',
+ 'attribute' =>
+ array (
+ '@xmlns:foo' => 'http://namespace.foo',
+ 'foo:legs' => '4',
+ 'foo:head' => '1',
+ ),
+ ),
+ );
+
+ \assert($actual === $expected, __($actual, $expected));
+ });
+
+ it('should return an stdClass Object', function () {
+
+ $xmlStr = "\n"
+ . " Herbivore\n"
+ . " \n"
+ . " 4\n"
+ . " 1\n"
+ . " \n"
+ . "";
+
+ $xml = new FluidXml();
+ $xml->addChild($xmlStr);
+ $object = $xml->exportTo('object');
+
+ \assert_is_a($object, '\stdClass');
+ });
+
+ it('should return a stdClass object in the correct format', function () {
+
+ $xmlStr = "\n"
+ . " Herbivore\n"
+ . " \n"
+ . " 4\n"
+ . " 1\n"
+ . " \n"
+ . "";
+
+ $xml = new FluidXml();
+ $xml->addChild($xmlStr);
+ $actual = $xml->exportTo('object');
+
+ $expected = new stdClass();
+ $expected->animal = new stdClass();
+ $expected->animal->{'@xmlns:foo'} = 'http://namespace.foo';
+ $expected->animal->{'@operation'} = 'create';
+ $expected->animal->type = 'Herbivore';
+ $expected->animal->attribute = new stdClass();
+ $expected->animal->attribute->{'@xmlns:foo'} = 'http://namespace.foo';
+ $expected->animal->attribute->{'foo:legs'} = '4';
+ $expected->animal->attribute->{'foo:head'} = '2';
+
+ \assert($actual === $expected, __($actual, $expected));
+ });
+ });
+
describe('.save()', function () {
it('should be fluid', function () {
$file = "{$this->out_dir}.test_save0.xml";
diff --git a/support/composer.json b/support/composer.json
index b26628a..13e1ce4 100644
--- a/support/composer.json
+++ b/support/composer.json
@@ -23,7 +23,8 @@
}
},
"require": {
- "php": ">=5.6"
+ "php": ">=5.6",
+ "pear/xml_serializer": "^0.22.0"
},
"require-dev": {
"peridot-php/peridot": "1.*",