Backport Hash::sort() support for type locale.

This commit is contained in:
mscherer 2016-08-26 14:32:21 +02:00
parent 0565081db6
commit dab4b85596
2 changed files with 31 additions and 0 deletions

View file

@ -1360,6 +1360,34 @@ class HashTest extends CakeTestCase {
$this->assertEquals($sorted, $b);
}
/**
* Test sort() with locale option.
*
* @return void
*/
public function testSortLocale() {
// get the current locale
$oldLocale = setlocale(LC_COLLATE, '0');
// the de_DE.utf8 locale must be installed on the system where the test is performed
setlocale(LC_COLLATE, 'de_DE.utf8');
$items = array(
array('Item' => array('entry' => 'Übergabe')),
array('Item' => array('entry' => 'Ostfriesland')),
array('Item' => array('entry' => 'Äpfel')),
array('Item' => array('entry' => 'Apfel')),
);
$result = Hash::sort($items, '{n}.Item.entry', 'asc', 'locale');
$expected = array(
array('Item' => array('entry' => 'Apfel')),
array('Item' => array('entry' => 'Äpfel')),
array('Item' => array('entry' => 'Ostfriesland')),
array('Item' => array('entry' => 'Übergabe')),
);
$this->assertEquals($expected, $result);
// change to the original locale
setlocale(LC_COLLATE, $oldLocale);
}
/**
* test sorting with out of order keys.
*

View file

@ -836,6 +836,7 @@ class Hash {
* - `regular` For regular sorting (don't change types)
* - `numeric` Compare values numerically
* - `string` Compare values as strings
* - `locale` Compare items as strings, based on the current locale
* - `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'
*
@ -904,6 +905,8 @@ class Hash {
$type = SORT_STRING;
} elseif ($type === 'natural') {
$type = SORT_NATURAL;
} elseif ($type === 'locale') {
$type = SORT_LOCALE_STRING;
} else {
$type = SORT_REGULAR;
}