Skip to content

Commit 2ce47e1

Browse files
authored
Merge pull request #27 from yokai-php/configurable-enum
Configurable enum
2 parents 7cf0e23 + 9b8b058 commit 2ce47e1

21 files changed

+523
-221
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
composer.lock
33
/vendor/
44
/build/
5+
.idea/
6+
docker-compose.yml

DependencyInjection/EnumExtension.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Symfony\Component\DependencyInjection\ContainerBuilder;
77
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
88
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
9+
use Yokai\EnumBundle\Enum\EnumInterface;
910

1011
/**
1112
* @author Yann Eugoné <[email protected]>
@@ -27,5 +28,10 @@ public function load(array $configs, ContainerBuilder $container)
2728
$xmlLoader->load('forms.xml');
2829
$xmlLoader->load('validators.xml');
2930
$xmlLoader->load('twig.xml');
31+
32+
if (method_exists($container, 'registerForAutoconfiguration')) {
33+
$container->registerForAutoconfiguration(EnumInterface::class)
34+
->addTag('enum');
35+
}
3036
}
3137
}

Enum/ConfigurableEnum.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Yokai\EnumBundle\Enum;
4+
5+
/**
6+
* @author Yann Eugoné <[email protected]>
7+
*/
8+
class ConfigurableEnum implements EnumInterface
9+
{
10+
/**
11+
* @var string
12+
*/
13+
private $name;
14+
15+
/**
16+
* @var array
17+
*/
18+
private $choices;
19+
20+
/**
21+
* @param string $name
22+
* @param array $choices
23+
*/
24+
public function __construct($name, array $choices)
25+
{
26+
$this->name = $name;
27+
$this->choices = $choices;
28+
}
29+
30+
/**
31+
* @inheritdoc
32+
*/
33+
public function getName()
34+
{
35+
return $this->name;
36+
}
37+
38+
/**
39+
* @inheritdoc
40+
*/
41+
public function getChoices()
42+
{
43+
return $this->choices;
44+
}
45+
}

Enum/ConfigurableTranslatedEnum.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace Yokai\EnumBundle\Enum;
4+
5+
use Symfony\Component\Translation\TranslatorInterface;
6+
7+
/**
8+
* @author Yann Eugoné <[email protected]>
9+
*/
10+
class ConfigurableTranslatedEnum extends AbstractTranslatedEnum
11+
{
12+
/**
13+
* @var string
14+
*/
15+
private $name;
16+
17+
/**
18+
* @var array
19+
*/
20+
private $values;
21+
22+
/**
23+
* @param TranslatorInterface $translator
24+
* @param string $transPattern
25+
* @param string $name
26+
* @param array $values
27+
*/
28+
public function __construct(TranslatorInterface $translator, $transPattern, $name, array $values)
29+
{
30+
parent::__construct($translator, $transPattern);
31+
32+
$this->name = $name;
33+
$this->values = $values;
34+
}
35+
36+
/**
37+
* @inheritdoc
38+
*/
39+
public function getName()
40+
{
41+
return $this->name;
42+
}
43+
44+
/**
45+
* @inheritdoc
46+
*/
47+
public function getValues()
48+
{
49+
return $this->values;
50+
}
51+
}

Enum/EnumWithClassAsNameTrait.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Yokai\EnumBundle\Enum;
4+
5+
/**
6+
* @author Yann Eugoné <[email protected]>
7+
*/
8+
trait EnumWithClassAsNameTrait
9+
{
10+
/**
11+
* @return string
12+
*/
13+
public function getName()
14+
{
15+
return static::class;
16+
}
17+
}

README.md

Lines changed: 36 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -20,142 +20,86 @@ Installation
2020
### Add the bundle as dependency with Composer
2121

2222
``` bash
23-
$ php composer.phar require yokai/enum-bundle
23+
$ composer require yokai/enum-bundle
2424
```
2525

2626
### Enable the bundle in the kernel
2727

28-
``` php
28+
```php
2929
<?php
30-
// app/AppKernel.php
30+
// config/bundles.php
3131

32-
public function registerBundles()
33-
{
34-
$bundles = [
32+
return [
3533
// ...
36-
new Yokai\EnumBundle\YokaiEnumBundle(),
37-
];
38-
}
34+
Yokai\EnumBundle\YokaiEnumBundle::class => ['all' => true],
35+
];
3936
```
4037

4138

4239
Usage
4340
-----
4441

45-
Let's take an example : our application has some members and each member has a `gender` and a `state`.
42+
Let's take an example : our application has some members
43+
and each member has a `gender` which can be "male" (`m`) or "female" (`f`).
4644

4745
We first need to create the classes that will handle our enums :
4846

49-
``` php
47+
> **Note** this example is optimized for latest versions of Symfony, you will find more in dedicated doc file.
48+
49+
```php
5050
<?php
51-
// src/AppBundle/Enum/Member/GenderEnum.php
52-
namespace AppBundle\Enum\Member;
51+
// src/App/Enum/GenderEnum.php
52+
namespace App\Enum;
5353

5454
use Yokai\EnumBundle\Enum\EnumInterface;
55+
use Yokai\EnumBundle\Enum\EnumWithClassAsNameTrait;
5556

5657
class GenderEnum implements EnumInterface
5758
{
58-
const NAME = 'member.gender';
59+
use EnumWithClassAsNameTrait;
5960

6061
public function getChoices()
6162
{
62-
return ['male' => 'Male', 'female' => 'Female'];
63-
}
64-
65-
public function getName()
66-
{
67-
return static::NAME;
68-
}
69-
}
70-
```
71-
72-
``` php
73-
<?php
74-
// src/AppBundle/Enum/Member/StateEnum.php
75-
namespace AppBundle\Enum\Member;
76-
77-
use Yokai\EnumBundle\Enum\AbstractTranslatedEnum;
78-
79-
class StateEnum extends AbstractTranslatedEnum
80-
{
81-
const NAME = 'member.state';
82-
83-
protected function getValues()
84-
{
85-
return ['new', 'validated', 'disabled'];
86-
}
87-
88-
public function getName()
89-
{
90-
return static::NAME;
63+
return ['m' => 'Male', 'f' => 'Female'];
9164
}
9265
}
9366
```
9467

95-
Then we must declare these classes as services :
96-
97-
``` xml
98-
<!-- src/AppBundle/Resources/config/services.xml -->
99-
<services>
100-
<!-- ... -->
101-
102-
<service id="enum.member.gender" class="AppBundle\Enum\Member\GenderEnum" public="false">
103-
<tag name="enum"/>
104-
</service>
105-
106-
<service id="enum.member.state" class="AppBundle\Enum\Member\StateEnum"
107-
parent="enum.abstract_translated" public="false">
108-
<argument>choice.member.state.%s</argument>
109-
110-
<tag name="enum"/>
111-
</service>
112-
113-
</services>
114-
```
68+
If you are using [PSR-4 service discovery](https://symfony.com/blog/new-in-symfony-3-3-psr-4-based-service-discovery),
69+
then your service is already registered.
11570

11671
That's it, now the bundle know your enum services. You can start using it.
11772

11873
Adding validation to your model :
11974

120-
``` php
75+
```php
12176
<?php
122-
// src/AppBundle/Model/Member.php
123-
namespace AppBundle\Model;
77+
// src/App/Model/Member.php
78+
namespace App\Model;
12479

12580
use Yokai\EnumBundle\Validator\Constraints\Enum;
12681

12782
class Member
12883
{
129-
//...
130-
131-
/**
132-
* @var string
133-
*
134-
* @Enum("member.state")
135-
*/
136-
protected $state;
137-
13884
/**
13985
* @var string
14086
*
141-
* @Enum("member.gender")
87+
* @Enum("App\Enum\GenderEnum")
14288
*/
14389
protected $gender;
144-
145-
//...
14690
}
14791
```
14892

14993
Adding enum form types :
15094

151-
``` php
95+
```php
15296
<?php
153-
// src/AppBundle/Form/Type/MemberType.php
154-
namespace AppBundle\Form\Type;
97+
// src/App/Form/Type/MemberType.php
98+
namespace App\Form\Type;
15599

156-
use AppBundle\Enum\GenderEnum;
157-
use AppBundle\Enum\StateEnum;
100+
use App\Enum\GenderEnum;
158101
use Symfony\Component\Form\AbstractType;
102+
use Symfony\Component\Form\FormBuilderInterface;
159103
// For Symfony >= 2.8
160104
use Yokai\EnumBundle\Form\Type\EnumType;
161105

@@ -165,34 +109,30 @@ class MemberType extends AbstractType
165109
{
166110
$builder
167111
// Let the bundle guess the form type for you (requires that you configured the validation)
168-
->add('state')
169112
->add('gender')
170113

171114
// Manual form type binding for Symfony >= 2.8
172-
->add('state', EnumType::class, ['enum' => StateEnum::NAME])
173-
->add('gender', EnumType::class, ['enum' => GenderEnum::NAME])
115+
->add('gender', EnumType::class, ['enum' => GenderEnum::class])
174116

175117
// Manual form type binding for Symfony 2.7
176-
->add('state', 'enum', ['enum' => StateEnum::NAME])
177-
->add('gender', 'enum', ['enum' => GenderEnum::NAME])
118+
->add('gender', 'enum', ['enum' => GenderEnum::class])
178119
;
179120
}
180121
}
181122
```
182123

183-
184124
Displaying the label for an enum value within a template :
185125

186126
```twig
187-
{{ value|enum_label(constant('AppBundle\\Enum\\Member\\StateEnum::NAME')) }}
188-
{{ value|enum_label(constant('AppBundle\\Enum\\Member\\GenderEnum::NAME')) }}
127+
{{ value|enum_label('App\\Enum\\GenderEnum') }}
189128
```
190129

191130

192131
Recipes
193-
------------
132+
-------
194133

195134
- Usage in [SonataAdminBundle](https://github.com/sonata-project/SonataAdminBundle) : see [doc](Resources/doc/sonata-admin.md)
135+
- All the ways to declare [enums](Resources/doc/declaring-enum.md) or [translated enums](Resources/doc/declaring-translated-enum.md)
196136

197137

198138
MIT License
@@ -206,3 +146,7 @@ Authors
206146

207147
The bundle was originally created by [Yann Eugoné](https://github.com/yann-eugone).
208148
See the list of [contributors](https://github.com/yokai-php/enum-bundle/contributors).
149+
150+
---
151+
152+
Thank's to [Prestaconcept](https://github.com/prestaconcept) for supporting this bundle.

0 commit comments

Comments
 (0)