1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anetwork\Respond; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Response; |
6
|
|
|
|
7
|
|
|
class Main { |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Http status code |
11
|
|
|
* @var integer |
12
|
|
|
*/ |
13
|
|
|
protected $statusCode = 200; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Status text |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $statusText = 'success'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Error code, message and text-key |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $error; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Error code |
29
|
|
|
* @var integer |
30
|
|
|
*/ |
31
|
|
|
protected $errorCode; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Haeders |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
protected $headers = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Getter for $statusCode |
41
|
|
|
* @author Shima Payro <[email protected]> |
42
|
|
|
* @since May 2, 2016 9:46:27 AM |
43
|
|
|
* @uses |
44
|
|
|
* @see |
45
|
|
|
*/ |
46
|
|
|
public function getStatusCode() { |
47
|
|
|
|
48
|
|
|
return $this->statusCode; |
49
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Setter for $statusCode |
54
|
|
|
* @param integer $statusCode |
55
|
|
|
* @return App\Htpp\Responds\Respond |
56
|
|
|
* @author Shima Payro <[email protected]> |
57
|
|
|
* @since May 2, 2016 9:47:04 AM |
58
|
|
|
* @uses |
59
|
|
|
* @see |
60
|
|
|
*/ |
61
|
|
|
public function setStatusCode( $statusCode ) { |
62
|
|
|
|
63
|
|
|
$this->statusCode = $statusCode; |
64
|
|
|
|
65
|
|
|
return $this; |
66
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Getter for $statusText |
71
|
|
|
* @author Shima Payro <[email protected]> |
72
|
|
|
* @since May 2, 2016 9:47:36 AM |
73
|
|
|
* @uses |
74
|
|
|
* @see |
75
|
|
|
*/ |
76
|
|
|
public function getStatusText() { |
77
|
|
|
|
78
|
|
|
return $this->statusText; |
79
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Setter for $statusText |
84
|
|
|
* @param String $statusText |
85
|
|
|
* @return App\HtppApp\Htpp\Responds\Respond |
86
|
|
|
* @author Shima Payro <[email protected]> |
87
|
|
|
* @since May 2, 2016 9:48:23 AM |
88
|
|
|
* @uses |
89
|
|
|
* @see |
90
|
|
|
*/ |
91
|
|
|
public function setStatusText( $statusText ) { |
92
|
|
|
|
93
|
|
|
$this->statusText = $statusText; |
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Response |
101
|
|
|
* @param json $data |
102
|
|
|
* @return jsom |
103
|
|
|
* @author Shima Payro <[email protected]> |
104
|
|
|
* @since May 2, 2016 9:48:45 AM |
105
|
|
|
* @uses |
106
|
|
|
* @see |
107
|
|
|
*/ |
108
|
|
|
public function respond( $data ) { |
109
|
|
|
|
110
|
|
|
$result = array_filter( $this->getHeaders() ); |
111
|
|
|
|
112
|
|
|
if ( empty( $result ) ); |
113
|
|
|
return response()->json( $data, $this->getStatusCode() ); |
114
|
|
|
|
115
|
|
|
return response()->json( $data, $this->getStatusCode() ) |
|
|
|
|
116
|
|
|
->withHeaders( $this->getHeaders() ); |
117
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Response which conteins just a message |
122
|
|
|
* @param string $message |
123
|
|
|
* @author Shima Payro <[email protected]> |
124
|
|
|
* @since May 2, 2016 9:49:21 AM |
125
|
|
|
* @return json |
126
|
|
|
* @uses |
127
|
|
|
* @see |
128
|
|
|
*/ |
129
|
|
View Code Duplication |
public function respondWithMessage( $message = NULL ) { |
|
|
|
|
130
|
|
|
|
131
|
|
|
$res[ 'status' ] = $this->getStatusText(); |
|
|
|
|
132
|
|
|
|
133
|
|
|
//if it's about failure |
134
|
|
|
if ( $this->getErrorCode() ) { |
135
|
|
|
|
136
|
|
|
$res[ 'error' ] = $this->getErrorCode(); |
137
|
|
|
$res[ 'message' ] = $this->getErrorMessage(); |
138
|
|
|
|
139
|
|
|
} else { |
140
|
|
|
|
141
|
|
|
$res[ 'message' ] = $message; |
142
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $this->respond( $res ); |
|
|
|
|
146
|
|
|
|
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Set error code in our result |
151
|
|
|
* @author Mehdi Hosseini <[email protected]> |
152
|
|
|
* @since August 24, 2016 |
153
|
|
|
* @param $errorCode integer |
154
|
|
|
* @return instance |
155
|
|
|
*/ |
156
|
|
|
public function setErrorCode( $errorCode ) { |
157
|
|
|
|
158
|
|
|
$this->error = config( 'errors.' . $errorCode ); |
159
|
|
|
|
160
|
|
|
$this->errorCode = $errorCode; |
161
|
|
|
|
162
|
|
|
return $this; |
163
|
|
|
|
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Return Error code |
168
|
|
|
* @author Mehdi Hosseini <[email protected]> |
169
|
|
|
* @since August 24, 2016 |
170
|
|
|
* @return integer |
171
|
|
|
*/ |
172
|
|
|
public function getErrorCode() { |
173
|
|
|
|
174
|
|
|
return $this->errorCode; |
175
|
|
|
|
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Get error message |
180
|
|
|
* @author Mehdi Hosseini <[email protected]> |
181
|
|
|
* @since August 24, 2016 |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
|
|
public function getErrorMessage() { |
185
|
|
|
|
186
|
|
|
return $this->error['message']; |
187
|
|
|
|
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Get headers |
192
|
|
|
* @author Shima Payro <[email protected]> |
193
|
|
|
* @since Sep 13, 2016 |
194
|
|
|
* @return array |
195
|
|
|
*/ |
196
|
|
|
public function getHeaders() { |
197
|
|
|
|
198
|
|
|
return $this->headers; |
199
|
|
|
|
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Get headers |
204
|
|
|
* @author Shima Payro <[email protected]> |
205
|
|
|
* @since Sep 13, 2016 |
206
|
|
|
* @return array |
207
|
|
|
*/ |
208
|
|
|
public function setHeaders( $statusText = [] ) { |
|
|
|
|
209
|
|
|
|
210
|
|
|
$this->headers = $headers; |
|
|
|
|
211
|
|
|
|
212
|
|
|
return $this; |
213
|
|
|
|
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Response which contains status and data |
218
|
|
|
* @param json $data |
219
|
|
|
* @author Shima Payro <[email protected]> |
220
|
|
|
* @since May 2, 2016 9:50:19 AM |
221
|
|
|
* @return json |
222
|
|
|
* @uses |
223
|
|
|
* @see |
224
|
|
|
*/ |
225
|
|
View Code Duplication |
public function respondWithResult( $data = NULL ) { |
|
|
|
|
226
|
|
|
|
227
|
|
|
$res[ 'status' ] = $this->getStatusText(); |
|
|
|
|
228
|
|
|
|
229
|
|
|
//if it's about laravel validation error |
230
|
|
|
if ( $this->getErrorCode() && $this->getStatusCode() == 420 ) { |
231
|
|
|
|
232
|
|
|
$res[ 'error' ] = $this->getErrorCode(); |
233
|
|
|
$res[ 'message' ] = $data; |
234
|
|
|
|
235
|
|
|
} else { |
236
|
|
|
|
237
|
|
|
$res[ 'result' ] = $data; |
238
|
|
|
|
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
return $this->respond( $res ); |
|
|
|
|
242
|
|
|
|
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
} |
246
|
|
|
|
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.