| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- <?php
- namespace UCore\Enum;
- trait EnumCore
- {
- /**
- * 获取常量注释
- * @param string $key 常量名
- * @return string
- */
- public static function getDescription(string $key): string
- {
- return preg_replace('#[\*\s]*(^/|/$)[\*\s]*#', '', (new \ReflectionClassConstant(static::class, $key))->getDocComment());
- }
- /**
- * 获取 备注/描述
- * @return string
- */
- public function getDesc(): string
- {
- $key=$this->name;
- return preg_replace('#[\*\s]*(^/|/$)[\*\s]*#', '', (new \ReflectionClassConstant(static::class, $key))->getDocComment());
- }
- /**
- * 获取常量名和注释列表
- * @return array
- */
- public static function getKeyDescription(): array
- {
- $keys = self::cases();
- // dump($keys);
- $result = [];
- foreach ($keys as $key => $key_name) {
- $result[$key_name->name] = self::getDescription($key_name->name);
- }
- return $result;
- }
- /**
- * 获取描述=>数值
- * @return array
- */
- public static function getDescriptionValue(): array
- {
- $kd = self::getKeyDescription();
- $arr = self::toArray();
- $res = [];
- foreach ($arr as $key => $value) {
- $res[$kd[$key]] = $value;
- }
- return $res;
- }
- /**
- * 获取数值=>描述
- * @return array
- *
- */
- public static function getValueDescription(): array
- {
- $kd = self::getKeyDescription();
- $arr = self::toArray();
- // dd($kd,$arr);
- $res = [];
- foreach ($arr as $key => $value) {
- $res[$value] = $kd[$key];
- }
- return $res;
- }
- static public function toArray()
- {
- $keys = self::cases();
- $res = [];
- foreach ($keys as $key) {
- $res[$key->name] = $key->value();
- }
- return $res;
- }
- static public function keys()
- {
- $keys = self::cases();
- $res = [];
- foreach ($keys as $key) {
- $res[] = $key->name;
- }
- return $res;
- }
- }
|