Adding extra fixes, tests for Set::_ _flatten, fixes #4297

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@6573 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
nate 2008-03-15 05:30:30 +00:00
parent fa61b3fbda
commit 29fa485140
2 changed files with 43 additions and 7 deletions

View file

@ -1003,14 +1003,13 @@ class Set extends Object {
function __flatten($results, $key = null) { function __flatten($results, $key = null) {
$stack = array(); $stack = array();
foreach ($results as $k => $r) { foreach ($results as $k => $r) {
$id = $k;
if (!is_null($key)) {
$id = $key;
}
if (is_array($r)) { if (is_array($r)) {
$stack = array_merge($stack, Set::__flatten($r, $k)); $stack = array_merge($stack, Set::__flatten($r, $id));
} else { } else {
if (!empty($key)) {
$id = $key;
} else {
$id = $k;
}
$stack[] = array('id' => $id, 'value' => $r); $stack[] = array('id' => $id, 'value' => $r);
} }
} }
@ -1035,7 +1034,9 @@ class Set extends Object {
} }
array_multisort($values, $dir, $keys, $dir); array_multisort($values, $dir, $keys, $dir);
$sorted = array(); $sorted = array();
$keys = array_unique($keys);
foreach ($keys as $k) { foreach ($keys as $k) {
$sorted[] = $data[$k]; $sorted[] = $data[$k];
} }

View file

@ -221,6 +221,41 @@ class SetTest extends UnitTestCase {
); );
$a = Set::sort($a, '{n}.Person.name', 'asc'); $a = Set::sort($a, '{n}.Person.name', 'asc');
$this->assertIdentical($a, $b); $this->assertIdentical($a, $b);
// given a path to an array as the sort path, sort by lowest or highest value in array
$a = array(
array(7,6,4),
array(3,4,5),
array(3,2,1),
);
$b = array(
array(3,2,1),
array(3,4,5),
array(7,6,4),
);
$a = Set::sort($a, '{n}.{n}', 'asc');
$this->assertIdentical($a, $b);
// as per above, but nested arrays
$a = array(
array(7,6,4),
array(3,4,5),
array(3,2,array(1,1,1)),
);
$b = array(
array(3,2,array(1,1,1)),
array(3,4,5),
array(7,6,4),
);
$a = Set::sort($a, '{n}', 'asc');
$this->assertIdentical($a, $b);
// if every element doesn't have the matching key, the one without is compared as empty // if every element doesn't have the matching key, the one without is compared as empty
$a = array( $a = array(