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:
the_undefined 2008-05-15 14:48:49 +00:00
parent 68234dd529
commit ab0a607189
2 changed files with 94 additions and 59 deletions

View file

@ -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)) {
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;
} else {
$children = array_merge($children, Set::reverse($child));
}
$out = $object->attributes;
$multi = null;
foreach ($object->children as $child) {
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 {
$out[$child->name] = $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;
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;
}
}
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_'])) {

View file

@ -1417,27 +1417,26 @@ class SetTest extends UnitTestCase {
$expected = array('Rss' => array(
'version' => '2.0',
'Channel' => array(
array('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',
'Item' => array(
array(
'title' => 'constructng result array when using findall',
'link' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f',
'description' => "i'm using cakephp to construct a logical data model array that will be <br> passed to a flex app. I have the following model association: <br> ServiceDay-&gt;(hasMany)ServiceTi me-&gt;(hasMany)ServiceTimePrice. So what <br> the current output from my findall is something like this example: <br><p>Array( <br> [0] =&gt; Array(",
'guid' => array('isPermaLink' => 'true', 'value' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f'),
'author' => 'bmil...@gmail.com(bpscrugs)',
'pubDate' => 'Fri, 28 Dec 2007 00:44:14 UT',
),
array(
'title' => 'Re: share views between actions?',
'link' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8',
'description' => 'Then perhaps you might do us all a favour and refrain from replying to <br> things you do not understand. That goes especially for asinine comments. <br> Indeed. <br> To sum up: <br> No comment. <br> In my day, a simple &quot;RTFM&quot; would suffice. I\'ll keep in mind to ignore any <br> further responses from you. <br> You (and I) were referring to the *online documentation*, not other',
'guid' => array('isPermaLink' => 'true', 'value' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8'),
'author' => 'subtropolis.z...@gmail.com(subtropolis zijn)',
'pubDate' => 'Fri, 28 Dec 2007 00:45:01 UT'
)
'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&#39;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',
'Item' => array(
array(
'title' => 'constructng result array when using findall',
'link' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f',
'description' => "i'm using cakephp to construct a logical data model array that will be <br> passed to a flex app. I have the following model association: <br> ServiceDay-&gt;(hasMany)ServiceTi me-&gt;(hasMany)ServiceTimePrice. So what <br> the current output from my findall is something like this example: <br><p>Array( <br> [0] =&gt; Array(",
'guid' => array('isPermaLink' => 'true', 'value' => 'http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f'),
'author' => 'bmil...@gmail.com(bpscrugs)',
'pubDate' => 'Fri, 28 Dec 2007 00:44:14 UT',
),
array(
'title' => 'Re: share views between actions?',
'link' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8',
'description' => 'Then perhaps you might do us all a favour and refrain from replying to <br> things you do not understand. That goes especially for asinine comments. <br> Indeed. <br> To sum up: <br> No comment. <br> In my day, a simple &quot;RTFM&quot; would suffice. I\'ll keep in mind to ignore any <br> further responses from you. <br> You (and I) were referring to the *online documentation*, not other',
'guid' => array('isPermaLink' => 'true', 'value' => 'http://groups.google.com/group/cake-php/msg/8b350d898707dad8'),
'author' => 'subtropolis.z...@gmail.com(subtropolis zijn)',
'pubDate' => 'Fri, 28 Dec 2007 00:45:01 UT'
)
)
)
@ -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);
}