Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b26715f

Browse filesBrowse files
committed
Add choice_translation_locale option for Intl choice types
1 parent f77c1d0 commit b26715f
Copy full SHA for b26715f

File tree

5 files changed

+197
-151
lines changed
Filter options

5 files changed

+197
-151
lines changed
+58Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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 Yonel Ceruto <yonelceruto@gmail.com>
18+
*/
19+
class IntlCallbackChoiceLoader extends CallbackChoiceLoader
20+
{
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function loadChoicesForValues(array $values, $value = null)
25+
{
26+
// Optimize
27+
$values = array_filter($values);
28+
if (empty($values)) {
29+
return array();
30+
}
31+
32+
// If no callable is set, values are the same as choices
33+
if (null === $value) {
34+
return $values;
35+
}
36+
37+
return $this->loadChoiceList($value)->getChoicesForValues($values);
38+
}
39+
40+
/**
41+
* {@inheritdoc}
42+
*/
43+
public function loadValuesForChoices(array $choices, $value = null)
44+
{
45+
// Optimize
46+
$choices = array_filter($choices);
47+
if (empty($choices)) {
48+
return array();
49+
}
50+
51+
// If no callable is set, choices are the same as values
52+
if (null === $value) {
53+
return $choices;
54+
}
55+
56+
return $this->loadChoiceList($value)->getValuesForChoices($choices);
57+
}
58+
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/Type/CountryType.php
+34-37Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,44 @@
1212
namespace Symfony\Component\Form\Extension\Core\Type;
1313

1414
use Symfony\Component\Form\AbstractType;
15-
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
1615
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
16+
use Symfony\Component\Form\ChoiceList\Loader\IntlCallbackChoiceLoader;
1717
use Symfony\Component\Intl\Intl;
18+
use Symfony\Component\OptionsResolver\Options;
1819
use Symfony\Component\OptionsResolver\OptionsResolver;
1920

2021
class CountryType extends AbstractType implements ChoiceLoaderInterface
2122
{
2223
/**
23-
* Country loaded choice list.
24+
* BC layer.
2425
*
25-
* The choices are lazy loaded and generated from the Intl component.
26-
*
27-
* {@link \Symfony\Component\Intl\Intl::getRegionBundle()}.
28-
*
29-
* @var ArrayChoiceList
26+
* @var ChoiceLoaderInterface
3027
*/
31-
private $choiceList;
28+
private $choiceLoader;
3229

3330
/**
3431
* {@inheritdoc}
3532
*/
3633
public function configureOptions(OptionsResolver $resolver)
3734
{
3835
$resolver->setDefaults(array(
39-
'choice_loader' => $this,
36+
'choice_loader' => function (Options $options) {
37+
$choiceTranslationLocale = $options['choice_translation_locale'];
38+
$choiceLoader = new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale) {
39+
return array_flip(Intl::getRegionBundle()->getCountryNames($choiceTranslationLocale));
40+
});
41+
42+
// BC layer
43+
$self = clone $this;
44+
$self->choiceLoader = $choiceLoader;
45+
46+
return $self;
47+
},
4048
'choice_translation_domain' => false,
49+
'choice_translation_locale' => null,
4150
));
51+
52+
$resolver->setAllowedTypes('choice_translation_locale', array('null', 'string'));
4253
}
4354

4455
/**
@@ -59,51 +70,37 @@ public function getBlockPrefix()
5970

6071
/**
6172
* {@inheritdoc}
73+
*
74+
* @deprecated since Symfony 4.2
6275
*/
6376
public function loadChoiceList($value = null)
6477
{
65-
if (null !== $this->choiceList) {
66-
return $this->choiceList;
67-
}
78+
@trigger_error(sprintf('Method "%s" is deprecated since Symfony 4.2. Use the "choice_loader" option instead.', __METHOD__), E_USER_DEPRECATED);
6879

69-
return $this->choiceList = new ArrayChoiceList(array_flip(Intl::getRegionBundle()->getCountryNames()), $value);
80+
return $this->choiceLoader->loadChoiceList($value);
7081
}
7182

7283
/**
7384
* {@inheritdoc}
85+
*
86+
* @deprecated since Symfony 4.2
7487
*/
7588
public function loadChoicesForValues(array $values, $value = null)
7689
{
77-
// Optimize
78-
$values = array_filter($values);
79-
if (empty($values)) {
80-
return array();
81-
}
82-
83-
// If no callable is set, values are the same as choices
84-
if (null === $value) {
85-
return $values;
86-
}
87-
88-
return $this->loadChoiceList($value)->getChoicesForValues($values);
90+
@trigger_error(sprintf('Method "%s" is deprecated since Symfony 4.2. Use the "choice_loader" option instead.', __METHOD__), E_USER_DEPRECATED);
91+
92+
return $this->choiceLoader->loadChoiceList($value)->getChoicesForValues($values);
8993
}
9094

9195
/**
9296
* {@inheritdoc}
97+
*
98+
* @deprecated since Symfony 4.2
9399
*/
94100
public function loadValuesForChoices(array $choices, $value = null)
95101
{
96-
// Optimize
97-
$choices = array_filter($choices);
98-
if (empty($choices)) {
99-
return array();
100-
}
101-
102-
// If no callable is set, choices are the same as values
103-
if (null === $value) {
104-
return $choices;
105-
}
106-
107-
return $this->loadChoiceList($value)->getValuesForChoices($choices);
102+
@trigger_error(sprintf('Method "%s" is deprecated since Symfony 4.2. Use the "choice_loader" option instead.', __METHOD__), E_USER_DEPRECATED);
103+
104+
return $this->choiceLoader->loadChoiceList($value)->getValuesForChoices($choices);
108105
}
109106
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/Type/CurrencyType.php
+35-38Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,44 @@
1212
namespace Symfony\Component\Form\Extension\Core\Type;
1313

1414
use Symfony\Component\Form\AbstractType;
15-
use Symfony\Component\Form\ChoiceList\ArrayChoiceList;
1615
use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
16+
use Symfony\Component\Form\ChoiceList\Loader\IntlCallbackChoiceLoader;
1717
use Symfony\Component\Intl\Intl;
18+
use Symfony\Component\OptionsResolver\Options;
1819
use Symfony\Component\OptionsResolver\OptionsResolver;
1920

2021
class CurrencyType extends AbstractType implements ChoiceLoaderInterface
2122
{
2223
/**
23-
* Currency loaded choice list.
24+
* BC layer.
2425
*
25-
* The choices are lazy loaded and generated from the Intl component.
26-
*
27-
* {@link \Symfony\Component\Intl\Intl::getCurrencyBundle()}.
28-
*
29-
* @var ArrayChoiceList
26+
* @var ChoiceLoaderInterface
3027
*/
31-
private $choiceList;
28+
private $choiceLoader;
3229

3330
/**
3431
* {@inheritdoc}
3532
*/
3633
public function configureOptions(OptionsResolver $resolver)
3734
{
3835
$resolver->setDefaults(array(
39-
'choice_loader' => $this,
36+
'choice_loader' => function (Options $options) {
37+
$choiceTranslationLocale = $options['choice_translation_locale'];
38+
$choiceLoader = new IntlCallbackChoiceLoader(function () use ($choiceTranslationLocale) {
39+
return array_flip(Intl::getCurrencyBundle()->getCurrencyNames($choiceTranslationLocale));
40+
});
41+
42+
// BC layer
43+
$self = clone $this;
44+
$self->choiceLoader = $choiceLoader;
45+
46+
return $self;
47+
},
4048
'choice_translation_domain' => false,
49+
'choice_translation_locale' => null,
4150
));
51+
52+
$resolver->setAllowedTypes('choice_translation_locale', array('null', 'string'));
4253
}
4354

4455
/**
@@ -54,56 +65,42 @@ public function getParent()
5465
*/
5566
public function getBlockPrefix()
5667
{
57-
return 'currency';
68+
return 'country';
5869
}
5970

6071
/**
6172
* {@inheritdoc}
73+
*
74+
* @deprecated since Symfony 4.2
6275
*/
6376
public function loadChoiceList($value = null)
6477
{
65-
if (null !== $this->choiceList) {
66-
return $this->choiceList;
67-
}
78+
@trigger_error(sprintf('Method "%s" is deprecated since Symfony 4.2. Use the "choice_loader" option instead.', __METHOD__), E_USER_DEPRECATED);
6879

69-
return $this->choiceList = new ArrayChoiceList(array_flip(Intl::getCurrencyBundle()->getCurrencyNames()), $value);
80+
return $this->choiceLoader->loadChoiceList($value);
7081
}
7182

7283
/**
7384
* {@inheritdoc}
85+
*
86+
* @deprecated since Symfony 4.2
7487
*/
7588
public function loadChoicesForValues(array $values, $value = null)
7689
{
77-
// Optimize
78-
$values = array_filter($values);
79-
if (empty($values)) {
80-
return array();
81-
}
82-
83-
// If no callable is set, values are the same as choices
84-
if (null === $value) {
85-
return $values;
86-
}
87-
88-
return $this->loadChoiceList($value)->getChoicesForValues($values);
90+
@trigger_error(sprintf('Method "%s" is deprecated since Symfony 4.2. Use the "choice_loader" option instead.', __METHOD__), E_USER_DEPRECATED);
91+
92+
return $this->choiceLoader->loadChoiceList($value)->getChoicesForValues($values);
8993
}
9094

9195
/**
9296
* {@inheritdoc}
97+
*
98+
* @deprecated since Symfony 4.2
9399
*/
94100
public function loadValuesForChoices(array $choices, $value = null)
95101
{
96-
// Optimize
97-
$choices = array_filter($choices);
98-
if (empty($choices)) {
99-
return array();
100-
}
101-
102-
// If no callable is set, choices are the same as values
103-
if (null === $value) {
104-
return $choices;
105-
}
106-
107-
return $this->loadChoiceList($value)->getValuesForChoices($choices);
102+
@trigger_error(sprintf('Method "%s" is deprecated since Symfony 4.2. Use the "choice_loader" option instead.', __METHOD__), E_USER_DEPRECATED);
103+
104+
return $this->choiceLoader->loadChoiceList($value)->getValuesForChoices($choices);
108105
}
109106
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.