Skip to content

Repository files navigation

Enum

Emulated enumeration objects in PHP.

The interface is similar to SplEnum but doesn't require any PHP extensions and provides more functionality.

https://travis-ci.com/kuria/enum.svg?branch=master
  • immutability
  • ensured unique keys and values
  • simple to use (just extend a class and define some class constants)
  • many methods related to keys
  • values and their enumeration, maps and checking
  • enum instances
  • detailed exception messages
  • PHP 7.1+

The static Enum class provides access to the defined key-value pairs.

<?phpuseKuria\Enum\Enum;
abstractclass DayOfTheWeek extends Enum
{
constMONDAY = 0;
constTUESDAY = 1;
constWEDNESDAY = 2;
constTHURSDAY = 3;
constFRIDAY = 4;
constSATURDAY = 5;
constSUNDAY = 6;
}

Note

Private and protected constants are ignored.

To define key-value pairs using some other source than class constants, override the static determineKeyToValueMap() method:

<?phpuseKuria\Enum\Enum;
abstractclass Example extends Enum
{
protectedstaticfunctiondetermineKeyToValueMap(): array
{
return [
'FOO' => 'bar',
'BAZ' => 'qux',
'QUUX' => 'quuz',
];
}
}

Only string, integer and null values are supported.

Values must be unique when used as an array key. See Value type coercion.

Values are looked up and compared with the same type-coercion rules as PHP array keys. See Value type coercion.

Verify the existence of a key or a value:

<?phpvar_dump(
DayOfTheWeek::hasKey('MONDAY'),
DayOfTheWeek::hasValue(0)
);

Output:

bool(true)
bool(true)

Make sure a key or a value exists, otherwise throw an exception:

<?php
DayOfTheWeek::ensureKey('MONDAY');
DayOfTheWeek::ensureValue(0);

See Error handling.

Keys and values can be looked up using their counterpart:

<?phpvar_dump(
DayOfTheWeek::getValue('FRIDAY'),
DayOfTheWeek::getKey(4)
);

Output:

int(4)
string(6) "FRIDAY"

Note

If the key or value doesn't exist, an exception will be thrown. See Error handling.

To get NULL instead of an exception, use the findValue() or findKey() method instead.

<?phpecho'DayOfTheWeek::getKeys(): '; print_r(DayOfTheWeek::getKeys());
echo'DayOfTheWeek::getValues(): '; print_r(DayOfTheWeek::getValues());
echo'DayOfTheWeek::getMap(): '; print_r(DayOfTheWeek::getMap());
echo'DayOfTheWeek::getKeyMap(): '; print_r(DayOfTheWeek::getKeyMap());
echo'DayOfTheWeek::getValueMap(): '; print_r(DayOfTheWeek::getValueMap());

Output:

DayOfTheWeek::getKeys(): Array
(
[0] => MONDAY
[1] => TUESDAY
[2] => WEDNESDAY
[3] => THURSDAY
[4] => FRIDAY
[5] => SATURDAY
[6] => SUNDAY
)
DayOfTheWeek::getValues(): Array
(
[0] => 0
[1] => 1
[2] => 2
[3] => 3
[4] => 4
[5] => 5
[6] => 6
)
DayOfTheWeek::getMap(): Array
(
[MONDAY] => 0
[TUESDAY] => 1
[WEDNESDAY] => 2
[THURSDAY] => 3
[FRIDAY] => 4
[SATURDAY] => 5
[SUNDAY] => 6
)
DayOfTheWeek::getKeyMap(): Array
(
[MONDAY] => 1
[TUESDAY] => 1
[WEDNESDAY] => 1
[THURSDAY] => 1
[FRIDAY] => 1
[SATURDAY] => 1
[SUNDAY] => 1
)
DayOfTheWeek::getValueMap(): Array
(
[0] => MONDAY
[1] => TUESDAY
[2] => WEDNESDAY
[3] => THURSDAY
[4] => FRIDAY
[5] => SATURDAY
[6] => SUNDAY
)

A pair is an array with a single key and the corresponding value. They can be retrieved using either the key or the value:

<?phpvar_dump(DayOfTheWeek::getPair(DayOfTheWeek::MONDAY));
var_dump(DayOfTheWeek::getPairByKey('FRIDAY'));

Output:

array(1) {
["MONDAY"]=>
int(0)
}
array(1) {
["FRIDAY"]=>
int(4)
}
<?phpvar_dump(DayOfTheWeek::count());

Output:

int(7)

The EnumObject class extends from Enum and adds factory methods to create instances.

<?phpuseKuria\Enum\EnumObject;
/** * @method static static RED() * @method static static GREEN() * @method static static BLUE() */class Color extends EnumObject
{
constRED = 'r';
constGREEN = 'g';
constBLUE = 'b';
}

Note

The @method annotations are not required, but they will aid in code-completion and inspection.

See Magic static factory methods.

Instances can be created by one of the factory methods. Those instances are cached internally and reused, so that multiple calls to the factory methods with the same key or value will yield the same instance.

Enum instances cannot be cloned.

<?php$color = Color::fromValue(Color::RED);
var_dump($color);

Output:

object(Foo\Color)#5 (2) {
["key"]=>
string(3) "RED"
["value"]=>
string(1) "r"
}
<?php$color = Color::fromKey('GREEN');
var_dump($color);

Output:

object(Foo\Color)#3 (2) {
["key"]=>
string(5) "GREEN"
["value"]=>
string(1) "g"
}

For every key there is a static method with the same name, which returns an instance for that key-value pair.

<?php$color = Color::BLUE();
var_dump($color);

Output:

object(Foo\Color)#5 (2) {
["key"]=>
string(4) "BLUE"
["value"]=>
string(1) "b"
}

Warning

Magic static factory method names are case-sensitive.

<?phpvar_dump(Color::all());

Output:

array(3) {
["RED"]=>
object(Foo\Color)#5 (2) {
["key"]=>
string(3) "RED"
["value"]=>
string(1) "r"
}
["GREEN"]=>
object(Foo\Color)#4 (2) {
["key"]=>
string(5) "GREEN"
["value"]=>
string(1) "g"
}
["BLUE"]=>
object(Foo\Color)#2 (2) {
["key"]=>
string(4) "BLUE"
["value"]=>
string(1) "b"
}
}
<?php$color = Color::RED();
var_dump(
$color->key(),
$color->value()
);

Output:

string(3) "RED"
string(1) "r"
<?php$color = Color::GREEN();
var_dump($color->pair());

Output:

array(1) {
["GREEN"]=>
string(1) "g"
}
<?php$color = Color::RED();
var_dump(
$color->is('RED'), // compare key$color->is('GREEN'), // compare key$color->equals('r'), // compare value$color->equals('g') // compare value
);

Output:

bool(true)
bool(false)
bool(true)
bool(false)

Converting an instance to a string will yield its value (cast to a string):

<?php$color = Color::BLUE();
echo$color;

Output:

b

Most error states are handled by throwing an exception.

All exceptions thrown by the enum classes implement Kuria\Enum\Exception\ExceptionInterface.

  • Kuria\Enum\Exception\InvalidKeyException is thrown when a key doesn't exist
  • Kuria\Enum\Exception\InvalidValueException is thrown when a value doesn't exist
  • Kuria\Enum\Exception\InvalidMethodException is thrown when a magic factory method doesn't exist
  • Kuria\Enum\Exception\DuplicateValueException is thrown when an enum class defines duplicate values

Values are looked up and compared with the same type-coercion rules as PHP array keys. See PHP manual for a detailed explanation.

With string, integer and null being the supported value types, this means that the following values are equal:

  • null and "" (an empty string)
  • 123 and "123" (a numeric string)

Note

The public API, e.g. Enum::getValue() and EnumObject::value(), always returns the value as defined by the enum class.

Note

Array key type coercion is NOT the same as loose comparison (==).

<?phpuseKuria\Enum\EnumObject;
class IntAndNullEnum extends EnumObject
{
constINT_KEY = 123;
constNULL_KEY = null;
}
class StringEnum extends EnumObject
{
constNUMERIC_STRING_KEY = '123';
constEMPTY_STRING_KEY = '';
}
// value checksvar_dump(
IntAndNullEnum::hasValue('123'),
IntAndNullEnum::hasValue('0123'),
IntAndNullEnum::hasValue(''),
IntAndNullEnum::hasValue(''),
StringEnum::hasValue(123),
StringEnum::hasValue('0123'),
StringEnum::hasValue(null),
StringEnum::hasValue('')
);
// value retrievalvar_dump(
(IntAndNullEnum::fromValue('123'))->value(),
(IntAndNullEnum::fromValue(''))->value(),
(StringEnum::fromValue(123))->value(),
(StringEnum::fromValue(null))->value()
);

Output for value checks:

bool(true) // '123' matches 123
bool(false) // '0123' does not match 123
bool(true) // '' matches NULL
bool(false) // ' ' does not match NULL
bool(true) // 123 matches '123'
bool(false) // '0123' does not match '123'
bool(true) // NULL matches ''
bool(false) // ' ' does not match ''

Output for value retrieval:

int(123) // enum created with '123' but 123 is returned
NULL // enum created with '' but NULL is returned
string(3) "123" // enum created with 123 but '123' is returned
string(0) "" // enum created with NULL but '' is returned

About

Emulated enumeration objects in PHP

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages