/** * @dataProvider asortProvider */ public function testAsortIntl($array, $sortFlag, $expected) { $this->skipIfIntlExtensionIsNotLoaded(); $collator = new \Collator('en'); $collator->asort($array, $sortFlag); $this->assertSame($expected, $array); }
protected function sortByValue($collection) { $collator = new \Collator('fr_FR'); $items = iterator_to_array($collection); $collator->asort($items); return new Map($items); }
/** * Sort an array of strings based on current locale. * * @param array &$sorted Array of strings. */ protected function _sortString(&$sorted) { if (empty($this->_collator)) { asort($sorted, SORT_LOCALE_STRING); } else { $this->_collator->asort($sorted, Collator::SORT_STRING); } }
/** * {@inheritdoc} */ public function getCurrencyNames($locale = null) { if (null === $locale) { $locale = \Locale::getDefault(); } $names = parent::getCurrencyNames($locale); $collator = new \Collator($locale); $collator->asort($names); return $names; }
/** * {@inheritdoc} */ public function getScriptNames($locale = null) { if (null === $locale) { $locale = \Locale::getDefault(); } $scripts = parent::getScriptNames($locale); $collator = new \Collator($locale); $collator->asort($scripts); return $scripts; }
/** * @dataProvider provideLocales */ public function testGetNames($displayLocale) { $names = $this->dataProvider->getNames($displayLocale); $keys = array_keys($names); sort($keys); $this->assertEquals(static::$currencies, $keys); // Names should be sorted $sortedNames = $names; $collator = new \Collator($displayLocale); $collator->asort($names); $this->assertSame($sortedNames, $names); }
public function getNames($displayLocale = null) { if (null === $displayLocale) { $displayLocale = Locale::getDefault(); } $names = $this->reader->readEntry($this->path, $displayLocale, array('Names')); if ($names instanceof \Traversable) { $names = iterator_to_array($names); } $collator = new \Collator($displayLocale); $collator->asort($names); return $names; }
/** * Returns the language names for a locale * * @param string $locale The locale to use for the language names * @return array The language names with their codes as keys * @throws RuntimeException When the resource bundles cannot be loaded */ public static function getDisplayLanguages($locale) { if (!isset(self::$languages[$locale])) { $bundle = new \ResourceBundle($locale, __DIR__ . '/Resources/data/lang'); if (null === $bundle) { throw new \RuntimeException('The language resource bundle could not be loaded'); } $collator = new \Collator($locale); $languages = array(); foreach ($bundle->get('Languages') as $code => $name) { // "mul" is the code for multiple languages if ('mul' !== $code) { $languages[$code] = $name; } } $collator->asort($languages); self::$languages[$locale] = $languages; } return self::$languages[$locale]; }
/** * @param array $array * @param bool $keepKeys * * @return array */ public function sort(&$array, $keepKeys = false) { $me = $this; if ($keepKeys) { if (isset($this->collator)) { $result = $this->collator->asort($array); } else { $result = uasort($array, function ($a, $b) use($me) { return $me->compare($a, $b); }); } } else { if (isset($this->collator)) { $result = $this->collator->sort($array); } else { $result = usort($array, function ($a, $b) use($me) { return $me->compare($a, $b); }); } } return $result; }
/** * {@inheritdoc} */ public function getCountryNames($locale = null) { if (null === $locale) { $locale = \Locale::getDefault(); } $countries = parent::getCountryNames($locale); // "ZZ" is the code for unknown country unset($countries['ZZ']); // Global countries (f.i. "America") have numeric codes // Countries have alphabetic codes foreach ($countries as $code => $name) { // is_int() does not work, since some numbers start with '0' and // thus are stored as strings. // The (string) cast is necessary since ctype_digit() returns false // for integers. if (ctype_digit((string) $code)) { unset($countries[$code]); } } $collator = new \Collator($locale); $collator->asort($countries); return $countries; }
/** * Sorts the data array for a given locale, using the locale translations. * It is UTF-8 aware if the Collator class is available (requires the intl * extension). * * @param string $locale The locale whose collation rules should be used. * @param array $data Array of strings to sort. * @return array The $data array, sorted. */ protected function sortData($locale, $data) { $locale = $this->_getLocale($locale); if (is_array($data)) { if (class_exists('Collator')) { $collator = new \Collator($locale); $collator->asort($data); } else { asort($data); } } return $data; }
//integer from string $pref = cms_db_prefix(); $fillers = str_repeat('?,', $vc - 1) . '?'; $sql = 'SELECT COUNT(1) AS num FROM ' . $pref . 'module_tmt_groups WHERE group_id IN (' . $fillers . ') AND flags = 0'; $inact = $db->GetOne($sql, $vals); $toggled = $inact !== FALSE && (int) $inact == 0 ? '0' : '1'; $sql = 'UPDATE ' . $pref . 'module_tmt_groups SET flags=' . $toggled . ' WHERE group_id IN (' . $fillers . ')'; $db->Execute($sql, $vals); } } } elseif (isset($params['sort'])) { if (version_compare(PHP_VERSION, '5.3.0') >= 0 && extension_loaded('intl')) { $locale = setlocale(LC_COLLATE, '0'); //TODO $cl = new Collator($locale); $cl->asort($params['group_name'], Collator::SORT_STRING); } else { asort($params['group_name'], SORT_LOCALE_STRING); } //probably too bad about i18n $ord = 1; $pref = cms_db_prefix(); $sql = 'UPDATE ' . $pref . 'module_tmt_groups SET displayorder=? WHERE group_id=?'; foreach ($params['group_name'] as $indx => &$name) { $id = (int) $params['group_active'][$indx]; $db->Execute($sql, array($ord, $id)); $ord++; } unset($name); } $this->Redirect($id, 'defaultadmin', '', array('showtab' => 1));
public function getAbsence() { $data = $this->getData(); if ($data != false) { $header = array(); $items = array(); $other = array(); $first = true; $count = count($data); foreach ($data as $value) { $value = html_entity_decode(trim($value)); if ($first) { $header = mb_strtolower($value, 'UTF-8'); $header = ucfirst($header); if ($count == 1) { array_push($items, $this->msg); } $first = false; } else { if (preg_match("/\\([\\w\\W]+[\\w\\W]+[\\w\\W]\\)/", $value)) { array_push($items, $value); } else { array_push($other, $value); } } } // Sort items alphabetically $col = new \Collator('is_IS'); $col->asort($items); $items = array_values($items); } else { $header = $this->problem; } return array('header' => $header, 'items' => $items, 'other' => $other); }
function get_data($index, $dataDir, $locale = 'en', $constraint = null) { $data = array(); $bundle = load_resource_bundle($locale, $dataDir); foreach ($bundle->get($index) as $code => $name) { if (null !== $constraint) { if ($constraint($code)) { $data[$code] = $name; } continue; } $data[$code] = $name; } $collator = new \Collator($locale); $collator->asort($data); return $data; }
/** * Returns the locale names for a locale * * @param string $locale The locale to use for the locale names * @return array The locale names with their codes as keys * @throws RuntimeException When the resource bundles cannot be loaded */ static public function getDisplayLocales($locale) { if (!isset(self::$locales[$locale])) { $bundle = \ResourceBundle::create($locale, __DIR__.'/Resources/data/names'); if (null === $bundle) { throw new \RuntimeException('The locale resource bundle could not be loaded'); } $collator = new \Collator($locale); $locales = array(); foreach ($bundle->get('Locales') as $code => $name) { $locales[$code] = $name; } $collator->asort($locales); self::$locales[$locale] = $locales; } return self::$locales[$locale]; }
/** * Gets the field choices for the given field. * * @param string $fieldId * @param string|array $choices Serialized string or array of choices; key * is choide ID * @param boolean $master If true, gets the master phrase values; otherwise, * phrases * * @return array Choices */ public function getThreadFieldChoices($fieldId, $choices, $master = false) { if (!is_array($choices)) { $choices = $choices ? @unserialize($choices) : array(); } if (!$master) { foreach ($choices as $value => &$text) { $text = new XenForo_Phrase($this->getThreadFieldChoicePhraseName($fieldId, $value)); } } $xenOptions = XenForo_Application::get('options'); if ($xenOptions->waindigo_customFields_sortChoicesAlphabetically) { if (!$master && version_compare(phpversion(), '5.3.0', '>') && extension_loaded('intl')) { $visitor = XenForo_Visitor::getInstance(); $language = $visitor->getLanguage(); $col = new \Collator($language['language_code']); $col->asort($choices); } else { asort($choices, SORT_LOCALE_STRING); } } return $choices; }
/** * Locale aware sorting, the key associations are kept, values are sorted alphabetically. * * @param array $arr array to be sorted (reference) * @param int $sortflag One of Collator::SORT_REGULAR, Collator::SORT_NUMERIC, Collator::SORT_STRING * @return void modifies parameter */ public static function asort(array &$arr, $sortflag = null) { if (self::ensure_collator_available()) { if (!isset($sortflag)) { $sortflag = Collator::SORT_REGULAR; } self::$collator->asort($arr, $sortflag); return; } asort($arr, SORT_LOCALE_STRING); }
/** * Locale aware sorting, the key associations are kept, values are sorted alphabetically. * * @param array $arr array to be sorted (reference) * @param int $sortflag One of collatorlib::SORT_NUMERIC, collatorlib::SORT_STRING, collatorlib::SORT_NATURAL, collatorlib::SORT_REGULAR * optionally "|" collatorlib::CASE_SENSITIVE * @return bool True on success */ public static function asort(array &$arr, $sortflag = collatorlib::SORT_STRING) { if (empty($arr)) { // nothing to do return true; } $original = null; $casesensitive = (bool) ($sortflag & collatorlib::CASE_SENSITIVE); $sortflag = $sortflag & ~collatorlib::CASE_SENSITIVE; if ($sortflag != collatorlib::SORT_NATURAL and $sortflag != collatorlib::SORT_STRING) { $casesensitive = false; } if (self::ensure_collator_available()) { if ($sortflag == collatorlib::SORT_NUMERIC) { $flag = Collator::SORT_NUMERIC; } else { if ($sortflag == collatorlib::SORT_REGULAR) { $flag = Collator::SORT_REGULAR; } else { $flag = Collator::SORT_STRING; } } if ($sortflag == collatorlib::SORT_NATURAL) { $original = $arr; if ($sortflag == collatorlib::SORT_NATURAL) { foreach ($arr as $key => $value) { $arr[$key] = self::naturalise((string) $value); } } } if ($casesensitive) { self::$collator->setAttribute(Collator::CASE_FIRST, Collator::UPPER_FIRST); } else { self::$collator->setAttribute(Collator::CASE_FIRST, Collator::OFF); } $result = self::$collator->asort($arr, $flag); if ($original) { self::restore_array($arr, $original); } return $result; } // try some fallback that works at least for English if ($sortflag == collatorlib::SORT_NUMERIC) { return asort($arr, SORT_NUMERIC); } else { if ($sortflag == collatorlib::SORT_REGULAR) { return asort($arr, SORT_REGULAR); } } if (!$casesensitive) { $original = $arr; foreach ($arr as $key => $value) { $arr[$key] = textlib::strtolower($value); } } if ($sortflag == collatorlib::SORT_NATURAL) { $result = natsort($arr); } else { $result = asort($arr, SORT_LOCALE_STRING); } if ($original) { self::restore_array($arr, $original); } return $result; }
public function getNames($displayLocale = null) { if (null === $displayLocale) { $displayLocale = Locale::getDefault(); } // ==================================================================== // For reference: It is NOT possible to return names indexed by // numeric code here, because some numeric codes map to multiple // 3-letter codes (e.g. 32 => "ARA", "ARP", "ARS") // ==================================================================== $names = $this->reader->readEntry($this->path, $displayLocale, array('Names')); if ($names instanceof \Traversable) { $names = iterator_to_array($names); } $index = static::INDEX_NAME; array_walk($names, function (&$value) use($index) { $value = $value[$index]; }); // Sorting by value cannot be done during bundle generation, because // binary bundles are always sorted by keys $collator = new \Collator($displayLocale); $collator->asort($names); return $names; }
/** * Sort an array and maintain index association, use Collate from the * PECL "intl" package, if available, for UTF-8 sorting (ex: list of countries). * On Debian/Ubuntu: apt-get install php5-intl * * @param array $array array of values * * @return array Sorted array * @static */ static function asort($array = array()) { $lcMessages = CRM_Utils_System::getUFLocale(); if ($lcMessages && $lcMessages != 'en_US' && class_exists('Collator')) { $collator = new Collator($lcMessages . '.utf8'); $collator->asort($array); } else { asort($array); } return $array; }
/** * @param string $path * @param array $pages * @param string $order_by * @param array $manual * * @throws \RuntimeException * @internal */ protected function buildSort($path, array $pages, $order_by = 'default', $manual = null) { $list = []; $header_default = null; $header_query = null; $sort_flags = SORT_NATURAL | SORT_FLAG_CASE; // do this header query work only once if (strpos($order_by, 'header.') === 0) { $header_query = explode('|', str_replace('header.', '', $order_by)); if (isset($header_query[1])) { $header_default = $header_query[1]; } } foreach ($pages as $key => $info) { $child = isset($this->instances[$key]) ? $this->instances[$key] : null; if (!$child) { throw new \RuntimeException("Page does not exist: {$key}"); } switch ($order_by) { case 'title': $list[$key] = $child->title(); break; case 'date': $list[$key] = $child->date(); $sort_flags = SORT_REGULAR; break; case 'modified': $list[$key] = $child->modified(); $sort_flags = SORT_REGULAR; break; case 'slug': $list[$key] = $child->slug(); break; case 'basename': $list[$key] = basename($key); break; case is_string($header_query[0]): $child_header = new Header((array) $child->header()); $header_value = $child_header->get($header_query[0]); if ($header_value) { $list[$key] = $header_value; } else { $list[$key] = $header_default ?: $key; } $sort_flags = SORT_REGULAR; break; case 'manual': case 'default': default: $list[$key] = $key; $sort_flags = SORT_REGULAR; } } // handle special case when order_by is random if ($order_by == 'random') { $list = $this->arrayShuffle($list); } else { // else just sort the list according to specified key if (extension_loaded('intl')) { $col = new \Collator(setlocale(LC_COLLATE, 0)); //`setlocale` with a 0 param returns the current locale set $col->asort($list, $sort_flags); } else { asort($list, $sort_flags); } } // Move manually ordered items into the beginning of the list. Order of the unlisted items does not change. if (is_array($manual) && !empty($manual)) { $new_list = []; $i = count($manual); foreach ($list as $key => $dummy) { $info = $pages[$key]; $order = array_search($info['slug'], $manual); if ($order === false) { $order = $i++; } $new_list[$key] = (int) $order; } $list = $new_list; // Apply manual ordering to the list. asort($list); } foreach ($list as $key => $sort) { $info = $pages[$key]; $this->sort[$path][$order_by][$key] = $info; } }
/** * Returns the locale names for a locale * * @param string $locale The locale to use for the locale names * * @return array The locale names with their codes as keys * * @throws \RuntimeException When the resource bundles cannot be loaded */ public static function getDisplayLocales($locale) { if (!isset(self::$locales[$locale])) { $bundle = \ResourceBundle::create($locale, self::getIcuDataDirectory() . '/names'); if (null === $bundle) { throw new \RuntimeException(sprintf('The locale resource bundle could not be loaded for locale "%s"', $locale)); } $collator = new \Collator($locale); $locales = array(); $bundleLocales = $bundle->get('Locales') ?: array(); foreach ($bundleLocales as $code => $name) { $locales[$code] = $name; } $fallbackLocale = self::getFallbackLocale($locale); if (null !== $fallbackLocale) { $locales = array_merge(self::getDisplayLocales($fallbackLocale), $locales); } $collator->asort($locales); self::$locales[$locale] = $locales; } return self::$locales[$locale]; }
/** * List flags * * @param string $flag_path The flag directory path * @param string $value Selected flag * @return string $list Html code */ public function get_dir_flag_list($flag_path, $value) { $list = ''; $this->user->add_lang_ext('ernadoo/phpbbdirectory', 'directory_flags'); $flags = $this->dir_helper->preg_grep_keys('/^DIR_FLAG_CODE_/i', $this->user->lang); if (extension_loaded('intl')) { $locale = $this->user->lang['USER_LANG']; $col = new \Collator($locale); $col->asort($flags); } else { asort($flags); } foreach ($flags as $file => $name) { $img_file = strtolower(substr(strrchr($file, '_'), 1)) . '.png'; if (file_exists($flag_path . $img_file)) { $list .= '<option value="' . $img_file . '" ' . ($img_file == $value ? 'selected="selected"' : '') . '>' . $name . '</option>'; } } return $list; }