Skip to content

Commit a173bdb

Browse files
author
igor-chepurnoi
committed
fix code style, update tests
1 parent b224a3e commit a173bdb

File tree

2 files changed

+54
-34
lines changed

2 files changed

+54
-34
lines changed

helpers/BaseEnum.php

+22-34
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,22 @@
33
namespace yii2mod\enum\helpers;
44

55
use ReflectionClass;
6+
use UnexpectedValueException;
67
use Yii;
78
use yii\helpers\ArrayHelper;
8-
use yii\web\BadRequestHttpException;
99

1010
/**
1111
* Class BaseEnum
1212
*
13-
* @author Dmitry Semenov <[email protected]>
14-
*
1513
* @package yii2mod\enum\helpers
1614
*/
1715
abstract 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
/**

tests/EnumTest.php

+32
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,36 @@ public function testValidation()
2727
$this->assertTrue(BooleanEnum::isValidValue(1));
2828
$this->assertFalse(BooleanEnum::isValidValue('YES'));
2929
}
30+
31+
public function testCreateByName()
32+
{
33+
$enum = BooleanEnum::createByName('YES');
34+
35+
$this->assertEquals(BooleanEnum::YES, $enum->getValue());
36+
$this->assertTrue(array_key_exists($enum->getName(), BooleanEnum::getConstantsByName()));
37+
}
38+
39+
/**
40+
* @expectedException \UnexpectedValueException
41+
*/
42+
public function testFailedCreateByName()
43+
{
44+
BooleanEnum::createByName('not existing name');
45+
}
46+
47+
public function testCreateByValue()
48+
{
49+
$enum = BooleanEnum::createByValue(BooleanEnum::YES);
50+
51+
$this->assertEquals(BooleanEnum::YES, $enum->getValue());
52+
$this->assertTrue(array_key_exists($enum->getName(), BooleanEnum::getConstantsByName()));
53+
}
54+
55+
/**
56+
* @expectedException \UnexpectedValueException
57+
*/
58+
public function testFailedCreateByValue()
59+
{
60+
BooleanEnum::createByValue('not existing value');
61+
}
3062
}

0 commit comments

Comments
 (0)