1
|
|
|
<?php namespace BuildR\Foundation\Enumeration; |
2
|
|
|
|
3
|
|
|
use BuildR\Foundation\Exception\BadMethodCallException; |
4
|
|
|
use BuildR\Foundation\Exception\UnexpectedValueException; |
5
|
|
|
use \ReflectionClass; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Solves a common PHP problem, that language not has enumeration type, by |
9
|
|
|
* allowing class constants to used to be enumeration |
10
|
|
|
* values thought PHP reflection API. |
11
|
|
|
* |
12
|
|
|
* BuildR PHP Framework |
13
|
|
|
* |
14
|
|
|
* @author Zoltán Borsos <[email protected]> |
15
|
|
|
* @package Foundation |
16
|
|
|
* @subpackage Object |
17
|
|
|
* |
18
|
|
|
* @copyright Copyright 2015, Zoltán Borsos. |
19
|
|
|
* @license https://github.com/Zolli/BuildR/blob/master/LICENSE.md |
20
|
|
|
* @link https://github.com/Zolli/BuildR |
21
|
|
|
*/ |
22
|
|
|
abstract class AbstractEnumeration { |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @type array |
26
|
|
|
*/ |
27
|
|
|
private static $cache = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @type string |
31
|
|
|
*/ |
32
|
|
|
protected $value; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* AbstractEnumeration Constructor. Create a new instance from this enumeration. |
36
|
|
|
* When the given value is not a valid element of the current enumeration a |
37
|
|
|
* UnexpectedValueException will be thrown. |
38
|
|
|
* |
39
|
|
|
* @param string $value The |
40
|
|
|
* |
41
|
|
|
* @throws \BuildR\Foundation\Exception\UnexpectedValueException |
42
|
|
|
*/ |
43
|
20 |
|
public function __construct($value) { |
44
|
20 |
|
if(!$this->isValid($value)) { |
45
|
4 |
|
throw new UnexpectedValueException('This enumeration is not contains any constant like: ' . $value); |
46
|
|
|
} |
47
|
|
|
|
48
|
20 |
|
$this->value = $value; |
49
|
20 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Returns the key of the current element |
53
|
|
|
* |
54
|
|
|
* @return NULL|string |
55
|
|
|
*/ |
56
|
1 |
|
public function getKey() { |
57
|
1 |
|
$value = self::find($this->value); |
58
|
|
|
|
59
|
|
|
//In real life NULL should be never happen! |
60
|
1 |
|
return ($value === FALSE) ? NULL : $value; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Returns the current element value |
65
|
|
|
* |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
1 |
|
public function getValue() { |
69
|
1 |
|
return $this->value; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Validate the currently set value in the enumeration |
74
|
|
|
* |
75
|
|
|
* @param string $value |
76
|
|
|
* |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
20 |
|
public function isValid($value) { |
80
|
20 |
|
return (bool) in_array($value, self::toArray()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Returns the current value as string representation |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
1 |
|
public function __toString() { |
89
|
1 |
|
return (string) $this->value; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Translates the current enumeration class to an array. This function use |
94
|
|
|
* runtime caching mechanism, all enumeration constants stored in a static |
95
|
|
|
* value. |
96
|
|
|
* |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
20 |
|
public static function toArray() { |
100
|
20 |
|
$enumClass = get_called_class(); |
101
|
|
|
|
102
|
20 |
|
if(!array_key_exists($enumClass, self::$cache)) { |
103
|
1 |
|
$reflector = new ReflectionClass($enumClass); |
104
|
1 |
|
self::$cache[$enumClass] = $reflector->getConstants(); |
105
|
1 |
|
} |
106
|
|
|
|
107
|
20 |
|
return self::$cache[$enumClass]; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Find a key in the enumeration by its value. If this value |
112
|
|
|
* is exist in the current enumeration this function returns the |
113
|
|
|
* corresponding key, NULL otherwise. |
114
|
|
|
* |
115
|
|
|
* @param string $value The value |
116
|
|
|
* |
117
|
|
|
* @return NULL|string |
118
|
|
|
*/ |
119
|
2 |
|
public static function find($value) { |
120
|
2 |
|
$result = array_search($value, self::toArray(), TRUE); |
121
|
|
|
|
122
|
2 |
|
return ($result === FALSE) ? NULL : $result; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Returns the length of the current enumeration. |
127
|
|
|
* |
128
|
|
|
* @return ins |
129
|
|
|
*/ |
130
|
1 |
|
public static function count() { |
131
|
1 |
|
return (int) count(self::toArray()); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Returns all key from the current enumeration as array |
136
|
|
|
* |
137
|
|
|
* @return array |
138
|
|
|
*/ |
139
|
1 |
|
public static function getKeys() { |
140
|
1 |
|
return array_keys(self::toArray()); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Determines that the given key is exist in the current enumeration. |
145
|
|
|
* If the key is not a string a BadMethodCallException will be thrown. |
146
|
|
|
* |
147
|
|
|
* @param string $key The desired key |
148
|
|
|
* |
149
|
|
|
* @return bool |
150
|
|
|
* |
151
|
|
|
* @throws \BuildR\Foundation\Exception\BadMethodCallException |
152
|
|
|
*/ |
153
|
7 |
|
public static function isValidKey($key) { |
154
|
7 |
|
if(!is_string($key)) { |
155
|
6 |
|
throw new BadMethodCallException('The key must be a string! ' . gettype($key) . ' given!'); |
156
|
|
|
} |
157
|
|
|
|
158
|
1 |
|
return array_key_exists($key, self::toArray()); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* This PHP magic method is a proxy for creating new instance from |
163
|
|
|
* enumerations. The method name used as the class constructor parameter. |
164
|
|
|
* The passed arguments ignored. Validation is not needed here because |
165
|
|
|
* is handled by the constructor itself. |
166
|
|
|
* |
167
|
|
|
* @param string $name The method name |
168
|
|
|
* @param array $arguments Passed arguments |
169
|
|
|
* |
170
|
|
|
* @return static |
171
|
|
|
*/ |
172
|
20 |
|
public static function __callStatic($name, $arguments) { |
|
|
|
|
173
|
20 |
|
return new static(constant("static::$name")); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.