File tree 7 files changed +283
-91
lines changed
src/Symfony/Component/Form
Tests/Extension/Core/Type
7 files changed +283
-91
lines changed Original file line number Diff line number Diff line change 11
11
12
12
namespace Symfony \Component \Form \Extension \Core \Type ;
13
13
14
- use Symfony \Component \Form \AbstractType ;
15
14
use Symfony \Component \Intl \Intl ;
16
- use Symfony \Component \OptionsResolver \OptionsResolver ;
17
15
18
- class CountryType extends AbstractType
16
+ class CountryType extends ImmutableChoiceType
19
17
{
20
18
/**
21
19
* {@inheritdoc}
22
20
*/
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 ()
21
+ public function getBlockPrefix ()
35
22
{
36
- return __NAMESPACE__ . ' \ChoiceType ' ;
23
+ return ' country ' ;
37
24
}
38
25
39
26
/**
40
27
* {@inheritdoc}
28
+ *
29
+ * Returns the country choices.
30
+ *
31
+ * The choices are generated from the Intl component
32
+ * {@link \Symfony\Component\Intl\Intl::getRegionBundle()}.
33
+ *
34
+ * @return array The country choices
41
35
*/
42
- public function getBlockPrefix ()
36
+ protected function loadChoices ()
43
37
{
44
- return ' country ' ;
38
+ return array_flip (Intl:: getRegionBundle ()-> getCountryNames ()) ;
45
39
}
46
40
}
Original file line number Diff line number Diff line change 11
11
12
12
namespace Symfony \Component \Form \Extension \Core \Type ;
13
13
14
- use Symfony \Component \Form \AbstractType ;
15
14
use Symfony \Component \Intl \Intl ;
16
- use Symfony \Component \OptionsResolver \OptionsResolver ;
17
15
18
- class CurrencyType extends AbstractType
16
+ class CurrencyType extends ImmutableChoiceType
19
17
{
20
18
/**
21
19
* {@inheritdoc}
22
20
*/
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 ()
35
22
{
36
- return __NAMESPACE__ . ' \ChoiceType ' ;
23
+ return ' currency ' ;
37
24
}
38
25
39
26
/**
40
27
* {@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
41
35
*/
42
- public function getBlockPrefix ()
36
+ protected function loadChoices ()
43
37
{
44
- return ' currency ' ;
38
+ return array_flip (Intl:: getCurrencyBundle ()-> getCurrencyNames ()) ;
45
39
}
46
40
}
Original file line number Diff line number Diff line change
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
+ {
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
+ }
Original file line number Diff line number Diff line change 11
11
12
12
namespace Symfony \Component \Form \Extension \Core \Type ;
13
13
14
- use Symfony \Component \Form \AbstractType ;
15
14
use Symfony \Component \Intl \Intl ;
16
- use Symfony \Component \OptionsResolver \OptionsResolver ;
17
15
18
- class LanguageType extends AbstractType
16
+ class LanguageType extends ImmutableChoiceType
19
17
{
20
18
/**
21
19
* {@inheritdoc}
22
20
*/
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 ()
35
22
{
36
- return __NAMESPACE__ . ' \ChoiceType ' ;
23
+ return ' language ' ;
37
24
}
38
25
39
26
/**
40
27
* {@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
41
35
*/
42
- public function getBlockPrefix ()
36
+ protected function loadChoices ()
43
37
{
44
- return ' language ' ;
38
+ return array_flip (Intl:: getLanguageBundle ()-> getLanguageNames ()) ;
45
39
}
46
40
}
Original file line number Diff line number Diff line change 11
11
12
12
namespace Symfony \Component \Form \Extension \Core \Type ;
13
13
14
- use Symfony \Component \Form \AbstractType ;
15
14
use Symfony \Component \Intl \Intl ;
16
- use Symfony \Component \OptionsResolver \OptionsResolver ;
17
15
18
- class LocaleType extends AbstractType
16
+ class LocaleType extends ImmutableChoiceType
19
17
{
20
18
/**
21
19
* {@inheritdoc}
22
20
*/
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 ()
35
22
{
36
- return __NAMESPACE__ . ' \ChoiceType ' ;
23
+ return ' locale ' ;
37
24
}
38
25
39
26
/**
40
27
* {@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
41
35
*/
42
- public function getBlockPrefix ()
36
+ protected function loadChoices ()
43
37
{
44
- return ' locale ' ;
38
+ return array_flip (Intl:: getLocaleBundle ()-> getLocaleNames ()) ;
45
39
}
46
40
}
Original file line number Diff line number Diff line change 11
11
12
12
namespace Symfony \Component \Form \Extension \Core \Type ;
13
13
14
- use Symfony \Component \Form \AbstractType ;
15
- use Symfony \Component \OptionsResolver \OptionsResolver ;
16
-
17
- class TimezoneType extends AbstractType
14
+ class TimezoneType extends ImmutableChoiceType
18
15
{
19
16
/**
20
17
* Stores the available timezone choices.
@@ -26,28 +23,17 @@ class TimezoneType extends AbstractType
26
23
/**
27
24
* {@inheritdoc}
28
25
*/
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 ()
41
27
{
42
- return __NAMESPACE__ . ' \ChoiceType ' ;
28
+ return ' timezone ' ;
43
29
}
44
30
45
31
/**
46
32
* {@inheritdoc}
47
33
*/
48
- public function getBlockPrefix ()
34
+ public function loadChoices ()
49
35
{
50
- return ' timezone ' ;
36
+ return self :: getTimezones () ;
51
37
}
52
38
53
39
/**
You can’t perform that action at this time.
0 commit comments