1515 */
1616abstract class Enum
1717{
18- protected static array $ cacheItems = [];
18+ protected static array $ cacheData = [];
1919 protected static array $ cacheInstances = [];
2020
2121 /**
@@ -76,27 +76,10 @@ public static function isValid($id, array $filter = []): bool
7676 */
7777 public static function toArray (array $ filter = []): array
7878 {
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 ) {
10083 foreach ($ filter as $ key => $ filterItem ) {
10184 if (is_int ($ key )) {
10285 $ operator = $ filterItem [0 ];
@@ -146,7 +129,7 @@ public static function toArray(array $filter = []): array
146129 }
147130 } else {
148131 return call_user_func_array (
149- [$ class , $ operator ],
132+ [get_called_class () , $ operator ],
150133 array_merge ([$ item ], array_slice ($ filterItem , 1 ))
151134 );
152135 }
@@ -159,7 +142,6 @@ public static function toArray(array $filter = []): array
159142 }
160143 return true ;
161144 });
162- return $ items ;
163145 }
164146
165147 /**
@@ -207,6 +189,21 @@ public static function toObjects(array $filter = []): array
207189 return $ objects ;
208190 }
209191
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+
210207 /**
211208 * @param string $name
212209 * @return mixed
@@ -223,11 +220,42 @@ public function __get($name)
223220 throw new LogicException ('Getting unknown property: ' . get_class ($ this ) . ':: ' . $ name );
224221 }
225222
226- /**
227- * @return string
228- */
229- public function __toString ()
223+ public function __toString (): string
230224 {
231225 return (string )$ this ->id ;
232226 }
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+ }
233261}
0 commit comments