33namespace yii2mod \enum \helpers ;
44
55use ReflectionClass ;
6+ use UnexpectedValueException ;
67use Yii ;
78use yii \helpers \ArrayHelper ;
8- use yii \web \BadRequestHttpException ;
99
1010/**
1111 * Class BaseEnum
1212 *
13- * @author Dmitry Semenov <disemx@gmail.com>
14- *
1513 * @package yii2mod\enum\helpers
1614 */
1715abstract class BaseEnum
1816{
17+ /**
18+ * @var string message category
19+ */
20+ public static $ messageCategory = 'app ' ;
21+
1922 /**
2023 * The cached list of constants by name.
2124 *
@@ -30,58 +33,40 @@ abstract class BaseEnum
3033 */
3134 private static $ byValue = [];
3235
33- /**
34- * The value managed by this type instance.
35- *
36- * @var mixed
37- */
38- private $ value ;
39-
4036 /**
4137 * @var array list of properties
4238 */
4339 private static $ list ;
4440
4541 /**
46- * @var string message category
42+ * The value managed by this type instance.
43+ *
44+ * @var mixed
4745 */
48- public static $ messageCategory = ' app ' ;
46+ private $ _value ;
4947
5048 /**
5149 * Sets the value that will be managed by this type instance.
5250 *
5351 * @param mixed $value The value to be managed
5452 *
55- * @throws BadRequestHttpException If the value is not valid
53+ * @throws UnexpectedValueException If the value is not valid
5654 */
5755 public function __construct ($ value )
5856 {
5957 if (!self ::isValidValue ($ value )) {
60- throw new BadRequestHttpException ( );
58+ throw new UnexpectedValueException ( " Value ' { $ value } ' is not part of the enum " . get_called_class () );
6159 }
6260
63- $ this ->value = $ value ;
64- }
65-
66- /**
67- * Creates a new type instance for a called name.
68- *
69- * @param string $name The name of the value
70- * @param array $arguments An ignored list of arguments
71- *
72- * @return $this The new type instance
73- */
74- public static function __callStatic ($ name , array $ arguments = [])
75- {
76- return self ::createByName ($ name );
61+ $ this ->_value = $ value ;
7762 }
7863
7964 /**
8065 * Creates a new type instance using the name of a value.
8166 *
8267 * @param string $name The name of a value
8368 *
84- * @throws \yii\web\BadRequestHttpException
69+ * @throws UnexpectedValueException
8570 *
8671 * @return $this The new type instance
8772 */
@@ -90,7 +75,7 @@ public static function createByName($name)
9075 $ constants = self ::getConstantsByName ();
9176
9277 if (!array_key_exists ($ name , $ constants )) {
93- throw new BadRequestHttpException ( );
78+ throw new UnexpectedValueException ( " Name ' { $ name } ' is not exists in the enum constants list " . get_called_class () );
9479 }
9580
9681 return new static ($ constants [$ name ]);
@@ -115,7 +100,7 @@ public static function getValueByName($value)
115100 *
116101 * @param mixed $value The value
117102 *
118- * @throws \yii\web\BadRequestHttpException
103+ * @throws UnexpectedValueException
119104 *
120105 * @return $this The new type instance
121106 */
@@ -124,7 +109,7 @@ public static function createByValue($value)
124109 $ constants = self ::getConstantsByValue ();
125110
126111 if (!array_key_exists ($ value , $ constants )) {
127- throw new BadRequestHttpException ( );
112+ throw new UnexpectedValueException ( " Value ' { $ value } ' is not exists in the enum constants list " . get_called_class () );
128113 }
129114
130115 return new static ($ value );
@@ -140,10 +125,12 @@ public static function createByValue($value)
140125 public static function listData ()
141126 {
142127 $ class = get_called_class ();
128+
143129 if (!isset (self ::$ list [$ class ])) {
144130 $ reflection = new ReflectionClass ($ class );
145131 self ::$ list [$ class ] = $ reflection ->getStaticPropertyValue ('list ' );
146132 }
133+
147134 $ result = ArrayHelper::getColumn (self ::$ list [$ class ], function ($ value ) {
148135 return Yii::t (self ::$ messageCategory , $ value );
149136 });
@@ -161,6 +148,7 @@ public static function listData()
161148 public static function getLabel ($ value )
162149 {
163150 $ list = static ::$ list ;
151+
164152 if (isset ($ list [$ value ])) {
165153 return Yii::t (static ::$ messageCategory , $ list [$ value ]);
166154 }
@@ -235,7 +223,7 @@ public function getName()
235223 {
236224 $ constants = self ::getConstantsByValue ();
237225
238- return $ constants [$ this ->value ];
226+ return $ constants [$ this ->_value ];
239227 }
240228
241229 /**
@@ -245,7 +233,7 @@ public function getName()
245233 */
246234 public function getValue ()
247235 {
248- return $ this ->value ;
236+ return $ this ->_value ;
249237 }
250238
251239 /**
0 commit comments