opus-online / yii2-enum
此包的最新版本(v1.0)没有提供许可证信息。
Yii2 的枚举扩展
此包的规范存储库似乎已丢失,因此该包已被冻结。
v1.0
2014-08-12 14:12 UTC
Requires
- symfony/yaml: 2.5.*
- yiisoft/yii2: *
This package is not auto-updated.
Last update: 2024-01-15 23:36:08 UTC
README
此组件允许您在 Yaml 定义文件中定义枚举类型,并构建与这些类型对应的类常数的 PHP 类。
安装
将以下内容添加到您的 composer.json 中
{ "require": { "opus-online/yii2-enum": "*" } }
将 EnumController
类附加到您的项目中。您可以通过创建一个新的控制器,该控制器扩展 EnumController
,或者您可以通过覆盖控制台应用程序中的 coreCommands
并在那里添加控制器来实现。
'enum' => [ 'class' => EnumController::className(), // add the path alias of the Yaml file here 'definitionAlias' => '@app/build/enum.yml', ],
用法
在 Yaml 文件中定义您的类型
Gender: - MALE - FEMALE - OTHER # you can also use shorter syntax UserStatus: [ACTIVE, INACTIVE, BLOCKED] # and you can define an explicit value for the type TrendDirection: - INCREASING value: 1 - DECREASING: value: -1 - NOT_DETERMINED
运行构建命令以生成定义的 PHP 类。
php yii enum/build my\\ns @app/src/enum
这将在命名空间 my\ns
(注意双 \\
)下创建类型类,并将文件放入与 @app/src/enum
路径别名相对应的目录中。
之后,您可以像这样使用枚举类型
echo UserStatus::INACTIVE; // INACTIVE echo TrendDirection::DECREASING; // -1 // return all values of a type TrendDirection::getList(); // return a human-readable label for one value (by constant name) echo TrendDirection::getLabel('NOT_DETERMINED'); // Not determined // returns an array where type value is the key and human readable // label is the value. This can be useful when populating drop-downs TrendDirection::getListLabels();