1 | <?php |
||
17 | abstract class AbstractEnum implements EnumInterface |
||
18 | { |
||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | private $value; |
||
23 | |||
24 | /** |
||
25 | * An array of created instances to ensure singleton |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | private static $instances = []; |
||
30 | |||
31 | /** |
||
32 | * Constructor |
||
33 | * |
||
34 | * @param string $value |
||
35 | * @throws RuntimeException If the value is not valid |
||
36 | */ |
||
37 | 6 | final private function __construct($value) |
|
45 | |||
46 | /** |
||
47 | * Return the value as a string |
||
48 | * |
||
49 | * @return string |
||
50 | */ |
||
51 | 4 | public function __toString() |
|
55 | |||
56 | /** |
||
57 | * Do a comparison to check if the enum values are equal |
||
58 | * |
||
59 | * @param string $value |
||
60 | * @return bool |
||
61 | */ |
||
62 | 5 | public function equals($value) |
|
66 | |||
67 | /** |
||
68 | * Create a new instance of the enum |
||
69 | * |
||
70 | * @param string $value |
||
71 | * @return AbstractEnum |
||
72 | * @throws RuntimeException If the value is not valid |
||
73 | */ |
||
74 | 26 | public static function create($value) |
|
86 | |||
87 | /** |
||
88 | * Return true if the value is valid for the enum |
||
89 | * |
||
90 | * @param string $value |
||
91 | * @return bool |
||
92 | */ |
||
93 | 10 | public static function exists($value) |
|
97 | |||
98 | /** |
||
99 | * Get an array of the enum values |
||
100 | * |
||
101 | * @return array |
||
102 | */ |
||
103 | 12 | public static function values() |
|
107 | |||
108 | /** |
||
109 | * Return enum as an array with keys matching values |
||
110 | * |
||
111 | * @return array |
||
112 | */ |
||
113 | 1 | public static function toArray() |
|
117 | |||
118 | /** |
||
119 | * Get the current enum value |
||
120 | * |
||
121 | * @return mixed |
||
122 | */ |
||
123 | 13 | public function getValue() |
|
127 | |||
128 | /** |
||
129 | * Allow enum instantiation by magic method |
||
130 | * |
||
131 | * @param string $name |
||
132 | * @param $arguments |
||
133 | * @return static |
||
134 | * @throws RuntimeException If the value is not valid |
||
135 | * @throws BadMethodCallException If the constant doesn't exist in the class |
||
136 | */ |
||
137 | 4 | public static function __callStatic($name, $arguments) |
|
147 | } |
||
148 |