mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 11:28:25 +00:00
Refactored Set::reverse and tweaked the expectation schema for consistency, fixes #4643
git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6888 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
parent
68234dd529
commit
ab0a607189
2 changed files with 94 additions and 59 deletions
|
@ -988,48 +988,48 @@ class Set extends Object {
|
|||
function reverse($object) {
|
||||
$out = array();
|
||||
if (is_a($object, 'XmlNode')) {
|
||||
if (isset($object->name) && isset($object->children)) {
|
||||
if ($object->name === '#document' && !empty($object->children)) {
|
||||
$out = Set::reverse($object->children[0]);
|
||||
} else {
|
||||
$children = array();
|
||||
if (!empty($object->children)) {
|
||||
$out = $object->attributes;
|
||||
$multi = null;
|
||||
foreach ($object->children as $child) {
|
||||
$childName = Inflector::camelize($child->name);
|
||||
if (count($child->children) > 1 && isset($child->name)) {
|
||||
$children[$childName][] = Set::reverse($child);
|
||||
} elseif ($child->name == '#text') {
|
||||
$object->value = $child->value;
|
||||
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])) {
|
||||
if (!isset($multi)) {
|
||||
$multi = array($out[$child->name]);
|
||||
}
|
||||
$multi[] = $value;
|
||||
} else {
|
||||
$children = array_merge($children, Set::reverse($child));
|
||||
$out[$child->name] = $value;
|
||||
}
|
||||
continue;
|
||||
} else {
|
||||
$value = Set::reverse($child);
|
||||
}
|
||||
$key = Inflector::camelize($child->name);
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
$camelName = Inflector::camelize($object->name);
|
||||
unset($parent);
|
||||
|
||||
if (isset($child) && is_object($child)) {
|
||||
$parent =& $child->parent();
|
||||
}
|
||||
|
||||
if (!empty($object->attributes) && !empty($children)) {
|
||||
$out[$camelName] = array_merge($object->attributes, $children);
|
||||
} elseif (!empty($object->attributes) && !empty($object->value)) {
|
||||
$out[$object->name] = array_merge($object->attributes, array('value' => $object->value));
|
||||
} elseif (!empty($object->attributes)) {
|
||||
$out[$camelName] = $object->attributes;
|
||||
} elseif (
|
||||
(!empty($children) && (isset($children[$childName][0]) || isset($children[$child->name][0]))) ||
|
||||
(!empty($children) && count($parent->children) > 1 && count($child->children) == 0)
|
||||
) {
|
||||
$out = $children;
|
||||
} elseif (!empty($children)) {
|
||||
$out[$camelName] = $children;
|
||||
} elseif (!empty($object->value)) {
|
||||
$out[$object->name] = $object->value;
|
||||
}
|
||||
if (isset($multi)) {
|
||||
unset($out[$object->children[0]->name]);
|
||||
foreach ($multi as $item) {
|
||||
$out[] = array(Inflector::camelize($object->children[0]->name) => $item);
|
||||
}
|
||||
}
|
||||
return $out;
|
||||
} else if (is_object($object)) {
|
||||
$keys = get_object_vars($object);
|
||||
if (isset($keys['_name_'])) {
|
||||
|
|
|
@ -1417,7 +1417,7 @@ class SetTest extends UnitTestCase {
|
|||
$expected = array('Rss' => array(
|
||||
'version' => '2.0',
|
||||
'Channel' => array(
|
||||
array('title' => 'Cake PHP Google Group',
|
||||
'title' => 'Cake PHP Google Group',
|
||||
'link' => 'http://groups.google.com/group/cake-php',
|
||||
'description' => 'Search this group before posting anything. There are over 20,000 posts and it's very likely your question was answered before. Visit the IRC channel #cakephp at irc.freenode.net for live chat with users and developers of Cake. If you post, tell us the version of Cake, PHP, and database.',
|
||||
'language' => 'en',
|
||||
|
@ -1440,7 +1440,6 @@ class SetTest extends UnitTestCase {
|
|||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
));
|
||||
$this->assertEqual($result, $expected);
|
||||
$string ='<data><post title="Title of this post" description="cool" /></data>';
|
||||
|
@ -1452,7 +1451,43 @@ class SetTest extends UnitTestCase {
|
|||
|
||||
$xml = new Xml('<example><item><title>An example of a correctly reversed XMLNode</title><desc/></item></example>');
|
||||
$result = Set::reverse($xml);
|
||||
$expected = array('Item' => array(array('title' => 'An example of a correctly reversed XMLNode')));
|
||||
$expected = array('Example' =>
|
||||
array(
|
||||
'Item' => array(
|
||||
'title' => 'An example of a correctly reversed XMLNode',
|
||||
'Desc' => array(),
|
||||
)
|
||||
)
|
||||
);
|
||||
$this->assertIdentical($result, $expected);
|
||||
|
||||
$xml = new Xml('<example><item attr="123"><titles><title>title1</title><title>title2</title></titles></item></example>');
|
||||
$result = Set::reverse($xml);
|
||||
$expected =
|
||||
array('Example' => array(
|
||||
'Item' => array(
|
||||
'attr' => '123',
|
||||
'Titles' => array(
|
||||
array('Title' => 'title1'),
|
||||
array('Title' => 'title2'),
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
$this->assertIdentical($result, $expected);
|
||||
|
||||
$xml = new Xml('<example attr="ex_attr"><item attr="123"><titles>list</titles>textforitems</item></example>');
|
||||
$result = Set::reverse($xml);
|
||||
$expected =
|
||||
array('Example' => array(
|
||||
'attr' => 'ex_attr',
|
||||
'Item' => array(
|
||||
'attr' => '123',
|
||||
'titles' => 'list',
|
||||
'value' => 'textforitems'
|
||||
)
|
||||
)
|
||||
);
|
||||
$this->assertIdentical($result, $expected);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue