Explorar el Código

refactor(app/utils): 重构 EnumUtil 类

- 优化了枚举验证逻辑,提高了代码可读性和维护性
- 新增 getEnumConstants 和 isValidEnumArray 方法,用于获取枚举常量和验证枚举数组
- 改进了错误处理,使错误信息更加清晰
- 优化了字段映射获取逻辑,提高了代码复用性
shichen hace 11 meses
padre
commit
e45ac3e2c1
Se han modificado 1 ficheros con 59 adiciones y 64 borrados
  1. 59 64
      app/utils/EnumUtil.php

+ 59 - 64
app/utils/EnumUtil.php

@@ -3,6 +3,7 @@
3 3
 namespace app\utils;
4 4
 
5 5
 use ReflectionClass;
6
+use ReflectionException;
6 7
 
7 8
 /**
8 9
  * 智能枚举验证器
@@ -22,53 +23,89 @@ class EnumUtil
22 23
         $errors = [];
23 24
         
24 25
         try {
25
-            $reflection = new ReflectionClass($enumClassOrInstance);
26
-            $constants = $reflection->getConstants();
26
+            $constants = self::getEnumConstants($enumClassOrInstance);
27 27
 
28 28
             foreach ($constants as $constName => $enumValues) {
29
-                if (!is_array($enumValues)) {
30
-                    continue;
31
-                }
32
-                
33
-                // 跳过MAP格式的枚举定义(包含code字段的)
34
-                if (self::isMapFormat($enumValues)) {
29
+                if (!self::isValidEnumArray($enumValues)) {
35 30
                     continue;
36 31
                 }
37 32
 
38
-                // 将枚举常量名转换为可能的请求字段名
39 33
                 $fieldName = self::constToFieldName($constName);
40 34
 
41
-                // 检查请求数据中是否存在该字段
42 35
                 if (array_key_exists($fieldName, $requestData)) {
43
-                    $value = $requestData[$fieldName];
44
-                    $result = self::validateEnumValue($value, $enumValues, $constName);
36
+                    $result = self::validateEnumValue($requestData[$fieldName], $enumValues, $constName);
45 37
                     if (!$result['valid']) {
46 38
                         $errors[] = $result['error'];
47 39
                     }
48 40
                 }
49 41
             }
50 42
 
51
-        } catch (\ReflectionException $e) {
43
+        } catch (ReflectionException $e) {
52 44
             return [
53 45
                 'valid' => false,
54 46
                 'errors' => ["枚举验证异常: " . $e->getMessage()]
55 47
             ];
56 48
         }
57 49
 
58
-        if (!empty($errors)) {
59
-            return [
60
-                'valid' => false,
61
-                'errors' => $errors
62
-            ];
63
-        }
64
-
65
-        return [
50
+        return empty($errors) ? [
66 51
             'valid' => true,
67 52
             'errors' => []
53
+        ] : [
54
+            'valid' => false,
55
+            'errors' => $errors
68 56
         ];
69 57
     }
70 58
 
71 59
     /**
60
+     * 获取枚举类的所有字段映射
61
+     *
62
+     * @param string|object $enumClassOrInstance 枚举类名或实例
63
+     * @return array 字段映射 [字段名 => 枚举值数组]
64
+     */
65
+    public static function getFieldMappings($enumClassOrInstance): array
66
+    {
67
+        $mappings = [];
68
+        
69
+        try {
70
+            $constants = self::getEnumConstants($enumClassOrInstance);
71
+
72
+            foreach ($constants as $constName => $enumValues) {
73
+                if (self::isValidEnumArray($enumValues)) {
74
+                    $mappings[self::constToFieldName($constName)] = $enumValues;
75
+                }
76
+            }
77
+
78
+        } catch (ReflectionException $e) {
79
+            // 忽略异常,返回空数组
80
+        }
81
+
82
+        return $mappings;
83
+    }
84
+
85
+    /**
86
+     * 获取枚举类的常量
87
+     *
88
+     * @param string|object $enumClassOrInstance
89
+     * @return array
90
+     * @throws ReflectionException
91
+     */
92
+    private static function getEnumConstants($enumClassOrInstance): array
93
+    {
94
+        return (new ReflectionClass($enumClassOrInstance))->getConstants();
95
+    }
96
+
97
+    /**
98
+     * 检查是否为有效的枚举数组(非MAP格式的数组)
99
+     *
100
+     * @param mixed $enumValues
101
+     * @return bool
102
+     */
103
+    private static function isValidEnumArray($enumValues): bool
104
+    {
105
+        return is_array($enumValues) && !empty($enumValues) && !self::isMapFormat($enumValues);
106
+    }
107
+
108
+    /**
72 109
      * 将枚举常量名转换为字段名
73 110
      *
74 111
      * @param string $constName 常量名称
@@ -76,7 +113,6 @@ class EnumUtil
76 113
      */
77 114
     private static function constToFieldName(string $constName): string
78 115
     {
79
-        // 将下划线命名转换为蛇形命名(小写+下划线)
80 116
         return strtolower($constName);
81 117
     }
82 118
 
@@ -90,24 +126,13 @@ class EnumUtil
90 126
      */
91 127
     private static function validateEnumValue($value, array $enumValues, string $constName): array
92 128
     {
93
-        if (!is_array($enumValues) || empty($enumValues)) {
94
-            return [
95
-                'valid' => false,
96
-                'error' => '枚举值配置错误'
97
-            ];
98
-        }
99
-
100
-        // 提取所有有效值(简单格式的键)
101 129
         $validValues = array_keys($enumValues);
102
-
103
-        // 处理字符串数字和整数的类型转换
104 130
         $normalizedValue = is_numeric($value) ? (int)$value : $value;
105 131
         
106 132
         if (!in_array($normalizedValue, $validValues, true)) {
107
-            $fieldName = self::constToFieldName($constName);
108 133
             return [
109 134
                 'valid' => false,
110
-                'error' => "{$fieldName}参数错误,值:{$value}"
135
+                'error' => self::constToFieldName($constName) . "参数错误,值:{$value}"
111 136
             ];
112 137
         }
113 138
 
@@ -118,34 +143,6 @@ class EnumUtil
118 143
     }
119 144
 
120 145
     /**
121
-     * 获取枚举类的所有字段映射
122
-     *
123
-     * @param string|object $enumClassOrInstance 枚举类名或实例
124
-     * @return array 字段映射 [字段名 => 枚举值数组]
125
-     */
126
-    public static function getFieldMappings($enumClassOrInstance): array
127
-    {
128
-        $mappings = [];
129
-        
130
-        try {
131
-            $reflection = new ReflectionClass($enumClassOrInstance);
132
-            $constants = $reflection->getConstants();
133
-
134
-            foreach ($constants as $constName => $enumValues) {
135
-                if (is_array($enumValues) && !self::isMapFormat($enumValues)) {
136
-                    $fieldName = self::constToFieldName($constName);
137
-                    $mappings[$fieldName] = $enumValues;
138
-                }
139
-            }
140
-
141
-        } catch (\ReflectionException $e) {
142
-            // 忽略异常,返回空数组
143
-        }
144
-
145
-        return $mappings;
146
-    }
147
-
148
-    /**
149 146
      * 检查枚举值数组是否为MAP格式(包含code字段)
150 147
      *
151 148
      * @param array $enumValues 枚举值数组
@@ -153,12 +150,10 @@ class EnumUtil
153 150
      */
154 151
     private static function isMapFormat(array $enumValues): bool
155 152
     {
156
-        // 如果是空数组,跳过
157 153
         if (empty($enumValues)) {
158 154
             return false;
159 155
         }
160 156
         
161
-        // 检查第一个元素是否包含code字段
162 157
         $firstValue = reset($enumValues);
163 158
         return is_array($firstValue) && isset($firstValue['code']);
164 159
     }