jomisacu/enumerations
Composer 安装命令:
composer require jomisacu/enumerations
包简介
A simple way to handle enumerations in php
README 文档
README
A simple class to handle enumerations
why?
In some situations we have to handle groups of static values, enumerations comes to deal with these values in a safe way. Instead of write raw values in your code you can use objects, preventing typo errors and behaviour bugs. It is not about wraps values into constants, not, you can use TypeHint to avoid wrong calls in methods and constructors.
Use cases
Static values: statuses, database enums and options
If you have a currier system you can handle the package states using enumerations: sent, in way, received. Example:
<?php final class PakageState extends Jomisacu\Enumerations\Enumeration { public const SENT = 'sent'; public const IN_WAY = 'in-way'; public const RECEIVED = 'received'; } // retrieving from raw values $state = PackageState::fromValue($valueFromRequest); // throws an exception if invalid value // TypeHint class Package { private PackageState $state; public function changePackageState(PackageState $state) { $this->state = $state; } public function getPackageState() { return $this->state; } } // printing the value $package = new Package; $state = PackageState::fromValue($valueFromRequest); // throws an exception if invalid value $package->changePackageState($state); echo $package->getPackageState(); // prints sent, in-way or received
Now, you can handle these values in a safe way.
External values
In some situations we need to handle values that can change by external reasons. Imagine an university that offers multiple college career and by convention they take the decision of apply three code character. Over the time, careers are added, subjects change, etc., etc. So they decide add a prefix to expand the size of code and maintain the same size for all. i.e: Software Engineer could have the code 'XYZ' but after change could be '0XYZ'.
The previous change introduces the problems below:
- Logic broken because the raw values in the code not have mean
- The values in the database are now corrupted because no match with the real values
- No safe way to replace the values in a production system, we need to catch errors by the way
The solutions are the enumerations. See the example...
class Career extends Jomisacu\Enumerations\Enumeration { // WTF??? What is this? // using our own value we drop the dependency with external sources // but below we will see how to deal with these values // the values in the database are the values that we decided in the class constants public const SOFTWARE_ENGINEER = "a372d961-22d9-4cc4-a9ee-4cb47a15b26d"; public const ARCHITECT = "6d8165dc-621d-4279-bc71-4e4f4782d972"; // we always can get the current code // if external code changes we only need to update the code here // the values in the database are the values that we decided in the class constants public function getCode() { $codes = [ self::SOFTWARE_ENGINEER => '0XYZ', self::ARCHITECT => '0YYK', ]; return $codes[(string) $this->getValue()]; } // now, we can store a reference to the previous code, so we can interop with old formats public function getOldCode() { $codes = [ self::SOFTWARE_ENGINEER => 'XYZ', self::ARCHITECT => 'YYK', ]; return $codes[(string) $this->getValue()]; } }
Enjoy it!
jomisacu/enumerations 适用场景与选型建议
jomisacu/enumerations 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.67k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 12 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 jomisacu/enumerations 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jomisacu/enumerations 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.67k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 5
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-12-11