This repository aims to provide simple enumeration implementation to Symfony.
$ composer require yokai/enum-bundle<?phpreturn [
Yokai\EnumBundle\YokaiEnumBundle::class => ['all' => true],
];Let's take an example : our application has some members
and each member has a status which can be :
new, labelled as "New"validated, labelled as "Validated"disabled, labelled as "Disabled"
We first need to create the class that will handle our enum :
<?phpdeclare(strict_types=1);
namespaceApp\Enum;
useYokai\EnumBundle\Enum;
class StatusEnum extends Enum
{
publicfunction__construct()
{
parent::__construct(['New' => 'new', 'Validated' => 'validated', 'Disabled' => 'disabled']);
}
}That's it, the bundle now knows your enum.
note : every enum has a name. That name is the enum identifier across your application. You can use any string for that purpose, as long it is unique. Using the enum class here is a very common way to do.
We will now be able to configure Member's model validation :
<?phpdeclare(strict_types=1);
namespaceApp\Model;
useApp\Enum\StatusEnum;
useYokai\EnumBundle\Validator\Constraints\Enum;
class Member
{
#[Enum(enum: StatusEnum::class)]
public ?string$status = null;
}Now that validation is configured, the only thing we have to do is to add a field on our form :
<?phpdeclare(strict_types=1);
namespaceApp\Form\Type;
useApp\Model\Member;
useSymfony\Component\Form\AbstractType;
useSymfony\Component\Form\FormBuilderInterface;
useSymfony\Component\OptionsResolver\OptionsResolver;
class MemberType extends AbstractType
{
publicfunctionbuildForm(FormBuilderInterface$builder, array$options): void
{
$builder// Because we added the #[Enum] constraint to Member::$status property// the bundle will be able to find out the appropriate form type automatically
->add('status')
;
}
publicfunctionconfigureOptions(OptionsResolver$resolver): void
{
$resolver->setDefault('data_class', Member::class);
}
}Display label of any enum value within a Twig template :
{{ member.status|enum_label('App\\Enum\\StatusEnum') }}Now, maybe you will need to display the enum label in different locales.
We got you covered here with a dedicated base class for your translated enums :
<?phpdeclare(strict_types=1);
namespaceApp\Enum;
useSymfony\Contracts\Translation\TranslatorInterface;
useYokai\EnumBundle\TranslatedEnum;
class StatusEnum extends TranslatedEnum
{
publicfunction__construct(TranslatorInterface$translator)
{
parent::__construct(['new', 'validated', 'disabled'], $translator, 'status.%s');
}
}Now you can create the translation keys in your catalog :
# translations/messages.en.yamlstatus.new: Newstatus.validated: Validatedstatus.disabled: Disabled# translations/messages.fr.yamlstatus.new: Nouveaustatus.validated: Validéstatus.disabled: Désactivénote : the translation key format is generated using the
$transPatternconstructor argument, which must be valid a sprintf pattern (containing one%s)
See examples from unit test suite & associated tests.
See example Symfony project in integration test suite.
- Creating enums
- Creating translated enums
- Integration with PHP native enum
- Integration with myclabs/php-enum
- Migrating from standard Symfony
- Integration with SonataAdminBundle
License can be found here.
The bundle was originally created by Yann Eugoné. See the list of contributors.
Thank's to PrestaConcept for supporting this bundle.