Skip to content

Commit

Permalink
fix code style, update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
igor-chepurnoi committed Nov 22, 2016
1 parent b224a3e commit a173bdb
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 34 deletions.
56 changes: 22 additions & 34 deletions helpers/BaseEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@
namespace yii2mod\enum\helpers;

use ReflectionClass;
use UnexpectedValueException;
use Yii;
use yii\helpers\ArrayHelper;
use yii\web\BadRequestHttpException;

/**
* Class BaseEnum
*
* @author Dmitry Semenov <disemx@gmail.com>
*
* @package yii2mod\enum\helpers
*/
abstract class BaseEnum
{
/**
* @var string message category
*/
public static $messageCategory = 'app';

/**
* The cached list of constants by name.
*
Expand All @@ -30,58 +33,40 @@ abstract class BaseEnum
*/
private static $byValue = [];

/**
* The value managed by this type instance.
*
* @var mixed
*/
private $value;

/**
* @var array list of properties
*/
private static $list;

/**
* @var string message category
* The value managed by this type instance.
*
* @var mixed
*/
public static $messageCategory = 'app';
private $_value;

/**
* Sets the value that will be managed by this type instance.
*
* @param mixed $value The value to be managed
*
* @throws BadRequestHttpException If the value is not valid
* @throws UnexpectedValueException If the value is not valid
*/
public function __construct($value)
{
if (!self::isValidValue($value)) {
throw new BadRequestHttpException();
throw new UnexpectedValueException("Value '{$value}' is not part of the enum " . get_called_class());
}

$this->value = $value;
}

/**
* Creates a new type instance for a called name.
*
* @param string $name The name of the value
* @param array $arguments An ignored list of arguments
*
* @return $this The new type instance
*/
public static function __callStatic($name, array $arguments = [])
{
return self::createByName($name);
$this->_value = $value;
}

/**
* Creates a new type instance using the name of a value.
*
* @param string $name The name of a value
*
* @throws \yii\web\BadRequestHttpException
* @throws UnexpectedValueException
*
* @return $this The new type instance
*/
Expand All @@ -90,7 +75,7 @@ public static function createByName($name)
$constants = self::getConstantsByName();

if (!array_key_exists($name, $constants)) {
throw new BadRequestHttpException();
throw new UnexpectedValueException("Name '{$name}' is not exists in the enum constants list " . get_called_class());
}

return new static($constants[$name]);
Expand All @@ -115,7 +100,7 @@ public static function getValueByName($value)
*
* @param mixed $value The value
*
* @throws \yii\web\BadRequestHttpException
* @throws UnexpectedValueException
*
* @return $this The new type instance
*/
Expand All @@ -124,7 +109,7 @@ public static function createByValue($value)
$constants = self::getConstantsByValue();

if (!array_key_exists($value, $constants)) {
throw new BadRequestHttpException();
throw new UnexpectedValueException("Value '{$value}' is not exists in the enum constants list " . get_called_class());
}

return new static($value);
Expand All @@ -140,10 +125,12 @@ public static function createByValue($value)
public static function listData()
{
$class = get_called_class();

if (!isset(self::$list[$class])) {
$reflection = new ReflectionClass($class);
self::$list[$class] = $reflection->getStaticPropertyValue('list');
}

$result = ArrayHelper::getColumn(self::$list[$class], function ($value) {
return Yii::t(self::$messageCategory, $value);
});
Expand All @@ -161,6 +148,7 @@ public static function listData()
public static function getLabel($value)
{
$list = static::$list;

if (isset($list[$value])) {
return Yii::t(static::$messageCategory, $list[$value]);
}
Expand Down Expand Up @@ -235,7 +223,7 @@ public function getName()
{
$constants = self::getConstantsByValue();

return $constants[$this->value];
return $constants[$this->_value];
}

/**
Expand All @@ -245,7 +233,7 @@ public function getName()
*/
public function getValue()
{
return $this->value;
return $this->_value;
}

/**
Expand Down
32 changes: 32 additions & 0 deletions tests/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,36 @@ public function testValidation()
$this->assertTrue(BooleanEnum::isValidValue(1));
$this->assertFalse(BooleanEnum::isValidValue('YES'));
}

public function testCreateByName()
{
$enum = BooleanEnum::createByName('YES');

$this->assertEquals(BooleanEnum::YES, $enum->getValue());
$this->assertTrue(array_key_exists($enum->getName(), BooleanEnum::getConstantsByName()));
}

/**
* @expectedException \UnexpectedValueException
*/
public function testFailedCreateByName()
{
BooleanEnum::createByName('not existing name');
}

public function testCreateByValue()
{
$enum = BooleanEnum::createByValue(BooleanEnum::YES);

$this->assertEquals(BooleanEnum::YES, $enum->getValue());
$this->assertTrue(array_key_exists($enum->getName(), BooleanEnum::getConstantsByName()));
}

/**
* @expectedException \UnexpectedValueException
*/
public function testFailedCreateByValue()
{
BooleanEnum::createByValue('not existing value');
}
}

0 comments on commit a173bdb

Please sign in to comment.