From 9cab8ae58a29ecf7387e6865aa170715caeabf04 Mon Sep 17 00:00:00 2001 From: Stefan Parviainen Date: Tue, 30 Dec 2014 19:57:12 +0100 Subject: Language names via intl library. Fixes #773 --- library/intl/src/Country/Country.php | 168 +++++++++++++++++++++ library/intl/src/Country/CountryInterface.php | 99 ++++++++++++ library/intl/src/Country/CountryRepository.php | 124 +++++++++++++++ .../src/Country/CountryRepositoryInterface.php | 31 ++++ 4 files changed, 422 insertions(+) create mode 100644 library/intl/src/Country/Country.php create mode 100644 library/intl/src/Country/CountryInterface.php create mode 100644 library/intl/src/Country/CountryRepository.php create mode 100644 library/intl/src/Country/CountryRepositoryInterface.php (limited to 'library/intl/src/Country') diff --git a/library/intl/src/Country/Country.php b/library/intl/src/Country/Country.php new file mode 100644 index 000000000..5ac65fd0d --- /dev/null +++ b/library/intl/src/Country/Country.php @@ -0,0 +1,168 @@ +getCountryCode(); + } + + /** + * {@inheritdoc} + */ + public function getCountryCode() + { + return $this->countryCode; + } + + /** + * {@inheritdoc} + */ + public function setCountryCode($countryCode) + { + $this->countryCode = $countryCode; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function getName() + { + return $this->name; + } + + /** + * {@inheritdoc} + */ + public function setName($name) + { + $this->name = $name; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function getThreeLetterCode() + { + return $this->threeLetterCode; + } + + /** + * {@inheritdoc} + */ + public function setThreeLetterCode($threeLetterCode) + { + $this->threeLetterCode = $threeLetterCode; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function getNumericCode() + { + return $this->numericCode; + } + + /** + * {@inheritdoc} + */ + public function setNumericCode($numericCode) + { + $this->numericCode = $numericCode; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function getTelephoneCode() + { + return $this->telephoneCode; + } + + /** + * {@inheritdoc} + */ + public function setTelephoneCode($telephoneCode) + { + $this->telephoneCode = $telephoneCode; + + return $this; + } + + /** + * {@inheritdoc} + */ + public function getLocale() + { + return $this->locale; + } + + /** + * {@inheritdoc} + */ + public function setLocale($locale) + { + $this->locale = $locale; + + return $this; + } +} diff --git a/library/intl/src/Country/CountryInterface.php b/library/intl/src/Country/CountryInterface.php new file mode 100644 index 000000000..245a49be9 --- /dev/null +++ b/library/intl/src/Country/CountryInterface.php @@ -0,0 +1,99 @@ +definitionPath = $definitionPath ? $definitionPath : __DIR__ . '/../../resources/country/'; + } + + /** + * {@inheritdoc} + */ + public function get($countryCode, $locale = null, $fallbackLocale = null) + { + $locale = $this->resolveLocale($locale, $fallbackLocale); + $definitions = $this->loadDefinitions($locale); + if (!isset($definitions[$countryCode])) { + throw new UnknownCountryException($countryCode); + } + + return $this->createCountryFromDefinition($definitions[$countryCode], $locale); + } + + /** + * {@inheritdoc} + */ + public function getAll($locale = null, $fallbackLocale = null) + { + $locale = $this->resolveLocale($locale, $fallbackLocale); + $definitions = $this->loadDefinitions($locale); + $countries = array(); + foreach ($definitions as $countryCode => $definition) { + $countries[$countryCode] = $this->createCountryFromDefinition($definition, $locale); + } + + return $countries; + } + + /** + * Loads the country definitions for the provided locale. + * + * @param string $locale The desired locale. + * + * @return array + */ + protected function loadDefinitions($locale) + { + if (!isset($this->definitions[$locale])) { + $filename = $this->definitionPath . $locale . '.json'; + $this->definitions[$locale] = json_decode(file_get_contents($filename), true); + + // Make sure the base definitions have been loaded. + if (empty($this->baseDefinitions)) { + $this->baseDefinitions = json_decode(file_get_contents($this->definitionPath . 'base.json'), true); + } + // Merge-in base definitions. + foreach ($this->definitions[$locale] as $countryCode => $definition) { + $this->definitions[$locale][$countryCode] += $this->baseDefinitions[$countryCode]; + } + } + + return $this->definitions[$locale]; + } + + /** + * Creates a country object from the provided definition. + * + * @param array $definition The country definition. + * @param string $locale The locale of the country definition. + * + * @return Country + */ + protected function createCountryFromDefinition(array $definition, $locale) + { + $country = new Country(); + $country->setCountryCode($definition['code']); + $country->setName($definition['name']); + $country->setLocale($locale); + if (isset($definition['three_letter_code'])) { + $country->setThreeLetterCode($definition['three_letter_code']); + } + if (isset($definition['numeric_code'])) { + $country->setNumericCode($definition['numeric_code']); + } + if (isset($definition['telephone_code'])) { + $country->setTelephoneCode($definition['telephone_code']); + } + + return $country; + } +} diff --git a/library/intl/src/Country/CountryRepositoryInterface.php b/library/intl/src/Country/CountryRepositoryInterface.php new file mode 100644 index 000000000..ae1cbd0c9 --- /dev/null +++ b/library/intl/src/Country/CountryRepositoryInterface.php @@ -0,0 +1,31 @@ +