Blame view

addons/exam/enum/BaseEnum.php 1.71 KB
2db47680   hexiaodong   chore: 初始提交 php 遗...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
  <?php

  

  namespace addons\exam\enum;

  

  use ReflectionClassConstant;

  

  class BaseEnum

  {

      /**

       * 获取所有常量

       * @return array

       */

      public static function getConst(): array

      {

          $objClass = new \ReflectionClass(get_called_class());

          return $objClass->getConstants();

      }

  

      /**

       * 获取所有常量名

       * @return array

       */

      public static function getConstantsKeys(): array

      {

          return array_keys(self::getConst());

      }

  

      /**

       * 获取所有常量值

       * @return array

       */

      public static function getConstantsValues(): array

      {

          return array_values(self::getConst());

      }

  

      /**

       * 获取常量注释

       * @param string $key 常量名

       * @return string

       */

      public static function getDescription(string $key): string

      {

          return preg_replace('#[\*\s]*(^/|/$)[\*\s]*#', '', (new ReflectionClassConstant(static::class, $key))->getDocComment());

      }

  

      /**

       * 获取常量名和注释列表

       * @return array

       */

      public static function getKeyDescription(): array

      {

          $keys   = self::getConstantsKeys();

          $result = [];

  

          foreach ($keys as $key => $key_name) {

              $result[$key_name] = self::getDescription($key_name);

          }

  

          return $result;

      }

  

      /**

       * 获取常量值和注释列表

       * @return array

       */

      public static function getValueDescription(): array

      {

          $const  = self::getConst();

          $result = [];

  

          foreach ($const as $key => $value) {

              $result[$value] = self::getDescription($key);

          }

  

          return $result;

      }

  }