承接 webdevcave/enum-index-accessor 相关项目开发

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

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

webdevcave/enum-index-accessor

最新稳定版本:v1.0

Composer 安装命令:

composer require webdevcave/enum-index-accessor

包简介

Dynamic index accessing made simple

README 文档

README

Install with composer:

composer install webdevcave/enum-index-accessor

Why?

From php 8.1 or newer, the possibility organized enumerators was made real. It works just fine until we have to read those values dynamically. Here is an example:

enum HexColors: string {
    case RED = '#FF0000';
    case GREEN = '#00FF00';
    case BLUE = '#0000FF';
    case WHITE = '#FFFFFF';
    case BLACK = '#000000';
    // and so on...
}

$index = 'BLUE'; // Imagine this is a dynamic value
$constant = HexColors::class."::$index"; // In a real-world application HexColors
                                         // will most probably be declared under a namespace
$color = null;

// Before we proceed, we have to ensure the specified index exists or our code will break
if (defined($constant)) {
    $color = constant($constant)->value; // Now we read its value
}

// You will probably want to assign a default value in case something went wrong
if (is_null($color)) {
    $color = HexColors::RED->value;
}

// Now we can finally proceed with our task...

Does this looks too verbose and/or messy for your taste? Now imagine if your application have to read multiple values like this.

That's why we created this package! Now you can do the same task this way:

$color = HexColors::tryValue($index) ?? HexColors::value('RED');

How?

use \WebdevCave\EnumIndexAccessor\BackedEnumIndexAccessor; // step 1: Import the trait

enum HexColors: string {
    case RED = '#FF0000';
    case GREEN = '#00FF00';
    case BLUE = '#0000FF';
    case WHITE = '#FFFFFF';
    case BLACK = '#000000';
    // and so on...
    
    use BackedEnumIndexAccessor; // step 2: use it
}

~ And voila! Magic 🪄!

Other use cases...

We followed the php team standards for naming the methods for ease of use. Here is a list of all of them:

HexColors::hasIndex($index); // Checks if a case statement was set in the enumerator (boolean)
HexColors::index($index); // Read the object from given index (skips index check)
HexColors::tryIndex($index); // Read the object from given index (null on non-existent)
HexColors::value($index); // Read the value from given index (skips index check)
HexColors::tryValue($index); // Read the value from given index (null on non-existent)

For pure enumerators (without backing values), use the pure enumerator trait as follows:

use \WebdevCave\EnumIndexAccessor\PureEnumIndexAccessor; // step 1: Import the trait

enum Fruits {
    case ORANGE;
    case PEAR;
    case APPLE;
    // and so on...
    
    use PureEnumIndexAccessor; // step 2: use it
}

Important note: The methods value and tryValue are not available as pure enumerators doesn't carry any values on them

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-04-24