mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
Refactoring Model::set()
Added Set::reverse(0 to convert an object to an array git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5368 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
parent
da2988a22b
commit
5e2b574e2f
2 changed files with 55 additions and 15 deletions
|
@ -738,23 +738,17 @@ class Model extends Overloadable {
|
|||
*/
|
||||
function set($one, $two = null) {
|
||||
if (is_object($one)) {
|
||||
if (is_a($one, 'xmlnode') || is_a($one, 'XMLNode')) {
|
||||
if ($one->name != Inflector::underscore($this->name)) {
|
||||
if (is_object($one->child(Inflector::underscore($this->name)))) {
|
||||
$one = $one->child(Inflector::underscore($this->name));
|
||||
$one = $one->attributes;
|
||||
$one = Set::reverse($one);
|
||||
$count = count($one);
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
if(isset($one[$i])){
|
||||
if($i === 0) {
|
||||
$one[$this->name] = $one[$i];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
} elseif (is_a($one, 'stdclass') || is_a($one, 'stdClass')) {
|
||||
$one = get_object_vars($one);
|
||||
$keys = array_keys($one);
|
||||
$count = count($keys);
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
if ($keys[$i] == '__identity__' || is_array($one[$keys[$i]]) || is_object($one[$keys[$i]])) {
|
||||
unset($one[$keys[$i]]);
|
||||
$name = array_keys($one[$i]);
|
||||
$one[$name[0]] = $one[$i][$name[0]];
|
||||
}
|
||||
unset($one[$i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -603,5 +603,51 @@ class Set extends Object {
|
|||
}
|
||||
return $list;
|
||||
}
|
||||
|
||||
function reverse($object) {
|
||||
if (is_object($object)) {
|
||||
$merge = 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;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
} elseif (is_a($object, 'stdclass') || is_a($object, 'stdClass')) {
|
||||
$object = get_object_vars($object);
|
||||
$keys = array_keys($object);
|
||||
$count = count($keys);
|
||||
|
||||
for ($i = 0; $i < $count; $i++) {
|
||||
if ($keys[$i] == '__identity__') {
|
||||
unset($object[$keys[$i]]);
|
||||
} elseif (is_array($object[$keys[$i]])) {
|
||||
$keys1 = array_keys($object[$keys[$i]]);
|
||||
$count1 = count($keys1);
|
||||
for ($ii = 0; $ii < $count1; $ii++) {
|
||||
$merge[$keys[$i]][$ii] = Set::reverse($object[$keys[$i]][$ii]);
|
||||
}
|
||||
unset($object[$keys[$i]]);
|
||||
} elseif (is_object($object[$keys[$i]])) {
|
||||
$object1 = get_object_vars($one[$keys[$i]]);
|
||||
$keys1 = array_keys($object1);
|
||||
$count1 = count($keys1);
|
||||
for ($ii = 0; $ii < $count1; $ii++) {
|
||||
$merge[$keys[$i]][$ii] = Set::reverse($object1[$keys1[$i]][$ii]);
|
||||
}
|
||||
unset($object[$keys[$i]]);
|
||||
}
|
||||
}
|
||||
}
|
||||
$return = $object;
|
||||
if(!empty($merge)) {
|
||||
$object = array($object, $merge);
|
||||
}
|
||||
return $object;
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
Loading…
Add table
Reference in a new issue