3
3
namespace yii2mod \enum \helpers ;
4
4
5
5
use ReflectionClass ;
6
+ use UnexpectedValueException ;
6
7
use Yii ;
7
8
use yii \helpers \ArrayHelper ;
8
- use yii \web \BadRequestHttpException ;
9
9
10
10
/**
11
11
* Class BaseEnum
12
12
*
13
- * @author Dmitry Semenov <[email protected] >
14
- *
15
13
* @package yii2mod\enum\helpers
16
14
*/
17
15
abstract class BaseEnum
18
16
{
17
+ /**
18
+ * @var string message category
19
+ */
20
+ public static $ messageCategory = 'app ' ;
21
+
19
22
/**
20
23
* The cached list of constants by name.
21
24
*
@@ -30,58 +33,40 @@ abstract class BaseEnum
30
33
*/
31
34
private static $ byValue = [];
32
35
33
- /**
34
- * The value managed by this type instance.
35
- *
36
- * @var mixed
37
- */
38
- private $ value ;
39
-
40
36
/**
41
37
* @var array list of properties
42
38
*/
43
39
private static $ list ;
44
40
45
41
/**
46
- * @var string message category
42
+ * The value managed by this type instance.
43
+ *
44
+ * @var mixed
47
45
*/
48
- public static $ messageCategory = ' app ' ;
46
+ private $ _value ;
49
47
50
48
/**
51
49
* Sets the value that will be managed by this type instance.
52
50
*
53
51
* @param mixed $value The value to be managed
54
52
*
55
- * @throws BadRequestHttpException If the value is not valid
53
+ * @throws UnexpectedValueException If the value is not valid
56
54
*/
57
55
public function __construct ($ value )
58
56
{
59
57
if (!self ::isValidValue ($ value )) {
60
- throw new BadRequestHttpException ( );
58
+ throw new UnexpectedValueException ( " Value ' { $ value } ' is not part of the enum " . get_called_class () );
61
59
}
62
60
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 ;
77
62
}
78
63
79
64
/**
80
65
* Creates a new type instance using the name of a value.
81
66
*
82
67
* @param string $name The name of a value
83
68
*
84
- * @throws \yii\web\BadRequestHttpException
69
+ * @throws UnexpectedValueException
85
70
*
86
71
* @return $this The new type instance
87
72
*/
@@ -90,7 +75,7 @@ public static function createByName($name)
90
75
$ constants = self ::getConstantsByName ();
91
76
92
77
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 () );
94
79
}
95
80
96
81
return new static ($ constants [$ name ]);
@@ -115,7 +100,7 @@ public static function getValueByName($value)
115
100
*
116
101
* @param mixed $value The value
117
102
*
118
- * @throws \yii\web\BadRequestHttpException
103
+ * @throws UnexpectedValueException
119
104
*
120
105
* @return $this The new type instance
121
106
*/
@@ -124,7 +109,7 @@ public static function createByValue($value)
124
109
$ constants = self ::getConstantsByValue ();
125
110
126
111
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 () );
128
113
}
129
114
130
115
return new static ($ value );
@@ -140,10 +125,12 @@ public static function createByValue($value)
140
125
public static function listData ()
141
126
{
142
127
$ class = get_called_class ();
128
+
143
129
if (!isset (self ::$ list [$ class ])) {
144
130
$ reflection = new ReflectionClass ($ class );
145
131
self ::$ list [$ class ] = $ reflection ->getStaticPropertyValue ('list ' );
146
132
}
133
+
147
134
$ result = ArrayHelper::getColumn (self ::$ list [$ class ], function ($ value ) {
148
135
return Yii::t (self ::$ messageCategory , $ value );
149
136
});
@@ -161,6 +148,7 @@ public static function listData()
161
148
public static function getLabel ($ value )
162
149
{
163
150
$ list = static ::$ list ;
151
+
164
152
if (isset ($ list [$ value ])) {
165
153
return Yii::t (static ::$ messageCategory , $ list [$ value ]);
166
154
}
@@ -235,7 +223,7 @@ public function getName()
235
223
{
236
224
$ constants = self ::getConstantsByValue ();
237
225
238
- return $ constants [$ this ->value ];
226
+ return $ constants [$ this ->_value ];
239
227
}
240
228
241
229
/**
@@ -245,7 +233,7 @@ public function getName()
245
233
*/
246
234
public function getValue ()
247
235
{
248
- return $ this ->value ;
236
+ return $ this ->_value ;
249
237
}
250
238
251
239
/**
0 commit comments