carlin / laravel-dict
There is no license information available for the latest version (v1.0.3) of this package.
v1.0.3
2024-05-27 09:38 UTC
Requires
- php: >=8.0
- bensampo/laravel-enum: >=4.0
- laravel/framework: >=8
- nikic/php-parser: >=4.10
This package is auto-updated.
Last update: 2024-10-27 10:36:38 UTC
README
Scan the specified path to obtain enumeration information through reflection
安装:
composer require carlin/laravel-dict
发布配置文件:
php artisan vendor:publish --provider "Carlin\LaravelDict\DictServiceProvider"
配置文件:
<?php return [ 'store'=>env('DICT_ENUM_STORE', env('CACHE_DRIVER', 'file')), //缓存驱动 'cache-key'=>env('DICT_ENUM_CACHE_KEY', 'dict-cache-key'), //缓存key 'cache-ttl'=> (int)env('DICT_ENUM_CACHE_TTL', 60 * 60 * 24 * 30), //缓存时间, 默认30天 'enum-scan-paths'=>[ //base_path('app/Enums/*.php'), //扫描路径 ], ];
例子:
<?php namespace App\Enums; use Carlin\LaravelDict\Dict; use BenSampo\Enum\Enum; class BaseEnum extends Enum { public static function getDescription(mixed $value): string { return Dict::getDescription(static::class, $value) ?? parent::getDescription($value); } public static function descriptions(): array { return Dict::getEnums(static::class); } }
<?php namespace App\Enums; use Carlin\LaravelDict\Attributes\EnumClass; use Carlin\LaravelDict\Attributes\EnumProperty; #[EnumClass( __CLASS__, //枚举类名 '布尔整型枚举', //枚举描述 'webApi', //枚举分组 [ 'test'=>1 //业务拓展字段(选填) ] )] //枚举类注解 class BoolIntEnums extends BaseEnum { //#[EnumProperty('是', ['test'=>2])] //业务拓展字段(选填) #[EnumProperty('是', ['test'=>2])] public const TRUE = 1; //#[EnumProperty('否', ['test'=>1])] #[EnumProperty('否')] public const FALSE = 0; }
<?php use Carlin\LaravelDict\Dict; //清除缓存 Dict::clearDictCache(); //获取枚举信息 Dict::getEnums(BoolIntEnums::class); //获取枚举信息 //getDescription Dict::getDescription(BoolIntEnums::class, 1); //获取枚举描述 //获取枚举字典列表 Dict::getDict(); //获取指定group字典 Dict::getByGroup('webApi');
Dict::getDict输出示例:
//获取枚举字典列表
Array
(
[App\Enums\BoolIntEnum] => Array
(
[name] => bool
[description] => 布尔值字典
[group] => webapi
[test] => 1 //业务拓展字段(选填)
...//其他业务拓展字段
[data] => Array
(
[0] => Array
(
[name] => 是
[code] => 1
[test] => 2 //业务拓展字段(选填)
...//其他业务拓展字段
)
[1] => Array
(
[name] => 否
[code] => 0
[test] => 1 //业务拓展字段(选填)
...//其他业务拓展字段
)
)
[class] => App\Enums\BoolIntEnum
)
)