承接 oskarstark/enum-helper 相关项目开发

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

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

oskarstark/enum-helper

最新稳定版本:1.8.1

Composer 安装命令:

composer require oskarstark/enum-helper

包简介

This library provides helpers for several enum operations

关键字:

README 文档

README

This library provides helpers for several enum operations:

  • compare
  • to array

It also provides an abstract EnumTestCase.

CI

Installation

composer require oskarstark/enum-helper

Usage

For example, you have the following Enum:

<?php

declare(strict_types=1);

namespace App\Enum;

enum Color: string
{
    case RED = 'red';
    case BLUE = 'blue';
}

You can use the following traits:

<?php

declare(strict_types=1);

namespace App\Enum;

+use OskarStark\Enum\Trait\Comparable;
+use OskarStark\Enum\Trait\ToArray;

enum Color: string
{
+   use Comparable;
+   use ToArray;

    case RED = 'red';
    case BLUE = 'blue';
}

Comparable Trait

This trait gives you the possibility to compare your enum with another one or a collection of enums like the following:

App\Enum\Color::RED->equals(App\Enum\Color::BLUE); // returns false
App\Enum\Color::RED->notEquals(App\Enum\Color::RED); // returns false
App\Enum\Color::RED->equalsOneOf([
    App\Enum\Color::BLUE,
    App\Enum\Color::RED,
]); // returns true

For example, you want to check if a color is a nice color:

<?php

declare(strict_types=1);

namespace App\Enum;

use OskarStark\Enum\Trait\Comparable;
use OskarStark\Enum\Trait\ToArray;

enum Color: string
{
    use Comparable;
    use ToArray;

    case RED = 'red';
    case BLUE = 'blue';
    case GREEN = 'green';
    
    public function isNice(): bool
    {
        return self::equalsOneOf([
            self::BLUE,
            self::GREEN
        ]);
    }
}
App\Enum\Color::RED->isNice(); // returns false
App\Enum\Color::BLUE->isNice(); // returns true

ToArray Trait

This trait gives you the possibility to get an enum as array like the following:

For Backed Enum

App\Enum\Color::toArray(); // returns ['RED' => 'red', 'BLUE' => 'blue']

For Non-Backed Enum

App\Enum\NonBackedEnum::toArray(); // returns ['RED' => 'RED', 'BLUE' => 'BLUE']

统计信息

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

GitHub 信息

  • Stars: 10
  • Watchers: 3
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04