updating set handling of xml and adding tests for model saving xml

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6274 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2007-12-29 20:24:10 +00:00
parent 9a8bd5680a
commit 7e845242d5
3 changed files with 112 additions and 8 deletions

View file

@ -760,17 +760,41 @@ class Set extends Object {
* @return array * @return array
*/ */
function reverse($object) { function reverse($object) {
if (is_a($object, 'xmlnode') || is_a($object, 'XMLNode')) {
if ($object->name != Inflector::underscore($this->name)) {
if (is_object($object->child(Inflector::underscore($this->name)))) {
$object = $object->child(Inflector::underscore($this->name));
$object = $object->attributes;
} else {
return null;
}
}
} else {
$out = array(); $out = array();
if (is_a($object, 'xmlnode') || is_a($object, 'XMLNode')) {
if (isset($object->name) && isset($object->children)) {
if ($object->name === 'root' && !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);
} else {
$children = array_merge($children, Set::reverse($child));
}
}
}
$camelName = Inflector::camelize($object->name);
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]))) {
$out = $children;
} elseif (!empty($children)) {
$out[$camelName] = $children;
} elseif (!empty($object->value)) {
$out[$object->name] = $object->value;
}
}
}
} else {
if (is_object($object)) { if (is_object($object)) {
$keys = get_object_vars($object); $keys = get_object_vars($object);
if (isset($keys['_name_'])) { if (isset($keys['_name_'])) {
@ -797,9 +821,9 @@ class Set extends Object {
} else { } else {
$out = $object; $out = $object;
} }
return $out;
} }
return $object; return $out;
} }
} }
?> ?>

View file

@ -1981,6 +1981,18 @@ class ModelTest extends CakeTestCase {
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
} }
function testSaveFromXml() {
if (!class_exists('Xml')) {
uses('xml');
}
$Article = new Article();
$result = $Article->save(new Xml('<article title="test xml"/>'));
$this->assertTrue($result);
$results = $Article->find(array('Article.title' => 'test xml'));
$this->assertTrue($results);
}
function testSaveHabtm() { function testSaveHabtm() {
$this->model =& new Article(); $this->model =& new Article();

View file

@ -820,5 +820,73 @@ class SetTest extends UnitTestCase {
$result = $set->pushDiff($array2); $result = $set->pushDiff($array2);
$this->assertIdentical($result, $array1+$array2); $this->assertIdentical($result, $array1+$array2);
} }
function testXmlSetReverse() {
if (!class_exists('Xml')) {
uses('Xml');
}
$string = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<rss version="2.0">
<channel>
<title>Cake PHP Google Group</title>
<link>http://groups.google.com/group/cake-php</link>
<description>Search this group before posting anything. There are over 20,000 posts and it&amp;#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.</description>
<language>en</language>
<item>
<title>constructng result array when using findall</title>
<link>http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f</link>
<description>i&#39;m using cakephp to construct a logical data model array that will be &lt;br&gt; passed to a flex app. I have the following model association: &lt;br&gt; ServiceDay-&amp;gt;(hasMany)ServiceTi me-&amp;gt;(hasMany)ServiceTimePrice. So what &lt;br&gt; the current output from my findall is something like this example: &lt;br&gt; &lt;p&gt;Array( &lt;br&gt; [0] =&amp;gt; Array(</description>
<guid isPermaLink="true">http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f</guid>
<author>bmil...@gmail.com(bpscrugs)</author>
<pubDate>Fri, 28 Dec 2007 00:44:14 UT</pubDate>
</item>
<item>
<title>Re: share views between actions?</title>
<link>http://groups.google.com/group/cake-php/msg/8b350d898707dad8</link>
<description>Then perhaps you might do us all a favour and refrain from replying to &lt;br&gt; things you do not understand. That goes especially for asinine comments. &lt;br&gt; Indeed. &lt;br&gt; To sum up: &lt;br&gt; No comment. &lt;br&gt; In my day, a simple &amp;quot;RTFM&amp;quot; would suffice. I&#39;ll keep in mind to ignore any &lt;br&gt; further responses from you. &lt;br&gt; You (and I) were referring to the *online documentation*, not other</description>
<guid isPermaLink="true">http://groups.google.com/group/cake-php/msg/8b350d898707dad8</guid>
<author>subtropolis.z...@gmail.com(subtropolis zijn)</author>
<pubDate>Fri, 28 Dec 2007 00:45:01 UT</pubDate>
</item>
</channel>
</rss>';
$xml = new Xml($string);
$result = Set::reverse($xml);
$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&#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'
)
)
)
)
)
);
$this->assertEqual($result, $expected);
$string ='<data><post title="Title of this post" description="cool" /></data>';
$xml = new Xml($string);
$result = Set::reverse($xml);
$expected = array('Data' => array('Post' => array('title' => 'Title of this post', 'description' => 'cool')));
$this->assertEqual($result, $expected);
}
} }
?> ?>