1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class Api |
4
|
|
|
* |
5
|
|
|
* @link https://www.icy2003.com/ |
6
|
|
|
* @author icy2003 <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2017, icy2003 |
8
|
|
|
*/ |
9
|
|
|
namespace icy2003\php\iapis; |
10
|
|
|
|
11
|
|
|
use Exception; |
12
|
|
|
use icy2003\php\I; |
13
|
|
|
use icy2003\php\ihelpers\Arrays; |
14
|
|
|
use icy2003\php\ihelpers\Http; |
15
|
|
|
use icy2003\php\ihelpers\Json; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* 搜集的 API 接口 |
19
|
|
|
*/ |
20
|
|
|
class Api |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* API 返回原始数组 |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $_result = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* 成功判断 |
32
|
|
|
* |
33
|
|
|
* @return boolean |
34
|
|
|
*/ |
35
|
|
|
public function isSuccess() |
36
|
|
|
{ |
37
|
|
|
return true; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* 返回错误信息 |
42
|
|
|
* |
43
|
|
|
* @return array|string |
44
|
|
|
*/ |
45
|
|
|
public function getError() |
46
|
|
|
{ |
47
|
|
|
return []; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* 获取结果 |
52
|
|
|
* |
53
|
|
|
* @param string $key 如果有此参数,表示取某个属性 |
54
|
|
|
* |
55
|
|
|
* @return mixed |
56
|
|
|
*/ |
57
|
|
|
public function getResult($key = null) |
58
|
|
|
{ |
59
|
|
|
if ($this->isSuccess()) { |
60
|
|
|
if (null === $key) { |
61
|
|
|
return $this->_result; |
62
|
|
|
} else { |
63
|
|
|
return I::get($this->_result, $key); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
$error = $this->getError(); |
67
|
|
|
if (is_array($error)) { |
|
|
|
|
68
|
|
|
return $error; |
69
|
|
|
} |
70
|
|
|
throw new Exception((string) $error); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* toArray 时调用的函数 |
75
|
|
|
* |
76
|
|
|
* @var callback |
77
|
|
|
*/ |
78
|
|
|
protected $_toArrayCall; |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* 智能返回有效数据 |
82
|
|
|
* |
83
|
|
|
* - 如果数据缺失,请使用 getResult() 获取原始数据 |
84
|
|
|
* |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
|
|
public function toArray() |
88
|
|
|
{ |
89
|
|
|
return (array) I::call($this->_toArrayCall, [$this->getResult()]); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* 选项列表 |
94
|
|
|
* |
95
|
|
|
* @var array |
96
|
|
|
*/ |
97
|
|
|
protected $_options = []; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* 设置多个选项 |
101
|
|
|
* |
102
|
|
|
* @param array $options |
103
|
|
|
* |
104
|
|
|
* @return static |
105
|
|
|
*/ |
106
|
|
|
public function setOptions($options) |
107
|
|
|
{ |
108
|
|
|
$this->_options = Arrays::merge($this->_options, $options); |
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* 设置单个选项 |
114
|
|
|
* |
115
|
|
|
* @param string $option |
116
|
|
|
* @param mixed $value |
117
|
|
|
* |
118
|
|
|
* @return static |
119
|
|
|
*/ |
120
|
|
|
public function setOption($option, $value) |
121
|
|
|
{ |
122
|
|
|
$this->_options[$option] = $value; |
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* 从 $_options 筛选某些字段 |
128
|
|
|
* |
129
|
|
|
* @param array $keys |
130
|
|
|
* |
131
|
|
|
* @return array |
132
|
|
|
*/ |
133
|
|
|
public function filterOptions($keys) |
134
|
|
|
{ |
135
|
|
|
return Arrays::some($this->_options, $keys); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* toString 魔术方法 |
140
|
|
|
* |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
|
|
public function __toString() |
144
|
|
|
{ |
145
|
|
|
return Json::encode($this->_result); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|