-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
466 additions
and
262 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.DS_Store | ||
vendor/* | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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异常 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
{ | ||
|
||
/** | ||
|
@@ -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; | ||
} | ||
|
||
/** | ||
|
@@ -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; | ||
} | ||
|
||
} |
Oops, something went wrong.