Permalink
Browse files

[Locale][Form][Validator] Refactored code to new Locale component, im…

…plemented Country constraint
  • Loading branch information...
1 parent 93d3716 commit fdb7f84c7def4f11fa66d12a2b7e57f3cb229bf9 @webmozart webmozart committed with fabpot Dec 15, 2010
Showing 476 changed files with 267 additions and 82 deletions.
@@ -2,6 +2,8 @@
namespace Symfony\Component\Form;
+use Symfony\Component\Locale\Locale;
+
/**
* A field for selecting from a list of countries
*
@@ -10,53 +12,12 @@
class CountryField extends ChoiceField
{
/**
- * Caches the country choices in different locales
- * @var array
- */
- protected static $countries;
-
- /**
* @inheritDoc
*/
protected function configure()
{
- $this->addOption('choices', self::getCountryChoices($this->locale));
+ $this->addOption('choices', Locale::getDisplayCountries($this->locale));
parent::configure();
}
-
- /**
- * Returns the list of countries for a locale
- *
- * @param string $locale The locale to use for the country names
- * @throws RuntimeException When the resource bundles cannot be loaded
- */
- protected static function getCountryChoices($locale)
- {
- if (!isset(self::$countries[$locale])) {
- $bundle = new \ResourceBundle($locale, __DIR__.'/Resources/data/region');
-
- if ($bundle === null) {
- throw new RuntimeException('The region resource bundle could not be loaded');
- }
-
- $collator = new \Collator($locale);
- $countries = array();
-
- foreach ($bundle->get('Countries') as $code => $name) {
- // Global regions (f.i. "America") have numeric codes
- // Countries have alphabetic codes
- // "ZZ" is the code for unknown region
- if (ctype_alpha($code) && $code !== 'ZZ') {
- $countries[$code] = $name;
- }
- }
-
- $collator->asort($countries);
-
- self::$countries[$locale] = $countries;
- }
-
- return self::$countries[$locale];
- }
}
@@ -2,6 +2,8 @@
namespace Symfony\Component\Form;
+use Symfony\Component\Locale\Locale;
+
/**
* A field for selecting from a list of languages
*
@@ -10,51 +12,12 @@
class LanguageField extends ChoiceField
{
/**
- * Caches the language choices in different locales
- * @var array
- */
- protected static $languages;
-
- /**
* @inheritDoc
*/
protected function configure()
{
- $this->addOption('choices', self::getLanguageChoices($this->locale));
+ $this->addOption('choices', Locale::getDisplayLanguages($this->locale));
parent::configure();
}
-
- /**
- * Returns the list of languages for a locale
- *
- * @param string $locale The locale to use for the language names
- * @throws RuntimeException When the resource bundles cannot be loaded
- */
- protected static function getLanguageChoices($locale)
- {
- if (!isset(self::$languages[$locale])) {
- $bundle = new \ResourceBundle($locale, __DIR__.'/Resources/data/lang');
-
- if ($bundle === null) {
- 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 ($code !== 'mul') {
- $languages[$code] = $name;
- }
- }
-
- $collator->asort($languages);
-
- self::$languages[$locale] = $languages;
- }
-
- return self::$languages[$locale];
- }
}
@@ -0,0 +1,119 @@
+<?php
+
+namespace Symfony\Component\Locale;
+
+/*
+ * This file is part of the Symfony framework.
+ *
+ * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
+ *
+ * This source file is subject to the MIT license that is bundled
+ * with this source code in the file LICENSE.
+ */
+
+class Locale extends \Locale
+{
+ /**
+ * Caches the countries in different locales
+ * @var array
+ */
+ protected static $countries = array();
+
+ /**
+ * Caches the languages in different locales
+ * @var array
+ */
+ protected static $languages = array();
+
+ /**
+ * Returns the country names for a locale
+ *
+ * @param string $locale The locale to use for the country names
+ * @return array The country names with their codes as keys
+ * @throws RuntimeException When the resource bundles cannot be loaded
+ */
+ public static function getDisplayCountries($locale)
+ {
+ if (!isset(self::$countries[$locale])) {
+ $bundle = new \ResourceBundle($locale, __DIR__.'/Resources/data/region');
+
+ if ($bundle === null) {
+ throw new \RuntimeException('The country resource bundle could not be loaded');
+ }
+
+ $collator = new \Collator($locale);
+ $countries = array();
+
+ foreach ($bundle->get('Countries') as $code => $name) {
+ // Global countries (f.i. "America") have numeric codes
+ // Countries have alphabetic codes
+ // "ZZ" is the code for unknown country
+ if (ctype_alpha($code) && $code !== 'ZZ') {
+ $countries[$code] = $name;
+ }
+ }
+
+ $collator->asort($countries);
+
+ self::$countries[$locale] = $countries;
+ }
+
+ return self::$countries[$locale];
+ }
+
+ /**
+ * Returns all available country codes
+ *
+ * @return array The country codes
+ * @throws RuntimeException When the resource bundles cannot be loaded
+ */
+ public static function getCountries()
+ {
+ return array_keys(self::getDisplayCountries(self::getDefault()));
+ }
+
+ /**
+ * 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 ($bundle === null) {
+ 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 ($code !== 'mul') {
+ $languages[$code] = $name;
+ }
+ }
+
+ $collator->asort($languages);
+
+ self::$languages[$locale] = $languages;
+ }
+
+ return self::$languages[$locale];
+ }
+
+ /**
+ * Returns all available language codes
+ *
+ * @return array The language codes
+ * @throws RuntimeException When the resource bundles cannot be loaded
+ */
+ public static function getLanguages()
+ {
+ return array_keys(self::getDisplayLanguages(self::getDefault()));
+ }
+}
Oops, something went wrong. Retry.

0 comments on commit fdb7f84

Please sign in to comment.