Fixes #53, ordering of XML::toArray() operations.

This commit is contained in:
predominant 2010-03-26 19:59:09 +11:00
parent 5d3f0d7fe0
commit 0dfc07ba0d
2 changed files with 39 additions and 1 deletions

View file

@ -691,6 +691,7 @@ class XmlNode extends Object {
foreach ($this->children as $child) { foreach ($this->children as $child) {
$key = $camelize ? Inflector::camelize($child->name) : $child->name; $key = $camelize ? Inflector::camelize($child->name) : $child->name;
//debug($key);
if (is_a($child, 'XmlTextNode')) { if (is_a($child, 'XmlTextNode')) {
$out['value'] = $child->value; $out['value'] = $child->value;
@ -715,7 +716,7 @@ class XmlNode extends Object {
if (isset($out[$key]) || isset($multi[$key])) { if (isset($out[$key]) || isset($multi[$key])) {
if (!isset($multi[$key])) { if (!isset($multi[$key])) {
$multi[$key] = array($out[$key]); $multi[$key] = array($out[$key]);
unset($out[$key]); //unset($out[$key]);
} }
$multi[$key][] = $value; $multi[$key][] = $value;
} elseif (!empty($value)) { } elseif (!empty($value)) {

View file

@ -1266,6 +1266,43 @@ class XmlTest extends CakeTestCase {
) )
); );
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$text = '<main><first label="first type node 1" /><first label="first type node 2" /><second label="second type node" /></main>';
$xml = new Xml($text);
$result = $xml->toArray();
$expected = array(
'Main' => array(
'First' => array(
array('label' => 'first type node 1'),
array('label' => 'first type node 2')
),
'Second' => array('label'=>'second type node')
)
);
$this->assertIdentical($result,$expected);
$text = '<main><first label="first type node 1" /><first label="first type node 2" /><second label="second type node" /><collection><fifth label="fifth type node"/><third label="third type node 1"/><third label="third type node 2"/><third label="third type node 3"/><fourth label="fourth type node"/></collection></main>';
$xml = new Xml($text);
$result = $xml->toArray();
$expected = array(
'Main' => array(
'First' => array(
array('label' => 'first type node 1'),
array('label' => 'first type node 2')
),
'Second' => array('label'=>'second type node'),
'Collection' => array(
'Fifth' => array('label' => 'fifth type node'),
'Third' => array(
array('label' => 'third type node 1'),
array('label' => 'third type node 2'),
array('label' => 'third type node 3'),
),
'Fourth' => array('label' => 'fourth type node'),
)
)
);
$this->assertIdentical($result,$expected);
} }
/** /**