@@ -25,26 +25,26 @@ abstract class BaseEnum
25
25
*
26
26
* @var array
27
27
*/
28
- private static $ byName = [];
28
+ protected static $ byName = [];
29
29
30
30
/**
31
31
* The cached list of constants by value.
32
32
*
33
33
* @var array
34
34
*/
35
- private static $ byValue = [];
35
+ protected static $ byValue = [];
36
36
37
37
/**
38
38
* @var array list of properties
39
39
*/
40
- private static $ list ;
40
+ protected static $ list = [] ;
41
41
42
42
/**
43
43
* The value managed by this type instance.
44
44
*
45
45
* @var mixed
46
46
*/
47
- private $ _value ;
47
+ protected $ value ;
48
48
49
49
/**
50
50
* Sets the value that will be managed by this type instance.
@@ -55,11 +55,11 @@ abstract class BaseEnum
55
55
*/
56
56
public function __construct ($ value )
57
57
{
58
- if (!self ::isValidValue ($ value )) {
58
+ if (!static ::isValidValue ($ value )) {
59
59
throw new UnexpectedValueException ("Value ' {$ value }' is not part of the enum " . get_called_class ());
60
60
}
61
61
62
- $ this ->_value = $ value ;
62
+ $ this ->value = $ value ;
63
63
}
64
64
65
65
/**
@@ -73,7 +73,7 @@ public function __construct($value)
73
73
*/
74
74
public static function createByName ($ name )
75
75
{
76
- $ constants = self ::getConstantsByName ();
76
+ $ constants = static ::getConstantsByName ();
77
77
78
78
if (!array_key_exists ($ name , $ constants )) {
79
79
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)
91
91
*/
92
92
public static function getValueByName ($ value )
93
93
{
94
- $ list = self ::listData ();
95
-
96
- return array_search ($ value , $ list );
94
+ return array_search ($ value , static ::listData ());
97
95
}
98
96
99
97
/**
@@ -107,9 +105,7 @@ public static function getValueByName($value)
107
105
*/
108
106
public static function createByValue ($ value )
109
107
{
110
- $ constants = self ::getConstantsByValue ();
111
-
112
- if (!array_key_exists ($ value , $ constants )) {
108
+ if (!array_key_exists ($ value , static ::getConstantsByValue ())) {
113
109
throw new UnexpectedValueException ("Value ' {$ value }' is not exists in the enum constants list " . get_called_class ());
114
110
}
115
111
@@ -119,24 +115,13 @@ public static function createByValue($value)
119
115
/**
120
116
* Get list data
121
117
*
122
- * @static
123
- *
124
118
* @return mixed
125
119
*/
126
120
public static function listData ()
127
121
{
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 );
137
124
});
138
-
139
- return $ result ;
140
125
}
141
126
142
127
/**
@@ -166,22 +151,12 @@ public static function getConstantsByName()
166
151
{
167
152
$ class = get_called_class ();
168
153
169
- if (!isset ( self ::$ byName[ $ class ] )) {
154
+ if (!array_key_exists ( $ class , static ::$ byName )) {
170
155
$ 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 ();
182
157
}
183
158
184
- return self ::$ byName [$ class ];
159
+ return static ::$ byName [$ class ];
185
160
}
186
161
187
162
/**
@@ -193,26 +168,11 @@ public static function getConstantsByValue()
193
168
{
194
169
$ class = get_called_class ();
195
170
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 ());
213
173
}
214
174
215
- return self ::$ byValue [$ class ];
175
+ return static ::$ byValue [$ class ];
216
176
}
217
177
218
178
/**
@@ -222,9 +182,9 @@ public static function getConstantsByValue()
222
182
*/
223
183
public function getName ()
224
184
{
225
- $ constants = self ::getConstantsByValue ();
185
+ $ constants = static ::getConstantsByValue ();
226
186
227
- return $ constants [$ this ->_value ];
187
+ return $ constants [$ this ->value ];
228
188
}
229
189
230
190
/**
@@ -234,7 +194,7 @@ public function getName()
234
194
*/
235
195
public function getValue ()
236
196
{
237
- return $ this ->_value ;
197
+ return $ this ->value ;
238
198
}
239
199
240
200
/**
@@ -247,9 +207,7 @@ public function getValue()
247
207
*/
248
208
public static function isValidName ($ name )
249
209
{
250
- $ constants = self ::getConstantsByName ();
251
-
252
- return array_key_exists ($ name , $ constants );
210
+ return array_key_exists ($ name , static ::getConstantsByName ());
253
211
}
254
212
255
213
/**
@@ -262,9 +220,7 @@ public static function isValidName($name)
262
220
*/
263
221
public static function isValidValue ($ value )
264
222
{
265
- $ constants = self ::getConstantsByValue ();
266
-
267
- return array_key_exists ($ value , $ constants );
223
+ return array_key_exists ($ value , static ::getConstantsByValue ());
268
224
}
269
225
270
226
/**
@@ -293,6 +249,6 @@ public static function __callStatic($name, $arguments)
293
249
*/
294
250
public function __toString ()
295
251
{
296
- return (string )$ this ->_value ;
252
+ return (string ) $ this ->value ;
297
253
}
298
254
}
0 commit comments