mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-09-04 18:42:40 +00:00
Ability for Hash::sort to sort case-insensitively
This commit is contained in:
parent
6fae3f6bd0
commit
a217556c13
2 changed files with 99 additions and 5 deletions
|
@ -1301,6 +1301,43 @@ class HashTest extends CakeTestCase {
|
|||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test natural sorting ignoring case.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSortNaturalIgnoreCase() {
|
||||
if (version_compare(PHP_VERSION, '5.4.0', '<')) {
|
||||
$this->markTestSkipped('SORT_NATURAL is available since PHP 5.4.');
|
||||
}
|
||||
$items = array(
|
||||
array('Item' => array('image' => 'img1.jpg')),
|
||||
array('Item' => array('image' => 'img99.jpg')),
|
||||
array('Item' => array('image' => 'Img12.jpg')),
|
||||
array('Item' => array('image' => 'Img10.jpg')),
|
||||
array('Item' => array('image' => 'img2.jpg')),
|
||||
);
|
||||
$result = Hash::sort($items, '{n}.Item.image', 'desc', 'natural', true);
|
||||
$expected = array(
|
||||
array('Item' => array('image' => 'img99.jpg')),
|
||||
array('Item' => array('image' => 'Img12.jpg')),
|
||||
array('Item' => array('image' => 'Img10.jpg')),
|
||||
array('Item' => array('image' => 'img2.jpg')),
|
||||
array('Item' => array('image' => 'img1.jpg')),
|
||||
);
|
||||
$this->assertEquals($expected, $result);
|
||||
|
||||
$result = Hash::sort($items, '{n}.Item.image', 'asc', 'natural', true);
|
||||
$expected = array(
|
||||
array('Item' => array('image' => 'img1.jpg')),
|
||||
array('Item' => array('image' => 'img2.jpg')),
|
||||
array('Item' => array('image' => 'Img10.jpg')),
|
||||
array('Item' => array('image' => 'Img12.jpg')),
|
||||
array('Item' => array('image' => 'img99.jpg')),
|
||||
);
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that sort() with 'natural' type will fallback to 'regular' as SORT_NATURAL is introduced in PHP 5.4
|
||||
*
|
||||
|
@ -1355,7 +1392,7 @@ class HashTest extends CakeTestCase {
|
|||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSortString() {
|
||||
public function testSortStringKeys() {
|
||||
$toSort = array(
|
||||
'four' => array('number' => 4, 'some' => 'foursome'),
|
||||
'six' => array('number' => 6, 'some' => 'sixsome'),
|
||||
|
@ -1387,6 +1424,50 @@ class HashTest extends CakeTestCase {
|
|||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* test sorting with string ignoring case.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSortStringIgnoreCase() {
|
||||
$toSort = array(
|
||||
array('Item' => array('name' => 'bar')),
|
||||
array('Item' => array('name' => 'Baby')),
|
||||
array('Item' => array('name' => 'Baz')),
|
||||
array('Item' => array('name' => 'bat')),
|
||||
);
|
||||
$sorted = Hash::sort($toSort, '{n}.Item.name', 'asc', 'string', true);
|
||||
$expected = array(
|
||||
array('Item' => array('name' => 'Baby')),
|
||||
array('Item' => array('name' => 'bar')),
|
||||
array('Item' => array('name' => 'bat')),
|
||||
array('Item' => array('name' => 'Baz')),
|
||||
);
|
||||
$this->assertEquals($expected, $sorted);
|
||||
}
|
||||
|
||||
/**
|
||||
* test regular sorting ignoring case.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testSortRegularIgnoreCase() {
|
||||
$toSort = array(
|
||||
array('Item' => array('name' => 'bar')),
|
||||
array('Item' => array('name' => 'Baby')),
|
||||
array('Item' => array('name' => 'Baz')),
|
||||
array('Item' => array('name' => 'bat')),
|
||||
);
|
||||
$sorted = Hash::sort($toSort, '{n}.Item.name', 'asc', 'regular', true);
|
||||
$expected = array(
|
||||
array('Item' => array('name' => 'Baby')),
|
||||
array('Item' => array('name' => 'bar')),
|
||||
array('Item' => array('name' => 'bat')),
|
||||
array('Item' => array('name' => 'Baz')),
|
||||
);
|
||||
$this->assertEquals($expected, $sorted);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test insert()
|
||||
*
|
||||
|
|
|
@ -843,10 +843,11 @@ class Hash {
|
|||
* @param string $path A Set-compatible path to the array value
|
||||
* @param string $dir See directions above. Defaults to 'asc'.
|
||||
* @param string $type See direction types above. Defaults to 'regular'.
|
||||
* @param string $ignoreCase Case insensitive sorting. Defaults to false.
|
||||
* @return array Sorted array of data
|
||||
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/hash.html#Hash::sort
|
||||
*/
|
||||
public static function sort(array $data, $path, $dir = 'asc', $type = 'regular') {
|
||||
public static function sort(array $data, $path, $dir = 'asc', $type = 'regular', $ignoreCase = false) {
|
||||
if (empty($data)) {
|
||||
return array();
|
||||
}
|
||||
|
@ -870,9 +871,16 @@ class Hash {
|
|||
|
||||
$dir = strtolower($dir);
|
||||
$type = strtolower($type);
|
||||
if ($type === 'natural' && version_compare(PHP_VERSION, '5.4.0', '<')) {
|
||||
$type = 'regular';
|
||||
|
||||
// Natural and case insensitive sort is only supported from >= 5.4.0
|
||||
if (version_compare(PHP_VERSION, '5.4.0', '<')) {
|
||||
if ($type === 'natural' || $type === 'natural_ignore_case' || $type === 'regular_ignore_case') {
|
||||
$type = 'regular';
|
||||
} elseif ($type == 'string_ignore_case') {
|
||||
$type = 'string';
|
||||
}
|
||||
}
|
||||
|
||||
if ($dir === 'asc') {
|
||||
$dir = SORT_ASC;
|
||||
} else {
|
||||
|
@ -887,7 +895,12 @@ class Hash {
|
|||
} else {
|
||||
$type = SORT_REGULAR;
|
||||
}
|
||||
array_multisort($values, $dir, $type, $keys, $dir, $type);
|
||||
|
||||
if ($ignoreCase) {
|
||||
$values = array_map('strtolower', $values);
|
||||
}
|
||||
array_multisort($values, $dir, $type, $keys, $dir);
|
||||
|
||||
$sorted = array();
|
||||
$keys = array_unique($keys);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue