eclipxe/enum
Composer 安装命令:
composer require eclipxe/enum
包简介
Enum based on the Brent Roose enum idea https://stitcher.io/blog/php-enums
关键字:
README 文档
README
Enum based on the Brent Roose enum idea https://stitcher.io/blog/php-enums
After reading the article PHP Enums from Brent Roose and review the implementation made on spatie/enum I think that it overloaded my expectations. Maybe spatie/enum version 1.0 was more close to what I needed.
So, I created this framework-agnostic implementation library about the same concept.
As of PHP 8.1 enums are part of the language, it means that this library will not be required anymore.
Read the PHP documentation https://www.php.net/manual/en/language.enumerations.php and adapt your code.
One big difference between PHP enums and the objects on this library is that native PHP enums cannot
use the magic method __toString (they are not Stringable), and the enums on this library are.
Installation
Use composer, install using:
composer require eclipxe/enum
Usage
Enum in other languages are TEXT for code, INTEGER for values.
There are two meaningful information: index (integer) and value (string).
This library provides Eclipxe\Enum abstract class to be extended.
The value is the method's name as declared in docblock.
The index is the position (starting at zero) in the docblock.
Values are registered one by one taking the overridden value, or the method's name.
Indices are registered one by one taking the overridden index, or the maximum registered value plus 1.
Enum example
<?php /** * This is a common use case enum sample * source: tests/Fixtures/Stages.php * * @method static self created() * @method static self published() * @method static self reviewed() * @method static self purged() * * @method bool isCreated() * @method bool isPublished() * @method bool isReviewed() * @method bool isPurged() */ final class Stages extends Eclipxe\Enum\Enum { }
Creation of instances
You can create new instances from values using construct with value, construct with index or static method name.
<?php use Eclipxe\Enum\Tests\Fixtures\Stages; // create from value $purged = new Stages('purged'); // create from index $purged = new Stages(3); // create from an object that can be converted to string and contains the value $other = new Stages($purged); // create from static method $purged = Stages::purged(); // create from static method is not case-sensitive as methods are not $purged = Stages::{'PURGED'}(); // throws a BadMethodCallException because foobar is not part of the enum $purged = Stages::{'FOOBAR'}();
List all the options
The only static method exposed on the Enum is Enum::toArray(): array that export the list of registered
possible values as an array of indices and values.
<?php use Eclipxe\Enum\Tests\Fixtures\Stages; var_export(Stages::toArray()); /* [ 0 => 'created', 1 => 'published', 2 => 'reviewed', 3 => 'purged', ] */
Check if instance is of certain type
Use the methods is<name>() to compare to specific value.
You have to define these methods in your docblock to let your IDE or code analyzer detect what you are doing.
<?php use Eclipxe\Enum\Tests\Fixtures\Stages; $stage = Stages::purged(); $stage->isPurged(); // true $stage->isPublished(); // false $stage->{'isSomethingElse'}(); // false, even when SomethingElse is not defined $stage->{'SomethingElse'}(); // throw BadMethodCallException
Or use weak comparison (equality, not identity):
<?php use Eclipxe\Enum\Tests\Fixtures\Stages; $stage = Stages::purged(); var_export($stage === Stages::purged()); // false, is not the same identity var_export($stage == Stages::purged()); // true var_export($stage == Stages::published()); // false var_export($stage->value() === Stages::purged()->value()); // true (compare using value) var_export($stage->index() === Stages::purged()->index()); // true (compare using index)
Overriding values or indices
You can override values or indices by overriding the methods overrideValues() or overrideIndices().
Rules:
- Return
arraykey must be the name of the method as it was defined in the docblock section (case-sensitive). - If override's value is
nullthen it will not be overridden. - When override a value, if previous value exists then will throw a
ValueOverrideException. - When override an index, if previous value exists then will throw a
IndexOverrideException.
<?php /** * This is an enum case where names and values are overridden * * @method static self monday() * @method static self tuesday() * @method static self wednesday() * @method static self thursday() * @method static self friday() * @method static self saturday() * @method static self sunday() */ final class WeekDays extends \Eclipxe\Enum\Enum { protected static function overrideValues(): array { return [ 'monday' => 'Monday', 'tuesday' => 'Tuesday', 'wednesday' => 'Wednesday', 'thursday' => 'Thursday', 'friday' => 'Friday', 'saturday' => 'Saturday', 'sunday' => 'Sunday', ]; } protected static function overrideIndices(): array { return [ 'monday' => 1, ]; } }
This will define these array<index, value>, retrieved using static method WeekDays::toArray():
[
1 => 'Monday',
2 => 'Tuesday',
3 => 'Wednesday',
4 => 'Thursday',
5 => 'Friday',
6 => 'Saturday',
7 => 'Sunday',
];
Remember that Enum creation depends on registered values and indices, if and invalid value or index is used then an exception is thrown:
<?php use Eclipxe\Enum\Tests\Fixtures\WeekDays; new WeekDays(0); // throws IndexNotFoundException new WeekDays(1); // WeekDays {value: 'Monday', index: 1} new WeekDays('sunday'); // throws ValueNotFoundException (it is case-sensitive) new WeekDays('Sunday'); // WeekDays {value: 'Sunday', index: 7}
Extending
When creating an Enum extending from other, the parent Enum have priority on indices and values. You cannot override indices or values of previous classes.
I recommend you to declare your Enum classes as final to disable extension.
If using class extension, do not use @method static self name() syntax,
use @method static static name() syntax instead to help analysis tools.
See examples at tests/Fixtures/ColorsBasic.php, tests/Fixtures/ColorsExtended.php
and tests/Fixtures/ColorsExtendedWithBlackAndWhite.php.
Exceptions
Exceptions thrown from this package implements the empty interface Eclipxe\Enum\Exceptions\EnumExceptionInterface.
PHP Support
This library is compatible with at least the oldest PHP Supported Version with active support. Please, try to use PHP full potential.
We adhere to Semantic Versioning. We will not introduce any compatibility backwards change on major versions.
Internal classes (using @internal annotation) are not part of this agreement
as they must only exist inside this project. Do not use them in your project.
Contributing
Contributions are welcome! Please read CONTRIBUTING for details and don't forget to take a look the TODO and CHANGELOG files.
Copyright and License
The eclipxe/enum library is copyright © Carlos C Soto
and licensed for use under the MIT License (MIT). Please see LICENSE for more information.
eclipxe/enum 适用场景与选型建议
eclipxe/enum 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 210.53k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2019 年 03 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「reflection」 「enum」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 eclipxe/enum 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 eclipxe/enum 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 eclipxe/enum 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Library emulating the PHP internal reflection using just the tokenized source code.
Enum type behavior for Yii2 based on class constants
A PHP Abstract Enum Class
Parse use statements for a reflection object
Compatibility layer for emulating enumerations in PHP < 8.1 and native enumerations in PHP >= 8.1
Enum type behavior and helper for Yii2, for PostgreSQL only
统计信息
- 总下载量: 210.53k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 20
- 依赖项目数: 6
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-03-25