intrfce/enum-attribute-descriptors 问题修复 & 功能扩展

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

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

intrfce/enum-attribute-descriptors

最新稳定版本:v0.3.0

Composer 安装命令:

composer require intrfce/enum-attribute-descriptors

包简介

Use attributes to give your PHP enums titles and descriptions.

README 文档

README

Latest Version on Packagist Total Downloads GitHub Actions

Have you ever written an enum for something and wanted to have a "nice" version of the enum name, so you write something like this:

<?php

enum Colour: string {

    case RED = 'red';
    case BLUE = 'blue';
    case GREEN = 'green';

    public function getTitle()
    {
        return match($this->value) {
            'blue' => "Dark Blue",
            'red' => "Blood Red"
            default => ucfirst($this->value),
        };
    }
}

But the problem is, for each new case, you have to add something to the match statement, or HOPE that it'll print something out that's legible using the default fallback?

With this package, you can co-locate titles, and even descriptions, with your enum cases like so:

<?php

enum Colour: string {

    use HasAttributeDescriptors;

    #[Title('Blood Red')]
    #[Description('Our primary highlight colour')]
    case RED = 'red';

    #[Title('Dark Blue')]
    #[Description('Our primary logo colour')]
    case BLUE = 'blue';

    #[Title('Army Green')]
    #[Description('Only use this for background colours.')]
    case GREEN = 'green';
}

Neat huh!

Installation

You can install the package via composer:

composer require intrfce/enum-attribute-descriptors

Usage

Just add the Intrfce\EnumAttributeDescriptors\Concerns\HasAttributeDescriptors trait to your enum, and you're good to go!

Defining fallbacks.

If you have a situation where you don't want to (or can't) define a description or title for each case, you can override the titleFallback() and descriptionFallback() methods on your enum class.

public function titleFallback(): ?string
{
    return ucfirst($this->value);
}

public function descriptionFallback(): ?string
{
    return 'This option has no description yet';
}

Simple!

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-08-28