15
15
*/
16
16
abstract class Enum
17
17
{
18
- protected static array $ cacheItems = [];
18
+ protected static array $ cacheData = [];
19
19
protected static array $ cacheInstances = [];
20
20
21
21
/**
@@ -76,27 +76,10 @@ public static function isValid($id, array $filter = []): bool
76
76
*/
77
77
public static function toArray (array $ filter = []): array
78
78
{
79
- $ class = get_called_class ();
80
- if (!array_key_exists ($ class , static ::$ cacheItems )) {
81
- $ reflection = new \ReflectionClass ($ class );
82
- if (is_callable ([$ class , 'items ' ])) {
83
- /** @noinspection PhpUndefinedMethodInspection */
84
- $ items = $ class ::items ();
85
- array_walk ($ items , function (&$ item ) {
86
- $ item = is_array ($ item ) ? $ item : ['name ' => $ item ];
87
- });
88
- } else {
89
- $ items = array_fill_keys ($ reflection ->getConstants (), []);
90
- }
91
- foreach ($ reflection ->getConstants () as $ constant ) {
92
- if (!isset ($ items [$ constant ]['name ' ])) {
93
- $ items [$ constant ]['name ' ] = $ constant ;
94
- }
95
- $ items [$ constant ]['id ' ] = $ constant ;
96
- }
97
- static ::$ cacheItems [$ class ] = $ items ;
98
- }
99
- $ items = array_filter (static ::$ cacheItems [$ class ], function ($ item ) use ($ class , $ filter ) {
79
+ $ items = array_map (function (array $ data ) {
80
+ return $ data ['item ' ];
81
+ }, static ::getData ());
82
+ return array_filter ($ items , function ($ item ) use ($ filter ) {
100
83
foreach ($ filter as $ key => $ filterItem ) {
101
84
if (is_int ($ key )) {
102
85
$ operator = $ filterItem [0 ];
@@ -146,7 +129,7 @@ public static function toArray(array $filter = []): array
146
129
}
147
130
} else {
148
131
return call_user_func_array (
149
- [$ class , $ operator ],
132
+ [get_called_class () , $ operator ],
150
133
array_merge ([$ item ], array_slice ($ filterItem , 1 ))
151
134
);
152
135
}
@@ -159,7 +142,6 @@ public static function toArray(array $filter = []): array
159
142
}
160
143
return true ;
161
144
});
162
- return $ items ;
163
145
}
164
146
165
147
/**
@@ -207,6 +189,21 @@ public static function toObjects(array $filter = []): array
207
189
return $ objects ;
208
190
}
209
191
192
+ public static function __callStatic ($ name , $ arguments )
193
+ {
194
+ if ($ name === 'items ' ) {
195
+ return [];
196
+ }
197
+
198
+ foreach (static ::getData () as $ id => $ data ) {
199
+ if ($ data ['constantName ' ] === $ name ) {
200
+ return static ::get ($ id );
201
+ }
202
+ }
203
+
204
+ throw new \RuntimeException ();
205
+ }
206
+
210
207
/**
211
208
* @param string $name
212
209
* @return mixed
@@ -223,11 +220,42 @@ public function __get($name)
223
220
throw new LogicException ('Getting unknown property: ' . get_class ($ this ) . ':: ' . $ name );
224
221
}
225
222
226
- /**
227
- * @return string
228
- */
229
- public function __toString ()
223
+ public function __toString (): string
230
224
{
231
225
return (string )$ this ->id ;
232
226
}
227
+
228
+ private static function getData (): array
229
+ {
230
+ $ class = get_called_class ();
231
+ if (!array_key_exists ($ class , static ::$ cacheData )) {
232
+ $ reflection = new \ReflectionClass ($ class );
233
+
234
+ $ items = call_user_func ([$ class , 'items ' ]);
235
+
236
+ $ data = [];
237
+ foreach ($ reflection ->getConstants () as $ constantName => $ id ) {
238
+ if (array_key_exists ($ id , $ items )) {
239
+ $ item = is_array ($ items [$ id ]) ? $ items [$ id ] : [
240
+ 'name ' => $ items [$ id ],
241
+ ];
242
+ } else {
243
+ $ item = [];
244
+ }
245
+
246
+ $ item ['id ' ] = $ id ;
247
+ if (!array_key_exists ('name ' , $ item )) {
248
+ $ item ['name ' ] = $ id ;
249
+ }
250
+
251
+ $ data [$ id ] = [
252
+ 'constantName ' => $ constantName ,
253
+ 'item ' => $ item ,
254
+ ];
255
+ }
256
+
257
+ static ::$ cacheData [$ class ] = $ data ;
258
+ }
259
+ return static ::$ cacheData [$ class ];
260
+ }
233
261
}
0 commit comments