Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions source/FluidXml/FluidXml.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace FluidXml;

// PEAR
use XML_Unserializer;

/**
* @method array array()
* @method FluidXml namespace(...$arguments)
Expand Down Expand Up @@ -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()
{
Expand Down
100 changes: 100 additions & 0 deletions specs/FluidXml.php
Original file line number Diff line number Diff line change
Expand Up @@ -2318,6 +2318,106 @@ function addchild($parent, $i)
});
});

describe('.exportTo()', function () {
it('should return an array', function () {

$xmlStr = "<animal operation=\"create\">\n"
. " <type>Herbivore</type>\n"
. " <attribute xmlns:foo=\"http://namespace.foo\">\n"
. " <foo:legs>4</foo:legs>\n"
. " <foo:head>1</foo:head>\n"
. " </attribute>\n"
. "</animal>";

$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 = "<animal operation=\"create\">\n"
. " <type>Herbivore</type>\n"
. " <attribute xmlns:foo=\"http://namespace.foo\">\n"
. " <foo:legs>4</foo:legs>\n"
. " <foo:head>1</foo:head>\n"
. " </attribute>\n"
. "</animal>";

$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 = "<animal operation=\"create\">\n"
. " <type>Herbivore</type>\n"
. " <attribute xmlns:foo=\"http://namespace.foo\">\n"
. " <foo:legs>4</foo:legs>\n"
. " <foo:head>1</foo:head>\n"
. " </attribute>\n"
. "</animal>";

$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 = "<animal operation=\"create\">\n"
. " <type>Herbivore</type>\n"
. " <attribute xmlns:foo=\"http://namespace.foo\">\n"
. " <foo:legs>4</foo:legs>\n"
. " <foo:head>1</foo:head>\n"
. " </attribute>\n"
. "</animal>";

$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";
Expand Down
3 changes: 2 additions & 1 deletion support/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
}
},
"require": {
"php": ">=5.6"
"php": ">=5.6",
"pear/xml_serializer": "^0.22.0"
},
"require-dev": {
"peridot-php/peridot": "1.*",
Expand Down