@@ -25,26 +25,26 @@ abstract class BaseEnum
2525 *
2626 * @var array
2727 */
28- private static $ byName = [];
28+ protected static $ byName = [];
2929
3030 /**
3131 * The cached list of constants by value.
3232 *
3333 * @var array
3434 */
35- private static $ byValue = [];
35+ protected static $ byValue = [];
3636
3737 /**
3838 * @var array list of properties
3939 */
40- private static $ list ;
40+ protected static $ list = [] ;
4141
4242 /**
4343 * The value managed by this type instance.
4444 *
4545 * @var mixed
4646 */
47- private $ _value ;
47+ protected $ value ;
4848
4949 /**
5050 * Sets the value that will be managed by this type instance.
@@ -55,11 +55,11 @@ abstract class BaseEnum
5555 */
5656 public function __construct ($ value )
5757 {
58- if (!self ::isValidValue ($ value )) {
58+ if (!static ::isValidValue ($ value )) {
5959 throw new UnexpectedValueException ("Value ' {$ value }' is not part of the enum " . get_called_class ());
6060 }
6161
62- $ this ->_value = $ value ;
62+ $ this ->value = $ value ;
6363 }
6464
6565 /**
@@ -73,7 +73,7 @@ public function __construct($value)
7373 */
7474 public static function createByName ($ name )
7575 {
76- $ constants = self ::getConstantsByName ();
76+ $ constants = static ::getConstantsByName ();
7777
7878 if (!array_key_exists ($ name , $ constants )) {
7979 throw new UnexpectedValueException ("Name ' {$ name }' is not exists in the enum constants list " . get_called_class ());
@@ -91,9 +91,7 @@ public static function createByName($name)
9191 */
9292 public static function getValueByName ($ value )
9393 {
94- $ list = self ::listData ();
95-
96- return array_search ($ value , $ list );
94+ return array_search ($ value , static ::listData ());
9795 }
9896
9997 /**
@@ -107,9 +105,7 @@ public static function getValueByName($value)
107105 */
108106 public static function createByValue ($ value )
109107 {
110- $ constants = self ::getConstantsByValue ();
111-
112- if (!array_key_exists ($ value , $ constants )) {
108+ if (!array_key_exists ($ value , static ::getConstantsByValue ())) {
113109 throw new UnexpectedValueException ("Value ' {$ value }' is not exists in the enum constants list " . get_called_class ());
114110 }
115111
@@ -119,24 +115,13 @@ public static function createByValue($value)
119115 /**
120116 * Get list data
121117 *
122- * @static
123- *
124118 * @return mixed
125119 */
126120 public static function listData ()
127121 {
128- $ class = get_called_class ();
129-
130- if (!isset (self ::$ list [$ class ])) {
131- $ reflection = new ReflectionClass ($ class );
132- self ::$ list [$ class ] = $ reflection ->getStaticPropertyValue ('list ' );
133- }
134-
135- $ result = ArrayHelper::getColumn (self ::$ list [$ class ], function ($ value ) {
136- return Yii::t (self ::$ messageCategory , $ value );
122+ return ArrayHelper::getColumn (static ::$ list , function ($ value ) {
123+ return Yii::t (static ::$ messageCategory , $ value );
137124 });
138-
139- return $ result ;
140125 }
141126
142127 /**
@@ -166,22 +151,12 @@ public static function getConstantsByName()
166151 {
167152 $ class = get_called_class ();
168153
169- if (!isset ( self ::$ byName[ $ class ] )) {
154+ if (!array_key_exists ( $ class , static ::$ byName )) {
170155 $ reflection = new ReflectionClass ($ class );
171- self ::$ byName [$ class ] = $ reflection ->getConstants ();
172- while (false !== ($ reflection = $ reflection ->getParentClass ())) {
173- if (__CLASS__ === $ reflection ->getName ()) {
174- break ;
175- }
176-
177- self ::$ byName [$ class ] = array_replace (
178- $ reflection ->getConstants (),
179- self ::$ byName [$ class ]
180- );
181- }
156+ static ::$ byName [$ class ] = $ reflection ->getConstants ();
182157 }
183158
184- return self ::$ byName [$ class ];
159+ return static ::$ byName [$ class ];
185160 }
186161
187162 /**
@@ -193,26 +168,11 @@ public static function getConstantsByValue()
193168 {
194169 $ class = get_called_class ();
195170
196- if (!isset (self ::$ byValue [$ class ])) {
197- self ::getConstantsByName ();
198-
199- self ::$ byValue [$ class ] = [];
200-
201- foreach (self ::$ byName [$ class ] as $ name => $ value ) {
202- if (array_key_exists ($ value , self ::$ byValue [$ class ])) {
203- if (!is_array (self ::$ byValue [$ class ][$ value ])) {
204- self ::$ byValue [$ class ][$ value ] = [
205- self ::$ byValue [$ class ][$ value ],
206- ];
207- }
208- self ::$ byValue [$ class ][$ value ][] = $ name ;
209- } else {
210- self ::$ byValue [$ class ][$ value ] = $ name ;
211- }
212- }
171+ if (!isset (static ::$ byValue [$ class ])) {
172+ static ::$ byValue [$ class ] = array_flip (static ::getConstantsByName ());
213173 }
214174
215- return self ::$ byValue [$ class ];
175+ return static ::$ byValue [$ class ];
216176 }
217177
218178 /**
@@ -222,9 +182,9 @@ public static function getConstantsByValue()
222182 */
223183 public function getName ()
224184 {
225- $ constants = self ::getConstantsByValue ();
185+ $ constants = static ::getConstantsByValue ();
226186
227- return $ constants [$ this ->_value ];
187+ return $ constants [$ this ->value ];
228188 }
229189
230190 /**
@@ -234,7 +194,7 @@ public function getName()
234194 */
235195 public function getValue ()
236196 {
237- return $ this ->_value ;
197+ return $ this ->value ;
238198 }
239199
240200 /**
@@ -247,9 +207,7 @@ public function getValue()
247207 */
248208 public static function isValidName ($ name )
249209 {
250- $ constants = self ::getConstantsByName ();
251-
252- return array_key_exists ($ name , $ constants );
210+ return array_key_exists ($ name , static ::getConstantsByName ());
253211 }
254212
255213 /**
@@ -262,9 +220,7 @@ public static function isValidName($name)
262220 */
263221 public static function isValidValue ($ value )
264222 {
265- $ constants = self ::getConstantsByValue ();
266-
267- return array_key_exists ($ value , $ constants );
223+ return array_key_exists ($ value , static ::getConstantsByValue ());
268224 }
269225
270226 /**
@@ -293,6 +249,6 @@ public static function __callStatic($name, $arguments)
293249 */
294250 public function __toString ()
295251 {
296- return (string )$ this ->_value ;
252+ return (string ) $ this ->value ;
297253 }
298254}
0 commit comments