tkaratug/powered-enum 问题修复 & 功能扩展

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

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

tkaratug/powered-enum

最新稳定版本:0.0.1

Composer 安装命令:

composer require tkaratug/powered-enum

包简介

Some cool add-ons to PHP native enums.

README 文档

README

Latest Version on Packagist GitHub Tests Action Status Total Downloads

This package offers a trait that contains some cool features for native PHP enums.

Requirements

  • PHP 8.1 or higher.

Installation

composer require tkaratug/powered-enum

Declaration

All you need to do is use the PoweredEnum trait in your native PHP enums.

use Tkaratug\PoweredEnum\PoweredEnum;

enum MyEnum: int
{
    use PoweredEnum;
    
    case ONE    = 1;
    case TWO    = 2;
    case THREE  = 3;
}

Jump To

Methods

is(), isNot()

  • You can check the equality of a case against any name by passing it to the is() and isNot() methods.
$myEnum = MyEnum::ONE;

$myEnum->is(MyEnum::ONE);      // true
$myEnum->is(MyEnum::TWO);      // false

$myEnum->isNot(MyEnum::ONE);   // false
$myEnum->isNot(MyEnum::TWO);   // true

Static Methods

hasName()

  • You can check whether an enum has a case by given name via the hasName() method.
enum MyEnum: int {
    use PoweredEnum;

    case ONE = 1;
    case TWO = 2;
}

MyEnum::hasName('ONE');     // true
MyEnum::hasName('THREE');   // false

hasValue()

  • You can check whether an enum has a case by given value via the hasValue() method.
enum MyEnum: int {
    use PoweredEnum;

    case ONE = 1;
    case TWO = 2;
}

MyEnum::hasValue(1);   // true
MyEnum::hasValue(3);   // false

getNames()

  • You can get enum case names as an array by using the getNames() method.
enum MyEnum: int {
    use PoweredEnum;

    case ONE = 1;
    case TWO = 2;
}

MyEnum::getNames();   // ['ONE', 'TWO']

getValues()

  • You can get enum case values as an array by using the getValues() method.
enum MyEnum: int {
    use PoweredEnum;

    case ONE = 1;
    case TWO = 2;
}

MyEnum::getValues();   // [1, 2]

toArray()

  • You can get a combined array of the enum cases as value => name by using the toArray() method.
enum MyEnum: int {
    use PoweredEnum;

    case ONE = 1;
    case TWO = 2;
}

MyEnum::toArray();   // [1 => 'ONE', 2 => 'TWO']

getNamesExcept()

  • You can get names of enum cases as an array except for given ones by using the getNamesExcept() method.
enum MyEnum: int {
    use PoweredEnum;

    case ONE = 1;
    case TWO = 2;
    case THREE = 3;
}

MyEnum::getNamesExcept([MyEnum::ONE]);   // ['TWO', 'THREE']

getValuesExcept()

  • You can get values of enum cases as an array except for given ones by using the getValuesExcept() method.
enum MyEnum: int {
    use PoweredEnum;

    case ONE = 1;
    case TWO = 2;
    case THREE = 3;
}

MyEnum::getValuesExcept([MyEnum::ONE]);   // [2, 3]

toArrayExcept()

  • You can get a combined array of the enum cases as value => name except for given ones by using the toArrayExcept() method.
enum MyEnum: int {
    use PoweredEnum;

    case ONE = 1;
    case TWO = 2;
    case THREE = 3;
}

MyEnum::toArrayExcept([MyEnum::ONE]);   // [2 => 'TWO', 3 => 'THREE]

getRandomName()

  • You can get a random name of the enum cases by using the getRandomName() method.
enum MyEnum: int {
    use PoweredEnum;

    case ONE = 1;
}

MyEnum::getRandomName();   // ['ONE']

getRandomValue()

  • You can get a random value of the enum cases by using the getRandomValue() method.
enum MyEnum: int {
    use PoweredEnum;

    case ONE = 1;
}

MyEnum::getRandomValue();   // [1]

getRandomCase()

  • You can get a random case of the enum by using the getRandomCase() method.
enum MyEnum: int {
    use PoweredEnum;

    case ONE = 1;
}

MyEnum::getRandomCase();   // MyEnum::ONE

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

If you've found a bug regarding security please mail tkaratug@hotmail.com.tr instead of using the issue tracker.

Credits

License

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-10-12