chengkun
2025-09-12 26c5c0296e7c094f9a7ae4a4bb3c975796992eaf
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
<?php
 
 
namespace Alipay\EasySDK\Kernel\Util;
 
 
use AlibabaCloud\Tea\Model;
 
class JsonUtil
{
    public function toJsonString(array $input)
    {
        $result = [];
        foreach ($input as $k => $v) {
            if ($v instanceof Model) {
                $result[$k] = $this->getTeaModelMap($v);
            } else {
                $result[$k] = $v;
            }
        }
        return $result;
    }
 
    private function getTeaModelMap(Model $teaModel)
    {
        $result = [];
        foreach ($teaModel as $k => $v) {
            if ($v instanceof Model) {
                $k = $this->toUnderScore($k);
                $result[$k] = $this->getTeaModelMap($v);
            } else {
                if (empty($result)) {
                    $k = $this->toUnderScore($k);
                    $result[$k] = $v;
                } else {
                    $k = $this->toUnderScore($k);
                    $result[$k] = $v;
                }
            }
        }
        return $result;
    }
 
    /**
     * 驼峰命名转下划线命名
     * @param $str
     * @return string
     */
    private function toUnderScore($str)
    {
        $dstr = preg_replace_callback('/([A-Z]+)/', function ($matchs) {
            return '_' . strtolower($matchs[0]);
        }, $str);
        return trim(preg_replace('/_{2,}/', '_', $dstr), '_');
    }
}