PHP code example of lishun / enums

1. Go to this page and download the library: Download lishun/enums library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lishun / enums example snippets


use Lishun\Enums\Annotations\EnumCase;
use Lishun\Enums\Interfaces\EnumCaseInterface;
use Lishun\Enums\Traits\EnumCaseGet;

/**
 * @method getTest()
 */
enum DemoEnum implements EnumCaseInterface
{
    use EnumCaseGet;

    #[EnumCase(msg: '系统错误', data: 1, group: 'sys', ext: ['test'=>1,'type'=>2])]
    case SYSTEM_ERROR;

    #[EnumCase(msg: '系统错误2', data: 2, group: ['sys', 'sys2'])]
    case SYSTEM_ERROR2;

    #[EnumCase('系统错误3', 2)]
    case SYSTEM_ERROR3;
}



// 获取解释信息
DemoEnum::SYSTEM_ERROR->getMsg(); // msg:系统错误



// 获取枚举拓展数据
DemoEnum::SYSTEM_ERROR->getExt(); // ext: ['test'=>1,'type'=>2]



// 获取枚举拓展数据的某个值
DemoEnum::SYSTEM_ERROR->getExt('test'); // 1

// 这个方法需要你在原类上加上注释 @method getTest()
DemoEnum::SYSTEM_ERROR->getTest(); // 1

// 这个方法需要你在原类上加上注释 @method test()
DemoEnum::SYSTEM_ERROR->test(); // 1


// 获取枚举注解数据
DemoEnum::SYSTEM_ERROR->getData();


// 获取枚举附属数据
DemoEnum::SYSTEM_ERROR->getGroup();



// 获取枚举分组,将返回一个数组,如果传入值为单个的情况仅返回单个分组,传入值为数组的情况下会返回以分组名为key的多维数组
DemoEnum::getGroupEnums('sys');
DemoEnum::getGroupEnums(['sys','sys2']);
DemoEnum::getGroupEnums(DemoEnum::SYSTEM_ERROR->getGroup())

//数据结构如下:
DemoEnum::getGroupEnums('sys');
{
    "SYSTEM_ERROR": {
        "name": "SYSTEM_ERROR",
        "value": null,
        "msg": "系统错误",
        "data": 1,
        "group": "sys",
        "ext": [
            1,
            2,
            3
        ]
    },
    "SYSTEM_ERROR2": {
        "name": "SYSTEM_ERROR2",
        "value": null,
        "msg": "系统错误2",
        "data": 2,
        "group": [
            "sys",
            "sys2"
        ],
        "ext": null
    }
}

DemoEnum::getGroupEnums(['sys','sys2']);
 "SYSTEM_ERROR": {
        "name": "SYSTEM_ERROR",
        "value": null,
        "msg": "系统错误",
        "data": 1,
        "group": "sys",
        "ext": [
            1,
            2,
            3
        ]
    },
"SYSTEM_ERROR2": {
    "name": "SYSTEM_ERROR2",
    "value": null,
    "msg": "系统错误2",
    "data": 2,
    "group": [
        "sys",
        "sys2"
    ],
    "ext": null
},
 "SYSTEM_ERROR2": {
    "name": "SYSTEM_ERROR2",
    "value": null,
    "msg": "系统错误2",
    "data": 2,
    "group": [
        "sys",
        "sys2"
    ],
    "ext": null
}
    


// 将枚举转换为数组
DemoEnum::SYSTEM_ERROR2->toArray(); 
{
    "name": "SYSTEM_ERROR2",
    "value": null,
    "msg": "系统错误2",
    "data": 2,
    "group": [
        "sys",
        "sys2"
    ],
    "ext": null
}



use Lishun\Enums\Annotations\EnumCode;
use Lishun\Enums\Annotations\EnumCodePrefix;
use Lishun\Enums\Interfaces\EnumCodeInterface;
use Lishun\Enums\Traits\EnumCodeGet;

#[EnumCodePrefix(10, '系统错误码')]
enum DemoCode: int implements EnumCodeInterface
{
    use EnumCodeGet;

    // 错误码: 10500, 错误信息: 系统错误
    #[EnumCode('系统错误')]
    case SYSTEM_ERROR = 500;


    #[EnumCode(msg:'系统错误1')]
    case SYSTEM_ERROR1 = 501;
    
     #[EnumCode(msg:'系统错误2',ext:['test'=1])]
    case SYSTEM_ERROR2 = 502;
}


// 获取错误码
DemoCode::SYSTEM_ERROR->getCode(); // 10500



// 获取错误码解释
DemoCode::SYSTEM_ERROR->getMsg(); // 系统错误



// 获取错误码前缀
DemoCode::SYSTEM_ERROR->getPrefixCode(); // 10



// 获取错误码前缀注释
DemoCode::SYSTEM_ERROR->getPrefixMsg(); // 系统错误码

// 获取枚举拓展数据的某个值
DemoCode::SYSTEM_ERROR->getExt('test'); // 1

// 这个方法需要你在原类上加上注释 @method getTest()
DemoCode::SYSTEM_ERROR->getTest(); // 1

// 这个方法需要你在原类上加上注释 @method test()
DemoCode::SYSTEM_ERROR->test(); // 1


// 将枚举转换为数组
DemoEnum::SYSTEM_ERROR2->toArray(); 
{
    "name": "SYSTEM_ERROR",
    "value": 500,
    "msg": "系统错误",
    "code": 10500,
    "ext": null,
    "pre": {
        "prefixCode": 10,
        "prefixMsg": "系统错误码"
    }
}




// 将枚举转换为数组
throw new BusinessException(DemoCode::SYSTEM_ERROR);

// 通过接口判断是否为code的实现类
class BusinessException extends ServerException
{
    public function __construct(mixed $message = null,mixed $code = 0, Throwable $previous = null)
    {
        if ($message instanceof EnumCodeInterface) {
            $msg = $message->getMsg();
            $code = $message->getCode();
            parent::__construct($msg, $code, $previous);
        }else{
            parent::__construct($message, $code, $previous);
        }
    }
}