webdevcave/enum-index-accessor
Composer 安装命令:
composer require webdevcave/enum-index-accessor
包简介
Dynamic index accessing made simple
README 文档
README
Install with composer:
composer install webdevcave/enum-index-accessor
Why?
From php 8.1 or newer, the possibility organized enumerators was made real. It works just fine until we have to read those values dynamically. Here is an example:
enum HexColors: string { case RED = '#FF0000'; case GREEN = '#00FF00'; case BLUE = '#0000FF'; case WHITE = '#FFFFFF'; case BLACK = '#000000'; // and so on... } $index = 'BLUE'; // Imagine this is a dynamic value $constant = HexColors::class."::$index"; // In a real-world application HexColors // will most probably be declared under a namespace $color = null; // Before we proceed, we have to ensure the specified index exists or our code will break if (defined($constant)) { $color = constant($constant)->value; // Now we read its value } // You will probably want to assign a default value in case something went wrong if (is_null($color)) { $color = HexColors::RED->value; } // Now we can finally proceed with our task...
Does this looks too verbose and/or messy for your taste? Now imagine if your application have to read multiple values like this.
That's why we created this package! Now you can do the same task this way:
$color = HexColors::tryValue($index) ?? HexColors::value('RED');
How?
use \WebdevCave\EnumIndexAccessor\BackedEnumIndexAccessor; // step 1: Import the trait enum HexColors: string { case RED = '#FF0000'; case GREEN = '#00FF00'; case BLUE = '#0000FF'; case WHITE = '#FFFFFF'; case BLACK = '#000000'; // and so on... use BackedEnumIndexAccessor; // step 2: use it }
~ And voila! Magic 🪄!
Other use cases...
We followed the php team standards for naming the methods for ease of use. Here is a list of all of them:
HexColors::hasIndex($index); // Checks if a case statement was set in the enumerator (boolean) HexColors::index($index); // Read the object from given index (skips index check) HexColors::tryIndex($index); // Read the object from given index (null on non-existent) HexColors::value($index); // Read the value from given index (skips index check) HexColors::tryValue($index); // Read the value from given index (null on non-existent)
For pure enumerators (without backing values), use the pure enumerator trait as follows:
use \WebdevCave\EnumIndexAccessor\PureEnumIndexAccessor; // step 1: Import the trait enum Fruits { case ORANGE; case PEAR; case APPLE; // and so on... use PureEnumIndexAccessor; // step 2: use it }
Important note: The methods value and tryValue are not available as pure enumerators doesn't carry any values on them
webdevcave/enum-index-accessor 适用场景与选型建议
webdevcave/enum-index-accessor 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 180 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 04 月 24 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「extension」 「enum」 「accessor」 「enumerator」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 webdevcave/enum-index-accessor 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 webdevcave/enum-index-accessor 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 webdevcave/enum-index-accessor 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Utility to handle access to a data set by using access paths. Similar to and partly compatible with xpath and json-pointer.
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 PHP helper utility class for fluent interaction and manipulation with collection data using immutable bags
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
统计信息
- 总下载量: 180
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-04-24