| Total Complexity | 8 |
| Total Lines | 96 |
| 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 | throw new Exception($this->getError()); |
||
|
|
|||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * toArray 时调用的函数 |
||
| 71 | * |
||
| 72 | * @var callback |
||
| 73 | */ |
||
| 74 | protected $_toArrayCall; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * 智能返回有效数据 |
||
| 78 | * |
||
| 79 | * - 如果数据缺失,请使用 getResult() 获取原始数据 |
||
| 80 | * |
||
| 81 | * @return array |
||
| 82 | */ |
||
| 83 | public function toArray() |
||
| 84 | { |
||
| 85 | return (array)I::call($this->_toArrayCall, [$this->getResult()]); |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * 选项列表 |
||
| 90 | * |
||
| 91 | * @var array |
||
| 92 | */ |
||
| 93 | protected $_options = []; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * 设置选项 |
||
| 97 | * |
||
| 98 | * @param array $options |
||
| 99 | * |
||
| 100 | * @return static |
||
| 101 | */ |
||
| 102 | public function setOptions($options) |
||
| 103 | { |
||
| 104 | $this->_options = Arrays::merge($this->_options, $options); |
||
| 105 | return $this; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * toString 魔术方法 |
||
| 110 | * |
||
| 111 | * @return string |
||
| 112 | */ |
||
| 113 | public function __toString() |
||
| 116 | } |
||
| 117 | } |
||
| 118 |