From d07f5a33db08fed5cda43c62704c3c1a13397e6c Mon Sep 17 00:00:00 2001 From: creiner Date: Tue, 1 Oct 2019 15:07:45 +0200 Subject: [PATCH] [Form] Added CountryType option for using alpha3 country codes --- .../Form/Extension/Core/Type/CountryType.php | 7 ++-- .../Form/Extension/Core/Type/LanguageType.php | 7 ++-- .../Extension/Core/Type/CountryTypeTest.php | 36 +++++++++++++++++++ .../Extension/Core/Type/LanguageTypeTest.php | 34 ++++++++++++++++++ src/Symfony/Component/Form/composer.json | 2 +- 5 files changed, 81 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/Form/Extension/Core/Type/CountryType.php b/src/Symfony/Component/Form/Extension/Core/Type/CountryType.php index e15cd01c22..3055c1a5ce 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/CountryType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/CountryType.php @@ -42,16 +42,19 @@ class CountryType extends AbstractType implements ChoiceLoaderInterface $resolver->setDefaults([ 'choice_loader' => function (Options $options) { $choiceTranslationLocale = $options['choice_translation_locale']; + $alpha3 = $options['alpha3']; - return new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale) { - return array_flip(Countries::getNames($choiceTranslationLocale)); + return new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale, $alpha3) { + return array_flip($alpha3 ? Countries::getAlpha3Names($choiceTranslationLocale) : Countries::getNames($choiceTranslationLocale)); }); }, 'choice_translation_domain' => false, 'choice_translation_locale' => null, + 'alpha3' => false, ]); $resolver->setAllowedTypes('choice_translation_locale', ['null', 'string']); + $resolver->setAllowedTypes('alpha3', 'bool'); } /** diff --git a/src/Symfony/Component/Form/Extension/Core/Type/LanguageType.php b/src/Symfony/Component/Form/Extension/Core/Type/LanguageType.php index 4a0cbe1f8f..c369103781 100644 --- a/src/Symfony/Component/Form/Extension/Core/Type/LanguageType.php +++ b/src/Symfony/Component/Form/Extension/Core/Type/LanguageType.php @@ -42,16 +42,19 @@ class LanguageType extends AbstractType implements ChoiceLoaderInterface $resolver->setDefaults([ 'choice_loader' => function (Options $options) { $choiceTranslationLocale = $options['choice_translation_locale']; + $alpha3 = $options['alpha3']; - return new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale) { - return array_flip(Languages::getNames($choiceTranslationLocale)); + return new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale, $alpha3) { + return array_flip($alpha3 ? Languages::getAlpha3Names($choiceTranslationLocale) : Languages::getNames($choiceTranslationLocale)); }); }, 'choice_translation_domain' => false, 'choice_translation_locale' => null, + 'alpha3' => false, ]); $resolver->setAllowedTypes('choice_translation_locale', ['null', 'string']); + $resolver->setAllowedTypes('alpha3', 'bool'); } /** diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/CountryTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/CountryTypeTest.php index 98051fc871..ca8ffe3e8c 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/CountryTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/CountryTypeTest.php @@ -58,6 +58,42 @@ class CountryTypeTest extends BaseTypeTest $this->assertContainsEquals(new ChoiceView('MY', 'MY', 'Малайзія'), $choices); } + public function testAlpha3Option() + { + $choices = $this->factory + ->create(static::TESTED_TYPE, null, [ + 'alpha3' => true, + ]) + ->createView()->vars['choices']; + + // Don't check objects for identity + $this->assertContainsEquals(new ChoiceView('DEU', 'DEU', 'Germany'), $choices); + $this->assertContainsEquals(new ChoiceView('GBR', 'GBR', 'United Kingdom'), $choices); + $this->assertContainsEquals(new ChoiceView('USA', 'USA', 'United States'), $choices); + $this->assertContainsEquals(new ChoiceView('FRA', 'FRA', 'France'), $choices); + $this->assertContainsEquals(new ChoiceView('MYS', 'MYS', 'Malaysia'), $choices); + } + + /** + * @requires extension intl + */ + public function testChoiceTranslationLocaleAndAlpha3Option() + { + $choices = $this->factory + ->create(static::TESTED_TYPE, null, [ + 'choice_translation_locale' => 'uk', + 'alpha3' => true, + ]) + ->createView()->vars['choices']; + + // Don't check objects for identity + $this->assertContainsEquals(new ChoiceView('DEU', 'DEU', 'Німеччина'), $choices); + $this->assertContainsEquals(new ChoiceView('GBR', 'GBR', 'Велика Британія'), $choices); + $this->assertContainsEquals(new ChoiceView('USA', 'USA', 'Сполучені Штати'), $choices); + $this->assertContainsEquals(new ChoiceView('FRA', 'FRA', 'Франція'), $choices); + $this->assertContainsEquals(new ChoiceView('MYS', 'MYS', 'Малайзія'), $choices); + } + public function testUnknownCountryIsNotIncluded() { $choices = $this->factory->create(static::TESTED_TYPE, 'country') diff --git a/src/Symfony/Component/Form/Tests/Extension/Core/Type/LanguageTypeTest.php b/src/Symfony/Component/Form/Tests/Extension/Core/Type/LanguageTypeTest.php index 2000e1501e..13c90dd859 100644 --- a/src/Symfony/Component/Form/Tests/Extension/Core/Type/LanguageTypeTest.php +++ b/src/Symfony/Component/Form/Tests/Extension/Core/Type/LanguageTypeTest.php @@ -53,6 +53,40 @@ class LanguageTypeTest extends BaseTypeTest $this->assertContainsEquals(new ChoiceView('my', 'my', 'бірманська'), $choices); } + public function testAlpha3Option() + { + $choices = $this->factory + ->create(static::TESTED_TYPE, null, [ + 'alpha3' => true, + ]) + ->createView()->vars['choices']; + + // Don't check objects for identity + $this->assertContainsEquals(new ChoiceView('eng', 'eng', 'English'), $choices); + $this->assertContainsEquals(new ChoiceView('fra', 'fra', 'French'), $choices); + // Burmese has no three letter language code + $this->assertNotContainsEquals(new ChoiceView('my', 'my', 'Burmese'), $choices); + } + + /** + * @requires extension intl + */ + public function testChoiceTranslationLocaleAndAlpha3Option() + { + $choices = $this->factory + ->create(static::TESTED_TYPE, null, [ + 'choice_translation_locale' => 'uk', + 'alpha3' => true, + ]) + ->createView()->vars['choices']; + + // Don't check objects for identity + $this->assertContainsEquals(new ChoiceView('eng', 'eng', 'англійська'), $choices); + $this->assertContainsEquals(new ChoiceView('fra', 'fra', 'французька'), $choices); + // Burmese has no three letter language code + $this->assertNotContainsEquals(new ChoiceView('my', 'my', 'бірманська'), $choices); + } + public function testMultipleLanguagesIsNotIncluded() { $choices = $this->factory->create(static::TESTED_TYPE, 'language') diff --git a/src/Symfony/Component/Form/composer.json b/src/Symfony/Component/Form/composer.json index d81b150e91..0b1fb74ccc 100644 --- a/src/Symfony/Component/Form/composer.json +++ b/src/Symfony/Component/Form/composer.json @@ -18,7 +18,7 @@ "require": { "php": "^7.1.3", "symfony/event-dispatcher": "^4.3", - "symfony/intl": "^4.3|^5.0", + "symfony/intl": "^4.4|^5.0", "symfony/options-resolver": "~4.3|^5.0", "symfony/polyfill-ctype": "~1.8", "symfony/polyfill-mbstring": "~1.0",