8000 [Form] add abstract ImmutableChoiceType · symfony/symfony@dff2883 · GitHub
[go: up one dir, main page]

Skip to content

Commit dff2883

Browse files
committed
[Form] add abstract ImmutableChoiceType
1 parent 8298b1d commit dff2883

File tree

7 files changed

+336
-116
lines changed

7 files changed

+336
-116
lines changed

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

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@
1111

1212
namespace Symfony\Component\Form\Extension\Core\Type;
1313

14-
use Symfony\Component\Form\AbstractType;
1514
use Symfony\Component\Intl\Intl;
16-
use Symfony\Component\OptionsResolver\OptionsResolver;
1715

18-
class CountryType extends AbstractType
16+
class CountryType extends ImmutableChoiceType
1917
{
2018
/**
2119
* Stores the available country choices.
@@ -24,25 +22,6 @@ class CountryType extends AbstractType
2422
*/
2523
private static $countries;
2624

27-
/**
28-
* {@inheritdoc}
29-
*/
30-
public function configureOptions(OptionsResolver $resolver)
31-
{
32-
$resolver->setDefaults(array(
33-
'choices' => self::getCountries(),
34-
'choice_translation_domain' => false,
35-
));
36-
}
37-
38-
/**
39-
* {@inheritdoc}
40-
*/
41-
public function getParent()
42-
{
43-
return __NAMESPACE__.'\ChoiceType';
44-
}
45-
4625
/**
4726
* {@inheritdoc}
4827
*/
@@ -52,6 +31,8 @@ public function getBlockPrefix()
5231
}
5332

5433
/**
34+
* {@inheritdoc}
35+
*
5536
* Returns the country choices.
5637
*
5738
* The choices are generated from the Intl component
@@ -63,7 +44,7 @@ public function getBlockPrefix()
6344
*
6445
* @return array The country choices
6546
*/
66-
private static function getCountries()
47+
protected function loadChoices()
6748
{
6849
if (null === self::$countries) {
6950
self::$countries = array_flip(Intl::getRegionBundle()->getCountryNames());

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

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@
1111

1212
namespace Symfony\Component\Form\Extension\Core\Type;
1313

14-
use Symfony\Component\Form\AbstractType;
1514
use Symfony\Component\Intl\Intl;
16-
use Symfony\Component\OptionsResolver\OptionsResolver;
1715

18-
class CurrencyType extends AbstractType
16+
class CurrencyType extends ImmutableChoiceType
1917
{
2018
/**
2119
* Stores the available currency choices.
@@ -24,25 +22,6 @@ class CurrencyType extends AbstractType
2422
*/
2523
private static $currencies;
2624

27-
/**
28-
* {@inheritdoc}
29-
*/
30-
public function configureOptions(OptionsResolver $resolver)
31-
{
32-
$resolver->setDefaults(array(
33-
'choices' => self::getCurrencies(),
34-
'choice_translation_domain' => false,
35-
));
36-
}
37-
38-
/**
39-
* {@inheritdoc}
40-
*/
41-
public function getParent()
42-
{
43-
return __NAMESPACE__.'\ChoiceType';
44-
}
45-
4625
/**
4726
* {@inheritdoc}
4827
*/
@@ -52,6 +31,8 @@ public function getBlockPrefix()
5231
}
5332

5433
/**
34+
* {@inheritdoc}
35+
*
5536
* Returns the currency choices.
5637
*
5738
* The choices are generated from the Intl component
@@ -63,7 +44,7 @@ public function getBlockPrefix()
6344
*
6445
* @return array The currency choices
6546
*/
66-
private static function getCurrencies()
47+
protected function loadChoices()
6748
{
6849
if (null === self::$currencies) {
6950
self::$currencies = array_flip(Intl::getCurrencyBundle()->getCurrencyNames());
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\Extension\Core\Type;
13+
14+
use Symfony\Component\Form\AbstractType;
15+
use Symfony\Component\OptionsResolver\OptionsResolver;
16+
17+
/**
18+
* Base class for child of {@link ChoiceType}.
19+
*
20+
* Every class extending it is responsible for loading
21+
* the choices and eventually cache them to save performances
22+
* when the form type is used many times in one form or request.
23+
*
24+
* @author Jules Pietri <jules@heahprod.com>
25+
*/
26+
abstract class ImmutableChoiceType extends AbstractType
27+
{
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public function configureOptions(OptionsResolver $resolver)
32+
{
33+
$resolver->setDefaults(array(
34+
'choices' => $this->loadChoices(),
35+
'choice_translation_domain' => false,
36+
));
37+
}
38+
39+
/**
40+
* {@inheritdoc}
41+
*/
42+
public function getParent()
43+
{
44+
return __NAMESPACE__.'\ChoiceType';
45+
}
46+
47+
/**
48+
* Returns the choices.
49+
*
50+
* This method needs to be implemented to load
51+
* choices, from any data source.
52+
* The result should be cached for better performances.
53+
*
54+
* See {@link TimeZoneType} for example.
55+
*
56+
* @return array
57+
*/
58+
abstract protected function loadChoices();
59+
}

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

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@
1111

1212
namespace Symfony\Component\Form\Extension\Core\Type;
1313

14-
use Symfony\Component\Form\AbstractType;
1514
use Symfony\Component\Intl\Intl;
16-
use Symfony\Component\OptionsResolver\OptionsResolver;
1715

18-
class LanguageType extends AbstractType
16+
class LanguageType extends ImmutableChoiceType
1917
{
2018
/**
2119
* Stores the available language choices.
@@ -24,25 +22,6 @@ class LanguageType extends AbstractType
2422
*/
2523
private static $languages;
2624

27-
/**
28-
* {@inheritdoc}
29-
*/
30-
public function configureOptions(OptionsResolver $resolver)
31-
{
32-
$resolver->setDefaults(array(
33-
'choices' => self::getLanguages(),
34-
'choice_translation_domain' => false,
35-
));
36-
}
37-
38-
/**
39-
* {@inheritdoc}
40-
*/
41-
public function getParent()
42-
{
43-
return __NAMESPACE__.'\ChoiceType';
44-
}
45-
4625
/**
4726
* {@inheritdoc}
4827
*/
@@ -52,6 +31,8 @@ public function getBlockPrefix()
5231
}
5332

5433
/**
34+
* {@inheritdoc}
35+
*
5536
* Returns the language choices.
5637
*
5738
* The choices are generated from the Intl component
@@ -63,7 +44,7 @@ public function getBlockPrefix()
6344
*
6445
* @return array The language choices
6546
*/
66-
private static function getLanguages()
47+
protected function loadChoices()
6748
{
6849
if (null === self::$languages) {
6950
self::$languages = array_flip(Intl::getLanguageBundle()->getLanguageNames());

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

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@
1111

1212
namespace Symfony\Component\Form\Extension\Core\Type;
1313

14-
use Symfony\Component\Form\AbstractType;
1514
use Symfony\Component\Intl\Intl;
16-
use Symfony\Component\OptionsResolver\OptionsResolver;
1715

18-
class LocaleType extends AbstractType
16+
class LocaleType extends ImmutableChoiceType
1917
{
2018
/**
2119
* Stores the available locale choices.
@@ -24,25 +22,6 @@ class LocaleType extends AbstractType
2422
*/
2523
private static $locales;
2624

27-
/**
28-
* {@inheritdoc}
29-
*/
30-
public function configureOptions(OptionsResolver $resolver)
31-
{
32-
$resolver->setDefaults(array(
33-
'choices' => self::getLocales(),
34-
'choice_translation_domain' => false,
35-
));
36-
}
37-
38-
/**
39-
* {@inheritdoc}
40-
*/
41-
public function getParent()
42-
{
43-
return __NAMESPACE__.'\ChoiceType';
44-
}
45-
4625
/**
4726
* {@inheritdoc}
4827
*/
@@ -52,6 +31,8 @@ public function getBlockPrefix()
5231
}
5332

5433
/**
34+
* {@inheritdoc}
35+
*
5536
* Returns the locale choices.
5637
*
5738
* The choices are generated from the Intl component
@@ -63,7 +44,7 @@ public function getBlockPrefix()
6344
*
6445
* @return array The locale choices
6546
*/
66-
private static function getLocales()
47+
protected function loadChoices()
6748
{
6849
if (null === self::$locales) {
6950
self::$locales = array_flip(Intl::getLocaleBundle()->getLocaleNames());

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

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@
1111

1212
namespace Symfony\Component\Form\Extension\Core\Type;
1313

14-
use Symfony\Component\Form\AbstractType;
15-
use Symfony\Component\OptionsResolver\OptionsResolver;
16-
17-
class TimezoneType extends AbstractType
14+
class TimezoneType extends ImmutableChoiceType
1815
{
1916
/**
2017
* Stores the available timezone choices.
@@ -23,25 +20,6 @@ class TimezoneType extends AbstractType
2320
*/
2421
private static $timezones;
2522

26-
/**
27-
* {@inheritdoc}
28-
*/
29-
public function configureOptions(OptionsResolver $resolver)
30-
{
31-
$resolver->setDefaults(array(
32-
'choices' => self::getTimezones(),
33-
'choice_translation_domain' => false,
34-
));
35-
}
36-
37-
/**
38-
* {@inheritdoc}
39-
*/
40-
public function getParent()
41-
{
42-
return __NAMESPACE__.'\ChoiceType';
43-
}
44-
4523
/**
4624
* {@inheritdoc}
4725
*/
@@ -51,6 +29,8 @@ public function getBlockPrefix()
5129
}
5230

5331
/**
32+
* {@inheritdoc}
33+
*
5434
* Returns the timezone choices.
5535
*
5636
* The choices are generated from the ICU function
@@ -60,7 +40,7 @@ public function getBlockPrefix()
6040
*
6141
* @return array The timezone choices
6242
*/
63-
private static function getTimezones()
43+
protected function loadChoices()
6444
{
6545
if (null === self::$timezones) {
6646
self::$timezones = array();

0 commit comments

Comments
 (0)
0