Skip to content

Commit 34e3e78

Browse files
author
Yann Eugoné
committed
Merge pull request #3 from yann-eugone/symfony-3-compatibility
Symfony 3 compatibility
2 parents 4ea6b61 + 01c6a4d commit 34e3e78

File tree

5 files changed

+52
-22
lines changed

5 files changed

+52
-22
lines changed

.travis.yml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,9 @@ matrix:
1212
- php: 5.6
1313
env: SYMFONY_VERSION=2.7.*
1414
- php: 5.6
15-
env: SYMFONY_VERSION=2.8.*@dev
15+
env: SYMFONY_VERSION=2.8.*
1616
- php: 5.6
17-
env: SYMFONY_VERSION="3.0.x-dev as 2.8"
18-
allow_failures:
19-
- php: hhvm
20-
- env: SYMFONY_VERSION=2.8.*@dev
21-
- env: SYMFONY_VERSION="3.0.x-dev as 2.8"
17+
env: SYMFONY_VERSION="3.0.*"
2218

2319
sudo: false
2420

Form/Type/EnumType.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use EnumBundle\Registry\EnumRegistryInterface;
66
use Symfony\Component\Form\AbstractType;
7+
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
78
use Symfony\Component\OptionsResolver\Options;
89
use Symfony\Component\OptionsResolver\OptionsResolver;
910

@@ -32,6 +33,7 @@ public function configureOptions(OptionsResolver $resolver)
3233
{
3334
$resolver
3435
->setRequired('enum')
36+
->setDefault('choices_as_values', true)
3537
->setAllowedValues(
3638
'enum',
3739
function ($name) {
@@ -52,14 +54,26 @@ function (Options $options) {
5254
*/
5355
public function getParent()
5456
{
55-
return 'choice';
57+
if (!method_exists(AbstractType::class, 'getBlockPrefix')) {
58+
return 'choice'; //Symfony 2.x support
59+
}
60+
61+
return ChoiceType::class;
5662
}
5763

5864
/**
5965
* {@inheritdoc}
6066
*/
61-
public function getName()
67+
public function getBlockPrefix()
6268
{
6369
return 'enum';
6470
}
71+
72+
/**
73+
* {@inheritdoc}
74+
*/
75+
public function getName()
76+
{
77+
return $this->getBlockPrefix();
78+
}
6579
}

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,22 +153,24 @@ namespace AppBundle\Form\Type;
153153

154154
use AppBundle\Enum\GenderEnum;
155155
use AppBundle\Enum\StateEnum;
156-
//...
156+
use Symfony\Component\Form\AbstractType;
157+
// For Symfony >= 2.8
158+
use EnumBundle\Form\Type\EnumType;
157159

158-
class MemberType extends //...
160+
class MemberType extends AbstractType
159161
{
160-
//...
161-
162162
public function buildForm(FormBuilderInterface $builder, array $options)
163163
{
164164
$builder
165-
//...
165+
// For Symfony >= 2.8
166+
->add('state', EnumType::class, ['enum' => StateEnum::NAME])
167+
->add('gender', EnumType::class, ['enum' => GenderEnum::NAME])
168+
169+
// For Symfony 2.7
166170
->add('state', 'enum', ['enum' => StateEnum::NAME])
167171
->add('gender', 'enum', ['enum' => GenderEnum::NAME])
168172
;
169173
}
170-
171-
//...
172174
}
173175
```
174176

Tests/Form/Type/EnumTypeTest.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace EnumBundle\Tests\Form\Type;
44

5+
use EnumBundle\Form\Type\EnumType;
56
use EnumBundle\Tests\Form\TestExtension;
67
use EnumBundle\Tests\GenderEnum;
8+
use Symfony\Component\Form\AbstractType;
79
use Symfony\Component\Form\Test\TypeTestCase;
810

911
/**
@@ -26,18 +28,18 @@ protected function setUp()
2628
public function testEnumOptionIsRequired()
2729
{
2830
$this->setExpectedException('Symfony\Component\OptionsResolver\Exception\MissingOptionsException');
29-
$this->factory->create('enum');
31+
$this->createForm();
3032
}
3133

3234
public function testEnumOptionIsInvalid()
3335
{
3436
$this->setExpectedException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
35-
$this->factory->create('enum', null, ['enum' => 'state']);
37+
$this->createForm('state');
3638
}
3739

3840
public function testEnumOptionValid()
3941
{
40-
$form = $this->factory->create('enum', null, ['enum' => 'gender']);
42+
$form = $this->createForm('gender');
4143

4244
$this->assertEquals(['male' => 'Male', 'female' => 'Female'], $form->getConfig()->getOption('choices'));
4345
}
@@ -48,4 +50,20 @@ protected function getExtensions()
4850
new TestExtension($this->enumRegistry->reveal())
4951
];
5052
}
53+
54+
private function createForm($enum = null)
55+
{
56+
$options = [];
57+
if ($enum) {
58+
$options['enum'] = $enum;
59+
}
60+
61+
if (method_exists(AbstractType::class, 'getBlockPrefix')) {
62+
$name = EnumType::class; //Symfony 3.x support
63+
} else {
64+
$name = 'enum'; //Symfony 2.x support
65+
}
66+
67+
return $this->factory->create($name, null, $options);
68+
}
5169
}

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@
99
}
1010
],
1111
"require": {
12-
"php": ">=5.5",
13-
"symfony/framework-bundle": "~2.7",
12+
"php": ">=5.5.9",
13+
"symfony/framework-bundle": "~2.7|~3.0",
1414
"twig/twig": "~1.20|~2.0"
1515
},
1616
"require-dev": {
17-
"symfony/form": "~2.7",
18-
"symfony/validator": "~2.7",
17+
"symfony/form": "~2.7|~3.0",
18+
"symfony/validator": "~2.7|~3.0",
1919
"phpunit/phpunit": "~4.6"
2020
},
2121
"autoload": {

0 commit comments

Comments
 (0)