kambo / enum
Simple enumeration library
Installs: 2 688
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=5.5
This package is auto-updated.
Last update: 2024-10-19 16:54:31 UTC
README
Just another PHP enumeration library
Install
Prefered way to install library is with composer:
composer require kambo/enum
Usage
Enumeration is declared by implementing class Kambo\Enum\Enum
and adding class constants:
use Kambo\Enum\Enum; class Foo extends Enum { const BAR = 'bar'; const QUX = 'qux'; }
Base enum class implement following usefull methods:
toArray
convert whole enumeration to array with constant name in key and Enum instance in value
Following example code:
$array = Foo::toArray(); var_dump($array);
Will print:
array(2) { 'BAR' => string(3) "bar" 'QUX' => string(3) "qux" }
There is also alias method called values
which just called toArray
method.
inEnum
check if the provided value is in enumeration:
$existInEnum = Foo::inEnum('bar'); // print true as the value exists in enumeration echo $existInEnum; $existInEnum = Foo::inEnum('agh'); // print false as the value does not exists in enumeration echo $existInEnum;
License
The MIT License (MIT), https://opensource.org/licenses/MIT