2017 © Pedro PelĂĄez
 

library php-enum-symfony

Symfony PHP enum support

image

zlikavac32/php-enum-symfony

Symfony PHP enum support

  • Friday, January 26, 2018
  • by zlikavac32
  • Repository
  • 1 Watchers
  • 1 Stars
  • 36 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 20 % Grown

The README.md

PHP Enum Symfony

Build Status Latest Stable Version License Scrutinizer Code Quality Codacy Badge, (*1)

Symfony support for zlikavac32/php-enum., (*2)

Table of contents

  1. Installation
  2. Usage
    1. Form
    2. Validator
  3. Limitations

Installation

Recommended installation is through Composer., (*3)

composer require zlikavac32/php-enum-symfony

Usage

Assumption is that there exists a valid enum \YesNoEnum., (*4)

Form

Form type for enum is provided as \Zlikavac32\SymfonyEnum\Form\Type\EnumType. There is one required options enum_class which must contain enum class FQN., (*5)

Internally this extends \Symfony\Component\Form\Extension\Core\Type\ChoiceType and populates choices from the defining enum_class., (*6)

If any of the choices and/or choice_loader options is/are passed, an \LogicException will be thrown. Since these fields are overridden internally, passing them from the outside could cloud code's original purpose. Any other option provided by the \Symfony\Component\Form\Extension\Core\Type\ChoiceType can be used., (*7)

use \Zlikavac32\SymfonyEnum\Form\Type\EnumType;

class FormModel
{
    public $answer;

    // ...
}

$formModel = new FormModel();

$form = $this->createFormBuilder($formModel)
    ->add('answer', EnumType::class, [
        'enum_class' => \YesNoEnum::class
    ])
    // ...
    ->getForm();

Validator

Two constraints are provided, \Zlikavac32\SymfonyEnum\Validator\Constraints\ValidEnumElement and \Zlikavac32\SymfonyEnum\Validator\Constraints\ValidEnumElementName. Internally, they use \Symfony\Component\Validator\Constraints\Choice., (*8)

Required constraint argument is enumClass which must contain enum class FQN., (*9)

If any of the choices, callback and/or strict options is/are passed, an \LogicException will be thrown. Since these fields are overridden internally, passing them from the outside could cloud code's original purpose. Any other option provided by the \Symfony\Component\Validator\Constraints\Choice can be used., (*10)

  • ValidEnumElement - accepted values are null and any valid enum element from the defined enum class FQN
  • ValidEnumElementName - accepted values are null and any valid enum element name from the defined enum class FQN

Example for annotation use:, (*11)

/**
 * @ValidEnumElement(enumClass="\YesNoEnum")
 */

Limitations

Due to doctrine/common issue #794 with checks for aliased namespaces, validation of form enum element within an array will throw exception in following cases:, (*12)

  • on Windows validation does not work at all (due to the anonymous classes)
  • on Linux - short enum definition (one that uses eval()) does not work so the workaround is to manually instantiate elements
  • on OSX - have to check but I'd assume same as Linux

For more details on what's wrong and why, feel free to check related issue., (*13)

The Versions

26/01 2018

dev-master

9999999-dev

Symfony PHP enum support

  Sources   Download

MIT

The Requires

 

The Development Requires

by Marijan Ć uflaj

symfony enum php-enum enumeration symfony-enum

26/01 2018

0.2.0

0.2.0.0

Symfony PHP enum support

  Sources   Download

MIT

The Requires

 

The Development Requires

by Marijan Ć uflaj

symfony enum php-enum enumeration symfony-enum

21/11 2017

0.1.0

0.1.0.0

Symfony PHP enum support

  Sources   Download

MIT

The Requires

 

The Development Requires

by Marijan Ć uflaj

symfony enum php-enum enumeration symfony-enum