1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace MuCTS\Sobot\Contracts; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use MuCTS\Sobot\Exceptions\NotFoundException; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Response |
11
|
|
|
* @package MuCTS\Sobot\Contracts |
12
|
|
|
*/ |
13
|
|
|
abstract class Response |
14
|
|
|
{ |
15
|
|
|
use ResponseCnEnum; |
16
|
|
|
|
17
|
|
|
protected $sub_fields = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Response constructor. |
21
|
|
|
* @param $result |
22
|
|
|
* @throws NotFoundException |
23
|
|
|
*/ |
24
|
|
|
public function __construct($result) |
25
|
|
|
{ |
26
|
|
|
$this->init($result); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* 实例化 |
31
|
|
|
* |
32
|
|
|
* @param $result |
33
|
|
|
* @throws NotFoundException |
34
|
|
|
* @author herry.yao <[email protected]> |
35
|
|
|
* @version 1.2.2 |
36
|
|
|
* @date 2020-08-05 |
37
|
|
|
*/ |
38
|
|
|
protected function init($result) |
39
|
|
|
{ |
40
|
|
|
foreach ($result as $key => $value) { |
41
|
|
|
if (is_array($value)) { |
42
|
|
|
$this->{$key} = $this->iniArray($key, $value); |
43
|
|
|
} else { |
44
|
|
|
$this->{$key} = $value; |
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* 数组 |
51
|
|
|
* |
52
|
|
|
* @param array $array |
53
|
|
|
* @return bool |
54
|
|
|
* @author herry.yao <[email protected]> |
55
|
|
|
* @version 1.2.2 |
56
|
|
|
* @date 2020-08-05 |
57
|
|
|
*/ |
58
|
|
|
protected function isKeyValue(array $array) |
59
|
|
|
{ |
60
|
|
|
foreach ($array as $key => $value) { |
61
|
|
|
if (is_numeric($key)) { |
62
|
|
|
return false; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
return true; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* 断定是否是数组 |
70
|
|
|
* |
71
|
|
|
* @param array $arr |
72
|
|
|
* @return bool |
73
|
|
|
* @author herry.yao <[email protected]> |
74
|
|
|
* @version 1.2.2 |
75
|
|
|
* @date 2020-08-05 |
76
|
|
|
*/ |
77
|
|
|
protected function isArr(array $arr) |
78
|
|
|
{ |
79
|
|
|
foreach ($arr as $key => $value) { |
80
|
|
|
if (is_numeric($key)) { |
81
|
|
|
if (!is_array($value)) { |
82
|
|
|
continue; |
83
|
|
|
} |
84
|
|
|
if (!$this->isArr($value)) { |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
} else { |
88
|
|
|
return false; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* 获取类型文件 |
96
|
|
|
* |
97
|
|
|
* @param string $key |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
|
|
public function getClassName(string $key) |
101
|
|
|
{ |
102
|
|
|
$name = underline_to_hump($key); |
103
|
|
|
$class = in_array($key, $this->sub_fields) ? get_class($this) : get_class($this) . '\\' . $name; |
104
|
|
|
return class_exists($class) ? $class : get_namespace($this) . '\\' . $name; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* 初始化响应数据 |
109
|
|
|
* |
110
|
|
|
* @param string $key |
111
|
|
|
* @param array $value |
112
|
|
|
* @return array|mixed |
113
|
|
|
* @throws NotFoundException |
114
|
|
|
* @author herry.yao <[email protected]> |
115
|
|
|
* @version 1.2.2 |
116
|
|
|
* @date 2020-08-05 |
117
|
|
|
*/ |
118
|
|
|
protected function iniArray(string $key, array $value) |
119
|
|
|
{ |
120
|
|
|
$class = $this->getClassName($key); |
121
|
|
|
|
122
|
|
|
if (class_exists($class)) { |
123
|
|
|
if ($this->isKeyValue($value)) { |
124
|
|
|
$value = new $class($value); |
125
|
|
|
} elseif (!$this->isArr($value)) { |
126
|
|
|
foreach ($value as $k => $v) { |
127
|
|
|
$value[$k] = $this->iniArray($key, $v); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
return $value; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* get |
136
|
|
|
* @param $name |
137
|
|
|
* @return null |
138
|
|
|
* @author herry.yao <[email protected]> |
139
|
|
|
* @version 1.2.2 |
140
|
|
|
* @date 2020-08-06 |
141
|
|
|
*/ |
142
|
|
|
public function __get($name) |
143
|
|
|
{ |
144
|
|
|
$method = 'get' . underline_to_hump($name); |
145
|
|
|
if (method_exists($this, $method)) { |
146
|
|
|
return $this->{$method}(); |
147
|
|
|
} |
148
|
|
|
return null; |
149
|
|
|
} |
150
|
|
|
} |