Skip to content

Commit

Permalink
Add test case.
Browse files Browse the repository at this point in the history
  • Loading branch information
yinfuyuan committed May 21, 2020
1 parent 88c0832 commit f321214
Show file tree
Hide file tree
Showing 15 changed files with 466 additions and 262 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
vendor/*
composer.lock
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ Array enum provides the definition and use of key-value enums in the form of arr
If you use it as a unified error code return, it should look something like this

/**
* @method static ErrorCodeEnum OK
* @method static self OK
*
* @method static CodeEnum UNKNOWN_ERROR
* @method static self UNKNOWN_ERROR
*
* @method static ErrorCodeEnum ERROR_DATA_VALIDATION
* @method static ErrorCodeEnum ERROR_USER_INVALID
* @method static ErrorCodeEnum ERROR_CONFIG_ERROR
* @method static self ERROR_DATA_VALIDATION
* @method static self ERROR_USER_INVALID
* @method static self ERROR_CONFIG_ERROR
*/
class ErrorCodeEnum extends \PhpEnum\ArrayEnum
{
Expand Down Expand Up @@ -88,18 +88,18 @@ You can do this when you encounter an exception that returns the expected result
If you use it as enumeration, it should look something like this

/**
* @method static ArticleEnum TYPE_ARTICLE
* @method static ArticleEnum TYPE_STORY
* @method static ArticleEnum TYPE_NEWS
* @method static self TYPE_ARTICLE
* @method static self TYPE_STORY
* @method static self TYPE_NEWS
*
* @method static ArticleEnum SHARE_PRIVATE
* @method static ArticleEnum SHARE_PROTECTED
* @method static ArticleEnum SHARE_PUBLIC
* @method static self SHARE_PRIVATE
* @method static self SHARE_PROTECTED
* @method static self SHARE_PUBLIC
*
* @method static ArticleEnum STATUS_NORMAL
* @method static ArticleEnum STATUE_AUDIT
* @method static ArticleEnum STATUE_INACTIVE
* @method static ArticleEnum STATUS_INVALID
* @method static self STATUS_NORMAL
* @method static self STATUS_AUDIT
* @method static self STATUS_INACTIVE
* @method static self STATUS_INVALID
*/
class ArticleEnum extends \PhpEnum\ArrayEnum
{
Expand Down
28 changes: 28 additions & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
阅读此文档时,假设您已经了解了枚举的基础知识。如果没有,请先查阅相关文档。

枚举在每个项目都可能会被用到,只是定义和使用的方式大有不同,在php中大多数枚举是以常量或静态变量的形式存在的

PHP通过SPL类库为我们提供了枚举类SplEnum,但遗憾的是,首先它提供的方法需要再次进行封装才能满足一些需求,其次它需要以扩展的方式安装才能进行使用。

由此,许多优秀的开发人员提供了许多功能强大的枚举类库,而此枚举类库提供了更严谨的使用方式。

规范:

1. 在定义枚举时应该使用const关键字进行声明,而枚举名称需要全部大写
2. 在定义枚举值时,所有枚举值的类型和长度应该一致
3. 一组枚举命名应该使用统一前缀
4. 定义枚举时同时也应该定义枚举对应的注释

例:

/**
* @method static StatusEnum STATUS_NORMAL
* @method static StatusEnum STATUS_INVALID
*/
class StatusEnum extends \PhpEnum\ArrayEnum
{
const STATUS_NORMAL = ['1', 'normal'];
const STATUS_INVALID = ['9', 'invalid'];
}

当枚举属性值为null或者未定义枚举属性时 都会返回null,但是如果常量未定义,会产生一个 E_WARNING 级别的错误。在php7以上版本会抛出一个ErrorException异常
18 changes: 18 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
phpunit -c phpunit.xml
-->
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="PHP Enum Test Suite">
<directory suffix=".php">./tests</directory>
</testsuite>
</testsuites>
</phpunit>
159 changes: 18 additions & 141 deletions src/ArrayEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
*
* @author yinfuyuan <[email protected]>
* @link https://github.com/yinfuyuan/php-enum
* @license https://opensource.org/licenses/GPL-3.0
*/
abstract class ArrayEnum extends Enum
abstract class ArrayEnum extends ListEnum
{

/**
Expand All @@ -24,86 +23,53 @@ abstract class ArrayEnum extends Enum
*
* @var mixed $key
*/
private $key;
protected $enum_key;

/**
* The enum value.
*
* @var mixed $value
*/
private $value;

/**
* Create a new array enum instance.
*
* @param array $attributes
* @return void
*
* @throws \InvalidArgumentException
* @throws \LengthException
*/
public function __construct($attributes)
{
parent::__construct($attributes);
$this->key = reset($attributes);
$this->value = end($attributes);
}
protected $enum_value;

/**
* Get the enum key.
*
* @return mixed
*/
public function getKey()
public function getEnumKey()
{
return $this->key;
return parent::getKey();
}

/**
* Get the enum value.
*
* @return mixed
*/
public function getValue()
{
return $this->value;
}

/**
* Compare the keys to be equal.
*
* @param mixed $key
* @return bool
*/
public function keyEquals($key)
public function getEnumValue()
{
return $this->getKey() == $key;
return parent::getValue();
}

/**
* Compare the values to be equal.
* Get the enum key.
*
* @param mixed $value
* @return bool
* @return mixed
*/
public function valueEquals($value)
public function getKey()
{
return $this->getValue() == $value;
return $this->enum_key;
}

/**
* Get the array enum all keys.
* Get the enum value.
*
* @param string $prefix
* @return array
* @return mixed
*/
public static function getKeys($prefix = '')
public function getValue()
{

$values = self::getValues($prefix);

return array_keys($values);

return $this->enum_value;
}

/**
Expand Down Expand Up @@ -132,102 +98,13 @@ public static function getValues($prefix = '')
}

/**
* Determine if the key exists.
*
* @param mixed $key
* @param string $prefix
* @return bool
*/
public static function keyExist($key, $prefix = '')
{

$values = self::getValues($prefix);

if(empty($values[$key])) {
return false;
}

return true;

}

/**
* Determine if the value exists.
*
* @param mixed $value
* @param string $prefix
* @return bool
*/
public static function valueExist($value, $prefix = '')
{

$values = self::getValues($prefix);

$key = array_search($value, $values);

if(false === $key) {
return false;
}

return true;

}

/**
* Search the key based on the value.
*
* @param mixed $value
* @param string $prefix
* @return mixed|null
*/
public static function searchKey($value, $prefix = '')
{

$values = self::getValues($prefix);

$key = array_search($value, $values);

if(false === $key) {
return null;
}

return $key;

}

/**
* Search the value based on the key.
*
* @param mixed $key
* @param string $prefix
* @return mixed|null
*/
public static function searchValue($key, $prefix = '')
{

$values = self::getValues($prefix);

if(empty($values[$key])) {
return null;
}

return $values[$key];

}

/**
* Get enum size.
* Get enum attribute length.
*
* @param string $prefix
* @return int
*/
public static function getSize($prefix = '')
public static function getLength()
{

$values = self::getValues($prefix);

return count($values);

return self::$ENUM_LENGTH;
}

}
Loading

0 comments on commit f321214

Please sign in to comment.