|
1
|
|
|
<?php namespace Arcanedev\LaravelApiHelper\Http; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanedev\LaravelApiHelper\Contracts\Http\JsonResponse as JsonResponseContract; |
|
4
|
|
|
use Illuminate\Contracts\Routing\ResponseFactory; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class JsonResponder |
|
8
|
|
|
* |
|
9
|
|
|
* @package Arcanedev\LaravelApiHelper\Http |
|
10
|
|
|
* @author ARCANEDEV <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
class JsonResponder implements JsonResponseContract |
|
13
|
|
|
{ |
|
14
|
|
|
/* ----------------------------------------------------------------- |
|
15
|
|
|
| Properties |
|
16
|
|
|
| ----------------------------------------------------------------- |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The response factory. |
|
21
|
|
|
* |
|
22
|
|
|
* @var \Illuminate\Contracts\Routing\ResponseFactory |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $response; |
|
25
|
|
|
|
|
26
|
|
|
/* ----------------------------------------------------------------- |
|
27
|
|
|
| Constructor |
|
28
|
|
|
| ----------------------------------------------------------------- |
|
29
|
|
|
*/ |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* JsonResponse constructor. |
|
33
|
|
|
* |
|
34
|
|
|
* @param \Illuminate\Contracts\Routing\ResponseFactory $response |
|
35
|
|
|
*/ |
|
36
|
21 |
|
public function __construct(ResponseFactory $response) |
|
37
|
|
|
{ |
|
38
|
21 |
|
$this->response = $response; |
|
39
|
21 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/* ----------------------------------------------------------------- |
|
42
|
|
|
| Main Methods |
|
43
|
|
|
| ----------------------------------------------------------------- |
|
44
|
|
|
*/ |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Respond with a success response. |
|
48
|
|
|
* |
|
49
|
|
|
* @param array $data |
|
50
|
|
|
* @param int $status |
|
51
|
|
|
* @param string $code |
|
52
|
|
|
* @param array $headers |
|
53
|
|
|
* @param int $options |
|
54
|
|
|
* |
|
55
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
56
|
|
|
*/ |
|
57
|
12 |
|
public function success(array $data, $status = 200, $code = 'success', array $headers = [], $options = 0) |
|
58
|
|
|
{ |
|
59
|
12 |
|
return $this->respond($data, $status, $code, $headers, $options); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Respond with an error response. |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $message |
|
66
|
|
|
* @param int $status |
|
67
|
|
|
* @param string $code |
|
68
|
|
|
* @param array $headers |
|
69
|
|
|
* @param int $options |
|
70
|
|
|
* |
|
71
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
72
|
|
|
*/ |
|
73
|
9 |
|
public function error($message, $status = 400, $code = 'error', array $headers = [], $options = 0) |
|
74
|
|
|
{ |
|
75
|
9 |
|
return $this->respond(compact('message'), $status, $code, $headers, $options); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Respond with a json response. |
|
80
|
|
|
* |
|
81
|
|
|
* @param array $data |
|
82
|
|
|
* @param int $status |
|
83
|
|
|
* @param string $code |
|
84
|
|
|
* @param array $headers |
|
85
|
|
|
* @param int $options |
|
86
|
|
|
* |
|
87
|
|
|
* @return \Illuminate\Http\JsonResponse |
|
88
|
|
|
*/ |
|
89
|
21 |
|
public function respond(array $data, $status = 200, $code, array $headers = [], $options = 0) |
|
90
|
|
|
{ |
|
91
|
21 |
|
return $this->response->json( |
|
92
|
21 |
|
array_merge(compact('status', 'code'), $data), $status, $headers, $options |
|
93
|
7 |
|
); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|