2
2
3
3
namespace vjik \enum ;
4
4
5
+ use LogicException ;
6
+ use ReflectionException ;
7
+ use UnexpectedValueException ;
8
+
5
9
/**
6
10
* Abstract Enum class
7
- *
8
11
* @property mixed $id
9
12
* @property mixed $name
10
13
*/
11
14
abstract class Enum
12
15
{
13
16
14
-
15
17
/**
16
18
* @var array
17
19
*/
18
20
protected static $ _cache = [];
19
21
20
-
21
22
/**
22
- * @var mixed
23
+ * @var int|string
23
24
*/
24
25
protected $ id ;
25
26
26
-
27
27
/**
28
28
* @var string
29
29
*/
30
30
protected $ name ;
31
31
32
-
33
32
/**
34
- * @param mixed $id
35
- *
36
- * @throws \UnexpectedValueException
33
+ * @param int|string $id
34
+ * @throws UnexpectedValueException
35
+ * @throws ReflectionException
37
36
*/
38
37
public function __construct ($ id )
39
38
{
40
39
if (!static ::isValid ($ id )) {
41
- throw new \ UnexpectedValueException ("Value ' $ id' is not part of the enum " . get_called_class ());
40
+ throw new UnexpectedValueException ("Value ' $ id' is not part of the enum " . get_called_class ());
42
41
}
43
42
foreach (static ::toArray ()[$ id ] as $ k => $ v ) {
44
43
$ this ->$ k = $ v ;
45
44
}
46
45
}
47
46
48
-
49
47
/**
50
48
* Проверяет входит ли значение в допустимые
51
- *
52
- * @param $id
53
- * @param $filter
54
- *
49
+ * @param int|string $id
50
+ * @param array $filter
55
51
* @return bool
52
+ * @throws ReflectionException
56
53
*/
57
54
public static function isValid ($ id , array $ filter = [])
58
55
{
59
56
return in_array ($ id , static ::toIds ($ filter ), true );
60
57
}
61
58
62
-
63
59
/**
64
60
* Все доступные значения в виде массива с данными
65
- *
66
61
* @param array $filter ['key' => 'value', ['operator', 'key', 'value'], …]
67
- *
68
62
* @return array enum-значение - ключ, массив с данными - значение
63
+ * @throws ReflectionException
69
64
*/
70
65
public static function toArray (array $ filter = [])
71
66
{
@@ -146,13 +141,11 @@ public static function toArray(array $filter = [])
146
141
return $ items ;
147
142
}
148
143
149
-
150
144
/**
151
145
* Все доступные значения в виде массива
152
- *
153
146
* @param array $filter
154
- *
155
147
* @return array
148
+ * @throws ReflectionException
156
149
*/
157
150
public static function toIds (array $ filter = [])
158
151
{
@@ -163,13 +156,11 @@ public static function toIds(array $filter = [])
163
156
return $ ids ;
164
157
}
165
158
166
-
167
159
/**
168
160
* Все доступные значение с именами
169
- *
170
161
* @param array $filter
171
- *
172
162
* @return array enum-значение - ключ, имя - значение
163
+ * @throws ReflectionException
173
164
*/
174
165
public static function toList (array $ filter = [])
175
166
{
@@ -180,13 +171,11 @@ public static function toList(array $filter = [])
180
171
return $ list ;
181
172
}
182
173
183
-
184
174
/**
185
175
* Все доступные значения в виде объектов
186
- *
187
176
* @param array $filter
188
- *
189
177
* @return array
178
+ * @throws ReflectionException
190
179
*/
191
180
public static function toObjects (array $ filter = [])
192
181
{
@@ -197,13 +186,10 @@ public static function toObjects(array $filter = [])
197
186
return $ objects ;
198
187
}
199
188
200
-
201
189
/**
202
- * @param $name
203
- *
190
+ * @param string $name
204
191
* @return mixed
205
- *
206
- * @throws \LogicException
192
+ * @throws LogicException
207
193
*/
208
194
public function __get ($ name )
209
195
{
@@ -213,10 +199,9 @@ public function __get($name)
213
199
} elseif (property_exists ($ this , $ name )) {
214
200
return $ this ->{$ name };
215
201
}
216
- throw new \ LogicException ('Getting unknown property: ' . get_class ($ this ) . ':: ' . $ name );
202
+ throw new LogicException ('Getting unknown property: ' . get_class ($ this ) . ':: ' . $ name );
217
203
}
218
204
219
-
220
205
/**
221
206
* @return string
222
207
*/
0 commit comments