/** * Magic method for shortcut message manipulation. * * @param string $name * @param array $args * * @return $this * @throws BadMethodCallException */ function __call($name, $args) { $allowed = ['add', 'has', 'remove', 'set']; foreach ($allowed as $method) { if (0 === strpos($name, $method)) { $key = Text::toLower(substr($name, strlen($method))); array_unshift($args, $key); return call_user_func_array([$this, $method], $args); } } throw new BadMethodCallException('Method unknown.'); }
/** * Sanitize given locale. * * Must be a simple locale without code set, currency or similar (e. g. "de_DE" or "de-de"). * * @param string|Locale $locale * @param string|Locale $default [optional] The default locale to return if $locale is invalid * * @return null|string */ public static function sanitizeLocale($locale, $default = null) { if ($locale instanceof Locale) { return $locale->getLocale(); } if ($default) { $default = Locale::sanitizeLocale($default, null); } $locale = (string) $locale; $locale = trim($locale); if (!$locale || !preg_match("/^" . Locale::REGEX_LOCALE . "\$/ui", $locale, $found)) { return $default; } $found['language'] = Text::toLower($found['language']); $found['country'] = Text::toUpper($found['country']); return sprintf("%s_%s", $found['language'], $found['country']); }
/** * Get locale with specified language. * * @param $language * * @return Locale|null * */ public function findByLanguage($language) { $language = Text::toLower($language); foreach ($this->getAvailable() as $locale) { if ($language == $locale->getLanguage(Text::LOWER)) { return $locale; } } return null; }