diff options
Diffstat (limited to 'vendor/commerceguys/intl/src/Language')
5 files changed, 132 insertions, 134 deletions
diff --git a/vendor/commerceguys/intl/src/Language/Language.php b/vendor/commerceguys/intl/src/Language/Language.php index 9958d5481..d8196f26c 100644 --- a/vendor/commerceguys/intl/src/Language/Language.php +++ b/vendor/commerceguys/intl/src/Language/Language.php @@ -2,7 +2,10 @@ namespace CommerceGuys\Intl\Language; -class Language implements LanguageEntityInterface +/** + * Represents a language. + */ +final class Language { /** * The two-letter language code. @@ -19,42 +22,56 @@ class Language implements LanguageEntityInterface protected $name; /** - * The language locale (i.e. "en-US"). + * The locale (i.e. "en-US"). * * @var string */ protected $locale; /** - * Returns the string representation of the Language. + * Creates a new Language instance. * - * @return string + * @param array $definition The definition array. */ - public function __toString() + public function __construct(array $definition) { - return $this->getLanguageCode(); + foreach (['language_code', 'name', 'locale'] as $requiredProperty) { + if (empty($definition[$requiredProperty])) { + throw new \InvalidArgumentException(sprintf('Missing required property "%s".', $requiredProperty)); + } + } + + $this->languageCode = $definition['language_code']; + $this->name = $definition['name']; + $this->locale = $definition['locale']; } /** - * {@inheritdoc} + * Returns the string representation of the Language. + * + * @return string */ - public function getLanguageCode() + public function __toString() { return $this->languageCode; } /** - * {@inheritdoc} + * Gets the two-letter language code. + * + * @return string */ - public function setLanguageCode($languageCode) + public function getLanguageCode() { - $this->languageCode = $languageCode; - - return $this; + return $this->languageCode; } /** - * {@inheritdoc} + * Gets the language name. + * + * This value is locale specific. + * + * @return string */ public function getName() { @@ -62,30 +79,14 @@ class Language implements LanguageEntityInterface } /** - * {@inheritdoc} - */ - public function setName($name) - { - $this->name = $name; - - return $this; - } - - /** - * {@inheritdoc} + * Gets the locale. + * + * The language name is locale specific. + * + * @return string */ public function getLocale() { return $this->locale; } - - /** - * {@inheritdoc} - */ - public function setLocale($locale) - { - $this->locale = $locale; - - return $this; - } } diff --git a/vendor/commerceguys/intl/src/Language/LanguageEntityInterface.php b/vendor/commerceguys/intl/src/Language/LanguageEntityInterface.php deleted file mode 100644 index e36e0bf77..000000000 --- a/vendor/commerceguys/intl/src/Language/LanguageEntityInterface.php +++ /dev/null @@ -1,24 +0,0 @@ -<?php - -namespace CommerceGuys\Intl\Language; - -interface LanguageEntityInterface extends LanguageInterface -{ - /** - * Sets the two-letter language code. - * - * @param string $languageCode The two-letter language code. - * - * @return self - */ - public function setLanguageCode($languageCode); - - /** - * Sets the language name. - * - * @param string $name The language name. - * - * @return self - */ - public function setName($name); -} diff --git a/vendor/commerceguys/intl/src/Language/LanguageInterface.php b/vendor/commerceguys/intl/src/Language/LanguageInterface.php deleted file mode 100644 index 14c603dfd..000000000 --- a/vendor/commerceguys/intl/src/Language/LanguageInterface.php +++ /dev/null @@ -1,23 +0,0 @@ -<?php - -namespace CommerceGuys\Intl\Language; - -interface LanguageInterface -{ - /** - * Gets the two-letter language code. - * - * @return string - */ - public function getLanguageCode(); - - /** - * Gets the language name. - * - * Note that certain locales have incomplete translations, in which - * case the english version of the language name is used instead. - * - * @return string - */ - public function getName(); -} diff --git a/vendor/commerceguys/intl/src/Language/LanguageRepository.php b/vendor/commerceguys/intl/src/Language/LanguageRepository.php index 4fc02f2ff..151619e1d 100644 --- a/vendor/commerceguys/intl/src/Language/LanguageRepository.php +++ b/vendor/commerceguys/intl/src/Language/LanguageRepository.php @@ -2,7 +2,7 @@ namespace CommerceGuys\Intl\Language; -use CommerceGuys\Intl\LocaleResolverTrait; +use CommerceGuys\Intl\Locale; use CommerceGuys\Intl\Exception\UnknownLanguageException; /** @@ -10,7 +10,26 @@ use CommerceGuys\Intl\Exception\UnknownLanguageException; */ class LanguageRepository implements LanguageRepositoryInterface { - use LocaleResolverTrait; + /** + * The default locale. + * + * @var string + */ + protected $defaultLocale; + + /** + * The fallback locale. + * + * @var string + */ + protected $fallbackLocale; + + /** + * The path where per-locale definitions are stored. + * + * @var string + */ + protected $definitionPath; /** * Per-locale language definitions. @@ -20,40 +39,90 @@ class LanguageRepository implements LanguageRepositoryInterface protected $definitions = []; /** + * The available locales. + * + * @var array + */ + protected $availableLocales = [ + 'af', 'agq', 'ak', 'am', 'ar', 'ar-EG', 'ar-LY', 'ar-SA', 'as', 'asa', + 'ast', 'az', 'az-Cyrl', 'bas', 'be', 'bem', 'bez', 'bg', 'bm', 'bn', + 'bn-IN', 'br', 'brx', 'bs', 'bs-Cyrl', 'ca', 'ccp', 'ce', 'cgg', 'chr', + 'ckb', 'cs', 'cy', 'da', 'dav', 'de', 'de-AT', 'de-CH', 'de-LU', 'dje', + 'dsb', 'dyo', 'dz', 'ebu', 'ee', 'el', 'en', 'en-001', 'en-AU', 'en-CA', + 'en-IN', 'en-NZ', 'eo', 'es', 'es-419', 'es-AR', 'es-BO', 'es-CL', + 'es-CO', 'es-CR', 'es-DO', 'es-EC', 'es-GT', 'es-HN', 'es-MX', 'es-NI', + 'es-PA', 'es-PE', 'es-PR', 'es-PY', 'es-SV', 'es-US', 'es-VE', 'et', + 'eu', 'ewo', 'fa', 'fa-AF', 'ff', 'fi', 'fil', 'fo', 'fr', 'fr-BE', + 'fr-CA', 'fr-CH', 'fur', 'fy', 'ga', 'gd', 'gl', 'gsw', 'gu', 'guz', + 'ha', 'ha-NE', 'he', 'hi', 'hr', 'hsb', 'hu', 'hy', 'id', 'ig', 'is', + 'it', 'ja', 'jmc', 'jv', 'ka', 'kab', 'kam', 'kde', 'kea', 'khq', 'ki', + 'kk', 'kln', 'km', 'kn', 'ko', 'kok', 'ks', 'ksb', 'ksf', 'ksh', 'ku', + 'ky', 'lag', 'lb', 'lg', 'lkt', 'ln', 'lo', 'lrc', 'lt', 'lu', 'luo', + 'luy', 'lv', 'mas', 'mer', 'mfe', 'mg', 'mgh', 'mk', 'ml', 'mn', 'mr', + 'ms', 'mt', 'mua', 'my', 'mzn', 'naq', 'nb', 'nd', 'ne', 'nl', 'nmg', + 'nn', 'nus', 'nyn', 'om', 'or', 'os', 'pa', 'pl', 'ps', 'ps-PK', 'pt', + 'pt-PT', 'qu', 'rm', 'rn', 'ro', 'ro-MD', 'rof', 'ru', 'rw', 'rwk', + 'sah', 'saq', 'sbp', 'sd', 'se', 'se-FI', 'seh', 'ses', 'sg', 'shi', + 'shi-Latn', 'si', 'sk', 'sl', 'smn', 'sn', 'so', 'sq', 'sr', + 'sr-Cyrl-BA', 'sr-Cyrl-ME', 'sr-Cyrl-XK', 'sr-Latn', 'sr-Latn-BA', + 'sr-Latn-ME', 'sr-Latn-XK', 'sv', 'sw', 'sw-CD', 'sw-KE', 'ta', 'te', + 'teo', 'tg', 'th', 'ti', 'tk', 'to', 'tr', 'tt', 'twq', 'tzm', 'ug', + 'uk', 'ur', 'ur-IN', 'uz', 'uz-Cyrl', 'vai', 'vai-Latn', 'vi', 'vun', + 'wae', 'wo', 'xog', 'yav', 'yi', 'yo', 'yo-BJ', 'yue', 'yue-Hans', + 'zgh', 'zh', 'zh-Hant', 'zh-Hant-HK', 'zu', + ]; + + /** * Creates a LanguageRepository instance. * + * @param string $defaultLocale The default locale. Defaults to 'en'. + * @param string $fallbackLocale The fallback locale. Defaults to 'en'. * @param string $definitionPath The path to the currency definitions. * Defaults to 'resources/language'. */ - public function __construct($definitionPath = null) + public function __construct($defaultLocale = 'en', $fallbackLocale = 'en', $definitionPath = null) { + $this->defaultLocale = $defaultLocale; + $this->fallbackLocale = $fallbackLocale; $this->definitionPath = $definitionPath ? $definitionPath : __DIR__ . '/../../resources/language/'; } /** * {@inheritdoc} */ - public function get($languageCode, $locale = null, $fallbackLocale = null) + public function get($languageCode, $locale = null) { - $locale = $this->resolveLocale($locale, $fallbackLocale); + $locale = $locale ?: $this->defaultLocale; + $locale = Locale::resolve($this->availableLocales, $locale, $this->fallbackLocale); $definitions = $this->loadDefinitions($locale); + $languageCode = Locale::canonicalize($languageCode); if (!isset($definitions[$languageCode])) { throw new UnknownLanguageException($languageCode); } + $language = new Language([ + 'language_code' => $languageCode, + 'name' => $definitions[$languageCode], + 'locale' => $locale, + ]); - return $this->createLanguageFromDefinition($languageCode, $definitions[$languageCode], $locale); + return $language; } /** * {@inheritdoc} */ - public function getAll($locale = null, $fallbackLocale = null) + public function getAll($locale = null) { - $locale = $this->resolveLocale($locale, $fallbackLocale); + $locale = $locale ?: $this->defaultLocale; + $locale = Locale::resolve($this->availableLocales, $locale, $this->fallbackLocale); $definitions = $this->loadDefinitions($locale); $languages = []; - foreach ($definitions as $languageCode => $definition) { - $languages[$languageCode] = $this->createLanguageFromDefinition($languageCode, $definition, $locale); + foreach ($definitions as $languageCode => $languageName) { + $languages[$languageCode] = new Language([ + 'language_code' => $languageCode, + 'name' => $languageName, + 'locale' => $locale, + ]); } return $languages; @@ -62,13 +131,14 @@ class LanguageRepository implements LanguageRepositoryInterface /** * {@inheritdoc} */ - public function getList($locale = null, $fallbackLocale = null) + public function getList($locale = null) { - $locale = $this->resolveLocale($locale, $fallbackLocale); + $locale = $locale ?: $this->defaultLocale; + $locale = Locale::resolve($this->availableLocales, $locale, $this->fallbackLocale); $definitions = $this->loadDefinitions($locale); $list = []; - foreach ($definitions as $languageCode => $definition) { - $list[$languageCode] = $definition['name']; + foreach ($definitions as $languageCode => $languageName) { + $list[$languageCode] = $languageName; } return $list; @@ -90,26 +160,4 @@ class LanguageRepository implements LanguageRepositoryInterface return $this->definitions[$locale]; } - - /** - * Creates a language object from the provided definition. - * - * @param string $languageCode The language code. - * @param array $definition The language definition. - * @param string $locale The locale of the language definition. - * - * @return Language - */ - protected function createLanguageFromDefinition($languageCode, array $definition, $locale) - { - $language = new Language(); - $setValues = \Closure::bind(function ($languageCode, $definition, $locale) { - $this->languageCode = $languageCode; - $this->name = $definition['name']; - $this->locale = $locale; - }, $language, '\CommerceGuys\Intl\Language\Language'); - $setValues($languageCode, $definition, $locale); - - return $language; - } } diff --git a/vendor/commerceguys/intl/src/Language/LanguageRepositoryInterface.php b/vendor/commerceguys/intl/src/Language/LanguageRepositoryInterface.php index dd14250e0..12480fc1b 100644 --- a/vendor/commerceguys/intl/src/Language/LanguageRepositoryInterface.php +++ b/vendor/commerceguys/intl/src/Language/LanguageRepositoryInterface.php @@ -8,34 +8,30 @@ namespace CommerceGuys\Intl\Language; interface LanguageRepositoryInterface { /** - * Returns a language instance matching the provided language code. + * Gets a language matching the provided language code. * - * @param string $languageCode The language code. - * @param string $locale The locale (i.e. fr-FR). - * @param string $fallbackLocale A fallback locale (i.e "en"). + * @param string $languageCode The language code. + * @param string $locale The locale (i.e. fr-FR). * - * @return LanguageInterface + * @return Language */ - public function get($languageCode, $locale = null, $fallbackLocale = null); + public function get($languageCode, $locale = null); /** - * Returns all language instances. + * Gets all languages. * - * @param string $locale The locale (i.e. fr-FR). - * @param string $fallbackLocale A fallback locale (i.e "en"). + * @param string $locale The locale (i.e. fr-FR). * - * @return array An array of languages implementing the LanguageInterface, - * keyed by language code. + * @return Language[] An array of languages, keyed by language code. */ - public function getAll($locale = null, $fallbackLocale = null); + public function getAll($locale = null); /** - * Returns a list of languages. + * Gets a list of languages. * - * @param string $locale The locale (i.e. fr-FR). - * @param string $fallbackLocale A fallback locale (i.e "en"). + * @param string $locale The locale (i.e. fr-FR). * * @return array An array of language names, keyed by language code. */ - public function getList($locale = null, $fallbackLocale = null); + public function getList($locale = null); } |