8000 [Form] Added CountryType option for using alpha3 country codes · symfony/symfony@d07f5a3 · GitHub
[go: up one dir, main page]

Skip to content

Commit d07f5a3

Browse files
creinerfabpot
authored andcommitted
[Form] Added CountryType option for using alpha3 country codes
1 parent 11da6f6 commit d07f5a3

File tree

5 files changed

+81
-5
lines changed

5 files changed

+81
-5
lines changed

src/Symfony/Component/Form/Extension/Core/Type/CountryType.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,19 @@ public function configureOptions(OptionsResolver $resolver)
4242
$resolver->setDefaults([
4343
'choice_loader' => function (Options $options) {
4444
$choiceTranslationLocale = $options['choice_translation_locale'];
45+
$alpha3 = $options['alpha3'];
4546

46-
return new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale) {
47-
return array_flip(Countries::getNames($choiceTranslationLocale));
47+
return new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale, $alpha3) {
48+
return array_flip($alpha3 ? Countries::getAlpha3Names($choiceTranslationLocale) : Countries::getNames($choiceTranslationLocale));
4849
});
4950
},
5051
'choice_translation_domain' => false,
5152
'choice_translation_locale' => null,
53+
'alpha3' => false,
5254
]);
5355

5456
$resolver->setAllowedTypes('choice_translation_locale', ['null', 'string']);
57+
$resolver->setAllowedTypes('alpha3', 'bool');
5558
}
5659

5760
/**

src/Symfony/Component/Form/Extension/Core/Type/LanguageType.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,19 @@ public function configureOptions(OptionsResolver $resolver)
4242
$resolver->setDefaults([
4343
'choice_loader' => function (Options $options) {
4444
$choiceTranslationLocale = $options['choice_translation_locale'];
45+
$alpha3 = $options['alpha3'];
4546

46-
return new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale) {
47-
return array_flip(Languages::getNames($choiceTranslationLocale));
47+
return new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale, $alpha3) {
48+
return array_flip($alpha3 ? Languages::getAlpha3Names($choiceTranslationLocale) : Languages::getNames($choiceTranslationLocale));
4849
});
4950
},
5051
'choice_translation_domain' => false,
5152
'choice_translation_locale' => null,
53+
'alpha3' => false,
5254
]);
5355

5456
$resolver->setAllowedTypes('choice_translation_locale', ['null', 'string']);
57+
$resolver->setAllowedTypes('alpha3', 'bool');
5558
}
5659

5760
/**

src/Symfony/Component/Form/Tests/Extension/Core/Type/CountryTypeTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,42 @@ public function testChoiceTranslationLocaleOption()
5858
$this->assertContainsEquals(new ChoiceView('MY', 'MY', 'Малайзія'), $choices);
5959
}
6060

61+
public function testAlpha3Option()
62+
{
63+
$choices = $this->factory
64+
->create(static::TESTED_TYPE, null, [
65+
'alpha3' => true,
66+
])
67+
->createView()->vars['choices'];
68+
69+
// Don't check objects for identity
70+
$this->assertContainsEquals(new ChoiceView('DEU', 'DEU', 'Germany'), $choices);
71+
$this->assertContainsEquals(new ChoiceView('GBR', 'GBR', 'United Kingdom'), $choices);
72+
$this->assertContainsEquals(new ChoiceView('USA', 'USA', 'United States'), $choices);
73+
$this->assertContainsEquals(new ChoiceView('FRA', 'FRA', 'France'), $choices);
74+
$this->assertContainsEquals(new ChoiceView('MYS', 'MYS', 'Malaysia'), $choices);
75+
}
76+
77+
/**
78+
* @requires extension intl
79+
*/
80+
public function testChoiceTranslationLocaleAndAlpha3Option()
81+
{
82+
$choices = $this->factory
83+
->create(static::TESTED_TYPE, null, [
84+
'choice_translation_locale' => 'uk',
85+
'alpha3' => true,
86+
])
87+
->createView()->vars['choices'];
88+
89+
// Don't check objects for identity
90+
$this->assertContainsEquals(new ChoiceView('DEU', 'DEU', 'Німеччина'), $choices);
91+
$this->assertContainsEquals(new ChoiceView('GBR', 'GBR', 'Велика Британія'), $choices);
92+
$this->assertContainsEquals(new ChoiceView('USA', 'USA', 'Сполучені Штати'), $choices);
93+
$this->assertContainsEquals(new ChoiceView('FRA', 'FRA', 'Франція'), $choices);
94+
$this->assertContainsEquals(new ChoiceView('MYS', 'MYS', 'Малайзія'), $choices);
95+
}
96+
6197
public function testUnknownCountryIsNotIncluded()
6298
{
6399
$choices = $this->factory->create(static::TESTED_TYPE, 'country')

src/Symfony/Component/Form/Tests/Extension/Core/Type/LanguageTypeTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,40 @@ public function testChoiceTranslationLocaleOption()
5353
$this->assertContainsEquals(new ChoiceView('my', 'my', 'бірманська'), $choices);
5454
}
5555

56+
public function testAlpha3Option()
57+
{
58+
$choices = $this->factory
59+
->create(static::TESTED_TYPE, null, [
60+
'alpha3' => true,
61+
])
62+
->createView()->vars['choices'];
63+
64+
// Don't check objects for identity
65+
$this->assertContainsEquals(new ChoiceView('eng', 'eng', 'English'), $choices);
66+
$this->assertContainsEquals(new ChoiceView('fra', 'fra', 'French'), $choices);
67+
// Burmese has no three letter language code
68+
$this->assertNotContainsEquals(new ChoiceView('my', 'my', 'Burmese'), $choices);
69+
}
70+
71+
/**
72+
* @requires extension intl
73+
*/
74+
public function testChoiceTranslationLocaleAndAlpha3Option()
75+
{
76+
$choices = $this->factory
77+
->create(static::TESTED_TYPE, null, [
78+
'choice_translation_locale' => 'uk',
79+
'alpha3' => true,
80+
])
81+
->createView()->vars['choices'];
82+
83+
// Don't check objects for identity
84+
$this->assertContainsEquals(new ChoiceView('eng', 'eng', 'англійська'), $choices);
85+
$this->assertContainsEquals(new ChoiceView('fra', 'fra', 'французька'), $choices);
86+
// Burmese has no three letter language code
87+
$this->assertNotContainsEquals(new ChoiceView('my', 'my', 'бірманська'), $choices);
88+
}
89+
5690
public function testMultipleLanguagesIsNotIncluded()
5791
{
5892
$choices = $this->factory->create(static::TESTED_TYPE, 'language')

src/Symfony/Component/Form/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"require": {
1919
"php": "^7.1.3",
2020
"symfony/event-dispatcher": "^4.3",
21-
"symfony/intl": "^4.3|^5.0",
21+
"symfony/intl": "^4.4|^5.0",
2222
"symfony/options-resolver": "~4.3|^5.0",
2323
"symfony/polyfill-ctype": "~1.8",
2424
"symfony/polyfill-mbstring": "~1.0",

0 commit comments

Comments
 (0)
0