Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixes #53, ordering of XML::toArray() operations.
  • Loading branch information
predominant committed Mar 26, 2010
1 parent 9f5949a commit ec3f4b8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
3 changes: 2 additions & 1 deletion cake/libs/xml.php
Expand Up @@ -664,6 +664,7 @@ function toArray($camelize = true) {


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;
Expand All @@ -688,7 +689,7 @@ function toArray($camelize = true) {
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)) {
Expand Down
37 changes: 37 additions & 0 deletions cake/tests/cases/libs/xml.test.php
Expand Up @@ -1244,6 +1244,43 @@ function testToArray() {
) )
); );
$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);
} }
/** /**
* testAppend method * testAppend method
Expand Down

0 comments on commit ec3f4b8

Please sign in to comment.