enum MyEnum
{
case First;
case Second;
case Third;
}
\OpenSoutheners\ExtendedPhp\Enums\is_enum(MyEnum::class); // true
enum_is_backed
Check if object or class is a backed PHP enum.
enum MyEnum
{
case First;
case Second;
case Third;
}
\OpenSoutheners\ExtendedPhp\Enums\enum_is_backed(MyEnum::class); // false
has_case
Check if object or class is a PHP enum that has the specified case.
enum MyEnum
{
case First;
case Second;
case Third;
}
\OpenSoutheners\ExtendedPhp\Enums\has_case(MyEnum::class, 'First'); // true
get_enum_class
Get class string from enum object.
enum MyEnum
{
case First;
case Second;
case Third;
}
\OpenSoutheners\ExtendedPhp\Enums\get_enum_class(MyEnum::First); // 'MyEnum'
enum_to_array
Converts PHP enum object or class into an array. In case of a backed enum it will add its values.
enum MyEnum
{
case First;
case Second;
case Third;
}
\OpenSoutheners\ExtendedPhp\Enums\enum_to_array(MyEnum::class);
// ['First', 'Second', 'Third']
enum_values
Gets array of values from PHP backed enum.
enum MyBackedEnum: string
{
case First = 'one';
case Second = 'two';
case Third = 'three';
}
\OpenSoutheners\ExtendedPhp\Enums\enum_values(MyBackedEnum::class);
// ['one', 'two', 'three']
GetsAttributes
This is not a function but still serves as a utility. Make sure you only use this with backed enums.
This trait will add some functions to the PHP enums so they can be converted to arrays to be used in multiple contexts (like HTML selects, etc).
Description
This is a PHP attribute to be used in those enum cases to be used along with GetsAttribute trait:
use OpenSoutheners\ExtendedPhp\Enums\Description;
use OpenSoutheners\ExtendedPhp\Enums\GetsAttributes;
enum MyBackedEnum: string
{
use GetsAttributes;
#[Description('First point')]
case First = 'one';
#[Description('Second point')]
case Second = 'two';
#[Description('Third point')]
case Third = 'three';
}
More details of their usage below.
asSelectArray
The only public method available on this trait which will get all described cases of your enum.
Now imagine the case having the enum shown before: