xaamin/enum 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

xaamin/enum

最新稳定版本:v1.0.4

Composer 安装命令:

composer require xaamin/enum

包简介

PHP Enum implementation

README 文档

README

This package offers enums in PHP. We don't use a simple "value" representation, so you're always working with the enum object. This allows for proper autocompletion and refactoring in IDEs.

Why?

Using an enum instead of class constants provides the following advantages:

  • You can enrich the enum with alias methods names (e.g. draft(), published(), …)
  • You can extend the enum to add new values (make your enum final to prevent it)
  • You can get a list of all the possible values (see below)

This Enum class is not intended to replace class constants, but only to be used when it makes sense.

Installation

composer require xaamin/enum

Declaration

use Xaamin\Enum\Enum;

class InvoiceStatus extends Enum
{
    protected $enum = [
        'pending' => 'invoice.pending',
        'paid' => 'invoice.paid',
        'overdue' => 'invoice.overdue',
    ];
}

Documentation

  • getName() Returns the name of the current value on Enum
  • getValue() Returns the current value of the enum
  • equals($enum) Tests whether 2 enum instances are equal (returns true if enum values are equal, false otherwise)

Static methods:

  • toArray() method Returns all possible values as an array (constant name in key, constant value in value)
  • keys() Returns the names (keys) of all constants in the Enum class
  • values() Returns instances of the Enum class of all Enum constants (constant name in key, Enum instance in value)
  • search() Return key for the searched value

Static methods

class InvoiceStatus extends Enum
{
    protected $enum = [
        'pending' => 'invoice.pending',
        'paid' => 'invoice.paid',
        'overdue' => 'invoice.overdue',
    ];
}

// Static method:
$invoice = InvoiceStatus::pending();
$invoice = InvoiceStatus::paid();

You can use phpdoc for autocompletion, this is supported in some IDEs:

/**
 * @method static self pending()
 * @method static self paid()
 * @method static self overdue()
 */
class InvoiceStatus extends Enum
{
    protected $enum = [
        'pending' => 'invoice.pending',
        'paid' => 'invoice.paid',
        'overdue' => 'invoice.overdue',
    ];
}

Usage

This is how an enum can be defined.

/**
 * @method static self pending()
 * @method static self paid()
 * @method static self overdue()
 */
class InvoiceStatus extends Enum
{
    protected $enum = [
        'pending' => 'invoice.pending',
        'paid' => 'invoice.paid',
        'overdue' => 'invoice.overdue',
    ];
}

This is how they are used:

public function setInvoiceStatus(InvoiceStatus $invoice)
{
    $this->invoice = $invoice;
}

// ...

$class->setInvoiceStatus(InvoiceStatus::paid());

This is how get we can get the enum name from a value:

$invoice = InvoiceStatus::search('invoice.overdue');

// $invoice is 'overdue'

Creating an enum from a value

$invoice = InvoiceStatus::make('paid');

Comparing enums

Enums can be compared using the equals method:

$invoice->equals($invoice);

You can also use dynamic is methods:

// return a boolean
$invoice->isPaid();

// return a boolean
InvoiceStatus::isPaid($invoice);

Note that if you want auto completion on these is methods, you must add extra doc blocks on your enum classes.

Search

You can search enum with its textual value:

$invoice = InvoiceStatus::search('invoice.pending');

// $invoice is 'pending'

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-02-28

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固