Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commitc81bf25

Browse files
committed
Add choice_translation_locale option for Intl choice types
1 parentf77c1d0 commitc81bf25

File tree

5 files changed

+188
-151
lines changed

5 files changed

+188
-151
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespaceSymfony\Component\Form\ChoiceList\Loader;
4+
5+
/**
6+
* Callback choice loader optimized for Intl choice types.
7+
*
8+
* @author Yonel Ceruto <yonelceruto@gmail.com>
9+
*/
10+
class IntlCallbackChoiceLoaderextends CallbackChoiceLoader
11+
{
12+
/**
13+
* {@inheritdoc}
14+
*/
15+
publicfunctionloadChoicesForValues(array$values,$value =null)
16+
{
17+
// Optimize
18+
$values =array_filter($values);
19+
if (empty($values)) {
20+
returnarray();
21+
}
22+
23+
// If no callable is set, values are the same as choices
24+
if (null ===$value) {
25+
return$values;
26+
}
27+
28+
return$this->loadChoiceList($value)->getChoicesForValues($values);
29+
}
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
publicfunctionloadValuesForChoices(array$choices,$value =null)
35+
{
36+
// Optimize
37+
$choices =array_filter($choices);
38+
if (empty($choices)) {
39+
returnarray();
40+
}
41+
42+
// If no callable is set, choices are the same as values
43+
if (null ===$value) {
44+
return$choices;
45+
}
46+
47+
return$this->loadChoiceList($value)->getValuesForChoices($choices);
48+
}
49+
}

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

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,44 @@
1212
namespaceSymfony\Component\Form\Extension\Core\Type;
1313

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

2021
class CountryTypeextends AbstractTypeimplements 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
publicfunctionconfigureOptions(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 =newIntlCallbackChoiceLoader(function ()use ($choiceTranslationLocale) {
39+
returnarray_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', ['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
publicfunctionloadChoiceList($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 =newArrayChoiceList(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
publicfunctionloadChoicesForValues(array$values,$value =null)
7689
{
77-
// Optimize
78-
$values =array_filter($values);
79-
if (empty($values)) {
80-
returnarray();
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
publicfunctionloadValuesForChoices(array$choices,$value =null)
95101
{
96-
// Optimize
97-
$choices =array_filter($choices);
98-
if (empty($choices)) {
99-
returnarray();
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‎

Lines changed: 35 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,44 @@
1212
namespaceSymfony\Component\Form\Extension\Core\Type;
1313

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

2021
class CurrencyTypeextends AbstractTypeimplements 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
publicfunctionconfigureOptions(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 =newIntlCallbackChoiceLoader(function ()use ($choiceTranslationLocale) {
39+
returnarray_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', ['null','string']);
4253
}
4354

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

6071
/**
6172
* {@inheritdoc}
73+
*
74+
* @deprecated since Symfony 4.2
6275
*/
6376
publicfunctionloadChoiceList($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 =newArrayChoiceList(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
publicfunctionloadChoicesForValues(array$values,$value =null)
7689
{
77-
// Optimize
78-
$values =array_filter($values);
79-
if (empty($values)) {
80-
returnarray();
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
publicfunctionloadValuesForChoices(array$choices,$value =null)
95101
{
96-
// Optimize
97-
$choices =array_filter($choices);
98-
if (empty($choices)) {
99-
returnarray();
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)

[8]ページ先頭

©2009-2025 Movatter.jp