定制 zlikavac32/php-enum-symfony 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

zlikavac32/php-enum-symfony

Composer 安装命令:

composer require zlikavac32/php-enum-symfony

包简介

Symfony PHP enum support

README 文档

README

Build Status Latest Stable Version License Scrutinizer Code Quality Codacy Badge

Symfony support for zlikavac32/php-enum.

Table of contents

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

Installation

Recommended installation is through Composer.

composer require zlikavac32/php-enum-symfony

Usage

Assumption is that there exists a valid enum \YesNoEnum.

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.

Internally this extends \Symfony\Component\Form\Extension\Core\Type\ChoiceType and populates choices from the defining enum_class.

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.

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.

Required constraint argument is enumClass which must contain enum class FQN.

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.

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

/**
 * @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:

  • 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.

统计信息

  • 总下载量: 86
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-11-21

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固