Skip to content

Commit f8e5ca3

Browse files
init
0 parents  commit f8e5ca3

File tree

4 files changed

+324
-0
lines changed

4 files changed

+324
-0
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
Enumerable helper
2+
========================================
3+
### Example enumerable class
4+
```php
5+
use yii2mod\enum\helpers\BaseEnum;
6+
class YesNoEnumerable extends BaseEnum
7+
{
8+
const YES = 1;
9+
const NO = 2;
10+
11+
public static $list = [
12+
self::YES => 'Yes',
13+
self::NO => 'No'
14+
];
15+
}
16+
```
17+
### Example usage
18+
```php
19+
var_dump(YesNoEnumerable::YES);
20+
var_dump(YesNoEnumerable::NO);
21+
var_dump(YesNoEnumerable::getConstantsByValue());
22+
var_dump(YesNoEnumerable::getConstantsByName());
23+
var_dump(YesNoEnumerable::isValidName(1)); // false
24+
var_dump(YesNoEnumerable::isValidName('YES'));
25+
var_dump(YesNoEnumerable::isValidValue(1));
26+
var_dump(YesNoEnumerable::isValidValue('YES')); //false
27+
var_dump(YesNoEnumerable::listData());
28+
var_dump(YesNoEnumerable::getLabel(1));
29+
var_dump(YesNoEnumerable::getLabel('YES')); // false
30+
```

composer.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "yii2mod/enum",
3+
"description": "Yii2 Enumerable helpers",
4+
"type": "yii2-extension",
5+
"keywords": ["yii2", "extension"],
6+
"license": "Apache-2.0",
7+
"authors": [
8+
{
9+
"name": "Dmitry Semenov",
10+
"email": "disemx@gmail.com"
11+
}
12+
],
13+
"autoload": {
14+
"psr-4": {
15+
"yii2mod\\enum\\": ""
16+
}
17+
}
18+
}

helpers/BaseEnum.php

Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
<?php
2+
namespace yii2mod\enum\helpers;
3+
4+
5+
use ReflectionClass;
6+
use yii\web\BadRequestHttpException;
7+
8+
/**
9+
* Class BaseEnum
10+
* @author Dmitry Semenov <disemx@gmail.com>
11+
* @see https://github.com/php-mountain/Enum/
12+
* @package yii2mod\enum\helpers
13+
*/
14+
abstract class BaseEnum
15+
{
16+
/**
17+
* The cached list of constants by name.
18+
*
19+
* @var array
20+
*/
21+
private static $byName = array();
22+
23+
/**
24+
* The cached list of constants by value.
25+
*
26+
* @var array
27+
*/
28+
private static $byValue = array();
29+
30+
/**
31+
* The value managed by this type instance.
32+
*
33+
* @var mixed
34+
*/
35+
private $value;
36+
37+
/**
38+
* @var array list of properties
39+
*/
40+
private static $list;
41+
42+
/**
43+
* Sets the value that will be managed by this type instance.
44+
*
45+
* @param mixed $value The value to be managed.
46+
*
47+
* @throws BadRequestHttpException If the value is not valid.
48+
*/
49+
public function __construct($value)
50+
{
51+
if (!self::isValidValue($value)) {
52+
throw new BadRequestHttpException;
53+
}
54+
55+
$this->value = $value;
56+
}
57+
58+
/**
59+
* Creates a new type instance for a called name.
60+
*
61+
* @param string $name The name of the value.
62+
* @param array $arguments An ignored list of arguments.
63+
*
64+
* @return $this The new type instance.
65+
*/
66+
public static function __callStatic($name, array $arguments = array())
67+
{
68+
return self::createByName($name);
69+
}
70+
71+
/**
72+
* Creates a new type instance using the name of a value.
73+
*
74+
* @param string $name The name of a value.
75+
*
76+
* @throws \yii\web\BadRequestHttpException
77+
* @return $this The new type instance.
78+
*
79+
*/
80+
public static function createByName($name)
81+
{
82+
$constants = self::getConstantsByName();
83+
84+
if (!array_key_exists($name, $constants)) {
85+
throw new BadRequestHttpException;
86+
}
87+
88+
return new static($constants[$name]);
89+
}
90+
91+
/**
92+
* get constant key by value(label)
93+
* @param $value
94+
* @return mixed
95+
*/
96+
public static function getValueByName($value){
97+
$list = self::listData();
98+
return array_search($value, $list);
99+
}
100+
/**
101+
* Creates a new type instance using the value.
102+
*
103+
* @param mixed $value The value.
104+
*
105+
* @throws \yii\web\BadRequestHttpException
106+
* @return $this The new type instance.
107+
*
108+
*/
109+
public static function createByValue($value)
110+
{
111+
$constants = self::getConstantsByValue();
112+
113+
if (!array_key_exists($value, $constants)) {
114+
throw new BadRequestHttpException;
115+
}
116+
117+
return new static($value);
118+
}
119+
120+
/**
121+
* @static
122+
* @return mixed
123+
*/
124+
public static function listData()
125+
{
126+
$class = get_called_class();
127+
if (!isset(self::$list[$class])) {
128+
$reflection = new ReflectionClass($class);
129+
self::$list[$class] = $reflection->getStaticPropertyValue('list');
130+
}
131+
return self::$list[$class];
132+
}
133+
134+
/**
135+
* @var string value
136+
* @return string label
137+
* @author Gladchenko Oleg
138+
*/
139+
public static function getLabel($value)
140+
{
141+
$list = self::listData();
142+
if (isset($list[$value])) {
143+
return \Yii::t('enum', $list[$value]);
144+
}
145+
return false;
146+
}
147+
148+
/**
149+
* Returns the list of constants (by name) for this type.
150+
*
151+
* @return array The list of constants by name.
152+
*/
153+
public static function getConstantsByName()
154+
{
155+
$class = get_called_class();
156+
157+
if (!isset(self::$byName[$class])) {
158+
$reflection = new ReflectionClass($class);
159+
self::$byName[$class] = $reflection->getConstants();
160+
while (false !== ($reflection = $reflection->getParentClass())) {
161+
if (__CLASS__ === $reflection->getName()) {
162+
break;
163+
}
164+
165+
self::$byName[$class] = array_replace(
166+
$reflection->getConstants(),
167+
self::$byName[$class]
168+
);
169+
}
170+
}
171+
172+
return self::$byName[$class];
173+
}
174+
175+
/**
176+
* Returns the list of constants (by value) for this type.
177+
*
178+
* @return array The list of constants by value.
179+
*/
180+
public static function getConstantsByValue()
181+
{
182+
$class = get_called_class();
183+
184+
if (!isset(self::$byValue[$class])) {
185+
self::getConstantsByName();
186+
187+
self::$byValue[$class] = array();
188+
189+
foreach (self::$byName[$class] as $name => $value) {
190+
if (array_key_exists($value, self::$byValue[$class])) {
191+
if (!is_array(self::$byValue[$class][$value])) {
192+
self::$byValue[$class][$value] = array(
193+
self::$byValue[$class][$value]
194+
);
195+
}
196+
self::$byValue[$class][$value][] = $name;;
197+
} else {
198+
self::$byValue[$class][$value] = $name;
199+
}
200+
}
201+
}
202+
203+
return self::$byValue[$class];
204+
}
205+
206+
/**
207+
* Returns the name of the value.
208+
*
209+
* @return array|string The name, or names, of the value.
210+
*/
211+
public function getName()
212+
{
213+
$constants = self::getConstantsByValue();
214+
215+
return $constants[$this->value];
216+
}
217+
218+
/**
219+
* Unwraps the type and returns the raw value.
220+
*
221+
* @return mixed The raw value managed by the type instance.
222+
*/
223+
public function getValue()
224+
{
225+
return $this->value;
226+
}
227+
228+
/**
229+
* Checks if a name is valid for this type.
230+
*
231+
* @param string $name The name of the value.
232+
*
233+
* @return boolean If the name is valid for this type, `true` is returned.
234+
* Otherwise, the name is not valid and `false` is returned.
235+
*/
236+
public static function isValidName($name)
237+
{
238+
$constants = self::getConstantsByName();
239+
240+
return array_key_exists($name, $constants);
241+
}
242+
243+
/**
244+
* Checks if a value is valid for this type.
245+
*
246+
* @param string $value The value.
247+
*
248+
* @return boolean If the value is valid for this type, `true` is returned.
249+
* Otherwise, the value is not valid and `false` is returned.
250+
*/
251+
public static function isValidValue($value)
252+
{
253+
$constants = self::getConstantsByValue();
254+
255+
return array_key_exists($value, $constants);
256+
}
257+
}

helpers/BooleanEnum.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
namespace yii2mod\enum\helpers;
3+
4+
5+
6+
/**
7+
* @author Kravchuk Dmitry
8+
* @package yii2mod\cms\models\enumerables
9+
*/
10+
class BooleanEnum extends BaseEnum
11+
{
12+
const YES = 1;
13+
const NO = 0;
14+
15+
public static $list = [
16+
self::NO => 'No',
17+
self::YES => 'Yes'
18+
];
19+
}

0 commit comments

Comments
 (0)