Browse Source

fix: 设置命名规范

wh 1 year ago
parent
commit
1101bcaffc
2 changed files with 68 additions and 38 deletions
  1. 4 2
      api/InterfaceApi/v1/Setting.php
  2. 64 36
      common/common.php

+ 4 - 2
api/InterfaceApi/v1/Setting.php

@@ -39,7 +39,9 @@ class Setting extends CController
     {
         $appId = $this->appidVal();
         $group  = $this->groupService->mapper->findGroupIdByAppid($appId);
-        $this->service->updatedByKeys($group?$group->id:'', $this->request->all()  );
+        $data   = $this->request->all() ;
+
+        $this->service->updatedByKeys($group?$group->id:'', hump_underline_conversion($data) );
         return $this->response->success('修改成功');
     }
 
@@ -64,7 +66,7 @@ class Setting extends CController
             }
             $result[$item['key']] = $item['value'];
         }
-        return $this->response->success('',$result);
+        return $this->response->success('',hump_underline_conversion($result,1));
     }
 
     public function appidVal()

+ 64 - 36
common/common.php

@@ -92,49 +92,77 @@ if (! function_exists('create_folders')) {
     }
 }
 
- 
-//下划线转驼峰
-//思路:
-//step1.原字符串转小写,原字符串中的分隔符用空格替换,在字符串开头加上分隔符
-//step2.将字符串中每个单词的首字母转换为大写,再去空格,去字符串首部附加的分隔符.
-function convertToCamelCase($uncamelized_words,$separator='_')
-{
-    $uncamelized_words = $separator. str_replace($separator, " ", strtolower($uncamelized_words));
-    return ltrim(str_replace(" ", "", ucwords($uncamelized_words)), $separator );
-}
 
-function convert_arr_key($list) {
-    $new = array();
-    if (is_object($list)) {
-        $list = json_decode(json_encode($list), true);
-    }
-    if (is_array($list)) {
-        $list = convert_key_up($list);
-        foreach  ($list as $k => $v) {
-            $new[$k] = convert_arr_key($v);
+
+if (! function_exists('hump_underline_conversion')) {
+    /**
+     * 描述:驼峰/下划线 互转
+     * 参数:{
+     *      "params": "待转换数组",
+     *      "type": "默认 0: 驼峰-》下划线;1:下划线-》驼峰"
+     * }
+     */
+    function hump_underline_conversion($params, $type = 0) {
+        $newArr = [];
+        if (!is_array($params) || empty($params)) return $newArr;
+        foreach ($params as $key => $val){
+            if ($type == 1) {
+                $newkey = preg_replace_callback('/([-_]+([a-z]{1}))/i', function ($matches) {
+                    return strtoupper($matches[2]);
+                }, $key);
+            } else {
+                $newkey = $key;
+                if (!strstr($key, '_')) {
+                    $key = str_replace("_", "", $key);
+                    $key = preg_replace_callback('/([A-Z]{1})/', function ($matches) {
+                        return '_' . strtolower($matches[0]);
+                    }, $key);
+                    $newkey = ltrim($key, "_");
+                }
+            }
+
+            $newArr[$newkey] = is_array($val) ? hump_underline_conversion($val, $type) : $val;
         }
-        return $new;
+        return $newArr;
     }
-    return $list;
 }
 
-function convert_key_up($array) {
-    if (!is_array($array) && !is_object($array)) {
-        return $array;
+if (! function_exists('convert_arr_key')) {
+    function convert_arr_key($list) {
+        $new = array();
+        if (is_object($list)) {
+            $list = json_decode(json_encode($list), true);
+        }
+        if (is_array($list)) {
+            $list = convert_key_up($list);
+            foreach  ($list as $k => $v) {
+                $new[$k] = convert_arr_key($v);
+            }
+            return $new;
+        }
+        return $list;
     }
-    $new = array();
-    foreach ($array as $k => $v) {
-        $temp =  explode ('_', $k);
-        $i = 0;
-        foreach ($temp as $kk => $vv) {
-            if ($i > 0) {
-                $temp[$kk] = ucfirst($vv);
+}
+
+if (! function_exists('convert_key_up')) {
+    function convert_key_up($array) {
+        if (!is_array($array) && !is_object($array)) {
+            return $array;
+        }
+        $new = array();
+        foreach ($array as $k => $v) {
+            $temp =  explode ('_', $k);
+            $i = 0;
+            foreach ($temp as $kk => $vv) {
+                if ($i > 0) {
+                    $temp[$kk] = ucfirst($vv);
+                }
+                $i++;
             }
-            $i++;
+            $temp = implode('', $temp);
+            $new[$temp] = $v;
         }
-        $temp = implode('', $temp);
-        $new[$temp] = $v;
+        unset($array);
+        return $new;
     }
-    unset($array);
-    return $new;
 }