Skip to content

Commit 4ab7316

Browse files
committed
Rewrite documentation
1 parent 7ecc05b commit 4ab7316

16 files changed

+760
-319
lines changed

README.md

Lines changed: 96 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
1-
YokaiEnumBundle
2-
==============
1+
# YokaiEnumBundle
32

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)
59
[![Latest Unstable Version](https://poser.pugx.org/yokai/enum-bundle/v/unstable)](https://packagist.org/packages/yokai/enum-bundle)
610
[![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)
812

913
This repository aims to provide simple enumeration implementation to Symfony.
1014

1115

12-
Installation
13-
------------
16+
## Installation
1417

1518
### Add the bundle as a dependency with Composer
1619

17-
``` bash
20+
```bash
1821
$ composer require yokai/enum-bundle
1922
```
2023

@@ -28,13 +31,17 @@ return [
2831
```
2932

3033

31-
Usage
32-
-----
34+
## Getting started
3335

3436
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
3643

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 :
3845

3946
```php
4047
<?php
@@ -43,26 +50,27 @@ declare(strict_types=1);
4350

4451
namespace App\Enum;
4552

46-
use Yokai\EnumBundle\EnumInterface;
47-
use Yokai\EnumBundle\EnumWithClassAsNameTrait;
53+
use Yokai\EnumBundle\Enum;
4854

49-
class GenderEnum implements EnumInterface
55+
class StatusEnum extends Enum
5056
{
51-
use EnumWithClassAsNameTrait;
52-
53-
public function getChoices(): array
57+
public function __construct()
5458
{
55-
return ['m' => 'Male', 'f' => 'Female'];
59+
parent::__construct(__CLASS__, ['New' => 'new', 'Validated' => 'validated', 'Disabled' => 'disabled']);
5660
}
5761
}
5862
```
5963

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.
6270
63-
That's it, now the bundle know your enum services. You can start using it.
71+
### Configuring validation
6472

65-
Add validation to any model :
73+
We will now be able to configure `Member`'s model validation :
6674

6775
```php
6876
<?php
@@ -71,19 +79,21 @@ declare(strict_types=1);
7179

7280
namespace App\Model;
7381

74-
use App\Enum\GenderEnum;
82+
use App\Enum\StatusEnum;
7583
use Yokai\EnumBundle\Validator\Constraints\Enum;
7684

7785
class Member
7886
{
7987
/**
80-
* @Enum(GenderEnum::class)
88+
* @Enum(StatusEnum::class)
8189
*/
82-
public ?string $gender = null;
90+
public ?string $status = null;
8391
}
8492
```
8593

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 :
8797

8898
```php
8999
<?php
@@ -102,8 +112,9 @@ class MemberType extends AbstractType
102112
public function buildForm(FormBuilderInterface $builder, array $options): void
103113
{
104114
$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')
107118
;
108119
}
109120

@@ -114,32 +125,79 @@ class MemberType extends AbstractType
114125
}
115126
```
116127

128+
### Rendering enum label
129+
117130
Display label of any enum value within a Twig template :
118131

119132
```twig
120-
{{ value|enum_label('App\\Enum\\GenderEnum') }}
133+
{{ member.status|enum_label('App\\Enum\\StatusEnum') }}
121134
```
122135

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+
123182

124-
Recipes
125-
-------
183+
## Recipes
126184

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)
129189

130190

131-
MIT License
132-
-----------
191+
## MIT License
133192

134193
License can be found [here](https://github.com/yokai-php/enum-bundle/blob/master/Resources/meta/LICENSE).
135194

136195

137-
Authors
138-
-------
196+
## Authors
139197

140198
The bundle was originally created by [Yann Eugoné](https://github.com/yann-eugone).
141199
See the list of [contributors](https://github.com/yokai-php/enum-bundle/contributors).
142200

143201
---
144202

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.

doc/declaring-enum.md

Lines changed: 0 additions & 130 deletions
This file was deleted.

0 commit comments

Comments
 (0)