11<?php
2- namespace yii2mod \enum \helpers ;
32
3+ namespace yii2mod \enum \helpers ;
44
55use ReflectionClass ;
6+ use Yii ;
7+ use yii \helpers \ArrayHelper ;
68use yii \web \BadRequestHttpException ;
79
810/**
@@ -18,14 +20,14 @@ abstract class BaseEnum
1820 *
1921 * @var array
2022 */
21- private static $ byName = array () ;
23+ private static $ byName = [] ;
2224
2325 /**
2426 * The cached list of constants by value.
2527 *
2628 * @var array
2729 */
28- private static $ byValue = array () ;
30+ private static $ byValue = [] ;
2931
3032 /**
3133 * The value managed by this type instance.
@@ -58,12 +60,12 @@ public function __construct($value)
5860 /**
5961 * Creates a new type instance for a called name.
6062 *
61- * @param string $name The name of the value.
62- * @param array $arguments An ignored list of arguments.
63+ * @param string $name The name of the value.
64+ * @param array $arguments An ignored list of arguments.
6365 *
6466 * @return $this The new type instance.
6567 */
66- public static function __callStatic ($ name , array $ arguments = array () )
68+ public static function __callStatic ($ name , array $ arguments = [] )
6769 {
6870 return self ::createByName ($ name );
6971 }
@@ -93,10 +95,12 @@ public static function createByName($name)
9395 * @param $value
9496 * @return mixed
9597 */
96- public static function getValueByName ($ value ){
98+ public static function getValueByName ($ value )
99+ {
97100 $ list = self ::listData ();
98101 return array_search ($ value , $ list );
99102 }
103+
100104 /**
101105 * Creates a new type instance using the value.
102106 *
@@ -118,6 +122,7 @@ public static function createByValue($value)
118122 }
119123
120124 /**
125+ * Get list data
121126 * @static
122127 * @return mixed
123128 */
@@ -128,19 +133,22 @@ public static function listData()
128133 $ reflection = new ReflectionClass ($ class );
129134 self ::$ list [$ class ] = $ reflection ->getStaticPropertyValue ('list ' );
130135 }
131- return self ::$ list [$ class ];
136+ $ result = ArrayHelper::getColumn (self ::$ list [$ class ], function ($ value ) {
137+ return Yii::t ('app ' , $ value );
138+ });
139+ return $ result ;
132140 }
133141
134142 /**
143+ * Get label by value
135144 * @var string value
136145 * @return string label
137- * @author Gladchenko Oleg
138146 */
139147 public static function getLabel ($ value )
140148 {
141149 $ list = self ::listData ();
142150 if (isset ($ list [$ value ])) {
143- return \ Yii::t ('enum ' , $ list [$ value ]);
151+ return Yii::t ('enum ' , $ list [$ value ]);
144152 }
145153 return null ;
146154 }
@@ -184,14 +192,14 @@ public static function getConstantsByValue()
184192 if (!isset (self ::$ byValue [$ class ])) {
185193 self ::getConstantsByName ();
186194
187- self ::$ byValue [$ class ] = array () ;
195+ self ::$ byValue [$ class ] = [] ;
188196
189197 foreach (self ::$ byName [$ class ] as $ name => $ value ) {
190198 if (array_key_exists ($ value , self ::$ byValue [$ class ])) {
191199 if (!is_array (self ::$ byValue [$ class ][$ value ])) {
192- self ::$ byValue [$ class ][$ value ] = array (
200+ self ::$ byValue [$ class ][$ value ] = [
193201 self ::$ byValue [$ class ][$ value ]
194- ) ;
202+ ] ;
195203 }
196204 self ::$ byValue [$ class ][$ value ][] = $ name ;;
197205 } else {
0 commit comments