承接 spareparts/enum 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

spareparts/enum

Composer 安装命令:

composer require spareparts/enum

包简介

Simple ENUM class.

关键字:

README 文档

README

PHP Enum done right

Build Status Scrutinizer Code Quality Code Coverage

Easy way to use enumerated values in PHP.

Installation

Please, use composer.

composer require spareparts/enum

Basic usage

/**
 * @method static OPEN
 * @method static CLOSED
 */
class WindowStateEnum extends \SpareParts\Enum\Enum
{
}

// obtain enum value
$state = WindowStateEnum::OPEN();

// assign enum value
$windows->state = WindowStateEnum::CLOSED();

// compare enum values
if ($window->state === WindowStateEnum::OPEN()) {
    ....
}

// use enum to guard method parameters
function changeWindowState(WindowStateEnum $newState) {
    ...
}

How to prepare Enum

  1. extend Enum class
  2. Annotate enum class with @method annotations describing Enum values

How to use Enum

There are two possible ways to use enum values, with first one being preferred.

  1. using static methods with same name as your desired value.

This works with help from magic __callStatic method, meaning you do not have to add any methods manually.

$state = WindowStateEnum::OPEN();

This method is preferred, as it nicely shows its intended value without having to use weakly guarded strings.

Important tip: To have values correctly autocompleted, use @method annotations, like this:

/**
 * @method static OPEN
 * @method static CLOSED
 */
class WindowStateEnum extends \SpareParts\Enum\Enum 
{
}

This way, your IDE should know WindowStateEnum has 2 methods OPEN and CLOSE and correctly hint on their names. In case you are using IDE without support for @method annotations, you can always just add those methods "for real" :)

  1. using instance() method with desired value as instance parameter
$state = WindowStateEnum::instance('OPEN');

There is nothing wrong with this approach, but mistakes/typos can be easily made.

Asking enum for unsupported value

In case you ask for value that is not in the enum values, an InvalidEnumValueException exception is thrown.

try {
    $window->setState(WindowStateEnum::FLYING());
} catch (InvalidEnumValueException $e) {
    echo "This is not a correct state for window to be in!";
}

Testing for multiple values

You can check for enum belonging to any of an enum group / set like this:

if ($state->isAnyOf([Window::OPEN(), Window::CLOSED()]));

统计信息

  • 总下载量: 29.5k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 1
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 0
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-01-09

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固