From dab4b85596f1ce6fbd8ac9891c1226cb1f0387b1 Mon Sep 17 00:00:00 2001
From: mscherer <mark.scherer@spryker.com>
Date: Fri, 26 Aug 2016 14:32:21 +0200
Subject: [PATCH] Backport Hash::sort() support for type locale.

---
 lib/Cake/Test/Case/Utility/HashTest.php | 28 +++++++++++++++++++++++++
 lib/Cake/Utility/Hash.php               |  3 +++
 2 files changed, 31 insertions(+)

diff --git a/lib/Cake/Test/Case/Utility/HashTest.php b/lib/Cake/Test/Case/Utility/HashTest.php
index d92c7614f..5af077282 100644
--- a/lib/Cake/Test/Case/Utility/HashTest.php
+++ b/lib/Cake/Test/Case/Utility/HashTest.php
@@ -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.
  *
diff --git a/lib/Cake/Utility/Hash.php b/lib/Cake/Utility/Hash.php
index 3669e1a64..1c6e0bf27 100644
--- a/lib/Cake/Utility/Hash.php
+++ b/lib/Cake/Utility/Hash.php
@@ -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;
 		}