Skip to content

Commit b80fdaf

Browse files
authored
Add method_options and choice_options to form (schmittjoh#188)
1 parent 4d003aa commit b80fdaf

File tree

4 files changed

+100
-56
lines changed

4 files changed

+100
-56
lines changed

.travis.yml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,11 @@ matrix:
1313
# HHVM
1414
- php: hhvm
1515
env: SYMFONY_VERSION=3.1.*
16-
# current PHP with all non-EOLed Symfony versions
16+
# current PHP with all relevant Symfony versions
1717
- php: 7.0
1818
env: SYMFONY_VERSION=2.3.*
19-
- php: 7.0
20-
env: SYMFONY_VERSION=2.7.*
2119
- php: 7.0
2220
env: SYMFONY_VERSION=2.8.*
23-
- php: 7.0
24-
env: SYMFONY_VERSION=3.0.*
2521
- php: 7.0
2622
env: SYMFONY_VERSION=3.1.*
2723
- php: 7.0

Form/ChoosePaymentMethodType.php

Lines changed: 64 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {

Tests/Form/ChoosePaymentMethodTypeTest.php

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ public function testDefaultMethod()
9999
'default_method' => 'foo',
100100
));
101101

102-
$this->assertTrue($form->isSynchronized());
103102
$this->assertEquals('foo', $form->get('method')->getConfig()->getOption('data'));
104103
}
105104

@@ -113,8 +112,6 @@ public function testAllowedMethods()
113112
'allowed_methods' => array('bar'),
114113
));
115114

116-
$this->assertTrue($form->isSynchronized());
117-
118115
$choices = $form->get('method')->getConfig()->getOption('choices');
119116
$this->assertArrayNotHasKey('form.label.foo', $choices);
120117
$this->assertArraySubset(array('form.label.bar' => 'bar'), $choices);
@@ -138,6 +135,37 @@ public function testLegacyAllowedMethods()
138135
$this->assertArraySubset(array('bar' => 'form.label.bar'), $choices);
139136
}
140137

138+
public function testMethodOptions()
139+
{
140+
$form = $this->createForm(array('method_options' => array(
141+
'foo' => array(
142+
'attr' => array('foo_attr'),
143+
),
144+
'bar' => array(
145+
'attr' => array('bar_attr'),
146+
),
147+
)));
148+
149+
foreach (array('foo', 'bar') as $method) {
150+
$this->assertArraySubset(
151+
array($method.'_attr'),
152+
$form->get('data_'.$method)->getConfig()->getOption('attr')
153+
);
154+
}
155+
}
156+
157+
public function testChoiceOptions()
158+
{
159+
$form = $this->createForm(array('choice_options' => array(
160+
'expanded' => false,
161+
'data' => 'baz',
162+
)));
163+
164+
$config = $form->get('method')->getConfig();
165+
$this->assertFalse($config->getOption('expanded'));
166+
$this->assertEquals('baz', $config->getOption('data'));
167+
}
168+
141169
private function createForm($options = array(), $data = array())
142170
{
143171
$options = array_merge(array(

Tests/Form/Transformer/ChoosePaymentMethodTransformerTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function testTransformNullData()
1919
*/
2020
public function testTransformNotPaymentInstructionObject()
2121
{
22-
$this->assertNull($this->transform(new self()));
22+
$this->transform(new self());
2323
}
2424

2525
public function testTransform()
@@ -68,7 +68,7 @@ public function testReverseTransformNullData()
6868
*/
6969
public function testReverseTransformNotArray()
7070
{
71-
$this->assertNull($this->reverseTransform(new self()));
71+
$this->reverseTransform(new self());
7272
}
7373

7474
/**
@@ -77,7 +77,7 @@ public function testReverseTransformNotArray()
7777
*/
7878
public function testReverseTransformNoAmount()
7979
{
80-
$this->assertNull($this->reverseTransform(array()));
80+
$this->reverseTransform(array());
8181
}
8282

8383
/**
@@ -86,7 +86,7 @@ public function testReverseTransformNoAmount()
8686
*/
8787
public function testReverseTransformNoCurrency()
8888
{
89-
$this->assertNull($this->reverseTransform(array(), array('amount' => '10.42')));
89+
$this->reverseTransform(array(), array('amount' => '10.42'));
9090
}
9191

9292
public function testReverseTransform()

0 commit comments

Comments
 (0)