@@ -46,32 +46,11 @@ public function buildForm(FormBuilderInterface $builder, array $options)
46
46
{
47
47
$ options ['available_methods ' ] = $ this ->getPaymentMethods ($ options ['allowed_methods ' ]);
48
48
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 );
58
50
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 );
75
54
}
76
55
77
56
$ self = $ this ;
@@ -90,6 +69,43 @@ public function buildForm(FormBuilderInterface $builder, array $options)
90
69
$ builder ->addModelTransformer ($ transformer );
91
70
}
92
71
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
+
93
109
public function validate (FormEvent $ event , array $ options )
94
110
{
95
111
$ form = $ event ->getForm ();
@@ -122,31 +138,35 @@ public function validate(FormEvent $event, array $options)
122
138
123
139
public function configureOptions (OptionsResolver $ resolver )
124
140
{
141
+ $ resolver ->setRequired (array (
142
+ 'amount ' ,
143
+ 'currency ' ,
144
+ ));
145
+
125
146
$ resolver ->setDefaults (array (
147
+ 'predefined_data ' => array (),
126
148
'allowed_methods ' => array (),
127
149
'default_method ' => null ,
128
- 'predefined_data ' => array (),
150
+ 'method_options ' => array (),
151
+ 'choice_options ' => array (),
129
152
));
130
153
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
+ );
135
163
136
164
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 );
143
166
} 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
+ }
150
170
}
151
171
}
152
172
@@ -200,9 +220,9 @@ private function applyErrorsToForm(FormInterface $form, Result $result)
200
220
}
201
221
}
202
222
203
- private function getPaymentMethods ($ allowedMethods = array () )
223
+ private function getPaymentMethods ($ allowedMethods )
204
224
{
205
- $ allowAllMethods = ! count ($ allowedMethods );
225
+ $ allowAllMethods = empty ($ allowedMethods );
206
226
$ availableMethods = array ();
207
227
208
228
foreach ($ this ->paymentMethods as $ methodKey => $ methodClass ) {
0 commit comments