1 | <?php |
||
8 | class ApiResponse extends JsonResponse |
||
9 | { |
||
10 | /** |
||
11 | * The API result code. |
||
12 | * |
||
13 | * @var int |
||
14 | */ |
||
15 | protected $code; |
||
16 | |||
17 | /** |
||
18 | * Create an ApiResponse instance. |
||
19 | * |
||
20 | * @param mixed $data |
||
21 | * @param int|null $code |
||
22 | * @param array $headers |
||
23 | * @param int $options |
||
24 | */ |
||
25 | public function __construct($data = null, $code = null, $headers = [], $options = 0) |
||
31 | |||
32 | /** |
||
33 | * Get the code key. |
||
34 | * |
||
35 | * @return string |
||
36 | */ |
||
37 | public static function codeKey() |
||
47 | |||
48 | /** |
||
49 | * Get the message key. |
||
50 | * |
||
51 | * @return string |
||
52 | */ |
||
53 | public static function messageKey() |
||
54 | { |
||
55 | static $messageKey = null; |
||
56 | |||
57 | if (is_null($messageKey)) { |
||
58 | $messageKey = config('support.api.key.message', 'message'); |
||
59 | } |
||
60 | |||
61 | return $messageKey; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Get the success code. |
||
66 | * |
||
67 | * @return int |
||
68 | */ |
||
69 | public static function successCode() |
||
70 | { |
||
71 | static $successCode = null; |
||
72 | |||
73 | if (is_null($successCode)) { |
||
74 | $successCode = (int) config('support.api.code.success', 1); |
||
75 | } |
||
76 | |||
77 | return $successCode; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Sets the data to be sent as JSON. |
||
82 | * |
||
83 | * @param mixed $data |
||
84 | * @return $this |
||
85 | */ |
||
86 | public function setData($data = null) |
||
108 | |||
109 | /** |
||
110 | * Convert an object to array. |
||
111 | * |
||
112 | * @param mixed $object |
||
113 | * @return array |
||
114 | */ |
||
115 | protected function convertObjectToArray($object) |
||
123 | |||
124 | /** |
||
125 | * Merge new data into the current data. |
||
126 | * |
||
127 | * @param array ...$data |
||
128 | * @return $this |
||
129 | */ |
||
130 | public function mergeData(array ...$data) |
||
134 | |||
135 | /** |
||
136 | * Remove empty elements from the current data. |
||
137 | * |
||
138 | * @return $this |
||
139 | */ |
||
140 | public function removeEmpty() |
||
144 | |||
145 | /** |
||
146 | * Get the API result code. |
||
147 | * |
||
148 | * @return int |
||
149 | */ |
||
150 | public function getCode() |
||
154 | |||
155 | /** |
||
156 | * Set the API result code. |
||
157 | * |
||
158 | * @param int $code |
||
159 | * @return $this |
||
160 | */ |
||
161 | public function setCode($code) |
||
167 | |||
168 | /** |
||
169 | * Set the API result code. |
||
170 | * |
||
171 | * @param int $code |
||
172 | * @return $this |
||
173 | */ |
||
174 | public function code($code) |
||
178 | |||
179 | /** |
||
180 | * Get the API result message. |
||
181 | * |
||
182 | * @return string |
||
183 | */ |
||
184 | public function getMessage() |
||
188 | |||
189 | /** |
||
190 | * Set the API result message. |
||
191 | * |
||
192 | * @param string $message |
||
193 | * @return $this |
||
194 | */ |
||
195 | public function setMessage($message) |
||
199 | |||
200 | /** |
||
201 | * Set the API result message. |
||
202 | * |
||
203 | * @param string $message |
||
204 | * @return $this |
||
205 | */ |
||
206 | public function message($message) |
||
210 | } |
||
211 |