konekt/enum
Lightweight PHP enum base class (pre-PHP 8.1) for defining value-safe constants via abstract Enum subclasses. Simple instantiation, validation and utilities, with docs and optional Laravel Eloquent integration via konekt/enum-eloquent.
It is possible to define a default value on Enum classes, by setting the value of the __DEFAULT constant:
class MemberRank extends \Konekt\Enum\Enum
{
const __DEFAULT = self::NOVICE;
const NOVICE = 'novice';
const SENIOR = 'senior';
const VETERAN = 'veteran';
const MASTER = 'master';
const LEGEND = 'legend';
}
In case you omit passing a value when creating an enum instance then the object will have the default value:
with plain constructor:
$rank = new MemberRank();
echo $rank->value();
// output: 'novice'
with factory method:
$rank = MemberRank::create();
echo $rank->value();
// output: 'novice'
If you don't set an explicit default value, then it is not possible to create an enum object without setting the value:
class ChessColor extends \Konekt\Enum\Enum
{
const WHITE = 'white';
const BLACK = 'black';
}
new ChessColor();
// throws: UnexpectedValueException: Given value () is not in enum `ChessColor`
ChessColor::create();
// throws: UnexpectedValueException: Given value () is not in enum `ChessColor`
In case you need to obtain the default value of an Enum class, there are two ways to do so:
__DEFAULT constant,defaultValue() method.class FooBar extends \Konekt\Enum\Enum
{
const FOO = 'foo';
const BAR = 'bar';
}
var_dump(FooBar::defaultValue());
// NULL
// since BarType has no default
var_dump(FooBar::__DEFAULT);
// NULL
class SpoinkBaz extends \Konekt\Enum\Enum
{
const __DEFAULT = self::SPOINK;
const SPOINK = 'spoink';
const BAZ = 'baz';
}
var_dump(SpoinkBaz::defaultValue());
// string(6) "spoink"
var_dump(SpoinkBaz::__DEFAULT);
// string(6) "spoink"
This is a v3.0 feature
It is possible to define the behavior for an enum so that if it receives a value that is not one of the predefined values, it falls back to the default instead of throwing an exception.
This can be done by setting the static variable
$unknownValuesFallbackToDefault to true:
class FallbackEnum extends \Konekt\Enum\Enum
{
const __DEFAULT = self::UNKNOWN;
const UNKNOWN = null;
const SOME_VALUE = 'some_value';
const GOOD_VALUE = 'good_value';
protected static bool $unknownValuesFallbackToDefault = true;
}
var_dump(FallbackEnum::create('bullshit_value'));
//class FallbackEnum#1 (1) {
// protected $value =>
// NULL
//}
!> Make sure to define $unknownValuesFallbackToDefault as static variable!
Next: Nullable Enums »
How can I help you explore Laravel packages today?