Add choice_translation_locale option for Intl choice types

This commit is contained in:
Yonel Ceruto 2018-04-05 16:33:25 -04:00
parent f77c1d0d09
commit e250dfa702
12 changed files with 379 additions and 4 deletions

34
UPGRADE-4.2.md Normal file
View File

@ -0,0 +1,34 @@
UPGRADE FROM 4.1 to 4.2
=======================
Form
----
* Deprecated the `ChoiceLoaderInterface` implementation in `CountryType`, `LanguageType`, `LocaleType` and `CurrencyType`, use the `choice_loader` option instead.
Before:
```php
class MyCountryType extends CountryType
{
public function loadChoiceList()
{
// override the method
}
}
```
After:
```php
class MyCountryType extends AbstractType
{
public function getParent()
{
return CountryType::class;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefault('choice_loader', ...); // override the option instead
}
}
```

View File

@ -1,6 +1,12 @@
CHANGELOG
=========
4.2.0
-----
* added `choice_translation_locale` option to `CountryType`, `LanguageType`, `LocaleType` and `CurrencyType`
* deprecated the `ChoiceLoaderInterface` implementation in `CountryType`, `LanguageType`, `LocaleType` and `CurrencyType`
4.1.0
-----

View File

@ -0,0 +1,59 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Form\ChoiceList\Loader;
/**
* Callback choice loader optimized for Intl choice types.
*
* @author Jules Pietri <jules@heahprod.com>
* @author Yonel Ceruto <yonelceruto@gmail.com>
*/
class IntlCallbackChoiceLoader extends CallbackChoiceLoader
{
/**
* {@inheritdoc}
*/
public function loadChoicesForValues(array $values, $value = null)
{
// Optimize
$values = array_filter($values);
if (empty($values)) {
return array();
}
// If no callable is set, values are the same as choices
if (null === $value) {
return $values;
}
return $this->loadChoiceList($value)->getChoicesForValues($values);
}
/**
* {@inheritdoc}
*/
public function loadValuesForChoices(array $choices, $value = null)
{
// Optimize
$choices = array_filter($choices);
if (empty($choices)) {
return array();
}
// If no callable is set, choices are the same as values
if (null === $value) {
return $choices;
}
return $this->loadChoiceList($value)->getValuesForChoices($choices);
}
}

View File

@ -14,7 +14,9 @@ namespace Symfony\Component\Form\Extension\Core\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
use Symfony\Component\Form\ChoiceList\Loader\IntlCallbackChoiceLoader;
use Symfony\Component\Intl\Intl;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
class CountryType extends AbstractType implements ChoiceLoaderInterface
@ -27,6 +29,8 @@ class CountryType extends AbstractType implements ChoiceLoaderInterface
* {@link \Symfony\Component\Intl\Intl::getRegionBundle()}.
*
* @var ArrayChoiceList
*
* @deprecated since Symfony 4.2
*/
private $choiceList;
@ -36,9 +40,18 @@ class CountryType extends AbstractType implements ChoiceLoaderInterface
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'choice_loader' => $this,
'choice_loader' => function (Options $options) {
$choiceTranslationLocale = $options['choice_translation_locale'];
return new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale) {
return array_flip(Intl::getRegionBundle()->getCountryNames($choiceTranslationLocale));
});
},
'choice_translation_domain' => false,
'choice_translation_locale' => null,
));
$resolver->setAllowedTypes('choice_translation_locale', array('null', 'string'));
}
/**
@ -59,9 +72,13 @@ class CountryType extends AbstractType implements ChoiceLoaderInterface
/**
* {@inheritdoc}
*
* @deprecated since Symfony 4.2
*/
public function loadChoiceList($value = null)
{
@trigger_error(sprintf('Method "%s" is deprecated since Symfony 4.2, use "choice_loader" option instead.', __METHOD__), E_USER_DEPRECATED);
if (null !== $this->choiceList) {
return $this->choiceList;
}
@ -71,9 +88,13 @@ class CountryType extends AbstractType implements ChoiceLoaderInterface
/**
* {@inheritdoc}
*
* @deprecated since Symfony 4.2
*/
public function loadChoicesForValues(array $values, $value = null)
{
@trigger_error(sprintf('Method "%s" is deprecated since Symfony 4.2, use "choice_loader" option instead.', __METHOD__), E_USER_DEPRECATED);
// Optimize
$values = array_filter($values);
if (empty($values)) {
@ -90,9 +111,13 @@ class CountryType extends AbstractType implements ChoiceLoaderInterface
/**
* {@inheritdoc}
*
* @deprecated since Symfony 4.2
*/
public function loadValuesForChoices(array $choices, $value = null)
{
@trigger_error(sprintf('Method "%s" is deprecated since Symfony 4.2, use "choice_loader" option instead.', __METHOD__), E_USER_DEPRECATED);
// Optimize
$choices = array_filter($choices);
if (empty($choices)) {

View File

@ -14,7 +14,9 @@ namespace Symfony\Component\Form\Extension\Core\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
use Symfony\Component\Form\ChoiceList\Loader\IntlCallbackChoiceLoader;
use Symfony\Component\Intl\Intl;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
class CurrencyType extends AbstractType implements ChoiceLoaderInterface
@ -27,6 +29,8 @@ class CurrencyType extends AbstractType implements ChoiceLoaderInterface
* {@link \Symfony\Component\Intl\Intl::getCurrencyBundle()}.
*
* @var ArrayChoiceList
*
* @deprecated since Symfony 4.2
*/
private $choiceList;
@ -36,9 +40,18 @@ class CurrencyType extends AbstractType implements ChoiceLoaderInterface
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'choice_loader' => $this,
'choice_loader' => function (Options $options) {
$choiceTranslationLocale = $options['choice_translation_locale'];
return new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale) {
return array_flip(Intl::getCurrencyBundle()->getCurrencyNames($choiceTranslationLocale));
});
},
'choice_translation_domain' => false,
'choice_translation_locale' => null,
));
$resolver->setAllowedTypes('choice_translation_locale', array('null', 'string'));
}
/**
@ -59,9 +72,13 @@ class CurrencyType extends AbstractType implements ChoiceLoaderInterface
/**
* {@inheritdoc}
*
* @deprecated since Symfony 4.2
*/
public function loadChoiceList($value = null)
{
@trigger_error(sprintf('Method "%s" is deprecated since Symfony 4.2, use "choice_loader" option instead.', __METHOD__), E_USER_DEPRECATED);
if (null !== $this->choiceList) {
return $this->choiceList;
}
@ -71,9 +88,13 @@ class CurrencyType extends AbstractType implements ChoiceLoaderInterface
/**
* {@inheritdoc}
*
* @deprecated since Symfony 4.2
*/
public function loadChoicesForValues(array $values, $value = null)
{
@trigger_error(sprintf('Method "%s" is deprecated since Symfony 4.2, use "choice_loader" option instead.', __METHOD__), E_USER_DEPRECATED);
// Optimize
$values = array_filter($values);
if (empty($values)) {
@ -90,9 +111,13 @@ class CurrencyType extends AbstractType implements ChoiceLoaderInterface
/**
* {@inheritdoc}
*
* @deprecated since Symfony 4.2
*/
public function loadValuesForChoices(array $choices, $value = null)
{
@trigger_error(sprintf('Method "%s" is deprecated since Symfony 4.2, use "choice_loader" option instead.', __METHOD__), E_USER_DEPRECATED);
// Optimize
$choices = array_filter($choices);
if (empty($choices)) {

View File

@ -14,7 +14,9 @@ namespace Symfony\Component\Form\Extension\Core\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
use Symfony\Component\Form\ChoiceList\Loader\IntlCallbackChoiceLoader;
use Symfony\Component\Intl\Intl;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
class LanguageType extends AbstractType implements ChoiceLoaderInterface
@ -27,6 +29,8 @@ class LanguageType extends AbstractType implements ChoiceLoaderInterface
* {@link \Symfony\Component\Intl\Intl::getLanguageBundle()}.
*
* @var ArrayChoiceList
*
* @deprecated since Symfony 4.2
*/
private $choiceList;
@ -36,9 +40,18 @@ class LanguageType extends AbstractType implements ChoiceLoaderInterface
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'choice_loader' => $this,
'choice_loader' => function (Options $options) {
$choiceTranslationLocale = $options['choice_translation_locale'];
return new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale) {
return array_flip(Intl::getLanguageBundle()->getLanguageNames($choiceTranslationLocale));
});
},
'choice_translation_domain' => false,
'choice_translation_locale' => null,
));
$resolver->setAllowedTypes('choice_translation_locale', array('null', 'string'));
}
/**
@ -59,9 +72,13 @@ class LanguageType extends AbstractType implements ChoiceLoaderInterface
/**
* {@inheritdoc}
*
* @deprecated since Symfony 4.2
*/
public function loadChoiceList($value = null)
{
@trigger_error(sprintf('Method "%s" is deprecated since Symfony 4.2, use "choice_loader" option instead.', __METHOD__), E_USER_DEPRECATED);
if (null !== $this->choiceList) {
return $this->choiceList;
}
@ -71,9 +88,13 @@ class LanguageType extends AbstractType implements ChoiceLoaderInterface
/**
* {@inheritdoc}
*
* @deprecated since Symfony 4.2
*/
public function loadChoicesForValues(array $values, $value = null)
{
@trigger_error(sprintf('Method "%s" is deprecated since Symfony 4.2, use "choice_loader" option instead.', __METHOD__), E_USER_DEPRECATED);
// Optimize
$values = array_filter($values);
if (empty($values)) {
@ -90,9 +111,13 @@ class LanguageType extends AbstractType implements ChoiceLoaderInterface
/**
* {@inheritdoc}
*
* @deprecated since Symfony 4.2
*/
public function loadValuesForChoices(array $choices, $value = null)
{
@trigger_error(sprintf('Method "%s" is deprecated since Symfony 4.2, use "choice_loader" option instead.', __METHOD__), E_USER_DEPRECATED);
// Optimize
$choices = array_filter($choices);
if (empty($choices)) {

View File

@ -14,7 +14,9 @@ namespace Symfony\Component\Form\Extension\Core\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
use Symfony\Component\Form\ChoiceList\Loader\IntlCallbackChoiceLoader;
use Symfony\Component\Intl\Intl;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
class LocaleType extends AbstractType implements ChoiceLoaderInterface
@ -27,6 +29,8 @@ class LocaleType extends AbstractType implements ChoiceLoaderInterface
* {@link \Symfony\Component\Intl\Intl::getLocaleBundle()}.
*
* @var ArrayChoiceList
*
* @deprecated since Symfony 4.2
*/
private $choiceList;
@ -36,9 +40,18 @@ class LocaleType extends AbstractType implements ChoiceLoaderInterface
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'choice_loader' => $this,
'choice_loader' => function (Options $options) {
$choiceTranslationLocale = $options['choice_translation_locale'];
return new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale) {
return array_flip(Intl::getLocaleBundle()->getLocaleNames($choiceTranslationLocale));
});
},
'choice_translation_domain' => false,
'choice_translation_locale' => null,
));
$resolver->setAllowedTypes('choice_translation_locale', array('null', 'string'));
}
/**
@ -59,9 +72,13 @@ class LocaleType extends AbstractType implements ChoiceLoaderInterface
/**
* {@inheritdoc}
*
* @deprecated since Symfony 4.2
*/
public function loadChoiceList($value = null)
{
@trigger_error(sprintf('Method "%s" is deprecated since Symfony 4.2, use "choice_loader" option instead.', __METHOD__), E_USER_DEPRECATED);
if (null !== $this->choiceList) {
return $this->choiceList;
}
@ -71,9 +88,13 @@ class LocaleType extends AbstractType implements ChoiceLoaderInterface
/**
* {@inheritdoc}
*
* @deprecated since Symfony 4.2
*/
public function loadChoicesForValues(array $values, $value = null)
{
@trigger_error(sprintf('Method "%s" is deprecated since Symfony 4.2, use "choice_loader" option instead.', __METHOD__), E_USER_DEPRECATED);
// Optimize
$values = array_filter($values);
if (empty($values)) {
@ -90,9 +111,13 @@ class LocaleType extends AbstractType implements ChoiceLoaderInterface
/**
* {@inheritdoc}
*
* @deprecated since Symfony 4.2
*/
public function loadValuesForChoices(array $choices, $value = null)
{
@trigger_error(sprintf('Method "%s" is deprecated since Symfony 4.2, use "choice_loader" option instead.', __METHOD__), E_USER_DEPRECATED);
// Optimize
$choices = array_filter($choices);
if (empty($choices)) {

View File

@ -0,0 +1,104 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Form\Tests\ChoiceList\Loader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
use Symfony\Component\Form\ChoiceList\LazyChoiceList;
use Symfony\Component\Form\ChoiceList\Loader\IntlCallbackChoiceLoader;
/**
* @author Jules Pietri <jules@heahprod.com>
* @author Yonel Ceruto <yonelceruto@gmail.com>
*/
class IntlCallbackChoiceLoaderTest extends TestCase
{
/**
* @var \Symfony\Component\Form\ChoiceList\Loader\IntlCallbackChoiceLoader
*/
private static $loader;
/**
* @var callable
*/
private static $value;
/**
* @var array
*/
private static $choices;
/**
* @var string[]
*/
private static $choiceValues;
/**
* @var \Symfony\Component\Form\ChoiceList\LazyChoiceList
*/
private static $lazyChoiceList;
public static function setUpBeforeClass()
{
self::$loader = new IntlCallbackChoiceLoader(function () {
return self::$choices;
});
self::$value = function ($choice) {
return $choice->value ?? null;
};
self::$choices = array(
(object) array('value' => 'choice_one'),
(object) array('value' => 'choice_two'),
);
self::$choiceValues = array('choice_one', 'choice_two');
self::$lazyChoiceList = new LazyChoiceList(self::$loader, self::$value);
}
public function testLoadChoiceList()
{
$this->assertInstanceOf(ChoiceListInterface::class, self::$loader->loadChoiceList(self::$value));
}
public function testLoadChoiceListOnlyOnce()
{
$loadedChoiceList = self::$loader->loadChoiceList(self::$value);
$this->assertSame($loadedChoiceList, self::$loader->loadChoiceList(self::$value));
}
public function testLoadChoicesForValuesLoadsChoiceListOnFirstCall()
{
$this->assertSame(
self::$loader->loadChoicesForValues(self::$choiceValues, self::$value),
self::$lazyChoiceList->getChoicesForValues(self::$choiceValues),
'Choice list should not be reloaded.'
);
}
public function testLoadValuesForChoicesLoadsChoiceListOnFirstCall()
{
$this->assertSame(
self::$loader->loadValuesForChoices(self::$choices, self::$value),
self::$lazyChoiceList->getValuesForChoices(self::$choices),
'Choice list should not be reloaded.'
);
}
public static function tearDownAfterClass()
{
self::$loader = null;
self::$value = null;
self::$choices = array();
self::$choiceValues = array();
self::$lazyChoiceList = null;
}
}

View File

@ -38,6 +38,25 @@ class CountryTypeTest extends BaseTypeTest
$this->assertContains(new ChoiceView('MY', 'MY', 'Malaysia'), $choices, '', false, false);
}
/**
* @requires extension intl
*/
public function testChoiceTranslationLocaleOption()
{
$choices = $this->factory
->create(static::TESTED_TYPE, null, array(
'choice_translation_locale' => 'uk',
))
->createView()->vars['choices'];
// Don't check objects for identity
$this->assertContains(new ChoiceView('DE', 'DE', 'Німеччина'), $choices, '', false, false);
$this->assertContains(new ChoiceView('GB', 'GB', 'Велика Британія'), $choices, '', false, false);
$this->assertContains(new ChoiceView('US', 'US', 'Сполучені Штати'), $choices, '', false, false);
$this->assertContains(new ChoiceView('FR', 'FR', 'Франція'), $choices, '', false, false);
$this->assertContains(new ChoiceView('MY', 'MY', 'Малайзія'), $choices, '', false, false);
}
public function testUnknownCountryIsNotIncluded()
{
$choices = $this->factory->create(static::TESTED_TYPE, 'country')

View File

@ -35,6 +35,23 @@ class CurrencyTypeTest extends BaseTypeTest
$this->assertContains(new ChoiceView('SIT', 'SIT', 'Slovenian Tolar'), $choices, '', false, false);
}
/**
* @requires extension intl
*/
public function testChoiceTranslationLocaleOption()
{
$choices = $this->factory
->create(static::TESTED_TYPE, null, array(
'choice_translation_locale' => 'uk',
))
->createView()->vars['choices'];
// Don't check objects for identity
$this->assertContains(new ChoiceView('EUR', 'EUR', 'євро'), $choices, '', false, false);
$this->assertContains(new ChoiceView('USD', 'USD', 'долар США'), $choices, '', false, false);
$this->assertContains(new ChoiceView('SIT', 'SIT', 'словенський толар'), $choices, '', false, false);
}
public function testSubmitNull($expected = null, $norm = null, $view = null)
{
parent::testSubmitNull($expected, $norm, '');

View File

@ -37,6 +37,25 @@ class LanguageTypeTest extends BaseTypeTest
$this->assertContains(new ChoiceView('my', 'my', 'Burmese'), $choices, '', false, false);
}
/**
* @requires extension intl
*/
public function testChoiceTranslationLocaleOption()
{
$choices = $this->factory
->create(static::TESTED_TYPE, null, array(
'choice_translation_locale' => 'uk',
))
->createView()->vars['choices'];
// Don't check objects for identity
$this->assertContains(new ChoiceView('en', 'en', 'англійська'), $choices, '', false, false);
$this->assertContains(new ChoiceView('en_GB', 'en_GB', 'British English'), $choices, '', false, false);
$this->assertContains(new ChoiceView('en_US', 'en_US', 'англійська (США)'), $choices, '', false, false);
$this->assertContains(new ChoiceView('fr', 'fr', 'французька'), $choices, '', false, false);
$this->assertContains(new ChoiceView('my', 'my', 'бірманська'), $choices, '', false, false);
}
public function testMultipleLanguagesIsNotIncluded()
{
$choices = $this->factory->create(static::TESTED_TYPE, 'language')

View File

@ -35,6 +35,23 @@ class LocaleTypeTest extends BaseTypeTest
$this->assertContains(new ChoiceView('zh_Hant_MO', 'zh_Hant_MO', 'Chinese (Traditional, Macau SAR China)'), $choices, '', false, false);
}
/**
* @requires extension intl
*/
public function testChoiceTranslationLocaleOption()
{
$choices = $this->factory
->create(static::TESTED_TYPE, null, array(
'choice_translation_locale' => 'uk',
))
->createView()->vars['choices'];
// Don't check objects for identity
$this->assertContains(new ChoiceView('en', 'en', 'англійська'), $choices, '', false, false);
$this->assertContains(new ChoiceView('en_GB', 'en_GB', 'англійська (Велика Британія)'), $choices, '', false, false);
$this->assertContains(new ChoiceView('zh_Hant_MO', 'zh_Hant_MO', 'китайська (традиційна, Макао, О.А.Р Китаю)'), $choices, '', false, false);
}
public function testSubmitNull($expected = null, $norm = null, $view = null)
{
parent::testSubmitNull($expected, $norm, '');