kambo/enum

Simple enumeration library

dev-master 2016-11-27 16:47 UTC

This package is auto-updated.

Last update: 2024-12-19 17:21:27 UTC


README

Build Status Scrutinizer Code Quality Code Coverage Software License

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