PHPackages                             catt-l/enum-hyperf - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. catt-l/enum-hyperf

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

catt-l/enum-hyperf
==================

Support enum for hyperf.

111PHP

Since Mar 14Pushed 4y ago1 watchersCompare

[ Source](https://github.com/CATT-L/enum-hyperf)[ Packagist](https://packagist.org/packages/catt-l/enum-hyperf)[ RSS](/packages/catt-l-enum-hyperf/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

适用于 Hyperf 的枚举组件
================

[](#适用于-hyperf-的枚举组件)

[![Powered By](https://camo.githubusercontent.com/50b4a6851f4d81b93fcc196216b4968892a07f030b1d22538bdab2bee0651b18/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f506f776572656425323062792d434154542d2d4c2d666636396234)](https://github.com/CATT-L)

- 继承 [`hyperf/constants`](https://github.com/hyperf/constants) 实现
- 类常量即枚举键值对
- 可实例化
- 支持 `hyperf/validation` 验证
- 实现 `CastsAttributes` 接口, 因此可在 `Model` 中设置 `cast` 类型转换
- 类型提示
- 属性转换
- 枚举比较
- 生成选项列表（供前端使用）
- 启动检测枚举值重复异常

> 基于 [`hyperf-ext/enum`](https://github.com/hyperf-ext/enum) , 在部分功能上做了取舍和改动

- [安装](#%E5%AE%89%E8%A3%85)
- [使用](#%E4%BD%BF%E7%94%A8)
    - [枚举定义](#%E6%9E%9A%E4%B8%BE%E5%AE%9A%E4%B9%89)
    - [实例属性](#%E5%AE%9E%E4%BE%8B%E5%B1%9E%E6%80%A7)
    - [实例类型转换](#%E5%AE%9E%E4%BE%8B%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2)
    - [实例比较](#%E5%AE%9E%E4%BE%8B%E6%AF%94%E8%BE%83)
    - [类型提示](#%E7%B1%BB%E5%9E%8B%E6%8F%90%E7%A4%BA)
    - [生成选项列表](#%E7%94%9F%E6%88%90%E9%80%89%E9%A1%B9%E5%88%97%E8%A1%A8)
    - [模型属性类型转换](#%E6%A8%A1%E5%9E%8B%E5%B1%9E%E6%80%A7%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2)
    - [启动自动检测枚举重复值](#%E5%90%AF%E5%8A%A8%E8%87%AA%E5%8A%A8%E6%A3%80%E6%B5%8B%E6%9E%9A%E4%B8%BE%E9%87%8D%E5%A4%8D%E5%80%BC)
    - [验证器](#%E9%AA%8C%E8%AF%81%E5%99%A8)
- [AbstractEnum 类参考](#AbstractEnum-%E7%B1%BB%E5%8F%82%E8%80%83)

安装
--

[](#安装)

```
composer require catt-l/enum-hyperf
```

使用
--

[](#使用)

#### 枚举定义

[](#枚举定义)

将所有可能值作为常量添加到枚举类, 给枚举类加上 `@Constants` 注解即可。

```
