@@ -17,7 +17,12 @@ abstract class Enum
1717 /**
1818 * @var array
1919 */
20- protected static $ _cache = [];
20+ protected static $ _cacheItems = [];
21+
22+ /**
23+ * @var array
24+ */
25+ protected static $ _cacheInstances = [];
2126
2227 /**
2328 * @var int|string
@@ -44,6 +49,22 @@ public function __construct($id)
4449 }
4550 }
4651
52+ /**
53+ * @param int|string $id
54+ * @return static
55+ * @throws ReflectionException
56+ * @throws UnexpectedValueException
57+ * @since 2.1.0
58+ */
59+ public static function get ($ id )
60+ {
61+ $ key = get_called_class () . '~ ' . $ id ;
62+ if (empty (static ::$ _cacheInstances [$ key ])) {
63+ static ::$ _cacheInstances [$ key ] = new static ($ id );
64+ }
65+ return static ::$ _cacheInstances [$ key ];
66+ }
67+
4768 /**
4869 * Проверяет входит ли значение в допустимые
4970 * @param int|string $id
@@ -65,7 +86,7 @@ public static function isValid($id, array $filter = [])
6586 public static function toArray (array $ filter = [])
6687 {
6788 $ class = get_called_class ();
68- if (!array_key_exists ($ class , static ::$ _cache )) {
89+ if (!array_key_exists ($ class , static ::$ _cacheItems )) {
6990 $ reflection = new \ReflectionClass ($ class );
7091 if (is_callable ([$ class , 'items ' ])) {
7192 /** @noinspection PhpUndefinedMethodInspection */
@@ -82,9 +103,9 @@ public static function toArray(array $filter = [])
82103 }
83104 $ items [$ constant ]['id ' ] = $ constant ;
84105 }
85- static ::$ _cache [$ class ] = $ items ;
106+ static ::$ _cacheItems [$ class ] = $ items ;
86107 }
87- $ items = array_filter (static ::$ _cache [$ class ], function ($ item ) use ($ filter ) {
108+ $ items = array_filter (static ::$ _cacheItems [$ class ], function ($ item ) use ($ filter ) {
88109 foreach ($ filter as $ key => $ filterItem ) {
89110 if (is_int ($ key )) {
90111 list ($ operator , $ key , $ value ) = $ filterItem ;
@@ -181,7 +202,7 @@ public static function toObjects(array $filter = [])
181202 {
182203 $ objects = [];
183204 foreach (static ::toIds ($ filter ) as $ id ) {
184- $ objects [$ id ] = new static ($ id );
205+ $ objects [$ id ] = static :: get ($ id );
185206 }
186207 return $ objects ;
187208 }
0 commit comments