From 7e845242d52a0d410a0e25c673afcbfeb6195a47 Mon Sep 17 00:00:00 2001 From: gwoo Date: Sat, 29 Dec 2007 20:24:10 +0000 Subject: [PATCH] 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 --- cake/libs/set.php | 40 ++++++++++--- cake/tests/cases/libs/model/model.test.php | 12 ++++ cake/tests/cases/libs/set.test.php | 68 ++++++++++++++++++++++ 3 files changed, 112 insertions(+), 8 deletions(-) diff --git a/cake/libs/set.php b/cake/libs/set.php index 69e0ed322..c224653a8 100644 --- a/cake/libs/set.php +++ b/cake/libs/set.php @@ -760,17 +760,41 @@ class Set extends Object { * @return array */ function reverse($object) { + $out = array(); 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; + if (isset($object->name) && isset($object->children)) { + if ($object->name === 'root' && !empty($object->children)) { + $out = Set::reverse($object->children[0]); } else { - return null; + $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 { - $out = array(); if (is_object($object)) { $keys = get_object_vars($object); if (isset($keys['_name_'])) { @@ -797,9 +821,9 @@ class Set extends Object { } else { $out = $object; } - return $out; + } - return $object; + return $out; } } ?> \ No newline at end of file diff --git a/cake/tests/cases/libs/model/model.test.php b/cake/tests/cases/libs/model/model.test.php index 5be8caa73..3e67468eb 100644 --- a/cake/tests/cases/libs/model/model.test.php +++ b/cake/tests/cases/libs/model/model.test.php @@ -1981,6 +1981,18 @@ class ModelTest extends CakeTestCase { $this->assertEqual($result, $expected); } + function testSaveFromXml() { + if (!class_exists('Xml')) { + uses('xml'); + } + $Article = new Article(); + $result = $Article->save(new Xml('
')); + $this->assertTrue($result); + + $results = $Article->find(array('Article.title' => 'test xml')); + $this->assertTrue($results); + } + function testSaveHabtm() { $this->model =& new Article(); diff --git a/cake/tests/cases/libs/set.test.php b/cake/tests/cases/libs/set.test.php index 57063f22a..6b543610e 100644 --- a/cake/tests/cases/libs/set.test.php +++ b/cake/tests/cases/libs/set.test.php @@ -820,5 +820,73 @@ class SetTest extends UnitTestCase { $result = $set->pushDiff($array2); $this->assertIdentical($result, $array1+$array2); } + + function testXmlSetReverse() { + if (!class_exists('Xml')) { + uses('Xml'); + } + $string = ' + + + Cake PHP Google Group + http://groups.google.com/group/cake-php + 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. + en + + constructng result array when using findall + http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f + 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( + http://groups.google.com/group/cake-php/msg/49bc00f3bc651b4f + bmil...@gmail.com(bpscrugs) + Fri, 28 Dec 2007 00:44:14 UT + + + Re: share views between actions? + http://groups.google.com/group/cake-php/msg/8b350d898707dad8 + 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 + http://groups.google.com/group/cake-php/msg/8b350d898707dad8 + subtropolis.z...@gmail.com(subtropolis zijn) + Fri, 28 Dec 2007 00:45:01 UT + + + '; + $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'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
passed to a flex app. I have the following model association:
ServiceDay->(hasMany)ServiceTi me->(hasMany)ServiceTimePrice. So what
the current output from my findall is something like this example:

Array(
[0] => 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
things you do not understand. That goes especially for asinine comments.
Indeed.
To sum up:
No comment.
In my day, a simple "RTFM" would suffice. I\'ll keep in mind to ignore any
further responses from you.
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 =''; + + $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); + + } } ?> \ No newline at end of file