mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 17:16:18 +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 $d;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Return array representation of current object.
|
* 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
|
* @return array Array representation
|
||||||
* @access public
|
* @access public
|
||||||
*/
|
*/
|
||||||
function toArray($object = null) {
|
function toArray($camelize = true) {
|
||||||
if ($object === null) {
|
$out = $this->attributes;
|
||||||
$object =& $this;
|
$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;
|
if (isset($multi)) {
|
||||||
$multi = null;
|
$out = array_merge($out, $multi);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return $out;
|
return $out;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1009,6 +1009,27 @@ class XmlTest extends CakeTestCase {
|
||||||
$this->assertEqual($node->toString(), '<xml xmlns:cake="http://cakephp.org" />');
|
$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() {
|
function testNumericDataHandling() {
|
||||||
$data = '<xml><data>012345</data></xml>';
|
$data = '<xml><data>012345</data></xml>';
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue