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

Skip to content

Commit 6360f60

Browse files
committed
[Form] add abstract ImmutableChoiceType
1 parent 2946932 commit 6360f60

File tree

7 files changed

+287
-88
lines changed

7 files changed

+287
-88
lines changed

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,37 @@
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
/**
21-
* {@inheritdoc}
19+
* Stores the available country choices.
20+
*
21+
* @var array
2222
*/
23-
public function configureOptions(OptionsResolver $resolver)
24-
{
25-
$resolver->setDefaults(array(
26-
'choices' => array_flip(Intl::getRegionBundle()->getCountryNames()),
27-
'choice_translation_domain' => false,
28-
));
29-
}
23+
private static $countries;
3024

3125
/**
3226
* {@inheritdoc}
3327
*/
34-
public function getParent()
28+
public function getBlockPrefix()
3529
{
36-
return __NAMESPACE__.'\ChoiceType';
30+
return 'country';
3731
}
3832

3933
/**
4034
* {@inheritdoc}
35+
*
36+
* Returns the country choices.
37+
*
38+
* The choices are generated from the Intl component
39+
* {@link \Symfony\Component\Intl\Intl::getRegionBundle()}.
40+
*
41+
* @return array The country choices
4142
*/
42-
public function getBlockPrefix()
43+
protected function loadChoices()
4344
{
44-
return 'country';
45+
return array_flip(Intl::getRegionBundle()->getCountryNames());
4546
}
4647
}

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

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,30 @@
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
* {@inheritdoc}
2220
*/
23-
public function configureOptions(OptionsResolver $resolver)
24-
{
25-
$resolver->setDefaults(array(
26-
'choices' => array_flip(Intl::getCurrencyBundle()->getCurrencyNames()),
27-
'choice_translation_domain' => false,
28-
));
29-
}
30-
31-
/**
32-
* {@inheritdoc}
33-
*/
34-
public function getParent()
21+
public function getBlockPrefix()
3522
{
36-
return __NAMESPACE__.'\ChoiceType';
23+
return 'currency';
3724
}
3825

3926
/**
4027
* {@inheritdoc}
28+
*
29+
* Returns the currency choices.
30+
*
31+
* The choices are generated from the Intl component
32+
* {@link \Symfony\Component\Intl\Intl::getCurrencyBundle()}.
33+
*
34+
* @return array The currency choices
4135
*/
42-
public function getBlockPrefix()
36+
protected function loadChoices()
4337
{
44-
return 'currency';
38+
return array_flip(Intl::getCurrencyBundle()->getCurrencyNames());
4539
}
4640
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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.
22+
*/
23+
abstract class ImmutableChoiceType extends AbstractType
24 10000 +
{
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function configureOptions(OptionsResolver $resolver)
29+
{
30+
$resolver->setDefaults(array(
31+
'choices' => $this->loadChoices(),
32+
'choice_translation_domain' => false,
33+
));
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
public function getParent()
40+
{
41+
return __NAMESPACE__.'\ChoiceType';
42+
}
43+
44+
/**
45+
* Returns the choices.
46+
*
47+
* This method needs to be implemented to load the
48+
* choices from any data source.
49+
* The result should be cached for better performance
50+
* when needed.
51+
*
52+
* See {@link TimeZoneType} for example.
53+
*
54+
* @return array
55+
*/
56+
abstract protected function loadChoices();
57+
}

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

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,30 @@
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
* {@inheritdoc}
2220
*/
23-
public function configureOptions(OptionsResolver $resolver)
24-
{
25-
$resolver->setDefaults(array(
26-
'choices' => array_flip(Intl::getLanguageBundle()->getLanguageNames()),
27-
'choice_translation_domain' => false,
28-
));
29-
}
30-
31-
/**
32-
* {@inheritdoc}
33-
*/
34-
public function getParent()
21+
public function getBlockPrefix()
3522
{
36-
return __NAMESPACE__.'\ChoiceType';
23+
return 'language';
3724
}
3825

3926
/**
4027
* {@inheritdoc}
28+
*
29+
* Returns the language choices.
30+
*
31+
* The choices are generated from the Intl component
32+
* {@link \Symfony\Component\Intl\Intl::getLanguageBundle()}.
33+
*
34+
* @return array The language choices
4135
*/
42-
public function getBlockPrefix()
36+
protected function loadChoices()
4337
{
44-
return 'language';
38+
return array_flip(Intl::getLanguageBundle()->getLanguageNames());
4539
}
4640
}

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

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,30 @@
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
* {@inheritdoc}
2220
*/
23-
public function configureOptions(OptionsResolver $resolver)
24-
{
25-
$resolver->setDefaults(array(
26-
'choices' => array_flip(Intl::getLocaleBundle()->getLocaleNames()),
27-
'choice_translation_domain' => false,
28-
));
29-
}
30-
31-
/**
32-
* {@inheritdoc}
33-
*/
34-
public function getParent()
21+
public function getBlockPrefix()
3522
{
36-
return __NAMESPACE__.'\ChoiceType';
23+
return 'locale';
3724
}
3825

3926
/**
4027
* {@inheritdoc}
28+
*
29+
* Returns the locale choices.
30+
*
31+
* The choices are generated from the Intl component
32+
* {@link \Symfony\Component\Intl\Intl::getLocaleBundle()}.
33+
*
34+
* @return array The locale choices
4135
*/
42-
public function getBlockPrefix()
36+
protected function loadChoices()
4337
{
44-
return 'locale';
38+
return array_flip(Intl::getLocaleBundle()->getLocaleNames());
4539
}
4640
}

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

Lines changed: 5 additions & 19 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.
@@ -26,28 +23,17 @@ class TimezoneType extends AbstractType
2623
/**
2724
* {@inheritdoc}
2825
*/
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()
26+
public function getBlockPrefix()
4127
{
42-
return __NAMESPACE__.'\ChoiceType';
28+
return 'timezone';
4329
}
4430

4531
/**
4632
* {@inheritdoc}
4733
*/
48-
public function getBlockPrefix()
34+
public function loadChoices()
4935
{
50-
return 'timezone';
36+
return self::getTimezones();
5137
}
5238

5339
/**

0 commit comments

Comments
 (0)
0