yokai/enum-bundle 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

yokai/enum-bundle

Composer 安装命令:

composer require yokai/enum-bundle

包简介

Simple enumeration system with Symfony integration

README 文档

README

Tests Coverage Contributors License

Latest Stable Version Latest Unstable Version Total Downloads Downloads Monthly

This repository aims to provide simple enumeration implementation to Symfony.

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"

Creating the enum

We first need to create the class that will handle our enum :

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

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.

Configuring validation

We will now be able to configure Member's model validation :

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

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

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

Translating your enum

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 :

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

# 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)

More examples

See examples from unit test suite & associated tests.

See example Symfony project in integration test suite.

Recipes

MIT License

License can be found here.

Authors

The bundle was originally created by Yann Eugoné. See the list of contributors.

Thank's to PrestaConcept for supporting this bundle.

yokai/enum-bundle 适用场景与选型建议

yokai/enum-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 102.1k 次下载、GitHub Stars 达 12, 最近一次更新时间为 2017 年 04 月 01 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 yokai/enum-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 yokai/enum-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 yokai/enum-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

  • Stars: 12
  • Watchers: 4
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-04-01