8000 Add choice_translation_locale option for Intl choice types · symfony/symfony@82381e4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 82381e4

Browse files
committed
Add choice_translation_locale option for Intl choice types
1 parent f77c1d0 commit 82381e4

File tree

12 files changed

+379
-4
lines changed

12 files changed

+379
-4
lines changed

UPGRADE-4.2.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
UPGRADE FROM 4.1 to 4.2
2+
=======================
3+
4+
Form
5+
----
6+
7+
* Deprecated the`ChoiceLoaderInterface` implementation in `CountryType`, `LanguageType`, `LocaleType` and `CurrencyType`, use the `choice_loader` option instead.
8+
9+
Before:
10+
```php
11+
class MyCountryType extends CountryType
12+
{
13+
public function loadChoiceList()
14+
{
15+
// override the method
16+
}
17+
}
18+
```
19+
20+
After:
21+
```php
22+
class MyCountryType extends AbstractType
23+
{
24+
public function getParent()
25+
{
26+
return CountryType::class;
27+
}
28+
29+
public function configureOptions(OptionsResolver $resolver)
30+
{
31+
$resolver->setDefault('choice_loader', ...); // override the option instead
32+
}
33+
}
34+
```

src/Symfony/Component/Form/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
CHANGELOG
22
=========
33

4+
4.2.0
5+
-----
6+
7+
* added `choice_translation_locale` option to `CountryType`, `LanguageType`, `LocaleType` and `CurrencyType`
8+
* deprecated `ChoiceLoaderInterface` implementation in `CountryType`, `LanguageType`, `LocaleType` and `CurrencyType`
9+
410
4.1.0
511
-----
612

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Form\ChoiceList\Loader;
13+
14+
/**
15+
* Callback choice loader optimized for Intl choice types.
16+
*
17+
* @author Jules Pietri <jules@heahprod.com>
18+
* @author Yonel Ceruto <yonelceruto@gmail.com>
19+
*/
20+
class IntlCallbackChoiceLoader extends CallbackChoiceLoader
21+
{
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public function loadChoicesForValues(array $values, $value = null)
26+
{
27+
// Optimize
28+
$values = array_filter($values);
29+
if (empty($values)) {
30+
return array();
31+
}
32+
33+
// If no callable is set, values are the same as choices
34+
if (null === $value) {
35+
return $values;
36+
}
37+
38+
return $this->loadChoiceList($value)->getChoicesForValues($values);
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
public function loadValuesForChoices(array $choices, $value = null)
45+
{
46+
// Optimize
47+
$choices = array_filter($choices);
48+
if (empty($choices)) {
49+
return array();
50+
}
51+
52+
// If no callable is set, choices are the same as values
53+
if (null === $value) {
54+
return $choices;
55+
}
56+
57+
return $this->loadChoiceList($value)->getValuesForChoices($choices);
58+
}
59+
}

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
use Symfony\Component\Form\AbstractType;
1515
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
1616
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
17+
use Symfony\Component\Form\ChoiceList\Loader\IntlCallbackChoiceLoader;
1718
use Symfony\Component\Intl\Intl;
19+
use Symfony\Component\OptionsResolver\Options;
1820
use Symfony\Component\OptionsResolver\OptionsResolver;
1921

2022
class CountryType extends AbstractType implements ChoiceLoaderInterface
@@ -27,6 +29,8 @@ class CountryType extends AbstractType implements ChoiceLoaderInterface
2729
* {@link \Symfony\Component\Intl\Intl::getRegionBundle()}.
2830
*
2931
* @var ArrayChoiceList
32+
*
33+
* @deprecated since Symfony 4.2
3034
*/
3135
private $choiceList;
3236

@@ -36,9 +40,18 @@ class CountryType extends AbstractType implements ChoiceLoaderInterface
3640
public function configureOptions(OptionsResolver $resolver)
3741
{
3842
$resolver->setDefaults(array(
39-
'choice_loader' => $this,
43+
'choice_loader' => function (Options $options) {
44+
$choiceTranslationLocale = $options['choice_translation_locale'];
45+
46+
return new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale) {
47+
return array_flip(Intl::getRegionBundle()->getCountryNames($choiceTranslationLocale));
48+
});
49+
},
4050
'choice_translation_domain' => false,
51+
'choice_translation_locale' => null,
4152
));
53+
54+
$resolver->setAllowedTypes('choice_translation_locale', array('null', 'string'));
4255
}
4356

4457
/**
@@ -59,9 +72,13 @@ public function getBlockPrefix()
5972

6073
/**
6174
* {@inheritdoc}
75+
*
76+
* @deprecated since Symfony 4.2
6277
*/
6378
public function loadChoiceList($value = null)
6479
{
80+
@trigger_error(sprintf('Method "%s" is deprecated since Symfony 4.2, use "choice_loader" option instead.', __METHOD__), E_USER_DEPRECATED);
81+
6582
if (null !== $this->choiceList) {
6683
return $this->choiceList;
6784
}
@@ -71,9 +88,13 @@ public function loadChoiceList($value = null)
7188

7289
/**
7390
* {@inheritdoc}
91+
*
92+
* @deprecated since Symfony 4.2
7493
*/
7594
public function loadChoicesForValues(array $values, $value = null)
7695
{
96+
@trigger_error(sprintf('Method "%s" is deprecated since Symfony 4.2, use "choice_l 1241 oader" option instead.', __METHOD__), E_USER_DEPRECATED);
97+
7798
// Optimize
7899
$values = array_filter($values);
79100
if (empty($values)) {
@@ -90,9 +111,13 @@ public function loadChoicesForValues(array $values, $value = null)
90111

91112
/**
92113
* {@inheritdoc}
114+
*
115+
* @deprecated since Symfony 4.2
93116
*/
94117
public function loadValuesForChoices(array $choices, $value = null)
95118
{
119+
@trigger_error(sprintf('Method "%s" is deprecated since Symfony 4.2, use "choice_loader" option instead.', __METHOD__), E_USER_DEPRECATED);
120+
96121
// Optimize
97122
$choices = array_filter($choices);
98123
if (empty($choices)) {

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
use Symfony\Component\Form\AbstractType;
1515
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
1616
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
17+
use Symfony\Component\Form\ChoiceList\Loader\IntlCallbackChoiceLoader;
1718
use Symfony\Component\Intl\Intl;
19+
use Symfony\Component\OptionsResolver\Options;
1820
use Symfony\Component\OptionsResolver\OptionsResolver;
1921

2022
class CurrencyType extends AbstractType implements ChoiceLoaderInterface
@@ -27,6 +29,8 @@ class CurrencyType extends AbstractType implements ChoiceLoaderInterface
2729
* {@link \Symfony\Component\Intl\Intl::getCurrencyBundle()}.
2830
*
2931
* @var ArrayChoiceList
32+
*
33+
* @deprecated since Symfony 4.2
3034
*/
3135
private $choiceList;
3236

@@ -36,9 +40,18 @@ class CurrencyType extends AbstractType implements ChoiceLoaderInterface
3640
public function configureOptions(OptionsResolver $resolver)
3741
{
3842
$resolver->setDefaults(array(
39-
'choice_loader' => $this,
43+
'choice_loader' => function (Options $options) {
44+
$choiceTranslationLocale = $options['choice_translation_locale'];
45+
46+
return new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale) {
47+
return array_flip(Intl::getCurrencyBundle()->getCurrencyNames($choiceTranslationLocale));
48+
});
49+
},
4050
'choice_translation_domain' => false,
51+
'choice_translation_locale' => null,
4152
));
53+
54+
$resolver->setAllowedTypes('choice_translation_locale', array('null', 'string'));
4255
}
4356

4457
/**
@@ -59,9 +72,13 @@ public function getBlockPrefix()
5972

6073
/**
6174
* {@inheritdoc}
75+
*
76+
* @deprecated since Symfony 4.2
6277
*/
6378
public function loadChoiceList($value = null)
6479
{
80+
@trigger_error(sprintf('Method "%s" is deprecated since Symfony 4.2, use "choice_loader" option instead.', __METHOD__), E_USER_DEPRECATED);
81+
6582
if (null !== $this->choiceList) {
6683
return $this->choiceList;
6784
}
@@ -71,9 +88,13 @@ public function loadChoiceList($value = null)
7188

7289
/**
7390
* {@inheritdoc}
91+
*
92+
* @deprecated since Symfony 4.2
7493
*/
7594
public function loadChoicesForValues(array $values, $value = null)
7695
{
96+
@trigger_error(sprintf('Method "%s" is deprecated since Symfony 4.2, use "choice_loader" option instead.', __METHOD__), E_USER_DEPRECATED);
97+
7798
// Optimize
7899
$values = array_filter($values);
79100
if (empty($values)) {
@@ -90,9 +111,13 @@ public function loadChoicesForValues(array $values, $value = null)
90111

91112
/**
92113
* {@inheritdoc}
114+
*
115+
* @deprecated since Symfony 4.2
93116
*/
94117
public function loadValuesForChoices(array $choices, $value = null)
95118
{
119+
@trigger_error(sprintf('Method "%s" is deprecated since Symfony 4.2, use "choice_loader" option instead.', __METHOD__), E_USER_DEPRECATED);
120+
96121
// Optimize
97122
$choices = array_filter($choices);
98123
if (empty($choices)) {

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
use Symfony\Component\Form\AbstractType;
1515
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
1616
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
17+
use Symfony\Component\Form\ChoiceList\Loader\IntlCallbackChoiceLoader;
1718
use Symfony\Component\Intl\Intl;
19+
use Symfony\Component\OptionsResolver\Options;
1820
use Symfony\Component\OptionsResolver\OptionsResolver;
1921

2022
class LanguageType extends AbstractType implements ChoiceLoaderInterface
@@ -27,6 +29,8 @@ class LanguageType extends AbstractType implements ChoiceLoaderInterface
2729
* {@link \Symfony\Component\Intl\Intl::getLanguageBundle()}.
2830
*
2931
* @var ArrayChoiceList
32+
*
33+
* @deprecated since Symfony 4.2
3034
*/
3135
private $choiceList;
3236

@@ -36,9 +40,18 @@ class LanguageType extends AbstractType implements ChoiceLoaderInterface
3640
public function configureOptions(OptionsResolver $resolver)
3741
{
3842
$resolver->setDefaults(array(
39-
'choice_loader' => $this,
43+
'choice_loader' => function (Options $options) {
44+
$choiceTranslationLocale = $options['choice_translation_locale'];
45+
46+
return new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale) {
47+
return array_flip(Intl::getLanguageBundle()->getLanguageNames($choiceTranslationLocale));
48+
});
49+
},
4050
'choice_translation_domain' => false,
51+
'choice_translation_locale' => null,
4152
));
53+
54+
$resolver->setAllowedTypes('choice_translation_locale', array('null', 'string'));
4255
}
4356

4457
/**
@@ -59,9 +72,13 @@ public function getBlockPrefix()
5972

6073
/**
6174
* {@inheritdoc}
75+
*
76+
* @deprecated since Symfony 4.2
6277
*/
6378
public function loadChoiceList($value = null)
6479
{
80+
@trigger_error(sprintf('Method "%s" is deprecated since Symfony 4.2, use "choice_loader" option instead.', __METHOD__), E_USER_DEPRECATED);
81+
6582
if (null !== $this->choiceList) {
6683
return $this->choiceList;
6784
}
@@ -71,9 +88,13 @@ public function loadChoiceList($value = null)
7188

7289
/**
7390
* {@inheritdoc}
91+
*
92+
* @deprecated since Symfony 4.2
7493
*/
7594
public function loadChoicesForValues(array $values, $value = null)
7695
{
96+
@trigger_error(sprintf('Method "%s" is deprecated since Symfony 4.2, use "choice_loader" option instead.', __METHOD__), E_USER_DEPRECATED);
97+
7798
// Optimize
7899
$values = array_filter($values);
79100
if (empty($values)) {
@@ -90,9 +111,13 @@ public function loadChoicesForValues(array $values, $value = null)
90111

91112
/**
92113
* {@inheritdoc}
114+
*
115+
* @deprecated since Symfony 4.2
93116
*/
94117
public function loadValuesForChoices(array $choices, $value = null)
95118
{
119+
@trigger_error(sprintf('Method "%s" is deprecated since Symfony 4.2, use "choice_loader" option instead.', __METHOD__), E_USER_DEPRECATED);
120+
96121
// Optimize
97122
$choices = array_filter($choices);
98123
if (empty($choices)) {

0 commit comments

Comments
 (0)
0