2017 © Pedro Peláez
 

library enum-bundle

Enum in PHP with Symfony & Doctrine integration

image

everlutionsk/enum-bundle

Enum in PHP with Symfony & Doctrine integration

  • Thursday, November 30, 2017
  • by ivanbarlog
  • Repository
  • 4 Watchers
  • 0 Stars
  • 254 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 4 Versions
  • 1 % Grown

The README.md

EnumBundle

Enum in PHP with Symfony & Doctrine integration., (*1)

Allows for cleaner, object-oriented SRP code., (*2)

Installation

composer require everlutionsk/enum-bundle, (*3)

$bundles = [
    new Everlution\EnumBundle\EnumBundle(),
]

Implementation

Create new Enum

<?php
// always use final
final class Gender extends Enum
{
    const MALE = 1;
    const FEMALE = 2;
    const OTHER = 3;
    public static function getChoices(): array
    {
        // 'string represtation' => self::constant
        return [
            'gender.male' => self::MALE,
            'gender.female' => self::FEMALE,
            'gender.other' => self::OTHER,
        ];
    }

    // following is optional
    public function isMale(): bool
    {
        return $this->isValue(self::MALE);
    }

    public function isFemale(): bool
    {
        return $this->isValue(self::FEMALE);
    }
}

Use it!

PHP:, (*4)

<?php
$male = new Gender(Gender::MALE);
var_dump($male->isMale()); // bool(true)
var_dump($male->isSameAs(new Gender(Gender::FEMALE))); // bool(false)
var_dump($male->isValue(Gender::FEMALE)); // bool(false)
echo $male->getValue(); // Gender::MALE
echo $male->getLabel(); // 'gender.male'
echo $male; // 'gender.male' (has __toString() implemented)

Twig:, (*5)

{{ gender|trans }}

Map Enum to database

In your app, create a service that's called exactly enum.db_map and make it implement EnumDBMapInterface., (*6)

Configuration in plain PHP is good for refactoring., (*7)

<?php
/**
 * Class EnumDBMap.
 *
 * @author Richard Popelis <richard@popelis.sk>
 */
class EnumDBMap implements EnumDBMapInterface
{
    public function getMap(): array
    {
        return [
            'enum_gender' => Gender::class,
            // 'enum_visibility' => Visibility::class, // and so on
        ];
    }
}

Use Enum in entities

class User
{
    /**
     * @var Gender
     * @ORM\Column(type="enum_gender")
     */
    private $gender;

    /**
     * @return Gender
     */
    public function getGender(): ?Gender
    {
        return $this->gender;
    }

    /**
     * @param Gender $gender
     * @return User
     */
    public function setGender(?Gender $gender): self
    {
        $this->gender = $gender;

        return $this;
    }
}

## usage
$user = new User;
$user->setGender(new Gender(Gender::MALE));

Use Enum seamlessly in forms

class UserType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('gender', EnumChoiceType::class, [
            'enum_class' => Gender::class,
            // optional blacklisting: remove OTHER from form field choices
            'enum_blacklist' => [Gender::OTHER],
            // optional whitelisting: show only specified values
            'enum_whitelist' => [Gender::MALE, Gender::FEMALE],
        ]);
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => User::class,
        ]);
    }
}

No DataTransformer needed to be explicitly declared. It is already built-in in the form type., (*8)

EnumRadioType is also available., (*9)

Happy Enum-ing!, (*10)

The Versions

30/11 2017

dev-master

9999999-dev

Enum in PHP with Symfony & Doctrine integration

  Sources   Download

MIT

The Requires

 

by Avatar riki137

30/11 2017

v1.1.0

1.1.0.0

Enum in PHP with Symfony & Doctrine integration

  Sources   Download

MIT

The Requires

 

by Avatar riki137

20/11 2017

dev-sf-4

dev-sf-4

Enum in PHP with Symfony & Doctrine integration

  Sources   Download

MIT

The Requires

 

by Avatar riki137

07/09 2017

1.0

1.0.0.0

Enum in PHP with Symfony & Doctrine integration

  Sources   Download

MIT

The Requires

 

by Avatar riki137