fuwasegu/php-enum-util 问题修复 & 功能扩展

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

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

fuwasegu/php-enum-util

最新稳定版本:3.0.0

Composer 安装命令:

composer require fuwasegu/php-enum-util

包简介

The extension library for PHP native enum.

README 文档

README

Coverage Status example workflow MIT License PHP Version Require Total Downloads

The extension library for PHP native enum.

📦 Installation

composer require fuwasegu/php-enum-util

✏️ Usage

This library provides traits to make Enum, implemented since PHP8.1, easier to use.

HasDescription trait

This trait provides an interface to the description() method and an implementation of the descriptions() method.

enum Status: string
{
    use HasDescription;

    case ACTIVE = 'active';

    case INACTIVE = 'inactive';

    case RETIED = 'retired';

    public function description(): string
    {
        return match ($this) {
            self::ACTIVE => 'State in which the employee is enrolled.',
            self::INACTIVE => 'State in which the employee is on administrative leave.',
            self::RETIED => 'State in which employee is retiring.',
        };
    }
}
// Getter for Enum description
Status::ACTIVE->description();

// Getter for Enum value and description maps
Status::descriptions();

HasLabel trait

This trait provides an interface to the label() method and an implementation of the labels() method.

enum Capital: int
{
    use HasLabel;

    case TOKYO = 1;

    case BEIJING = 2;

    case WASHINGTON = 3;

    case LONDON = 4;

    public function label(): string
    {
        return match ($this) {
            self::TOKYO => 'Tokyo',
            self::BEIJING => 'Beijing',
            self::WASHINGTON => 'Washington',
            self::LONDON => 'London',
        };
    }
}
// Getter for Enum label
Status::ACTIVE->label();

// Getter for Enum value and label maps
Status::labels();

📌 NOTICE:

There is no implementation difference between the HasLabel and HasDescription traits. The difference is the method name.

Choose the former if you want a short label for an enum, and choose the latter if you want a long description.

HasValues trait

This trait is the value version of the cases() method originally implemented in Enum, and provides auxiliary methods.

enum Status: string
{
    use HasValues;

    case ACTIVE = 'active';

    case INACTIVE = 'inactive';

    case RETIED = 'retired';
}
// Getter for Enum values
Status::values();

// Join Enum values with a string
Status::implodeValues();

HasNames trait

This trait is the name version of the cases() method originally implemented in Enum, and provides auxiliary methods.

enum Status: string
{
    use HasNames;

    case ACTIVE = 'active';

    case INACTIVE = 'inactive';

    case RETIED = 'retired';
}
// Getter for Enum names
Status::names();

// Join Enum names with a string
Status::implodeNames();

Comparable trait

This trait provides a method for easy comparison of Bakced Enums

enum Status: string
{
    use Comparable;

    case ACTIVE = 'active';

    case INACTIVE = 'inactive';

    case RETIED = 'retired';
}
// Compare Enum peer to peer
$maybeActive = Status::ACTIVE;
Status::ACTIVE->is($maybeActive); // true
Status::INACTIVE->isNot($maybeActive); // false

// Attempts to convert the compared int or string to an Enum with Enum::tryFrom before comparing
// If tryFrom is null, `isFrom()` returns false and `isNotFrom()` returns true.
$value = 'active';
Status::ACTIVE->isFrom($value); // true
Status::INACTIVE->isNotFrom($value); // false
Status::ACTIVE->isFrom('foo'); // false
Status::ACTIVE->isNotFrom('foo') // true

统计信息

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

GitHub 信息

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

其他信息

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