Skip to content

[WIP][Form] Allow to configure selectable locale and countries #28542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Allow usage of closures for filtering
  • Loading branch information
sstok committed Sep 23, 2018
commit 64e63f0a1b51505ce1b0e036167e014556ac188b
18 changes: 15 additions & 3 deletions src/Symfony/Component/Form/Extension/Core/ArrayInclusionFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,26 @@
*/
final class ArrayInclusionFilter
{
private $filterCallable;
private $acceptedKeys;

public function __construct(array $acceptedKeys)
public function __construct($filter)
{
$this->acceptedKeys = array_fill_keys($acceptedKeys, true);
if (\is_array($filter)) {
$this->acceptedKeys = array_fill_keys($filter, true);
$this->filterCallable = $this;
} else {
$this->acceptedKeys = [];
$this->filterCallable = $filter;
}
}

public function __invoke(string $k)
public function filter(array $choices)
{
return array_filter($choices, $this->filterCallable, ARRAY_FILTER_USE_BOTH);
}

public function __invoke($v, $k)
{
return isset($this->acceptedKeys[$k]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ public function configureOptions(OptionsResolver $resolver)
$countries = Intl::getRegionBundle()->getCountryNames($choiceTranslationLocale);

if (null !== $supportedCountries) {
$supportedCountries = array_map('strtoupper', $supportedCountries);
$countries = array_filter($countries, new ArrayInclusionFilter($supportedCountries), ARRAY_FILTER_USE_KEY);
if (is_array($supportedCountries)) {
$supportedCountries = array_map('strtoupper', $supportedCountries);
}

$countries = (new ArrayInclusionFilter($supportedCountries))->filter($countries);
}

return array_flip($countries);
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Form/Extension/Core/Type/LocaleType.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function configureOptions(OptionsResolver $resolver)
$locales = Intl::getLocaleBundle()->getLocaleNames($choiceTranslationLocale);

if (null !== $supportedLocales) {
$locales = array_filter($locales, new ArrayInclusionFilter($supportedLocales), ARRAY_FILTER_USE_KEY);
$locales = (new ArrayInclusionFilter($supportedLocales))->filter($locales);
}

return array_flip($locales);
Expand All @@ -61,7 +61,7 @@ public function configureOptions(OptionsResolver $resolver)
));

$resolver->setAllowedTypes('choice_translation_locale', array('null', 'string'));
$resolver->setAllowedTypes('supported_locales', array('null', 'string[]'));
$resolver->setAllowedTypes('supported_locales', array('null', 'string[]', 'Closure'));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,27 @@ public function testLocalesAreSelectable()
$this->assertContains(new ChoiceView('zh_Hant_MO', 'zh_Hant_MO', 'Chinese (Traditional, Macau SAR China)'), $choices, '', false, false);
}

public function testSupportedLocalesAreConfigurable()
public function testSupportedLocalesAreConfigurableByArray()
{
$choices = $this->factory->create(static::TESTED_TYPE, null, array('supported_locales' => array('en', 'zh_Hant_MO')))
->createView()->vars['choices'];

$this->assertContains(new ChoiceView('en', 'en', 'English'), $choices, '', false, false);
$this->assertContains(new ChoiceView('zh_Hant_MO', 'zh_Hant_MO', 'Chinese (Traditional, Macau SAR China)'), $choices, '', false, false);
$this->assertNotContains(new ChoiceView('en_GB', 'en_GB', 'English (United Kingdom)'), $choices, '', false, false);
$this->assertNotContains(new ChoiceView('af_ZA', 'af_ZA', 'Afrikaans (South Africa)'), $choices, '', false, false);
}

public function testSupportedLocalesAreConfigurableByClosure()
{
$choices = $this->factory->create(static::TESTED_TYPE, null, array('supported_locales' => function ($v, $k) {
return $k === 'zh_Hant_MO' || strpos($k, 'en') === 0;
}))->createView()->vars['choices'];

$this->assertContains(new ChoiceView('en', 'en', 'English'), $choices, '', false, false);
$this->assertContains(new ChoiceView('zh_Hant_MO', 'zh_Hant_MO', 'Chinese (Traditional, Macau SAR China)'), $choices, '', false, false);
$this->assertContains(new ChoiceView('en_GB', 'en_GB', 'English (United Kingdom)'), $choices, '', false, false);
$this->assertNotContains(new ChoiceView('af_ZA', 'af_ZA', 'Afrikaans (South Africa)'), $choices, '', false, false);
}

/**
Expand Down