Skip to content

Commit

Permalink
Fixing Xml::toArray() Where camelcased data was collapsed and corrupt…
Browse files Browse the repository at this point in the history
…ed. Causing unpredictable data structures with empty tags. Minor changes to existing behavior. See modified test cases. Fixes #8
  • Loading branch information
markstory committed Aug 14, 2009
1 parent ab50bbe commit 5a971dd
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
7 changes: 3 additions & 4 deletions cake/libs/xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,6 @@ function toArray($camelize = true) {
if ($child->attributes) {
$value = array_merge(array('value' => $value), $child->attributes);
}

if (isset($out[$child->name]) || isset($multi[$key])) {
if (!isset($multi[$key])) {
$multi[$key] = array($out[$child->name]);
Expand All @@ -689,15 +688,16 @@ function toArray($camelize = true) {
continue;
} elseif (count($child->children) === 0 && $child->value == '') {
$value = $child->attributes;

if (isset($out[$child->name]) || isset($multi[$key])) {
if (!isset($multi[$key])) {
$multi[$key] = array($out[$child->name]);
unset($out[$child->name]);
}
$multi[$key][] = $value;
} else {
} elseif (!empty($value)) {
$out[$key] = $value;
} else {
$out[$child->name] = $value;
}
continue;
} else {
Expand Down Expand Up @@ -909,7 +909,6 @@ function parse() {
for ($i = 0; $i < $count; $i++) {
$data = $vals[$i];
$data += array('tag' => null, 'value' => null, 'attributes' => array());

switch ($data['type']) {
case "open" :
$xml =& $xml->createElement($data['tag'], $data['value'], $data['attributes']);
Expand Down
56 changes: 55 additions & 1 deletion cake/tests/cases/libs/xml.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,60 @@ function testElementCollapsing() {

$this->assertEqual($result, $expected);
}
/**
* test that empty values do not casefold collapse
*
* @see http://code.cakephp.org/tickets/view/8
* @return void
**/
function testCaseFoldingWithEmptyValues() {
$filledValue = '<method name="set_user_settings">
<title>update user information</title>
<user>1</user>
<User>
<id>1</id>
<name>varchar(45)</name>
</User>
</method>';
$xml =& new XML($filledValue);
$expected = array(
'Method' => array(
'name' => 'set_user_settings',
'title' => 'update user information',
'user' => '1',
'User' => array(
'id' => 1,
'name' => 'varchar(45)',
),
)
);
$result = $xml->toArray();
$this->assertEqual($result, $expected);

$emptyValue ='<method name="set_user_settings">
<title>update user information</title>
<user></user>
<User>
<id>1</id>
<name>varchar(45)</name>
</User>
</method>';

$xml =& new XML($emptyValue);
$expected = array(
'Method' => array(
'name' => 'set_user_settings',
'title' => 'update user information',
'user' => array(),
'User' => array(
'id' => 1,
'name' => 'varchar(45)',
),
)
);
$result = $xml->toArray();
$this->assertEqual($result, $expected);
}
/**
* testMixedParsing method
*
Expand Down Expand Up @@ -921,7 +975,7 @@ function testToArray() {
'Example' => array(
'Item' => array(
'title' => 'An example of a correctly reversed XMLNode',
'Desc' => array(),
'desc' => array(),
)
)
);
Expand Down

0 comments on commit 5a971dd

Please sign in to comment.