skywalker-labs/enum 问题修复 & 功能扩展

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

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

skywalker-labs/enum

最新稳定版本:v1.0.0

Composer 安装命令:

composer require skywalker-labs/enum

包简介

A robust and type-safe Enum implementation for PHP, featuring singleton-based constants, mapping capabilities, and extensive compatibility from PHP 7.0 to 9.0.

README 文档

README

A robust, type-safe, and singleton-based Enum implementation for PHP.

This library provides a clean way to implement enums in PHP, ensuring that each enum value is a unique singleton instance. It works across PHP versions from 7.0 to 9.0+.

Installation

Install the package via Composer:

composer require skywalker-labs/enum

Basic Usage

To create an enum, extend Skywalker\Enum\AbstractEnum and define your constants as protected const.

use Skywalker\Enum\AbstractEnum;

/**
 * @method static self MONDAY()
 * @method static self TUESDAY()
 * ...
 */
final class WeekDay extends AbstractEnum
{
    protected const MONDAY = null;
    protected const TUESDAY = null;
    // ...
}

Pro Features

1. Flexible JSON Serialization

Control how your enums appear in API responses.

// Choose between 'name', 'value', or 'object'
AbstractEnum::$jsonMode = 'object';

echo json_encode(WeekDay::MONDAY());
// Output: {"name":"MONDAY","value":null,"label":"MONDAY","ordinal":0}

2. Native PHP Compatibility

Use aliases that match PHP 8.1+ native enums for a smoother transition.

$day = WeekDay::from('MONDAY');     // Throws exception if not found
$day = WeekDay::tryFrom('INVALID'); // Returns null if not found

3. Randomization

Perfect for seeders and unit tests.

$randomDay = WeekDay::random();

4. String Helpers

echo WeekDay::MONDAY()->lowerName(); // "monday"
echo WeekDay::MONDAY()->camelName(); // "Monday"

5. Collection Power-Ups (EnumSet)

Functional methods for modern collections.

$set = EnumSet::allOf(WeekDay::class);

$weekends = $set->filter(fn($d) => $d->isAnyOf([WeekDay::SATURDAY(), WeekDay::SUNDAY()]));
$names = $set->map(fn($d) => $d->name());

$first = $set->first(); // MONDAY
$last = $set->last();   // SUNDAY

Ultimate Edition Features

1. Navigational Helpers

Sequence through your enums easily.

$day = WeekDay::MONDAY();
$next = $day->next();         // TUESDAY
$prev = $day->previous();     // null

if ($day->isBefore(WeekDay::FRIDAY())) {
    echo "Hang in there!";
}

2. Set Theory (EnumSet)

Perform mathematical set operations.

$workDays = EnumSet::range(WeekDay::MONDAY(), WeekDay::FRIDAY());
$holidays = EnumSet::of(WeekDay::class, WeekDay::MONDAY());

$actualWork = $workDays->diff($holidays);
$allDays = $workDays->union($holidays);
$intersect = $workDays->intersect($holidays);
$restOfYear = $workDays->complement();

3. Tolerant Lookups

Handle user input with case-insensitivity.

$day = WeekDay::fromNameInsensitive('monday'); // Returns MONDAY instance

Advanced Features

Collection & UI Helpers

// Returns ['MONDAY' => 'MONDAY', 'TUESDAY' => 'TUESDAY', ...]
$options = WeekDay::toArray();

Type-Safe Comparisons

if ($day->isAnyOf([WeekDay::SATURDAY(), WeekDay::SUNDAY()])) {
    echo "It's the weekend!";
}

Bitmask Support

Efficiently store multiple enum values in a single integer.

$mask = $set->toBitmask(); // integer e.g. 5
$restored = EnumSet::fromBitmask(WeekDay::class, $mask);

License

This project is licensed under the MIT License. See the LICENSE file for details.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-02-15

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固