yethee/enum-bundle
Composer 安装命令:
composer require yethee/enum-bundle
包简介
This bundle provides a typed enumeration for Symfony applications
关键字:
README 文档
README
This bundle provides a typed enumeration for your Symfony2 project.
Note: For Symfony 2.0.x, you need to use the 1.0.0 release of the bundle.
Note: For Symfony less than 2.7, you need to use the 1.2.0 release of the bundle.
Features
- Provides base implementation for enum type.
- Provides base implementation for flags enum type (treated as a bit field, that is a set of flags).
- Uses enum types with Symfony's Form Component.
- Contains normalizer for Symfony's Serializer Component.
- Contains the custom handler for JMSSerializerBundle.
Installation
Add this bundle to your project
Install the bundle by running in a shell:
$ composer require yethee/enum-bundle
Add the bundle to your application kernel
<?php // app/AppKernel.php public function registerBundles() { return array( // ... new Biplane\EnumBundle\BiplaneEnumBundle(), // ... ); }
Usage
In order to create a typed enumeration, it's enough to extend the base class Biplane\EnumBundle\Enumeration\Enum
- define constants and implement
getPossibleValues()andgetReadables()methods. The first method should return an array of possible values of your enumeration, the second method returns a hash list of possible values and their human representations.
Below you can see a simple implementation of enumeration for user roles:
<?php use Biplane\EnumBundle\Enumeration\Enum; class UserRoles extends Enum { const MEMBER = 'ROLE_MEMBER'; const ADMIN = 'ROLE_ADMIN'; public static function getPossibleValues() { return array(static::MEMBER, static::ADMIN); } public static function getReadables() { return array(static::MEMBER => 'Member', static::ADMIN => 'Admin'); } }
You can create a new instance of the enumeration via the create() factory method,
which provides the base class:
$role = UserRoles::create(UserRoles::ADMIN);
If the argument contains an invalid value, an exception of
Biplane\EnumBundle\Exception\InvalidEnumArgumentException type will be thrown.
The following code example shows how to get the raw value or the human representation of the enumeration value from the object:
$role->getValue(); // returns string 'ROLE_ADMIN'
$role->getReadable(); // returns string 'Admin'
You can also convert the object to a string to obtain the human representation of the enumeration value:
(string)$role;
Bit flags support
You can extend Biplane\EnumBundle\Enumeration\FlaggedEnum for an enumeration, if a bitwise operation
is to be performed on a numeric value.
In this case define enumeration constants in powers of two, that is, 1, 2, 4, 8, and so on.
This means the individual flags in combined enumeration constants do not overlap. Also you can create
an enumerated constant for commonly used flag combinations, but the values of these constants must not be
returned by getPossibleValues() method.
Below you can see the implementation of flags enumeration for permissions list:
<?php use Biplane\EnumBundle\Enumeration\FlaggedEnum; class Permissions extends FlaggedEnum { const READ = 1; const WRITE = 2; const REMOVE = 4; const ALL = 7; public static function getPossibleValues() { return array(static::READ, static::WRITE, static::REMOVE); } public static function getReadables() { return array( static::READ => 'Read', static::WRITE => 'Write', static::REMOVE => 'Remove', static::ALL => 'All permissions', ); } }
You can use a bitwise operation on constants for creating a new instance of the enumeration:
$permissions = Permissions::create(Permissions::READ | Permissions::WRITE);
This type of enumeration provides some additional methods:
-
getFlags()returns an array of bit flags of enumeration value. For the previous example this method returnsarray(1, 2). -
hasFlag()returnstrueif specified flag is set in an numeric value.
Using with Doctrine ORM
You can store a raw value of the enum in the entity, and use casting type in getter and setter:
<?php use Doctrine\ORM\Mapping as ORM; class User { /** * @ORM\Column(type="string") */ private $role; public function getRole() { return UserRoles::create($this->role); } public function setRole(UserRoles $role) { $this->role = $role->getValue(); } }
Or you can create a custom type of DBAL for move the logic of casting type from the entity:
<?php namespace Acme\DemoBundle\Doctrine\Type; use Doctrine\DBAL\Types\StringType; use Doctrine\DBAL\Platforms\AbstractPlatform; class RoleType extends StringType { public function getName() { return 'role_enum'; } public function convertToDatabaseValue($value, AbstractPlatform $platform) { return $value->getValue(); } public function convertToPHPValue($value, AbstractPlatform $platform) { if ($value === null) { return null; } return UserRoles::create($value); } }
NOTE: You should manually convert the raw value to the valid type for your enumeration, before creating a new instance of the enumeration in
convertToPHPValuemethod. Because into this method always passed the value as string (or null).
After that you should register your type, this can be done through config:
# app/config/config.yml doctrine: dbal: types: role_enum: Acme\DemoBundle\Doctrine\Type\RoleType
Set your type in the mapping of the entity:
<?php use Doctrine\ORM\Mapping as ORM; class User { /** * @ORM\Column(type="role_enum") */ private $role; }
JMSSerializerBundle support
This bundle provides a custom handler for the serializer to allow serialize the Enum object as scalar value to json or xml format. The custom handler uses only for those typed enumerations that are specified in the configuration.
You can to register your typed enumerations through config:
# app/config/config.yml biplane_enum: serializer: types: - Acme\DemoBundle\Enum\MyEnum - Acme\UserBundle\Enum\UserRoles # etc
yethee/enum-bundle 适用场景与选型建议
yethee/enum-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 88.63k 次下载、GitHub Stars 达 31, 最近一次更新时间为 2012 年 03 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「typed enumeration」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 yethee/enum-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 yethee/enum-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 yethee/enum-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Linna Typed Array, typed arrays for PHP
Compatibility layer for emulating enumerations in PHP < 8.1 and native enumerations in PHP >= 8.1
A collection package that can be extended to implement things such as a Dependency Injection Container, RecordSet objects for housing database records, a bag of http cookies, or technically any collection of items that can be looped over and whose items can each be accessed using array-access syntax
Bundle for Doctrine enumerations extension for Postgres
Doctrine extension to manage enumerations in PostgreSQL
The typed array for PHP
统计信息
- 总下载量: 88.63k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 31
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2012-03-06