Permalink
Browse files

Fix Hash::sort() 'natural' type fallback on PHP < 5.4

  • Loading branch information...
majna committed Jan 24, 2013
1 parent 6ade91e commit 5250c9263578335827d2e813936060d8b3a44bf2
Showing with 28 additions and 5 deletions.
  1. +22 −0 lib/Cake/Test/Case/Utility/HashTest.php
  2. +6 −5 lib/Cake/Utility/Hash.php
@@ -1117,6 +1117,28 @@ public function testSortNatural() {
$this->assertEquals($expected, $result);
}
/**
* Test that sort() with 'natural' type will fallback to 'regular' as SORT_NATURAL is introduced in PHP 5.4
*
* @return void
*/
public function testSortNaturalFallbackToRegular() {
if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
$this->markTestSkipped('Skipping SORT_NATURAL fallback test on PHP >= 5.4');
}
$a = array(
0 => array('Person' => array('name' => 'Jeff')),
1 => array('Shirt' => array('color' => 'black'))
);
$b = array(
0 => array('Shirt' => array('color' => 'black')),
1 => array('Person' => array('name' => 'Jeff')),
);
$sorted = Hash::sort($a, '{n}.Person.name', 'asc', 'natural');
$this->assertEquals($sorted, $b);
}
/**
* test sorting with out of order keys.
*
View
@@ -732,10 +732,11 @@ public static function apply(array $data, $path, $function) {
*
* ## Sort types
*
* - `numeric` Sort by numeric value.
* - `regular` Sort by numeric value.
* - `string` Sort by numeric value.
* - `natural` Sort by natural order. Requires PHP 5.4 or greater.
* - `regular` For regular sorting (don't change types)
* - `numeric` Compare values numerically
* - `string` Compare values as strings
* - `natural` Compare items as strings using "natural ordering" in a human friendly way.
* Will sort foo10 below foo2 as an example. Requires PHP 5.4 or greater or it will fallback to 'regular'
*
* @param array $data An array of data to sort
* @param string $path A Set-compatible path to the array value
@@ -769,7 +770,7 @@ public static function sort(array $data, $path, $dir, $type = 'regular') {
$dir = strtolower($dir);
$type = strtolower($type);
if ($type == 'natural' && version_compare(PHP_VERSION, '5.4.0', '<')) {
$type == 'regular';
$type = 'regular';
}
if ($dir === 'asc') {
$dir = SORT_ASC;

0 comments on commit 5250c92

Please sign in to comment.