Total Complexity | 9 |
Total Lines | 100 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
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() |
||
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 | * toString 魔术方法 |
||
114 | * |
||
115 | * @return string |
||
116 | */ |
||
117 | public function __toString() |
||
120 | } |
||
121 | } |
||
122 |