Refactoring Set::reverse() and Model::set()

Set::reverse() will now reverse the object to an array that was created in Set::map();
Added testMapReverse() to Set tests.
Removed code in Model::set() that was no longer needed

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@5373 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
phpnut 2007-07-02 18:36:49 +00:00
parent 382a63651d
commit 3880f9ce13
3 changed files with 91 additions and 15 deletions

View file

@ -739,18 +739,6 @@ class Model extends Overloadable {
function set($one, $two = null) {
if (is_object($one)) {
$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 {
$name = array_keys($one[$i]);
$one[$name[0]] = $one[$i][$name[0]];
}
unset($one[$i]);
}
}
}
if (is_array($one)) {

View file

@ -682,7 +682,9 @@ class Set extends Object {
for ($i = 0; $i < $count; $i++) {
if ($keys[$i] == '__identity__') {
$key = $object[$keys[$i]];
unset($object[$keys[$i]]);
$object[$key] = $object;
} elseif (is_array($object[$keys[$i]])) {
$keys1 = array_keys($object[$keys[$i]]);
$count1 = count($keys1);
@ -701,11 +703,28 @@ class Set extends Object {
}
}
$return = $object;
if(!empty($merge)) {
$object = array($object, $merge);
$mergeKeys = array_keys($merge);
$objectKeys = array_keys($object);
$count = count($mergeKeys);
$change = $object;
for ($i = 0; $i < $count; $i++) {
foreach ($objectKeys as $key => $value) {
if(is_array($object[$value])) {
if(array_key_exists($mergeKeys[$i], $object[$value])) {
unset($change[$value][$mergeKeys[$i]]);
}
} else {
unset($change[$value]);
}
}
}
$object = Set::pushDiff($change, $merge);
}
return $object;
}
}
}
?>
?>

View file

@ -438,6 +438,75 @@ class SetTest extends UnitTestCase {
);
$this->assertIdentical($result, $expected);
}
}
function testMapReverse() {
$expected = array('Array1' => array(
'Array1Data1' => 'Array1Data1 value 1',
'Array1Data2' => 'Array1Data2 value 2',
'Array1Data3' => 'Array1Data3 value 3',
'Array1Data4' => 'Array1Data4 value 4',
'Array1Data5' => 'Array1Data5 value 5',
'Array1Data6' => 'Array1Data6 value 6',
'Array1Data7' => 'Array1Data7 value 7',
'Array1Data8' => 'Array1Data8 value 8'),
'Array2' => array(
0 => array(
'Array2Data1' => 1,
'Array2Data2' => 'Array2Data2 value 2',
'Array2Data3' => 'Array2Data3 value 2',
'Array2Data4' => 'Array2Data4 value 4'),
1 => array(
'Array2Data1' => 2,
'Array2Data2' => 'Array2Data2 value 2',
'Array2Data3' => 'Array2Data3 value 2',
'Array2Data4' => 'Array2Data4 value 4'),
2 => array(
'Array2Data1' => 3,
'Array2Data2' => 'Array2Data2 value 2',
'Array2Data3' => 'Array2Data3 value 2',
'Array2Data4' => 'Array2Data4 value 4'),
3 => array(
'Array2Data1' => 4,
'Array2Data2' => 'Array2Data2 value 2',
'Array2Data3' => 'Array2Data3 value 2',
'Array2Data4' => 'Array2Data4 value 4'),
4 => array(
'Array2Data1' => 5,
'Array2Data2' => 'Array2Data2 value 2',
'Array2Data3' => 'Array2Data3 value 2',
'Array2Data4' => 'Array2Data4 value 4')),
'Array3' => array(
0 => array(
'Array3Data1' => 1,
'Array3Data2' => 'Array3Data2 value 2',
'Array3Data3' => 'Array3Data3 value 2',
'Array3Data4' => 'Array3Data4 value 4'),
1 => array(
'Array3Data1' => 2,
'Array3Data2' => 'Array3Data2 value 2',
'Array3Data3' => 'Array3Data3 value 2',
'Array3Data4' => 'Array3Data4 value 4'),
2 => array(
'Array3Data1' => 3,
'Array3Data2' => 'Array3Data2 value 2',
'Array3Data3' => 'Array3Data3 value 2',
'Array3Data4' => 'Array3Data4 value 4'),
3 => array(
'Array3Data1' => 4,
'Array3Data2' => 'Array3Data2 value 2',
'Array3Data3' => 'Array3Data3 value 2',
'Array3Data4' => 'Array3Data4 value 4'),
4 => array(
'Array3Data1' => 5,
'Array3Data2' => 'Array3Data2 value 2',
'Array3Data3' => 'Array3Data3 value 2',
'Array3Data4' => 'Array3Data4 value 4')));
$map = Set::map($expected);
$result = Set::reverse($map);
$this->assertIdentical($result, $expected);
}
}
?>