@@ -46,32 +46,11 @@ public function buildForm(FormBuilderInterface $builder, array $options)
4646 {
4747 $ options ['available_methods ' ] = $ this ->getPaymentMethods ($ options ['allowed_methods ' ]);
4848
49- $ choiceType = Legacy::supportsFormTypeName ()
50- ? 'choice '
51- : 'Symfony\Component\Form\Extension\Core\Type\ChoiceType '
52- ;
53-
54- $ builderOptions = array (
55- 'expanded ' => true ,
56- 'data ' => $ options ['default_method ' ],
57- );
49+ $ this ->buildChoiceList ($ builder , $ options );
5850
59- $ builderOptions ['choices ' ] = array ();
60- foreach ($ options ['available_methods ' ] as $ methodKey => $ methodClass ) {
61- $ label = 'form.label. ' .$ methodKey ;
62-
63- if (Legacy::formChoicesAsValues ()) {
64- $ builderOptions ['choices ' ][$ methodKey ] = $ label ;
65- } else {
66- $ builderOptions ['choices ' ][$ label ] = $ methodKey ;
67- }
68- }
69-
70- $ builder ->add ('method ' , $ choiceType , $ builderOptions );
71-
72- foreach ($ options ['available_methods ' ] as $ methodKey => $ methodClass ) {
73- $ methodOptions = isset ($ options ['method_options ' ][$ methodKey ]) ? $ options ['method_options ' ] : array ();
74- $ builder ->add ('data_ ' .$ methodKey , $ methodClass , $ methodOptions );
51+ foreach ($ options ['available_methods ' ] as $ method => $ class ) {
52+ $ methodOptions = isset ($ options ['method_options ' ][$ method ]) ? $ options ['method_options ' ][$ method ] : array ();
53+ $ builder ->add ('data_ ' .$ method , $ class , $ methodOptions );
7554 }
7655
7756 $ self = $ this ;
@@ -90,6 +69,43 @@ public function buildForm(FormBuilderInterface $builder, array $options)
9069 $ builder ->addModelTransformer ($ transformer );
9170 }
9271
72+ protected function buildChoiceList (FormBuilderInterface $ builder , array $ options )
73+ {
74+ $ methods = $ options ['available_methods ' ];
75+ $ choiceOptions = $ options ['choice_options ' ];
76+
77+ $ options = array_merge (array (
78+ 'expanded ' => true ,
79+ 'data ' => $ options ['default_method ' ],
80+ ), $ options );
81+
82+ // Remove unwanted options
83+ $ options = array_intersect_key ($ options , array_flip (array (
84+ 'expanded ' ,
85+ 'data ' ,
86+ )));
87+
88+ $ options = array_merge ($ options , $ choiceOptions );
89+
90+ $ options ['choices ' ] = array ();
91+ foreach (array_keys ($ methods ) as $ method ) {
92+ $ label = 'form.label. ' .$ method ;
93+
94+ if (Legacy::formChoicesAsValues ()) {
95+ $ options ['choices ' ][$ method ] = $ label ;
96+ } else {
97+ $ options ['choices ' ][$ label ] = $ method ;
98+ }
99+ }
100+
101+ $ type = Legacy::supportsFormTypeName ()
102+ ? 'choice '
103+ : 'Symfony\Component\Form\Extension\Core\Type\ChoiceType '
104+ ;
105+
106+ $ builder ->add ('method ' , $ type , $ options );
107+ }
108+
93109 public function validate (FormEvent $ event , array $ options )
94110 {
95111 $ form = $ event ->getForm ();
@@ -122,31 +138,35 @@ public function validate(FormEvent $event, array $options)
122138
123139 public function configureOptions (OptionsResolver $ resolver )
124140 {
141+ $ resolver ->setRequired (array (
142+ 'amount ' ,
143+ 'currency ' ,
144+ ));
145+
125146 $ resolver ->setDefaults (array (
147+ 'predefined_data ' => array (),
126148 'allowed_methods ' => array (),
127149 'default_method ' => null ,
128- 'predefined_data ' => array (),
150+ 'method_options ' => array (),
151+ 'choice_options ' => array (),
129152 ));
130153
131- $ resolver ->setRequired (array (
132- 'amount ' ,
133- 'currency ' ,
134- ));
154+ $ allowedTypes = array (
155+ 'amount ' => array ('numeric ' , 'closure ' ),
156+ 'currency ' => 'string ' ,
157+ 'predefined_data ' => 'array ' ,
158+ 'allowed_methods ' => 'array ' ,
159+ 'default_method ' => array ('null ' , 'string ' ),
160+ 'method_options ' => 'array ' ,
161+ 'choice_options ' => 'array ' ,
162+ );
135163
136164 if (Legacy::supportsFormTypeConfigureOptions ()) {
137- $ resolver ->setAllowedTypes (array (
138- 'allowed_methods ' => 'array ' ,
139- 'amount ' => array ('numeric ' , 'closure ' ),
140- 'currency ' => 'string ' ,
141- 'predefined_data ' => 'array ' ,
142- ));
165+ $ resolver ->setAllowedTypes ($ allowedTypes );
143166 } else {
144- $ resolver
145- ->setAllowedTypes ('allowed_methods ' , 'array ' )
146- ->setAllowedTypes ('amount ' , array ('numeric ' , 'closure ' ))
147- ->setAllowedTypes ('currency ' , 'string ' )
148- ->setAllowedTypes ('predefined_data ' , 'array ' )
149- ;
167+ foreach ($ allowedTypes as $ key => $ value ) {
168+ $ resolver ->addAllowedTypes ($ key , $ value );
169+ }
150170 }
151171 }
152172
@@ -200,9 +220,9 @@ private function applyErrorsToForm(FormInterface $form, Result $result)
200220 }
201221 }
202222
203- private function getPaymentMethods ($ allowedMethods = array () )
223+ private function getPaymentMethods ($ allowedMethods )
204224 {
205- $ allowAllMethods = ! count ($ allowedMethods );
225+ $ allowAllMethods = empty ($ allowedMethods );
206226 $ availableMethods = array ();
207227
208228 foreach ($ this ->paymentMethods as $ methodKey => $ methodClass ) {
0 commit comments