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