2017 © Pedro PelĂĄez
 

library enum-bundle

Simple enumeration system with Symfony integration

image

yokai/enum-bundle

Simple enumeration system with Symfony integration

  • Saturday, March 3, 2018
  • by yann-eugone
  • Repository
  • 4 Watchers
  • 7 Stars
  • 11,012 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 7 Open issues
  • 20 Versions
  • 70 % Grown

The README.md

YokaiEnumBundle

Tests Coverage Contributors License, (*1)

Latest Stable Version Latest Unstable Version Total Downloads Downloads Monthly, (*2)

This repository aims to provide simple enumeration implementation to Symfony., (*3)

Installation

Add the bundle as a dependency with Composer

$ composer require yokai/enum-bundle

Enable the bundle in the kernel

<?php
return [
    Yokai\EnumBundle\YokaiEnumBundle::class => ['all' => true],
];

Getting started

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", (*4)

Creating the enum

We first need to create the class that will handle our enum :, (*5)

<?php

declare(strict_types=1);

namespace App\Enum;

use Yokai\EnumBundle\Enum;

class StatusEnum extends Enum
{
    public function __construct()
    {
        parent::__construct(['New' => 'new', 'Validated' => 'validated', 'Disabled' => 'disabled']);
    }
}

That's it, the bundle now knows your enum., (*6)

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., (*7)

Configuring validation

We will now be able to configure Member's model validation :, (*8)

<?php

declare(strict_types=1);

namespace App\Model;

use App\Enum\StatusEnum;
use Yokai\EnumBundle\Validator\Constraints\Enum;

class Member
{
     #[Enum(enum: StatusEnum::class)]
    public ?string $status = null;
}

Setting up the form

Now that validation is configured, the only thing we have to do is to add a field on our form :, (*9)

<?php

declare(strict_types=1);

namespace App\Form\Type;

use App\Model\Member;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class MemberType extends AbstractType
{
    public function buildForm(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')
        ;
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefault('data_class', Member::class);
    }
}

Rendering enum label

Display label of any enum value within a Twig template :, (*10)

{{ member.status|enum_label('App\\Enum\\StatusEnum') }}

Translating your enum

Now, maybe you will need to display the enum label in different locales., (*11)

We got you covered here with a dedicated base class for your translated enums :, (*12)

<?php

declare(strict_types=1);

namespace App\Enum;

use Symfony\Contracts\Translation\TranslatorInterface;
use Yokai\EnumBundle\TranslatedEnum;

class StatusEnum extends TranslatedEnum
{
    public function __construct(TranslatorInterface $translator)
    {
        parent::__construct(['new', 'validated', 'disabled'], $translator, 'status.%s');
    }
}

Now you can create the translation keys in your catalog :, (*13)

# translations/messages.en.yaml
status.new: New
status.validated: Validated
status.disabled: Disabled
# translations/messages.fr.yaml
status.new: Nouveau
status.validated: Validé
status.disabled: Désactivé

note : the translation key format is generated using the $transPattern constructor argument, which must be valid a sprintf pattern (containing one %s), (*14)

More examples

See examples from unit test suite & associated tests., (*15)

See example Symfony project in integration test suite., (*16)

Recipes

MIT License

License can be found here., (*17)

Authors

The bundle was originally created by Yann Eugoné. See the list of contributors., (*18)


Thank's to PrestaConcept for supporting this bundle., (*19)

The Versions

03/03 2018

3.x-dev

3.9999999.9999999.9999999-dev

Simple enumeration system with Symfony integration

  Sources   Download

MIT

The Requires

  • php >=5.6

 

The Development Requires

by Yann Eugoné

14/02 2018

dev-master

9999999-dev

Bring simple enumeration implementation to Symfony

  Sources   Download

MIT

The Requires

 

The Development Requires

by Yann Eugoné

14/02 2018

v2.2

2.2.0.0

Bring simple enumeration implementation to Symfony

  Sources   Download

MIT

The Requires

 

The Development Requires

by Yann Eugoné

13/02 2018

dev-auto-registration-deprecation

dev-auto-registration-deprecation

Bring simple enumeration implementation to Symfony

  Sources   Download

MIT

The Requires

 

The Development Requires

by Yann Eugoné

16/01 2018

dev-configurable-enum

dev-configurable-enum

Bring simple enumeration implementation to Symfony

  Sources   Download

MIT

The Requires

 

The Development Requires

by Yann Eugoné

16/01 2018

dev-travis-matrix-from-symfony-best-practices

dev-travis-matrix-from-symfony-best-practices

Bring simple enumeration implementation to Symfony

  Sources   Download

MIT

The Requires

 

The Development Requires

by Yann Eugoné

15/01 2018

dev-form-type-guesser

dev-form-type-guesser

Bring simple enumeration implementation to Symfony2

  Sources   Download

MIT

The Requires

 

The Development Requires

by Yann Eugoné

13/01 2018

dev-symfony-4-symfony-flex-support

dev-symfony-4-symfony-flex-support

Bring simple enumeration implementation to Symfony

  Sources   Download

MIT

The Requires

 

The Development Requires

by Yann Eugoné

13/01 2018

dev-bugfix-configuration-name

dev-bugfix-configuration-name

Bring simple enumeration implementation to Symfony2

  Sources   Download

MIT

The Requires

 

The Development Requires

by Yann Eugoné

06/12 2017

v2.1

2.1.0.0

Bring simple enumeration implementation to Symfony2

  Sources   Download

MIT

The Requires

 

The Development Requires

by Yann Eugoné

01/04 2017

dev-sensio-insight-fixes

dev-sensio-insight-fixes

Bring simple enumeration implementation to Symfony2

  Sources   Download

MIT

The Requires

 

The Development Requires

by Yann Eugoné

01/04 2017

v2.0

2.0.0.0

Bring simple enumeration implementation to Symfony2

  Sources   Download

MIT

The Requires

 

The Development Requires

by Yann Eugoné

01/04 2017

2.x-dev

2.9999999.9999999.9999999-dev

Bring simple enumeration implementation to Symfony2

  Sources   Download

MIT

The Requires

 

The Development Requires

by Yann Eugoné

04/03 2016

v1.6

1.6.0.0

Bring simple enumeration implementation to Symfony2

  Sources   Download

MIT

The Requires

 

The Development Requires

by Yann Eugoné

04/03 2016

v1.5

1.5.0.0

Bring simple enumeration implementation to Symfony2

  Sources   Download

MIT

The Requires

 

The Development Requires

by Yann Eugoné

28/12 2015

v1.4

1.4.0.0

Bring simple enumeration implementation to Symfony2

  Sources   Download

MIT

The Requires

 

The Development Requires

by Yann Eugoné

06/10 2015

v1.3

1.3.0.0

Bring simple enumeration implementation to Symfony2

  Sources   Download

Apache2

The Requires

 

The Development Requires

by Yann Eugoné

05/10 2015

v1.2

1.2.0.0

Bring simple enumeration implementation to Symfony2

  Sources   Download

Apache2

The Requires

 

The Development Requires

by Yann Eugoné

01/10 2015

v1.1

1.1.0.0

Bring simple enumeration implementation to Symfony2

  Sources   Download

Apache2

The Requires

 

The Development Requires

by Yann Eugoné

01/10 2015

v1.0

1.0.0.0

Bring simple enumeration implementation to Symfony2

  Sources   Download

Apache2

The Requires

 

The Development Requires

by Yann Eugoné