1
- YokaiEnumBundle
2
- ==============
1
+ # YokaiEnumBundle
3
2
4
- [ ![ Latest Stable Version] ( https://poser.pugx.org/yokai/enum-bundle/v/stable )] ( https://packagist.org/packages/yokai/enum-bundle )
3
+ [ ![ Tests] ( https://img.shields.io/github/workflow/status/yokai-php/enum-bundle/Tests?style=flat-square&label=tests )] ( https://github.com/yokai-php/enum-bundle/actions )
4
+ [ ![ Coverage] ( https://img.shields.io/codecov/c/github/yokai-php/enum-bundle?style=flat-square )] ( https://codecov.io/gh/yokai-php/enum-bundle )
5
+ [ ![ Contributors] ( https://img.shields.io/github/contributors/yokai-php/enum-bundle?style=flat-square )] ( https://github.com/yokai-php/enum-bundle/graphs/contributors )
6
+ [ ![ License] ( https://poser.pugx.org/yokai/enum-bundle/license )] ( https://packagist.org/packages/yokai/enum-bundle )
7
+
8
+ [ ![ Latest Stable Version] ( https://img.shields.io/packagist/v/yokai/enum-bundle?style=flat-square )] ( https://packagist.org/packages/yokai/enum-bundle )
5
9
[ ![ Latest Unstable Version] ( https://poser.pugx.org/yokai/enum-bundle/v/unstable )] ( https://packagist.org/packages/yokai/enum-bundle )
6
10
[ ![ Total Downloads] ( https://poser.pugx.org/yokai/enum-bundle/downloads )] ( https://packagist.org/packages/yokai/enum-bundle )
7
- [ ![ License ] ( https://poser.pugx.org/ yokai/enum-bundle/license )] ( https://packagist.org/packages/yokai/enum-bundle )
11
+ [ ![ Downloads Monthly ] ( https://img.shields.io/packagist/dm/ yokai/enum-bundle?style=flat-square )] ( https://packagist.org/packages/yokai/enum-bundle/stats )
8
12
9
13
This repository aims to provide simple enumeration implementation to Symfony.
10
14
11
15
12
- Installation
13
- ------------
16
+ ## Installation
14
17
15
18
### Add the bundle as a dependency with Composer
16
19
17
- ``` bash
20
+ ``` bash
18
21
$ composer require yokai/enum-bundle
19
22
```
20
23
@@ -28,13 +31,17 @@ return [
28
31
```
29
32
30
33
31
- Usage
32
- -----
34
+ ## Getting started
33
35
34
36
Let's take an example : our application has some members
35
- and each member has a ` gender ` which can be "male" (` m ` ) or "female" (` f ` ).
37
+ and each member has a ` status ` which can be :
38
+ - ` new ` , labelled as "New"
39
+ - ` validated ` , labelled as "Validated"
40
+ - ` disabled ` , labelled as "Disabled"
41
+
42
+ ### Creating the enum
36
43
37
- We first need to create the classes that will handle our enums :
44
+ We first need to create the class that will handle our enum :
38
45
39
46
``` php
40
47
<?php
@@ -43,26 +50,27 @@ declare(strict_types=1);
43
50
44
51
namespace App\Enum;
45
52
46
- use Yokai\EnumBundle\EnumInterface;
47
- use Yokai\EnumBundle\EnumWithClassAsNameTrait;
53
+ use Yokai\EnumBundle\Enum;
48
54
49
- class GenderEnum implements EnumInterface
55
+ class StatusEnum extends Enum
50
56
{
51
- use EnumWithClassAsNameTrait;
52
-
53
- public function getChoices(): array
57
+ public function __construct()
54
58
{
55
- return ['m ' => 'Male ', 'f ' => 'Female'] ;
59
+ parent::__construct(__CLASS__, ['New ' => 'new ', 'Validated ' => 'validated', 'Disabled' => 'disabled']) ;
56
60
}
57
61
}
58
62
```
59
63
60
- If you are using [ PSR-4 service discovery] ( https://symfony.com/blog/new-in-symfony-3-3-psr-4-based-service-discovery )
61
- (or Symfony default services file), then your service is already registered.
64
+ That's it, the bundle now knows your enum.
65
+
66
+ > ** note** : every enum has a ** name** .
67
+ > That name is the enum identifier across your application.
68
+ > You can use any string for that purpose, ** as long it is unique** .
69
+ > ** Using the enum class here** is a very common way to do.
62
70
63
- That's it, now the bundle know your enum services. You can start using it.
71
+ ### Configuring validation
64
72
65
- Add validation to any model :
73
+ We will now be able to configure ` Member ` 's model validation :
66
74
67
75
``` php
68
76
<?php
@@ -71,19 +79,21 @@ declare(strict_types=1);
71
79
72
80
namespace App\Model;
73
81
74
- use App\Enum\GenderEnum ;
82
+ use App\Enum\StatusEnum ;
75
83
use Yokai\EnumBundle\Validator\Constraints\Enum;
76
84
77
85
class Member
78
86
{
79
87
/**
80
- * @Enum(GenderEnum ::class)
88
+ * @Enum(StatusEnum ::class)
81
89
*/
82
- public ?string $gender = null;
90
+ public ?string $status = null;
83
91
}
84
92
```
85
93
86
- Add enumerated form fields to any form :
94
+ ### Setting up the form
95
+
96
+ Now that validation is configured, the only thing we have to do is to add a field on our form :
87
97
88
98
``` php
89
99
<?php
@@ -102,8 +112,9 @@ class MemberType extends AbstractType
102
112
public function buildForm(FormBuilderInterface $builder, array $options): void
103
113
{
104
114
$builder
105
- // The bundle will find out the form type for you (thanks to the Enum constraint we added to model)
106
- ->add('gender')
115
+ // Because we added the @Enum constraint to Member::$status property
116
+ // the bundle will be able to find out the appropriate form type automatically
117
+ ->add('status')
107
118
;
108
119
}
109
120
@@ -114,32 +125,79 @@ class MemberType extends AbstractType
114
125
}
115
126
```
116
127
128
+ ### Rendering enum label
129
+
117
130
Display label of any enum value within a Twig template :
118
131
119
132
``` twig
120
- {{ value |enum_label('App\\Enum\\GenderEnum ') }}
133
+ {{ member.status |enum_label('App\\Enum\\StatusEnum ') }}
121
134
```
122
135
136
+ ### Translating your enum
137
+
138
+ Now, maybe you will need to display the enum label in different locales.
139
+
140
+ We got you covered here with a dedicated base class for your translated enums :
141
+
142
+ ``` php
143
+ <?php
144
+
145
+ declare(strict_types=1);
146
+
147
+ namespace App\Enum;
148
+
149
+ use Symfony\Contracts\Translation\TranslatorInterface;
150
+ use Yokai\EnumBundle\TranslatedEnum;
151
+
152
+ class StatusEnum extends TranslatedEnum
153
+ {
154
+ public function __construct(TranslatorInterface $translator)
155
+ {
156
+ parent::__construct(__CLASS__, ['new', 'validated', 'disabled'], $translator, 'status.%s');
157
+ }
158
+ }
159
+ ```
160
+
161
+ Now you can create the translation keys in your catalog :
162
+
163
+ ``` yaml
164
+ # translations/messages.en.yaml
165
+ status.new : New
166
+ status.validated : Validated
167
+ status.disabled : Disabled
168
+ # translations/messages.fr.yaml
169
+ status.new : Nouveau
170
+ status.validated : Validé
171
+ status.disabled : Désactivé
172
+ ` ` `
173
+
174
+ > **note :** the translation key format is generated using the ` $transPattern` constructor argument,
175
+ > which must be valid a [sprintf](https://www.php.net/manual/en/function.sprintf.php) pattern (containing one `%s`)
176
+
177
+
178
+ # # More examples
179
+
180
+ See examples from [test suite](tests/Fixtures) & associated [tests](tests/EnumsFromFixturesTest.php).
181
+
123
182
124
- Recipes
125
- -------
183
+ # # Recipes
126
184
127
- - Usage in [ SonataAdminBundle] ( https://github.com/sonata-project/SonataAdminBundle ) : see [ doc] ( doc/sonata-admin.md )
128
- - All the ways to declare [ enums] ( doc/declaring-enum.md ) or [ translated enums] ( doc/declaring-translated-enum.md )
185
+ - Creating [enums](docs/creating-enum.md)
186
+ - Creating [translated enums](docs/creating-translated-enum.md)
187
+ - Migrating [from standard Symfony](docs/migrating-from-symfony-standard.md)
188
+ - Integration with [SonataAdminBundle](docs/sonata-admin-integration.md)
129
189
130
190
131
- MIT License
132
- -----------
191
+ # # MIT License
133
192
134
193
License can be found [here](https://github.com/yokai-php/enum-bundle/blob/master/Resources/meta/LICENSE).
135
194
136
195
137
- Authors
138
- -------
196
+ # # Authors
139
197
140
198
The bundle was originally created by [Yann Eugoné](https://github.com/yann-eugone).
141
199
See the list of [contributors](https://github.com/yokai-php/enum-bundle/contributors).
142
200
143
201
---
144
202
145
- Thank's to [ Prestaconcept ] ( https://github.com/prestaconcept ) for supporting this bundle.
203
+ Thank's to [PrestaConcept ](https://github.com/prestaconcept) for supporting this bundle.
0 commit comments