|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Support\Http; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Illuminate\Http\JsonResponse; |
|
7
|
|
|
|
|
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) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->code = is_null($code) ? static::successCode() : (int) $code; |
|
28
|
|
|
|
|
29
|
|
|
parent::__construct($data, 200, $headers, $options); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Get the code key. |
|
34
|
|
|
* |
|
35
|
|
|
* @return string |
|
36
|
|
|
*/ |
|
37
|
|
|
public static function codeKey() |
|
38
|
|
|
{ |
|
39
|
|
|
static $codeKey = null; |
|
40
|
|
|
|
|
41
|
|
|
if (is_null($codeKey)) { |
|
42
|
|
|
$codeKey = config('support.api.key.code', 'code'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $codeKey; |
|
46
|
|
|
} |
|
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) |
|
87
|
|
|
{ |
|
88
|
|
|
if ($data instanceof Model) { |
|
89
|
|
|
$data = [ |
|
90
|
|
|
snake_case(class_basename($data)) => $this->convertObjectToArray($data), |
|
91
|
|
|
]; |
|
92
|
|
|
} elseif (is_object($data)) { |
|
93
|
|
|
$data = $this->convertObjectToArray($data); |
|
94
|
|
|
} elseif (is_null($data)) { |
|
95
|
|
|
$data = []; |
|
96
|
|
|
} elseif (is_string($data)) { |
|
97
|
|
|
$data = [static::messageKey() => $data]; |
|
98
|
|
|
} elseif (! is_array($data)) { |
|
99
|
|
|
$data = [static::messageKey() => json_encode($data)]; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
if (! array_key_exists(static::codeKey(), $data)) { |
|
103
|
|
|
$data[static::codeKey()] = $this->getCode(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return parent::setData($data); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Convert an object to array. |
|
111
|
|
|
* |
|
112
|
|
|
* @param mixed $object |
|
113
|
|
|
* @return array |
|
114
|
|
|
*/ |
|
115
|
|
|
protected function convertObjectToArray($object) |
|
116
|
|
|
{ |
|
117
|
|
|
if (method_exists($object, 'toArray')) { |
|
118
|
|
|
return $object->toArray(); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return json_decode(json_encode($object, true), true); |
|
122
|
|
|
} |
|
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) |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->setData(array_replace($this->getData(true), ...$data)); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Remove empty elements from the current data. |
|
137
|
|
|
* |
|
138
|
|
|
* @return $this |
|
139
|
|
|
*/ |
|
140
|
|
|
public function removeEmpty() |
|
141
|
|
|
{ |
|
142
|
|
|
return $this->setData(array_filter($this->getData(true))); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Get the API result code. |
|
147
|
|
|
* |
|
148
|
|
|
* @return int |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getCode() |
|
151
|
|
|
{ |
|
152
|
|
|
return $this->code; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Set the API result code. |
|
157
|
|
|
* |
|
158
|
|
|
* @param int $code |
|
159
|
|
|
* @return $this |
|
160
|
|
|
*/ |
|
161
|
|
|
public function setCode($code) |
|
162
|
|
|
{ |
|
163
|
|
|
$this->code = (int) $code; |
|
164
|
|
|
|
|
165
|
|
|
return $this->mergeData([static::codeKey() => $this->code]); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* Set the API result code. |
|
170
|
|
|
* |
|
171
|
|
|
* @param int $code |
|
172
|
|
|
* @return $this |
|
173
|
|
|
*/ |
|
174
|
|
|
public function code($code) |
|
175
|
|
|
{ |
|
176
|
|
|
return $this->setCode($code); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Get the API result message. |
|
181
|
|
|
* |
|
182
|
|
|
* @return string |
|
183
|
|
|
*/ |
|
184
|
|
|
public function getMessage() |
|
185
|
|
|
{ |
|
186
|
|
|
return array_get($this->getData(true), static::messageKey()); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Set the API result message. |
|
191
|
|
|
* |
|
192
|
|
|
* @param string $message |
|
193
|
|
|
* @return $this |
|
194
|
|
|
*/ |
|
195
|
|
|
public function setMessage($message) |
|
196
|
|
|
{ |
|
197
|
|
|
return $this->mergeData([static::messageKey() => (string) $message]); |
|
198
|
|
|
} |
|
199
|
|
|
|
|
200
|
|
|
/** |
|
201
|
|
|
* Set the API result message. |
|
202
|
|
|
* |
|
203
|
|
|
* @param string $message |
|
204
|
|
|
* @return $this |
|
205
|
|
|
*/ |
|
206
|
|
|
public function message($message) |
|
207
|
|
|
{ |
|
208
|
|
|
return $this->setMessage($message); |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
|