mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
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:
parent
da39ce77b9
commit
92e04703b7
2 changed files with 64 additions and 43 deletions
|
@ -646,58 +646,58 @@ 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;
|
||||
function toArray($camelize = true) {
|
||||
$out = $this->attributes;
|
||||
$multi = null;
|
||||
|
||||
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]);
|
||||
unset($out[$child->name]);
|
||||
}
|
||||
$multi[$key][] = $value;
|
||||
} else {
|
||||
$out[$child->name] = $value;
|
||||
}
|
||||
continue;
|
||||
} else {
|
||||
$value = $child->toArray($camelize);
|
||||
}
|
||||
|
||||
if (!isset($out[$key])) {
|
||||
$out[$key] = $value;
|
||||
} else {
|
||||
if (!is_array($out[$key]) || !isset($out[$key][0])) {
|
||||
$out[$key] = array($out[$key]);
|
||||
}
|
||||
$out[$key][] = $value;
|
||||
}
|
||||
}
|
||||
if (is_a($object, 'XmlNode')) {
|
||||
$out = $object->attributes;
|
||||
$multi = null;
|
||||
foreach ($object->children as $child) {
|
||||
$key = Inflector::camelize($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]);
|
||||
unset($out[$child->name]);
|
||||
}
|
||||
$multi[$key][] = $value;
|
||||
} else {
|
||||
$out[$child->name] = $value;
|
||||
}
|
||||
continue;
|
||||
} else {
|
||||
$value = $this->toArray($child);
|
||||
}
|
||||
if (!isset($out[$key])) {
|
||||
$out[$key] = $value;
|
||||
} else {
|
||||
if (!is_array($out[$key]) || !isset($out[$key][0])) {
|
||||
$out[$key] = array($out[$key]);
|
||||
}
|
||||
$out[$key][] = $value;
|
||||
}
|
||||
}
|
||||
if (isset($multi)) {
|
||||
$out = array_merge($out, $multi);
|
||||
}
|
||||
|
||||
if (isset($multi)) {
|
||||
$out = array_merge($out, $multi);
|
||||
}
|
||||
return $out;
|
||||
}
|
||||
|
|
|
@ -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>';
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue