|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Shetabit\Multipay\Http; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
|
|
7
|
|
|
class Response implements \ArrayAccess |
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Response Date. |
|
12
|
|
|
* |
|
13
|
|
|
* @var Carbon |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $date; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Payment Driver. |
|
19
|
|
|
* |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $driver; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Response Data. |
|
26
|
|
|
* |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $data = []; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* response status code |
|
33
|
|
|
* @var |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $statusCode; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* response status message |
|
39
|
|
|
* @var string[] |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $statusMessages = [ |
|
42
|
|
|
// INFORMATIONAL CODES |
|
43
|
|
|
100 => 'Continue', |
|
44
|
|
|
101 => 'Switching Protocols', |
|
45
|
|
|
102 => 'Processing', |
|
46
|
|
|
// SUCCESS CODES |
|
47
|
|
|
200 => 'OK', |
|
48
|
|
|
201 => 'Created', |
|
49
|
|
|
202 => 'Accepted', |
|
50
|
|
|
203 => 'Non-Authoritative Information', |
|
51
|
|
|
204 => 'No Content', |
|
52
|
|
|
205 => 'Reset Content', |
|
53
|
|
|
206 => 'Partial Content', |
|
54
|
|
|
207 => 'Multi-status', |
|
55
|
|
|
208 => 'Already Reported', |
|
56
|
|
|
226 => 'IM Used', |
|
57
|
|
|
// REDIRECTION CODES |
|
58
|
|
|
300 => 'Multiple Choices', |
|
59
|
|
|
301 => 'Moved Permanently', |
|
60
|
|
|
302 => 'Found', |
|
61
|
|
|
303 => 'See Other', |
|
62
|
|
|
304 => 'Not Modified', |
|
63
|
|
|
305 => 'Use Proxy', |
|
64
|
|
|
306 => 'Switch Proxy', // Deprecated |
|
65
|
|
|
307 => 'Temporary Redirect', |
|
66
|
|
|
308 => 'Permanent Redirect', |
|
67
|
|
|
// CLIENT ERROR |
|
68
|
|
|
400 => 'Bad Request', |
|
69
|
|
|
401 => 'Unauthorized', |
|
70
|
|
|
402 => 'Payment Required', |
|
71
|
|
|
403 => 'Forbidden', |
|
72
|
|
|
404 => 'Not Found', |
|
73
|
|
|
405 => 'Method Not Allowed', |
|
74
|
|
|
406 => 'Not Acceptable', |
|
75
|
|
|
407 => 'Proxy Authentication Required', |
|
76
|
|
|
408 => 'Request Time-out', |
|
77
|
|
|
409 => 'Conflict', |
|
78
|
|
|
410 => 'Gone', |
|
79
|
|
|
411 => 'Length Required', |
|
80
|
|
|
412 => 'Precondition Failed', |
|
81
|
|
|
413 => 'Request Entity Too Large', |
|
82
|
|
|
414 => 'Request-URI Too Long', |
|
83
|
|
|
415 => 'Unsupported Media Type', |
|
84
|
|
|
416 => 'Requested range not satisfiable', |
|
85
|
|
|
417 => 'Expectation Failed', |
|
86
|
|
|
418 => 'I\'m a teapot', |
|
87
|
|
|
422 => 'Unprocessable Entity', |
|
88
|
|
|
423 => 'Locked', |
|
89
|
|
|
424 => 'Failed Dependency', |
|
90
|
|
|
425 => 'Too Early', |
|
91
|
|
|
426 => 'Upgrade Required', |
|
92
|
|
|
428 => 'Precondition Required', |
|
93
|
|
|
429 => 'Too Many Requests', |
|
94
|
|
|
431 => 'Request Header Fields Too Large', |
|
95
|
|
|
444 => 'Connection Closed Without Response', |
|
96
|
|
|
451 => 'Unavailable For Legal Reasons', |
|
97
|
|
|
499 => 'Client Closed Request', |
|
98
|
|
|
// SERVER ERROR |
|
99
|
|
|
500 => 'Internal Server Error', |
|
100
|
|
|
501 => 'Not Implemented', |
|
101
|
|
|
502 => 'Bad Gateway', |
|
102
|
|
|
503 => 'Service Unavailable', |
|
103
|
|
|
504 => 'Gateway Time-out', |
|
104
|
|
|
505 => 'HTTP Version not supported', |
|
105
|
|
|
506 => 'Variant Also Negotiates', |
|
106
|
|
|
507 => 'Insufficient Storage', |
|
107
|
|
|
508 => 'Loop Detected', |
|
108
|
|
|
510 => 'Not Extended', |
|
109
|
|
|
511 => 'Network Authentication Required', |
|
110
|
|
|
599 => 'Network Connect Timeout Error', |
|
111
|
|
|
]; |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Response constructor. |
|
115
|
|
|
* |
|
116
|
|
|
* @param string $driver |
|
117
|
|
|
* @param int $statusCode |
|
118
|
|
|
*/ |
|
119
|
|
|
public function __construct(string $driver, int $statusCode) |
|
120
|
|
|
{ |
|
121
|
|
|
$this->driver = $driver; |
|
122
|
|
|
$this->statusCode = $statusCode; |
|
123
|
|
|
$this->date = Carbon::now(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* set Response Data. |
|
129
|
|
|
* |
|
130
|
|
|
* @param $key |
|
131
|
|
|
* @param null $value |
|
|
|
|
|
|
132
|
|
|
* |
|
133
|
|
|
* @return $this |
|
134
|
|
|
*/ |
|
135
|
|
|
public function data($key, $value = null) : self |
|
136
|
|
|
{ |
|
137
|
|
|
$data = is_array($key) ? $key : [$key=>$value]; |
|
138
|
|
|
|
|
139
|
|
|
$this->data = array_merge($this->data, $data); |
|
140
|
|
|
|
|
141
|
|
|
return $this; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* get Response Data. |
|
146
|
|
|
* |
|
147
|
|
|
* @param null $key |
|
|
|
|
|
|
148
|
|
|
* |
|
149
|
|
|
* @return mixed |
|
150
|
|
|
*/ |
|
151
|
|
|
public function getData($key = null) |
|
152
|
|
|
{ |
|
153
|
|
|
return is_null($key) |
|
|
|
|
|
|
154
|
|
|
? $this->data |
|
155
|
|
|
: $this->data[$key] ?? null; |
|
156
|
|
|
} |
|
157
|
|
|
/** |
|
158
|
|
|
* Get Response Date. |
|
159
|
|
|
* |
|
160
|
|
|
* @return Carbon |
|
161
|
|
|
*/ |
|
162
|
|
|
public function getDate(): Carbon |
|
163
|
|
|
{ |
|
164
|
|
|
return $this->date; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Get Response ArvanCloud Driver. |
|
169
|
|
|
* |
|
170
|
|
|
* @return string |
|
171
|
|
|
*/ |
|
172
|
|
|
public function getDriver(): string |
|
173
|
|
|
{ |
|
174
|
|
|
return $this->driver; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Get Response Message. |
|
179
|
|
|
* |
|
180
|
|
|
* @return string |
|
181
|
|
|
*/ |
|
182
|
|
|
public function getStatusCode(): string |
|
183
|
|
|
{ |
|
184
|
|
|
return $this->statusCode; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Check if request status is success |
|
189
|
|
|
* @return bool |
|
190
|
|
|
*/ |
|
191
|
|
|
public function isSuccess(): bool |
|
192
|
|
|
{ |
|
193
|
|
|
$code = $this->getStatusCode(); |
|
194
|
|
|
return $code >= 200 && $code < 300; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Is the request forbidden due to ACLs? |
|
199
|
|
|
* |
|
200
|
|
|
* @return bool |
|
201
|
|
|
*/ |
|
202
|
|
|
public function isForbidden(): bool |
|
203
|
|
|
{ |
|
204
|
|
|
return (403 == $this->getStatusCode()); |
|
205
|
|
|
} |
|
206
|
|
|
/** |
|
207
|
|
|
* Is the current status "informational"? |
|
208
|
|
|
* |
|
209
|
|
|
* @return bool |
|
210
|
|
|
*/ |
|
211
|
|
|
public function isInformational(): bool |
|
212
|
|
|
{ |
|
213
|
|
|
$code = $this->getStatusCode(); |
|
214
|
|
|
return ($code >= 100 && $code < 200); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* Does the status code indicate the resource is not found? |
|
219
|
|
|
* |
|
220
|
|
|
* @return bool |
|
221
|
|
|
*/ |
|
222
|
|
|
public function isNotFound(): bool |
|
223
|
|
|
{ |
|
224
|
|
|
return (404 == $this->getStatusCode()); |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Does the status code indicate the resource is gone? |
|
229
|
|
|
* |
|
230
|
|
|
* @return bool |
|
231
|
|
|
*/ |
|
232
|
|
|
public function isGone(): bool |
|
233
|
|
|
{ |
|
234
|
|
|
return (410 == $this->getStatusCode()); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* Do we have a normal, OK response? |
|
239
|
|
|
* |
|
240
|
|
|
* @return bool |
|
241
|
|
|
*/ |
|
242
|
|
|
public function isOk(): bool |
|
243
|
|
|
{ |
|
244
|
|
|
return (200 == $this->getStatusCode()); |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Does the status code reflect a server error? |
|
249
|
|
|
* |
|
250
|
|
|
* @return bool |
|
251
|
|
|
*/ |
|
252
|
|
|
public function isServerError(): bool |
|
253
|
|
|
{ |
|
254
|
|
|
$code = $this->getStatusCode(); |
|
255
|
|
|
return (500 <= $code && 600 > $code); |
|
256
|
|
|
} |
|
257
|
|
|
|
|
258
|
|
|
/** |
|
259
|
|
|
* Do we have a redirect? |
|
260
|
|
|
* |
|
261
|
|
|
* @return bool |
|
262
|
|
|
*/ |
|
263
|
|
|
public function isRedirect(): bool |
|
264
|
|
|
{ |
|
265
|
|
|
$code = $this->getStatusCode(); |
|
266
|
|
|
return (300 <= $code && 400 > $code); |
|
267
|
|
|
} |
|
268
|
|
|
/** |
|
269
|
|
|
* Get status message |
|
270
|
|
|
* @return string |
|
271
|
|
|
*/ |
|
272
|
|
|
public function getStatusMessages(): string |
|
273
|
|
|
{ |
|
274
|
|
|
return $this->statusMessages[$this->getStatusCode()]; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* Set status messages |
|
279
|
|
|
* @param $code |
|
280
|
|
|
* @param string|null $message |
|
281
|
|
|
*/ |
|
282
|
|
|
public function setStatusMessages($code, string $message = null) |
|
283
|
|
|
{ |
|
284
|
|
|
$statusMessage = is_array($code) ? $code : [$code=>$message]; |
|
285
|
|
|
$this->statusMessages = array_merge($this->statusMessages, $statusMessage); |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* Throw Error if Request is Not Success |
|
290
|
|
|
* @param string $exception |
|
291
|
|
|
* @param string|null $message |
|
292
|
|
|
*/ |
|
293
|
|
|
public function throwError(string $exception, string $message = null) |
|
294
|
|
|
{ |
|
295
|
|
|
if (! $this->isSuccess()) { |
|
296
|
|
|
throw new $exception($message??$this->getStatusMessages()); |
|
297
|
|
|
} |
|
298
|
|
|
} |
|
299
|
|
|
|
|
300
|
|
|
/** |
|
301
|
|
|
* set Response Data. |
|
302
|
|
|
* |
|
303
|
|
|
* @param $name |
|
304
|
|
|
* @param $value |
|
305
|
|
|
*/ |
|
306
|
|
|
public function __set($name, $value) |
|
307
|
|
|
{ |
|
308
|
|
|
$this->data($name, $value); |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
/** |
|
312
|
|
|
* get Response Data. |
|
313
|
|
|
* |
|
314
|
|
|
* @param $name |
|
315
|
|
|
* |
|
316
|
|
|
* @return mixed|null |
|
317
|
|
|
*/ |
|
318
|
|
|
public function __get($name) |
|
319
|
|
|
{ |
|
320
|
|
|
return $this->GetData($name); |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
/** |
|
324
|
|
|
* @param mixed $offset |
|
325
|
|
|
* |
|
326
|
|
|
* @return bool |
|
327
|
|
|
*/ |
|
328
|
|
|
public function offsetExists($offset): bool |
|
329
|
|
|
{ |
|
330
|
|
|
return isset($this->data[$offset]); |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
/** |
|
334
|
|
|
* @param mixed $offset |
|
335
|
|
|
* |
|
336
|
|
|
* @return mixed |
|
337
|
|
|
*/ |
|
338
|
|
|
public function offsetGet($offset) |
|
339
|
|
|
{ |
|
340
|
|
|
return $this->data[$offset]; |
|
341
|
|
|
} |
|
342
|
|
|
|
|
343
|
|
|
/** |
|
344
|
|
|
* @param mixed $offset |
|
345
|
|
|
* @param mixed $value |
|
346
|
|
|
* |
|
347
|
|
|
* @return mixed|void |
|
348
|
|
|
*/ |
|
349
|
|
|
public function offsetSet($offset, $value) |
|
350
|
|
|
{ |
|
351
|
|
|
return $this->data[$offset] = $value; |
|
352
|
|
|
} |
|
353
|
|
|
|
|
354
|
|
|
/** |
|
355
|
|
|
* @param mixed $offset |
|
356
|
|
|
*/ |
|
357
|
|
|
public function offsetUnset($offset) |
|
358
|
|
|
{ |
|
359
|
|
|
unset($this->data[$offset]); |
|
360
|
|
|
} |
|
361
|
|
|
} |
|
362
|
|
|
|