Adding ability to turn off Camel Casing of nodes in Xml::toArray();

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7894 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
phpnut 2008-11-26 18:21:21 +00:00
parent da39ce77b9
commit 92e04703b7
2 changed files with 64 additions and 43 deletions

View file

@ -646,33 +646,32 @@ class XmlNode extends Object {
}
}
}
return $d;
}
/**
* Return array representation of current object.
*
* @param object optional used mainly by the method itself to reverse children
* @param boolean $camelize true will camelize child nodes, false will not alter node names
* @return array Array representation
* @access public
*/
function toArray($object = null) {
if ($object === null) {
$object =& $this;
}
if (is_a($object, 'XmlNode')) {
$out = $object->attributes;
function toArray($camelize = true) {
$out = $this->attributes;
$multi = null;
foreach ($object->children as $child) {
$key = Inflector::camelize($child->name);
foreach ($this->children as $child) {
$key = $camelize ? Inflector::camelize($child->name) : $child->name;
if (is_a($child, 'XmlTextNode')) {
$out['value'] = $child->value;
continue;
} elseif (isset($child->children[0]) && is_a($child->children[0], 'XmlTextNode')) {
$value = $child->children[0]->value;
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]);
@ -684,8 +683,9 @@ class XmlNode extends Object {
}
continue;
} else {
$value = $this->toArray($child);
$value = $child->toArray($camelize);
}
if (!isset($out[$key])) {
$out[$key] = $value;
} else {
@ -695,10 +695,10 @@ class XmlNode extends Object {
$out[$key][] = $value;
}
}
if (isset($multi)) {
$out = array_merge($out, $multi);
}
}
return $out;
}
/**

View file

@ -1009,6 +1009,27 @@ class XmlTest extends CakeTestCase {
$this->assertEqual($node->toString(), '<xml xmlns:cake="http://cakephp.org" />');
}
function testCamelize() {
$xmlString = '<methodCall><methodName>examples.getStateName</methodName>' .
'<params><param><value><i4>41</i4></value></param></params></methodCall>';
$Xml = new Xml($xmlString);
$expected = array(
'methodCall' => array(
'methodName' => 'examples.getStateName',
'params' => array(
'param' => array('value' => array('i4' => 41)))));
$this->assertEqual($expected, $Xml->toArray(false));
$Xml = new Xml($xmlString);
$expected = array(
'MethodCall' => array(
'methodName' => 'examples.getStateName',
'Params' => array(
'Param' => array('Value' => array('i4' => 41)))));
$this->assertEqual($expected, $Xml->toArray());
}
function testNumericDataHandling() {
$data = '<xml><data>012345</data></xml>';