1 | <?php |
||||
2 | |||||
3 | namespace Vahid\Respond; |
||||
4 | |||||
5 | class Main |
||||
6 | { |
||||
7 | |||||
8 | /** |
||||
9 | * Http status code |
||||
10 | * @var integer |
||||
11 | */ |
||||
12 | protected $statusCode = 200; |
||||
13 | |||||
14 | /** |
||||
15 | * Status text |
||||
16 | * @var string |
||||
17 | */ |
||||
18 | protected $statusText = 'success'; |
||||
19 | |||||
20 | /** |
||||
21 | * Error code, message and text-key |
||||
22 | * @var array |
||||
23 | */ |
||||
24 | protected $error; |
||||
25 | |||||
26 | /** |
||||
27 | * Error code |
||||
28 | * @var integer |
||||
29 | */ |
||||
30 | protected $errorCode; |
||||
31 | |||||
32 | /** |
||||
33 | * Haeders |
||||
34 | * @var array |
||||
35 | */ |
||||
36 | protected $headers = []; |
||||
37 | |||||
38 | /** |
||||
39 | * @var string |
||||
40 | */ |
||||
41 | protected $lang; |
||||
42 | |||||
43 | /** |
||||
44 | * @var array |
||||
45 | */ |
||||
46 | protected $config; |
||||
47 | |||||
48 | /** |
||||
49 | * @author Shahrokh Niakan <[email protected]> |
||||
50 | * @since Sep 24, 2016 |
||||
51 | */ |
||||
52 | public function __construct() |
||||
53 | { |
||||
54 | |||||
55 | |||||
56 | if (class_exists('\App')) { |
||||
57 | $this->lang = \App::getLocale(); |
||||
58 | } else { |
||||
59 | $this->lang = /** @scrutinizer ignore-call */ |
||||
60 | app('translator')->getLocale(); |
||||
61 | } |
||||
62 | |||||
63 | if (!file_exists(config_path($this->lang . '.php'))) { |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
64 | $this->config = include __DIR__ . '/../errors/lang/' . $this->lang . '.php'; |
||||
65 | } else { |
||||
66 | $this->config = include config_path($this->lang . '.php'); |
||||
67 | } |
||||
68 | |||||
69 | } |
||||
70 | |||||
71 | /** |
||||
72 | * Getter for $statusCode |
||||
73 | * @author Shima Payro <[email protected]> |
||||
74 | * @since May 2, 2016 9:46:27 AM |
||||
75 | * @uses |
||||
76 | * @see |
||||
77 | */ |
||||
78 | public function getStatusCode() |
||||
79 | { |
||||
80 | |||||
81 | return $this->statusCode; |
||||
82 | |||||
83 | } |
||||
84 | |||||
85 | /** |
||||
86 | * Setter for $statusCode |
||||
87 | * @param integer $statusCode |
||||
88 | * @return $this |
||||
89 | * @author Shima Payro <[email protected]> |
||||
90 | * @since May 2, 2016 9:47:04 AM |
||||
91 | * @uses |
||||
92 | * @see |
||||
93 | */ |
||||
94 | public function setStatusCode($statusCode) |
||||
95 | { |
||||
96 | |||||
97 | $this->statusCode = $statusCode; |
||||
98 | |||||
99 | return $this; |
||||
100 | |||||
101 | } |
||||
102 | |||||
103 | /** |
||||
104 | * Getter for $statusText |
||||
105 | * @author Shima Payro <[email protected]> |
||||
106 | * @since May 2, 2016 9:47:36 AM |
||||
107 | * @uses |
||||
108 | * @see |
||||
109 | */ |
||||
110 | public function getStatusText() |
||||
111 | { |
||||
112 | |||||
113 | return $this->statusText; |
||||
114 | |||||
115 | } |
||||
116 | |||||
117 | /** |
||||
118 | * Setter for $statusText |
||||
119 | * @param String $statusText |
||||
120 | * @return $this |
||||
121 | * @author Shima Payro <[email protected]> |
||||
122 | * @since May 2, 2016 9:48:23 AM |
||||
123 | * @uses |
||||
124 | * @see |
||||
125 | */ |
||||
126 | public function setStatusText($statusText) |
||||
127 | { |
||||
128 | |||||
129 | $this->statusText = $statusText; |
||||
130 | |||||
131 | return $this; |
||||
132 | |||||
133 | } |
||||
134 | |||||
135 | /** |
||||
136 | * Response |
||||
137 | * @param $data : json |
||||
0 ignored issues
–
show
|
|||||
138 | * @return mixed |
||||
139 | * @author Shima Payro <[email protected]> |
||||
140 | * @since May 2, 2016 9:48:45 AM |
||||
141 | * @uses |
||||
142 | * @see |
||||
143 | */ |
||||
144 | public function respond($data) |
||||
145 | { |
||||
146 | |||||
147 | $result = array_filter($this->getHeaders()); |
||||
148 | |||||
149 | if (empty($result)) |
||||
150 | return response()->json($data, $this->getStatusCode()); |
||||
0 ignored issues
–
show
The function
response was not found. Maybe you did not declare it correctly or list all dependencies?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
151 | |||||
152 | return response()->json($data, $this->getStatusCode()) |
||||
153 | ->withHeaders($this->getHeaders()); |
||||
154 | |||||
155 | } |
||||
156 | |||||
157 | /** |
||||
158 | * Response which conteins just a message |
||||
159 | * @param string $message |
||||
160 | * @return mixed |
||||
161 | * @since May 2, 2016 9:49:21 AM |
||||
162 | * @author Shima Payro <[email protected]> |
||||
163 | * @uses |
||||
164 | * @see |
||||
165 | */ |
||||
166 | public function respondWithMessage($message = null) |
||||
167 | { |
||||
168 | |||||
169 | $res['status'] = $this->getStatusText(); |
||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||
170 | |||||
171 | //if it's about failure |
||||
172 | if ($this->getErrorCode()) { |
||||
173 | |||||
174 | $res['error'] = $this->getErrorCode(); |
||||
175 | |||||
176 | if (is_null($message)) |
||||
177 | $res['message'] = $this->getErrorMessage(); |
||||
178 | else |
||||
179 | $res['message'] = $message; |
||||
180 | |||||
181 | } else { |
||||
182 | |||||
183 | $res['message'] = $message; |
||||
184 | |||||
185 | } |
||||
186 | |||||
187 | return $this->respond($res); |
||||
188 | |||||
189 | } |
||||
190 | |||||
191 | /** |
||||
192 | * Set error code in our result |
||||
193 | * @param $errorCode integer |
||||
194 | * @return $this |
||||
195 | * @author Mehdi Hosseini <[email protected]> |
||||
196 | * @since August 24, 2016 |
||||
197 | */ |
||||
198 | public function setErrorCode($errorCode) |
||||
199 | { |
||||
200 | |||||
201 | $this->error = $this->config[$errorCode]; |
||||
202 | |||||
203 | $this->errorCode = $errorCode; |
||||
204 | |||||
205 | return $this; |
||||
206 | |||||
207 | } |
||||
208 | |||||
209 | /** |
||||
210 | * Return Error code |
||||
211 | * @return integer |
||||
212 | * @since August 24, 2016 |
||||
213 | * @author Mehdi Hosseini <[email protected]> |
||||
214 | */ |
||||
215 | public function getErrorCode() |
||||
216 | { |
||||
217 | |||||
218 | return $this->errorCode; |
||||
219 | |||||
220 | } |
||||
221 | |||||
222 | /** |
||||
223 | * Get error message |
||||
224 | * @return string |
||||
225 | * @since August 24, 2016 |
||||
226 | * @author Mehdi Hosseini <[email protected]> |
||||
227 | */ |
||||
228 | public function getErrorMessage() |
||||
229 | { |
||||
230 | |||||
231 | return $this->error['message']; |
||||
232 | |||||
233 | } |
||||
234 | |||||
235 | /** |
||||
236 | * Get headers |
||||
237 | * @return array |
||||
238 | * @since Sep 13, 2016 |
||||
239 | * @author Shima Payro <[email protected]> |
||||
240 | */ |
||||
241 | public function getHeaders() |
||||
242 | { |
||||
243 | |||||
244 | return $this->headers; |
||||
245 | |||||
246 | } |
||||
247 | |||||
248 | /** |
||||
249 | * Set headers |
||||
250 | * @param array $headers |
||||
251 | * @return $this |
||||
252 | * @since Sep 13, 2016 |
||||
253 | * @author Shima Payro <[email protected]> |
||||
254 | */ |
||||
255 | public function setHeaders($headers = []) |
||||
256 | { |
||||
257 | |||||
258 | $this->headers = $headers; |
||||
259 | |||||
260 | return $this; |
||||
261 | |||||
262 | } |
||||
263 | |||||
264 | /** |
||||
265 | * Response which contains status and data |
||||
266 | * @param null|array $data |
||||
267 | * @return mixed |
||||
268 | * @since May 2, 2016 9:50:19 AM |
||||
269 | * @author Shima Payro <[email protected]> |
||||
270 | * @uses |
||||
271 | * @see |
||||
272 | */ |
||||
273 | public function respondWithResult($data = NULL) |
||||
274 | { |
||||
275 | |||||
276 | $res['status'] = $this->getStatusText(); |
||||
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
|
|||||
277 | |||||
278 | //if it's about laravel validation error |
||||
279 | if ($this->getErrorCode() && $this->getStatusCode() == 420) { |
||||
280 | |||||
281 | $res['error'] = $this->getErrorCode(); |
||||
282 | $res['message'] = $data; |
||||
283 | |||||
284 | } else { |
||||
285 | |||||
286 | $res['result'] = $data; |
||||
287 | |||||
288 | } |
||||
289 | |||||
290 | return $this->respond($res); |
||||
291 | |||||
292 | } |
||||
293 | |||||
294 | } |
||||
295 |