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

Skip to content

Commit 3aa5442

Browse files
committed
[Form] add abstract ImmutableChoiceType
1 parent 22b9dde commit 3aa5442

File tree

7 files changed

+313
-114
lines changed

7 files changed

+313
-114
lines changed

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

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,33 @@
1111

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

14-
use Symfony\Component\Form\AbstractType;
14+
use Symfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader;
1515
use Symfony\Component\Intl\Intl;
16-
use Symfony\Component\OptionsResolver\OptionsResolver;
1716

18-
class CountryType extends AbstractType
17+
class CountryType extends ImmutableChoiceType
1918
{
2019
/**
2120
* {@inheritdoc}
2221
*/
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-
}
30-
31-
/**
32-
* {@inheritdoc}
33-
*/
34-
public function getParent()
22+
public function getBlockPrefix()
3523
{
36-
return __NAMESPACE__.'\ChoiceType';
24+
return 'country';
3725
}
3826

3927
/**
4028
* {@inheritdoc}
29+
*
30+
* Returns the country lazy choice loader.
31+
*
32+
* The choices are generated from the Intl component
33+
* {@link \Symfony\Component\Intl\Intl::getRegionBundle()}.
34+
*
35+
* @return CallbackChoiceLoader The country choice loader
4136
*/
42-
public function getBlockPrefix()
37+
protected function getChoiceLoader()
4338
{
44-
return 'country';
39+
return new CallbackChoiceLoader(function() {
40+
return array_flip(Intl::getRegionBundle()->getCountryNames());
41+
});
4542
}
4643
}

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

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,33 @@
1111

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

14-
use Symfony\Component\Form\AbstractType;
14+
use Symfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader;
1515
use Symfony\Component\Intl\Intl;
16-
use Symfony\Component\OptionsResolver\OptionsResolver;
1716

18-
class CurrencyType extends AbstractType
17+
class CurrencyType extends ImmutableChoiceType
1918
{
2019
/**
2120
* {@inheritdoc}
2221
*/
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()
22+
public function getBlockPrefix()
3523
{
36-
return __NAMESPACE__.'\ChoiceType';
24+
return 'currency';
3725
}
3826

3927
/**
4028
* {@inheritdoc}
29+
*
30+
* Returns the currency lazy choice loader.
31+
*
32+
* The choices are generated from the Intl component
33+
* {@link \Symfony\Component\Intl\Intl::getCurrencyBundle()}.
34+
*
35+
* @return CallbackChoiceLoader The currency choice loader
4136
*/
42-
public function getBlockPrefix()
37+
protected function getChoiceLoader()
4338
{
44-
return 'currency';
39+
return new CallbackChoiceLoader(function () {
40+
return array_flip(Intl::getCurrencyBundle()->getCurrencyNames());
41+
});
4542
}
4643
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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\Form\ChoiceList\Loader\ChoiceLoaderInterface;
16+
use Symfony\Component\OptionsResolver\OptionsResolver;
17+
18+
/**
19+
* Base class for child of {@link ChoiceType}.
20+
*
21+
* Every class extending it is responsible for loading
22+
* the choices and eventually cache them.
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+
'choice_loader' => $this->getChoiceLoader(),
35+
'choice_translation_domain' => false,
36+
));
37+
38+
$resolver->setRequired(array('choice_loader'));
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
public function getParent()
45+
{
46+
return __NAMESPACE__.'\ChoiceType';
47+
}
48+
49+
/**
50+
* Returns the choice loader.
51+
*
52+
* This method needs to be implemented to load the
53+
* choices from the returned choice loader.
54+
*
55+
* The choice list generated by this loader may
56+
* eventually get cached by a {@link ChoiceListFactoryInterface},
57+
* since it get called once by request.
58+
*
59+
* See {@link TimeZoneType} for example.
60+
*
61+
* @return ChoiceLoaderInterface The choice loader
62+
*/
63+
abstract protected function getChoiceLoader();
64+
}

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

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,33 @@
1111

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

14-
use Symfony\Component\Form\AbstractType;
14+
use Symfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader;
1515
use Symfony\Component\Intl\Intl;
16-
use Symfony\Component\OptionsResolver\OptionsResolver;
1716

18-
class LanguageType extends AbstractType
17+
class LanguageType extends ImmutableChoiceType
1918
{
2019
/**
2120
* {@inheritdoc}
2221
*/
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()
22+
public function getBlockPrefix()
3523
{
36-
return __NAMESPACE__.'\ChoiceType';
24+
return 'language';
3725
}
3826

3927
/**
4028
* {@inheritdoc}
29+
*
30+
* Returns the language lazy choice loader.
31+
*
32+
* The choices are generated from the Intl component
33+
* {@link \Symfony\Component\Intl\Intl::getLanguageBundle()}.
34+
*
35+
* @return CallbackChoiceLoader The language choice loader
4136
*/
42-
public function getBlockPrefix()
37+
protected function getChoiceLoader()
4338
{
44-
return 'language';
39+
return new CallbackChoiceLoader(function() {
40+
return array_flip(Intl::getLanguageBundle()->getLanguageNames());
41+
});
4542
}
4643
}

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

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,36 +11,33 @@
1111

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

14-
use Symfony\Component\Form\AbstractType;
14+
use Symfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader;
1515
use Symfony\Component\Intl\Intl;
16-
use Symfony\Component\OptionsResolver\OptionsResolver;
1716

18-
class LocaleType extends AbstractType
17+
class LocaleType extends ImmutableChoiceType
1918
{
2019
/**
2120
* {@inheritdoc}
2221
*/
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()
22+
public function getBlockPrefix()
3523
{
36-
return __NAMESPACE__.'\Cho 10000 iceType';
24+
return 'locale';
3725
}
3826

3927
/**
4028
* {@inheritdoc}
29+
*
30+
* Returns the lazy locale choice loader.
31+
*
32+
* The choices are generated from the Intl component
33+
* {@link \Symfony\Component\Intl\Intl::getLocaleBundle()}.
34+
*
35+
* @return array The locale choice loader
4136
*/
42-
public function getBlockPrefix()
37+
protected function getChoiceLoader()
4338
{
44-
return 'locale';
39+
return new CallbackChoiceLoader(function () {
40+
return array_flip(Intl::getLocaleBundle()->getLocaleNames());
41+
});
4542
}
4643
}

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

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111

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

14-
use Symfony\Component\Form\AbstractType;
15-
use Symfony\Component\OptionsResolver\OptionsResolver;
14+
use Symfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader;
1615

17-
class TimezoneType extends AbstractType
16+
class TimezoneType extends ImmutableChoiceType
1817
{
1918
/**
2019
* Stores the available timezone choices.
@@ -26,63 +25,52 @@ class TimezoneType extends AbstractType
2625
/**
2726
* {@inheritdoc}
2827
*/
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()
28+
public function getBlockPrefix()
4129
{
42-
return __NAMESPACE__.'\ChoiceType';
30+
return 'timezone';
4331
}
4432

4533
/**
4634
* {@inheritdoc}
35+
*
36+
* Return the lazy timezone choice loader.
37+
*
38+
* The choices are generated from the ICU function
39+
* \DateTimeZone::listIdentifiers().
40+
*
41+
* @return CallbackChoiceLoader The timezone choice loader
4742
*/
48-
public function getBlockPrefix()
43+
public function getChoiceLoader()
4944
{
50-
return 'timezone';
45+
return new CallbackChoiceLoader(array(__CLASS__, 'getTimezones'));
5146
}
5247

5348
/**
54-
* Returns the timezone choices.
55-
*
56-
* The choices are generated from the ICU function
57-
* \DateTimeZone::listIdentifiers(). They are cached during a single request,
58-
* so multiple timezone fields on the same page don't lead to unnecessary
59-
* overhead.
49+
* Returns a normalized array of timezone choices.
6050
*
6151
* @return array The timezone choices
6252
*/
63-
private static function getTimezones()
53+
public static function getTimezones()
6454
{
65-
if (null === self::$timezones) {
66-
self::$timezones = array();
55+
$timezones = array();
6756

68-
foreach (\DateTimeZone::listIdentifiers() as $timezone) {
69-
$parts = explode('/', $timezone);
57+
foreach (\DateTimeZone::listIdentifiers() as $timezone) {
58+
$parts = explode('/', $timezone);
7059

71-
if (count($parts) > 2) {
72-
$region = $parts[0];
73-
$name = $parts[1].' - '.$parts[2];
74-
} elseif (count($parts) > 1) { 48DA
75-
$region = $parts[0];
76-
$name = $parts[1];
77-
} else {
78-
$region = 'Other';
79-
$name = $parts[0];
80-
}
81-
82-
self::$timezones[$region][str_replace('_', ' ', $name)] = $timezone;
60+
if (count($parts) > 2) {
61+
$region = $parts[0];
62+
$name = $parts[1].' - '.$parts[2];
63+
} elseif (count($parts) > 1) {
64+
$region = $parts[0];
65+
$name = $parts[1];
66+
} else {
67+
$region = 'Other';
68+
$name = $parts[0];
8369
}
70+
71+
$timezones[$region][str_replace('_', ' ', $name)] = $timezone;
8472
}
8573

86-
return self::$timezones;
74+
return $timezones;
8775
}
8876
}

0 commit comments

Comments
 (0)
0