-
Notifications
You must be signed in to change notification settings - Fork 0
/
NamedEnum.php
115 lines (98 loc) · 3.37 KB
/
NamedEnum.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
namespace Mesavolt\Enum;
use Mesavolt\Helper\VariableDumper;
/**
* Extend this class and implement a static $VALUE_NAMES property
* to benefit from all the methods defined here.
*/
abstract class NamedEnum
{
protected static $VALUE_NAMES = [];
/**
* Get all the values, indexed by their constant name as defined in the class.
*/
public static function constants(): array
{
$class = static::class;
$itemClass = new \ReflectionClass($class);
return $itemClass->getConstants();
}
/**
* Get all the names, indexed by their value.
*/
public static function getNames(?array $alternativeNames = null): array
{
return $alternativeNames ?: static::$VALUE_NAMES;
}
/**
* Get the name of a value, or null if the value doesn't exist.
*/
public static function getName($value, ?array $alternativeNames = null): ?string
{
return ($alternativeNames ?: static::$VALUE_NAMES)[$value] ?? null;
}
/**
* Get an array of all the values.
*/
public static function values(): array
{
return array_values(self::constants());
}
/**
* Get an array of all the names.
*/
public static function names(?array $alternativeNames = null): array
{
return array_values($alternativeNames ?: static::$VALUE_NAMES);
}
/**
* Get an array of all the values indexed by name
* (especially useful to use in a ChoiceType field in a Symfony Form).
*/
public static function choices(?array $alternativeNames = null): array
{
return array_flip($alternativeNames ?: static::$VALUE_NAMES);
}
/**
* Get all the values as an array of associative arrays of the form ['name' => NAME, 'value' => VALUE].
*/
public static function arrays(?array $alternativeNames = null): array
{
$array = [];
foreach(($alternativeNames ?: static::$VALUE_NAMES) as $value => $name) {
$array[] = ['name' => $name, 'value' => $value];
}
return $array;
}
/**
* Returns true if the specified value is declared as one of this enum values.
*/
public static function isValid($value, bool $nullable = false, bool $strictCheck = true): bool
{
// Null and nullable is allowed
if ($nullable && $value === null) {
return true;
}
return \in_array($value, self::constants(), $strictCheck);
}
/**
* Throws an exception if the specified value is not declared as one of this enum values.
*/
public static function ensureValid($value, bool $nullable = false, bool $strictCheck = true): void
{
if (!self::isValid($value, $nullable, $strictCheck)) {
$callerContext = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1];
$callerMethod = array_key_exists('class', $callerContext)
? $callerContext['class'].$callerContext['type'].$callerContext['function']
: $callerContext['file'].'::'.$callerContext['function'];
throw new \InvalidArgumentException(
sprintf(
'Invalid argument provided to %s - expected one of "%s", got "%s"',
$callerMethod,
implode(', ', self::constants()),
VariableDumper::dump($value)
)
);
}
}
}