From 29fa48514033b90aaab408fc9f5d4fba33c3166e Mon Sep 17 00:00:00 2001 From: nate Date: Sat, 15 Mar 2008 05:30:30 +0000 Subject: [PATCH] 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 --- cake/libs/set.php | 15 +++++++------ cake/tests/cases/libs/set.test.php | 35 ++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/cake/libs/set.php b/cake/libs/set.php index 6d9e50043..d8501030e 100644 --- a/cake/libs/set.php +++ b/cake/libs/set.php @@ -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]; } diff --git a/cake/tests/cases/libs/set.test.php b/cake/tests/cases/libs/set.test.php index 85cc00fcb..0ea51a3d8 100644 --- a/cake/tests/cases/libs/set.test.php +++ b/cake/tests/cases/libs/set.test.php @@ -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(