Merge pull request #9349 from cakephp/2.x-sort-locale

2.x sort locale backport
This commit is contained in:
Mark Story 2016-08-26 11:13:21 -04:00 committed by GitHub
commit 51963ab8fc
2 changed files with 35 additions and 0 deletions

View file

@ -1360,6 +1360,38 @@ 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');
$updated = setlocale(LC_COLLATE, 'de_DE.utf8');
$this->skipIf($updated === false, 'Could not set locale to de_DE.utf8, skipping test.');
$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;
}