99
1010/**
1111 * Class BaseEnum
12+ *
1213 * @author Dmitry Semenov <disemx@gmail.com>
14+ *
1315 * @package yii2mod\enum\helpers
1416 */
1517abstract class BaseEnum
@@ -48,14 +50,14 @@ abstract class BaseEnum
4850 /**
4951 * Sets the value that will be managed by this type instance.
5052 *
51- * @param mixed $value The value to be managed.
53+ * @param mixed $value The value to be managed
5254 *
53- * @throws BadRequestHttpException If the value is not valid.
55+ * @throws BadRequestHttpException If the value is not valid
5456 */
5557 public function __construct ($ value )
5658 {
5759 if (!self ::isValidValue ($ value )) {
58- throw new BadRequestHttpException ;
60+ throw new BadRequestHttpException () ;
5961 }
6062
6163 $ this ->value = $ value ;
@@ -64,10 +66,10 @@ public function __construct($value)
6466 /**
6567 * Creates a new type instance for a called name.
6668 *
67- * @param string $name The name of the value.
68- * @param array $arguments An ignored list of arguments.
69+ * @param string $name The name of the value
70+ * @param array $arguments An ignored list of arguments
6971 *
70- * @return $this The new type instance.
72+ * @return $this The new type instance
7173 */
7274 public static function __callStatic ($ name , array $ arguments = [])
7375 {
@@ -77,57 +79,62 @@ public static function __callStatic($name, array $arguments = [])
7779 /**
7880 * Creates a new type instance using the name of a value.
7981 *
80- * @param string $name The name of a value.
82+ * @param string $name The name of a value
8183 *
8284 * @throws \yii\web\BadRequestHttpException
83- * @return $this The new type instance.
8485 *
86+ * @return $this The new type instance
8587 */
8688 public static function createByName ($ name )
8789 {
8890 $ constants = self ::getConstantsByName ();
8991
9092 if (!array_key_exists ($ name , $ constants )) {
91- throw new BadRequestHttpException ;
93+ throw new BadRequestHttpException () ;
9294 }
9395
9496 return new static ($ constants [$ name ]);
9597 }
9698
9799 /**
98100 * get constant key by value(label)
101+ *
99102 * @param $value
103+ *
100104 * @return mixed
101105 */
102106 public static function getValueByName ($ value )
103107 {
104108 $ list = self ::listData ();
109+
105110 return array_search ($ value , $ list );
106111 }
107112
108113 /**
109114 * Creates a new type instance using the value.
110115 *
111- * @param mixed $value The value.
116+ * @param mixed $value The value
112117 *
113118 * @throws \yii\web\BadRequestHttpException
114- * @return $this The new type instance.
115119 *
120+ * @return $this The new type instance
116121 */
117122 public static function createByValue ($ value )
118123 {
119124 $ constants = self ::getConstantsByValue ();
120125
121126 if (!array_key_exists ($ value , $ constants )) {
122- throw new BadRequestHttpException ;
127+ throw new BadRequestHttpException () ;
123128 }
124129
125130 return new static ($ value );
126131 }
127132
128133 /**
129134 * Get list data
135+ *
130136 * @static
137+ *
131138 * @return mixed
132139 */
133140 public static function listData ()
@@ -140,12 +147,15 @@ public static function listData()
140147 $ result = ArrayHelper::getColumn (self ::$ list [$ class ], function ($ value ) {
141148 return Yii::t (self ::$ messageCategory , $ value );
142149 });
150+
143151 return $ result ;
144152 }
145153
146- /**
154+ /**
147155 * Get label by value
156+ *
148157 * @var string value
158+ *
149159 * @return string label
150160 */
151161 public static function getLabel ($ value )
@@ -154,13 +164,14 @@ public static function getLabel($value)
154164 if (isset ($ list [$ value ])) {
155165 return Yii::t (static ::$ messageCategory , $ list [$ value ]);
156166 }
167+
157168 return null ;
158169 }
159170
160171 /**
161172 * Returns the list of constants (by name) for this type.
162173 *
163- * @return array The list of constants by name.
174+ * @return array The list of constants by name
164175 */
165176 public static function getConstantsByName ()
166177 {
@@ -187,7 +198,7 @@ public static function getConstantsByName()
187198 /**
188199 * Returns the list of constants (by value) for this type.
189200 *
190- * @return array The list of constants by value.
201+ * @return array The list of constants by value
191202 */
192203 public static function getConstantsByValue ()
193204 {
@@ -202,10 +213,10 @@ public static function getConstantsByValue()
202213 if (array_key_exists ($ value , self ::$ byValue [$ class ])) {
203214 if (!is_array (self ::$ byValue [$ class ][$ value ])) {
204215 self ::$ byValue [$ class ][$ value ] = [
205- self ::$ byValue [$ class ][$ value ]
216+ self ::$ byValue [$ class ][$ value ],
206217 ];
207218 }
208- self ::$ byValue [$ class ][$ value ][] = $ name ;;
219+ self ::$ byValue [$ class ][$ value ][] = $ name ;
209220 } else {
210221 self ::$ byValue [$ class ][$ value ] = $ name ;
211222 }
@@ -218,7 +229,7 @@ public static function getConstantsByValue()
218229 /**
219230 * Returns the name of the value.
220231 *
221- * @return array|string The name, or names, of the value.
232+ * @return array|string The name, or names, of the value
222233 */
223234 public function getName ()
224235 {
@@ -230,7 +241,7 @@ public function getName()
230241 /**
231242 * Unwraps the type and returns the raw value.
232243 *
233- * @return mixed The raw value managed by the type instance.
244+ * @return mixed The raw value managed by the type instance
234245 */
235246 public function getValue ()
236247 {
@@ -240,10 +251,10 @@ public function getValue()
240251 /**
241252 * Checks if a name is valid for this type.
242253 *
243- * @param string $name The name of the value.
254+ * @param string $name The name of the value
244255 *
245- * @return boolean If the name is valid for this type, `true` is returned.
246- * Otherwise, the name is not valid and `false` is returned.
256+ * @return bool If the name is valid for this type, `true` is returned.
257+ * Otherwise, the name is not valid and `false` is returned
247258 */
248259 public static function isValidName ($ name )
249260 {
@@ -255,10 +266,10 @@ public static function isValidName($name)
255266 /**
256267 * Checks if a value is valid for this type.
257268 *
258- * @param string $value The value.
269+ * @param string $value The value
259270 *
260- * @return boolean If the value is valid for this type, `true` is returned.
261- * Otherwise, the value is not valid and `false` is returned.
271+ * @return bool If the value is valid for this type, `true` is returned.
272+ * Otherwise, the value is not valid and `false` is returned
262273 */
263274 public static function isValidValue ($ value )
264275 {
0 commit comments