Fix sorting empty data with Hash & Set.

Fixes #3420
This commit is contained in:
mark_story 2012-11-30 13:05:14 -05:00
parent f250592fee
commit 889c1ebc09
4 changed files with 12 additions and 0 deletions

View file

@ -928,6 +928,9 @@ class HashTest extends CakeTestCase {
* @return void
*/
public function testSort() {
$result = Hash::sort(array(), '{n}.name', 'asc');
$this->assertEquals(array(), $result);
$a = array(
0 => array(
'Person' => array('name' => 'Jeff'),

View file

@ -225,6 +225,9 @@ class SetTest extends CakeTestCase {
* @return void
*/
public function testSort() {
$result = Set::sort(array(), '{n}.name', 'asc');
$this->assertEquals(array(), $result);
$a = array(
0 => array('Person' => array('name' => 'Jeff'), 'Friend' => array(array('name' => 'Nate'))),
1 => array('Person' => array('name' => 'Tracy'),'Friend' => array(array('name' => 'Lindsay')))

View file

@ -730,6 +730,9 @@ class Hash {
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::sort
*/
public static function sort(array $data, $path, $dir, $type = 'regular') {
if (empty($data)) {
return array();
}
$originalKeys = array_keys($data);
$numeric = is_numeric(implode('', $originalKeys));
if ($numeric) {

View file

@ -956,6 +956,9 @@ class Set {
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/set.html#Set::sort
*/
public static function sort($data, $path, $dir) {
if (empty($data)) {
return $data;
}
$originalKeys = array_keys($data);
$numeric = false;
if (is_numeric(implode('', $originalKeys))) {