myclabs/php-enum
myclabs/php-enum brings SplEnum-style enums to PHP. Define enum classes, get autogenerated static constructors, validate values, compare instances, list possible values, and use enums as parameter/return types with extra methods—without a PHP extension.
Full Changelog: https://github.com/myclabs/php-enum/compare/1.8.4...1.8.5
Full Changelog: https://github.com/myclabs/php-enum/compare/1.8.3...1.8.4
#147 Fix Psalm annotation
#146 Fix Psalm annotation
Resolve deprecation on PHP 8.1 #145
Added:
Enum::from() method as an alternative to the constructor that is more strictly typed, and provides a forward compatible solution with PHP 8.1 enumsEnum::assertValidValue() to assert that a value is correctPsalm annotations have been improved as well.
Thanks @drealecs, @Ocramius, @michaelpetri, @gharlan, @simPod
#129 Performance improvement
Avoid conflicts with Doctrine Annotations by renaming the Psalm [@template](https://github.com/template) annotation into [@psalm-template](https://github.com/psalm-template).
#112 Improved Psalm typing
Psalm has been moved to require-dev ;)
#111 Add Psalm annotations
Fixed #95 with #97: handle inherited enums
equals() methodequals() method now accepts anything as a parameter#84 Require ext-json explicitly in composer.json to avoid bugs when the JSON extension is not installed
#83: It is now possible to create a new Enum instance from another Enum instance
#80 Fix validating key with null value #82 Fix coding style and setup https://prettyci.com
Fix #77 #78: better support for null values in enums.
#75 Performance optimizations
#71 Allow null in the equals() method
JsonSerializable interface to allow serializing the class to JSONRemove unneeded file permissions.
Bugfix in the equals() method
Issue: #47 Pull request: #48 Contributed by @peter-gribanov
Example:
class CommandAction extends Enum
{
const EDIT = 'edit';
}
class AccessRule extends Enum
{
const EDIT = 'edit';
}
Expected
CommandAction::EDIT()->equals(AccessRule::EDIT()); // false
Actual
CommandAction::EDIT()->equals(AccessRule::EDIT()); // true
#4 #39 #40: new equals() method to compare enums
$enum = MyEnum::FOO();
if ($enum->equals(MyEnum::BAR())) {
...
}
It behaves the same as ==:
if ($enum == MyEnum::BAR()) {
...
}
It was added because it carries a bit more sense than == which, because it is a loose comparison, is often source of confusion (comparing objects with == is often a no-go for good reasons).
#21 Allow to extend the class to fetch values from somewhere else than the class constants (e.g. a database, file, …)
New static method values(): returns instances of the Enum class of all Enum constants (constant name in key, Enum instance in value).
MyEnum::values()
search() is not type safeSmall performance enhancement.
A bunch of new methods were added in #5:
$value->getKey() Returns the key of the current value on EnumEnum::keys() Returns the names (keys) of all constants in the Enum classEnum::isValid() Check if tested value is valid on enum setEnum::isValidKey() Check if tested key is valid on enum setEnum::search() Return key for searched valueThe library is now PSR-4 compliant.
How can I help you explore Laravel packages today?