1212namespace Symfony \Component \Form \Extension \Core \Type ;
1313
1414use Symfony \Component \Form \AbstractType ;
15+ use Symfony \Component \Form \ChoiceList \ArrayChoiceList ;
16+ use Symfony \Component \Form \ChoiceList \Loader \ChoiceLoaderInterface ;
1517use Symfony \Component \Intl \Intl ;
1618use Symfony \Component \OptionsResolver \OptionsResolver ;
1719
18- class CurrencyType extends AbstractType
20+ class CurrencyType extends AbstractType implements ChoiceLoaderInterface
1921{
22+ /**
23+ * Currency loaded choice list.
24+ *
25+ * The choices are lazy loaded and generated from the Intl component.
26+ *
27+ * {@link \Symfony\Component\Intl\Intl::getCurrencyBundle()}.
28+ *
29+ * @var ArrayChoiceList
30+ */
31+ private $ choiceList ;
32+
2033 /**
2134 * {@inheritdoc}
2235 */
2336 public function configureOptions (OptionsResolver $ resolver )
2437 {
2538 $ resolver ->setDefaults (array (
26- 'choices ' => array_flip (Intl:: getCurrencyBundle ()-> getCurrencyNames ()) ,
39+ 'choice_loader ' => $ this ,
2740 'choice_translation_domain ' => false ,
2841 ));
2942 }
@@ -43,4 +56,52 @@ public function getBlockPrefix()
4356 {
4457 return 'currency ' ;
4558 }
59+
60+ /**
61+ * {@inheritdoc}
62+ */
63+ public function loadChoiceList ($ value = null )
64+ {
65+ if (null !== $ this ->choiceList ) {
66+ return $ this ->choiceList ;
67+ }
68+
69+ return $ this ->choiceList = new ArrayChoiceList (array_flip (Intl::getCurrencyBundle ()->getCurrencyNames ()), $ value );
70+ }
71+
72+ /**
73+ * {@inheritdoc}
74+ */
75+ public function loadChoicesForValues (array $ values , $ value = null )
76+ {
77+ // Optimize
78+ if (empty ($ values )) {
79+ return array ();
80+ }
81+
82+ // If no callable is set, values are the same as choices
83+ if (null === $ value ) {
84+ return $ values ;
85+ }
86+
87+ return $ this ->loadChoiceList ($ value )->getChoicesForValues ($ values );
88+ }
89+
90+ /**
91+ * {@inheritdoc}
92+ */
93+ public function loadValuesForChoices (array $ choices , $ value = null )
94+ {
95+ // Optimize
96+ if (empty ($ choices )) {
97+ return array ();
98+ }
99+
100+ // If no callable is set, choices are the same as values
101+ if (null === $ value ) {
102+ return $ choices ;
103+ }
104+
105+ return $ this ->loadChoiceList ($ value )->getValuesForChoices ($ choices );
106+ }
46107}
0 commit comments