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