aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--application/Utils.php51
-rw-r--r--tests/UtilsTest.php20
2 files changed, 62 insertions, 9 deletions
diff --git a/application/Utils.php b/application/Utils.php
index 35d65224..19fb7116 100644
--- a/application/Utils.php
+++ b/application/Utils.php
@@ -216,23 +216,56 @@ function is_session_id_valid($sessionId)
216function autoLocale($headerLocale) 216function autoLocale($headerLocale)
217{ 217{
218 // Default if browser does not send HTTP_ACCEPT_LANGUAGE 218 // Default if browser does not send HTTP_ACCEPT_LANGUAGE
219 $attempts = array('en_US'); 219 $attempts = array('en_US', 'en_US.utf8', 'en_US.UTF-8');
220 if (isset($headerLocale)) { 220 if (isset($headerLocale)) {
221 // (It's a bit crude, but it works very well. Preferred language is always presented first.) 221 // (It's a bit crude, but it works very well. Preferred language is always presented first.)
222 if (preg_match('/([a-z]{2})-?([a-z]{2})?/i', $headerLocale, $matches)) { 222 if (preg_match('/([a-z]{2,3})[-_]?([a-z]{2})?/i', $headerLocale, $matches)) {
223 $loc = $matches[1] . (!empty($matches[2]) ? '_' . strtoupper($matches[2]) : ''); 223 $first = [strtolower($matches[1]), strtoupper($matches[1])];
224 $attempts = array( 224 $separators = ['_', '-'];
225 $loc.'.UTF-8', $loc, str_replace('_', '-', $loc).'.UTF-8', str_replace('_', '-', $loc), 225 $encodings = ['utf8', 'UTF-8'];
226 $loc . '_' . strtoupper($loc).'.UTF-8', $loc . '_' . strtoupper($loc), 226 if (!empty($matches[2])) {
227 $loc . '_' . $loc.'.UTF-8', $loc . '_' . $loc, $loc . '-' . strtoupper($loc).'.UTF-8', 227 $second = [strtoupper($matches[2]), strtolower($matches[2])];
228 $loc . '-' . strtoupper($loc), $loc . '-' . $loc.'.UTF-8', $loc . '-' . $loc 228 $attempts = arrays_combination([$first, $separators, $second, ['.'], $encodings]);
229 ); 229 } else {
230 $attempts = arrays_combination([$first, $separators, $first, ['.'], $encodings]);
231 }
230 } 232 }
231 } 233 }
232 setlocale(LC_ALL, $attempts); 234 setlocale(LC_ALL, $attempts);
233} 235}
234 236
235/** 237/**
238 * Combine multiple arrays of string to get all possible strings.
239 * The order is important because this doesn't shuffle the entries.
240 *
241 * Example:
242 * [['a'], ['b', 'c']]
243 * will generate:
244 * - ab
245 * - ac
246 *
247 * TODO PHP 5.6: use the `...` operator instead of an array of array.
248 *
249 * @param array $items array of array of string
250 *
251 * @return array Combined string from the input array.
252 */
253function arrays_combination($items)
254{
255 $out = [''];
256 foreach ($items as $item) {
257 $add = [];
258 foreach ($item as $element) {
259 foreach ($out as $key => $existingEntry) {
260 $add[] = $existingEntry . $element;
261 }
262 }
263 $out = $add;
264 }
265 return $out;
266}
267
268/**
236 * Generates a default API secret. 269 * Generates a default API secret.
237 * 270 *
238 * Note that the random-ish methods used in this function are predictable, 271 * Note that the random-ish methods used in this function are predictable,
diff --git a/tests/UtilsTest.php b/tests/UtilsTest.php
index c885f552..b8f608b9 100644
--- a/tests/UtilsTest.php
+++ b/tests/UtilsTest.php
@@ -282,4 +282,24 @@ class UtilsTest extends PHPUnit_Framework_TestCase
282 $this->assertEquals('', normalize_spaces('')); 282 $this->assertEquals('', normalize_spaces(''));
283 $this->assertEquals(null, normalize_spaces(null)); 283 $this->assertEquals(null, normalize_spaces(null));
284 } 284 }
285
286 /**
287 * Test arrays_combine
288 */
289 public function testArraysCombination()
290 {
291 $arr = [['ab', 'cd'], ['ef', 'gh'], ['ij', 'kl'], ['m']];
292 $expected = [
293 'abefijm',
294 'cdefijm',
295 'abghijm',
296 'cdghijm',
297 'abefklm',
298 'cdefklm',
299 'abghklm',
300 'cdghklm',
301 ];
302 $this->assertEquals($expected, arrays_combination($arr));
303 }
304
285} 305}