aboutsummaryrefslogtreecommitdiffhomepage
path: root/application/Utils.php
diff options
context:
space:
mode:
Diffstat (limited to 'application/Utils.php')
-rw-r--r--application/Utils.php72
1 files changed, 52 insertions, 20 deletions
diff --git a/application/Utils.php b/application/Utils.php
index 19fb7116..a936b09f 100644
--- a/application/Utils.php
+++ b/application/Utils.php
@@ -225,44 +225,46 @@ function autoLocale($headerLocale)
225 $encodings = ['utf8', 'UTF-8']; 225 $encodings = ['utf8', 'UTF-8'];
226 if (!empty($matches[2])) { 226 if (!empty($matches[2])) {
227 $second = [strtoupper($matches[2]), strtolower($matches[2])]; 227 $second = [strtoupper($matches[2]), strtolower($matches[2])];
228 $attempts = arrays_combination([$first, $separators, $second, ['.'], $encodings]); 228 $attempts = cartesian_product_generator([$first, $separators, $second, ['.'], $encodings]);
229 } else { 229 } else {
230 $attempts = arrays_combination([$first, $separators, $first, ['.'], $encodings]); 230 $attempts = cartesian_product_generator([$first, $separators, $first, ['.'], $encodings]);
231 } 231 }
232 } 232 }
233 } 233 }
234 setlocale(LC_ALL, $attempts); 234 setlocale(LC_ALL, implode('implode', iterator_to_array($attempts)));
235} 235}
236 236
237/** 237/**
238 * Combine multiple arrays of string to get all possible strings. 238 * Build a Generator object representing the cartesian product from given $items.
239 * The order is important because this doesn't shuffle the entries.
240 * 239 *
241 * Example: 240 * Example:
242 * [['a'], ['b', 'c']] 241 * [['a'], ['b', 'c']]
243 * will generate: 242 * will generate:
244 * - ab 243 * [
245 * - ac 244 * ['a', 'b'],
246 * 245 * ['a', 'c'],
247 * TODO PHP 5.6: use the `...` operator instead of an array of array. 246 * ]
248 * 247 *
249 * @param array $items array of array of string 248 * @param array $items array of array of string
250 * 249 *
251 * @return array Combined string from the input array. 250 * @return Generator representing the cartesian product of given array.
251 *
252 * @see https://en.wikipedia.org/wiki/Cartesian_product
252 */ 253 */
253function arrays_combination($items) 254function cartesian_product_generator($items)
254{ 255{
255 $out = ['']; 256 if (empty($items)) {
256 foreach ($items as $item) { 257 yield [];
257 $add = []; 258 }
258 foreach ($item as $element) { 259 $subArray = array_pop($items);
259 foreach ($out as $key => $existingEntry) { 260 if (empty($subArray)) {
260 $add[] = $existingEntry . $element; 261 return;
261 } 262 }
263 foreach (cartesian_product_generator($items) as $item) {
264 foreach ($subArray as $value) {
265 yield $item + [count($item) => $value];
262 } 266 }
263 $out = $add;
264 } 267 }
265 return $out;
266} 268}
267 269
268/** 270/**
@@ -303,3 +305,33 @@ function normalize_spaces($string)
303{ 305{
304 return preg_replace('/\s{2,}/', ' ', trim($string)); 306 return preg_replace('/\s{2,}/', ' ', trim($string));
305} 307}
308
309/**
310 * Format the date according to the locale.
311 *
312 * Requires php-intl to display international datetimes,
313 * otherwise default format '%c' will be returned.
314 *
315 * @param DateTime $date to format.
316 * @param bool $intl Use international format if true.
317 *
318 * @return bool|string Formatted date, or false if the input is invalid.
319 */
320function format_date($date, $intl = true)
321{
322 if (! $date instanceof DateTime) {
323 return false;
324 }
325
326 if (! $intl || ! class_exists('IntlDateFormatter')) {
327 return strftime('%c', $date->getTimestamp());
328 }
329
330 $formatter = new IntlDateFormatter(
331 setlocale(LC_TIME, 0),
332 IntlDateFormatter::LONG,
333 IntlDateFormatter::LONG
334 );
335
336 return $formatter->format($date);
337}