Last active
July 10, 2019 19:07
-
-
Save mayoz/e200f86181a0645eb3056608e0828de3 to your computer and use it in GitHub Desktop.
Enum use case for PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Enums; | |
use ReflectionClass; | |
use UnexpectedValueException; | |
abstract class Enum | |
{ | |
/** | |
* Get the supported enum list. | |
* | |
* @return array | |
*/ | |
final public static function toArray(): array | |
{ | |
return (array) rescue(function () { | |
return (new ReflectionClass(static::class))->getConstants(); | |
}, null, false); | |
} | |
/** | |
* Get the keys of enums. | |
* | |
* @return string[] | |
*/ | |
final public static function names(): array | |
{ | |
return array_keys(static::toArray()); | |
} | |
/** | |
* Get the values of enums. | |
* | |
* @return mixed[] | |
*/ | |
final public static function values(): array | |
{ | |
return array_values(static::toArray()); | |
} | |
/** | |
* Get the maps of enums. | |
* | |
* @return array | |
*/ | |
final public static function maps(): array | |
{ | |
return array_replace(static::initial(), static::mapping()); | |
} | |
/** | |
* Prepare the initial mappings filling by null. | |
* | |
* @return array | |
*/ | |
final protected static function initial() | |
{ | |
return array_fill_keys(static::toArray(), null); | |
} | |
/** | |
* Get the maps of class constants. | |
* | |
* @return array | |
*/ | |
public static function mapping(): array | |
{ | |
return []; | |
} | |
/** | |
* Get the maps of enums. | |
* | |
* @param string $key | |
* @return mixed | |
* | |
* @throws UnexpectedValueException | |
*/ | |
final public static function map(string $key) | |
{ | |
if (array_key_exists($key, $maps = static::maps())) { | |
return $maps[$key]; | |
} | |
throw new UnexpectedValueException( | |
sprintf('Unable to found value of [%s] in the [%s] class', $key, static::class) | |
); | |
} | |
/** | |
* Get the enum values by joined with given glue. | |
* | |
* @param string $glue | |
* @return string | |
*/ | |
final public static function toImplode($glue = ','): string | |
{ | |
return implode($glue, static::values()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use App\Enums\Status; | |
var_dump(Status::names()); | |
/* | |
Output: | |
----- | |
array:2 [ | |
0 => "PASSIVE" | |
1 => "ACTIVE" | |
] | |
*/ | |
var_dump(Status::values()); | |
/* | |
array:2 [ | |
0 => 20 | |
1 => 30 | |
] | |
*/ | |
var_dump(Status::maps()); | |
/* | |
Output: | |
----- | |
array:2 [ | |
20 => array:2 [ | |
"label" => "passive" | |
"color" => "red" | |
] | |
30 => array:2 [ | |
"label" => "active" | |
"color" => "green" | |
] | |
] | |
*/ | |
var_dump(Status::map(0)); | |
/* | |
Output: | |
----- | |
array:2 [ | |
"label" => "passive" | |
"color" => "red" | |
] | |
*/ | |
var_dump(Status::map(1)); | |
/* | |
Output: | |
----- | |
array:2 [ | |
"label" => "active" | |
"color" => "green" | |
] | |
*/ | |
var_dump(Status::map(2)); | |
/* | |
Output: | |
----- | |
UnexpectedValueException | |
Unable to found value of [2] in the [App\Enums\Status] class | |
*/ | |
var_dump(Status::toArray()); | |
/* | |
Output: | |
----- | |
array:2 [ | |
"PASSIVE" => 20 | |
"ACTIVE" => 30 | |
] | |
*/ | |
var_dump(Status::toImplode()); | |
/* | |
Output: | |
----- | |
"20,30" | |
*/ | |
var_dump(Status::toImplode(' and ')); | |
/* | |
Output: | |
----- | |
"20 and 30" | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Enums; | |
class Status extends Enum | |
{ | |
/** | |
* The value of "passive" status. | |
* | |
* @const int | |
*/ | |
const PASSIVE = 20; | |
/** | |
* The value of "active" status. | |
* | |
* @const int | |
*/ | |
const ACTIVE = 30; | |
/** | |
* Get the maps of class constants. | |
* | |
* @return array | |
*/ | |
public static function mapping(): array | |
{ | |
return [ | |
static::PASSIVE => [ | |
'label' => 'passive', | |
'color' => 'red', | |
], | |
static::ACTIVE => [ | |
'label' => 'active', | |
'color' => 'green', | |
], | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment