mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
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:
parent
9a8bd5680a
commit
7e845242d5
3 changed files with 112 additions and 8 deletions
|
@ -760,17 +760,41 @@ class Set extends Object {
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
function reverse($object) {
|
function reverse($object) {
|
||||||
|
$out = array();
|
||||||
if (is_a($object, 'xmlnode') || is_a($object, 'XMLNode')) {
|
if (is_a($object, 'xmlnode') || is_a($object, 'XMLNode')) {
|
||||||
if ($object->name != Inflector::underscore($this->name)) {
|
if (isset($object->name) && isset($object->children)) {
|
||||||
if (is_object($object->child(Inflector::underscore($this->name)))) {
|
if ($object->name === 'root' && !empty($object->children)) {
|
||||||
$object = $object->child(Inflector::underscore($this->name));
|
$out = Set::reverse($object->children[0]);
|
||||||
$object = $object->attributes;
|
|
||||||
} else {
|
} 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 {
|
} else {
|
||||||
$out = array();
|
|
||||||
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
|
@ -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();
|
||||||
|
|
||||||
|
|
|
@ -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&#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'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(</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 <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</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'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->(hasMany)ServiceTi me->(hasMany)ServiceTimePrice. So what <br> the current output from my findall is something like this example: <br><p>Array( <br> [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 <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 "RTFM" 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);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
Loading…
Add table
Reference in a new issue