1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TreeHouse\Model\Config\Field; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Base Enum class |
7
|
|
|
* |
8
|
|
|
* Create an enum by implementing this class and adding class constants. |
9
|
|
|
*/ |
10
|
|
|
abstract class Enum |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Enum value |
14
|
|
|
* |
15
|
|
|
* @var mixed |
16
|
|
|
*/ |
17
|
|
|
protected $value; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var boolean |
21
|
|
|
*/ |
22
|
|
|
protected static $multiValued = false; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected static $cache = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Creates a new value of some type |
31
|
|
|
* |
32
|
|
|
* @param mixed $value |
33
|
|
|
* |
34
|
|
|
* @throws \UnexpectedValueException if incompatible type is given. |
35
|
|
|
*/ |
36
|
10 |
|
final public function __construct($value) |
37
|
|
|
{ |
38
|
10 |
|
if (!in_array($value, self::toArray())) { |
39
|
2 |
|
throw new \UnexpectedValueException("Value '$value' is not part of the enum " . get_called_class()); |
40
|
|
|
} |
41
|
|
|
|
42
|
8 |
|
$this->value = $value; |
43
|
8 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return string |
47
|
|
|
*/ |
48
|
2 |
|
final public function getName() |
49
|
|
|
{ |
50
|
2 |
|
return array_search($this->value, self::toArray()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return mixed |
55
|
|
|
*/ |
56
|
4 |
|
final public function getValue() |
57
|
|
|
{ |
58
|
4 |
|
return $this->value; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
2 |
|
final public function __toString() |
65
|
|
|
{ |
66
|
2 |
|
return (string) $this->value; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Returns all possible values as an array |
71
|
|
|
* |
72
|
|
|
* @return array Constant name in key, constant value in value |
73
|
|
|
*/ |
74
|
30 |
|
final public static function toArray() |
75
|
|
|
{ |
76
|
30 |
|
$class = get_called_class(); |
77
|
|
|
|
78
|
30 |
|
if (!isset(self::$cache[$class])) { |
79
|
4 |
|
self::$cache[$class] = (new \ReflectionClass($class))->getConstants(); |
80
|
4 |
|
} |
81
|
|
|
|
82
|
30 |
|
return self::$cache[$class]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return boolean |
87
|
|
|
*/ |
88
|
20 |
|
final public static function isMultiValued() |
89
|
|
|
{ |
90
|
20 |
|
return static::$multiValued; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Returns a value when called statically like so: MyEnum::SOME_VALUE() given SOME_VALUE is a class constant |
95
|
|
|
* |
96
|
|
|
* @param string $name |
97
|
|
|
* @param array $arguments |
98
|
|
|
* |
99
|
|
|
* @throws \BadMethodCallException |
100
|
|
|
* |
101
|
|
|
* @return static |
102
|
|
|
*/ |
103
|
|
|
final public static function __callStatic($name, $arguments) |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
if (defined("static::$name")) { |
106
|
|
|
return new static(constant("static::$name")); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
throw new \BadMethodCallException("No static method or enum constant '$name' in class " . get_called_class()); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.