mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-18 18:46:17 +00:00
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:
parent
fa61b3fbda
commit
29fa485140
2 changed files with 43 additions and 7 deletions
|
@ -1003,14 +1003,13 @@ class Set extends Object {
|
|||
function __flatten($results, $key = null) {
|
||||
$stack = array();
|
||||
foreach ($results as $k => $r) {
|
||||
$id = $k;
|
||||
if (!is_null($key)) {
|
||||
$id = $key;
|
||||
}
|
||||
if (is_array($r)) {
|
||||
$stack = array_merge($stack, Set::__flatten($r, $k));
|
||||
$stack = array_merge($stack, Set::__flatten($r, $id));
|
||||
} else {
|
||||
if (!empty($key)) {
|
||||
$id = $key;
|
||||
} else {
|
||||
$id = $k;
|
||||
}
|
||||
$stack[] = array('id' => $id, 'value' => $r);
|
||||
}
|
||||
}
|
||||
|
@ -1035,7 +1034,9 @@ class Set extends Object {
|
|||
}
|
||||
array_multisort($values, $dir, $keys, $dir);
|
||||
$sorted = array();
|
||||
|
||||
|
||||
$keys = array_unique($keys);
|
||||
|
||||
foreach ($keys as $k) {
|
||||
$sorted[] = $data[$k];
|
||||
}
|
||||
|
|
|
@ -221,6 +221,41 @@ class SetTest extends UnitTestCase {
|
|||
);
|
||||
$a = Set::sort($a, '{n}.Person.name', 'asc');
|
||||
$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
|
||||
$a = array(
|
||||
|
|
Loading…
Add table
Reference in a new issue