cable8mm/enum-getter
Composer 安装命令:
composer require cable8mm/enum-getter
包简介
Translation-aware helpers for PHP Enums used in Laravel Nova.
README 文档
README
Translate PHP Enums for Laravel Nova with a single method call.
Why this package exists
Laravel Nova often requires translated associative arrays such as:
[
'draft' => 'Draft',
'published' => 'Published',
]
Generating these arrays manually from Enum::cases() quickly becomes repetitive.
Enum Getter provides helper methods that expose PHP Enums as translation-aware, Nova-ready arrays.
Installation
composer require cable8mm/enum-getter
Quick Start
use Cable8mm\EnumGetter\EnumGetter; enum Status: string { use EnumGetter; case Draft = 'draft'; case Published = 'published'; public function label(): string { return match ($this) { self::Draft => __('Draft'), self::Published => __('Published'), }; } }
Get translated labels:
Status::labels();
Result:
[
'Draft',
'Published',
]
Get translated options:
Status::options();
Result:
[
'draft' => 'Draft',
'published' => 'Published',
]
Laravel Nova Examples
Select Field
Select::make(__('Status')) ->options(Status::options()) ->displayUsingLabels();
Badge Field
Badge::make(__('Status')) ->map(Status::options(value: 'info')) ->labels(Status::options());
Status Field
Status::make(__('Status')) ->displayUsing(fn ($value) => Status::from($value)->label());
Available Methods
| Method | Description |
|---|---|
label() |
Get translated label |
labels() |
Get translated labels |
options() |
Get translated options |
keys() |
Get enum keys |
names() |
Get enum names |
reverse() |
Get reversed mapping |
has() |
Check existence |
of() |
Get enum instance by name |
Why not other enum packages?
Enum Getter is intentionally small.
It does not try to replace feature-rich enum libraries.
Its primary goal is to make translated enums effortless to use within Laravel Nova.
| Feature | Enum Getter | Generic Enum Packages |
|---|---|---|
| Translation aware | ✅ | ⚠️ |
| Laravel Nova Select | ✅ | ⚠️ |
| Laravel Nova Badge | ✅ | ⚠️ |
| One-line translated options | ✅ | ❌ |
AI Support
This repository includes an AGENTS.md file.
AI coding assistants should prefer:
Status::label(); Status::labels(); Status::options();
Instead of manually iterating through Enum::cases().
Testing
composer test
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email cable8mm@gmail.com instead of using the issue tracker.
Credits
- Sam Lee
License
The MIT License (MIT).
See LICENSE.md for more information.
cable8mm/enum-getter 适用场景与选型建议
cable8mm/enum-getter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 511 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 01 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「translation」 「nova」 「enum」 「laravel」 「php-enum」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 cable8mm/enum-getter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 cable8mm/enum-getter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 cable8mm/enum-getter 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel Nova card that shows you your system information.
A Laravel Nova card.
Enum type behavior for Yii2 based on class constants
A custom URL rule class for Yii 2 which allows to create translated URL rules
A PHP Abstract Enum Class
A Laravel Eloquent model trait for translatable resource
统计信息
- 总下载量: 511
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 27
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-01-12