1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Flugg\Responder\Http; |
4
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
6
|
|
|
use Illuminate\Contracts\Support\Jsonable; |
7
|
|
|
use Illuminate\Http\JsonResponse; |
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
use JsonSerializable; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* This class is an abstract response builder and hold common functionality the success- |
13
|
|
|
* and error response buuilder classes. |
14
|
|
|
* |
15
|
|
|
* @package flugger/laravel-responder |
16
|
|
|
* @author Alexander Tømmerås <[email protected]> |
17
|
|
|
* @license The MIT License |
18
|
|
|
*/ |
19
|
|
|
abstract class ResponseBuilder implements Arrayable, Jsonable, JsonSerializable |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Flag indicating if success flag should be added to the serialized data. |
24
|
|
|
* |
25
|
|
|
* @var bool |
26
|
|
|
*/ |
27
|
|
|
protected $includeSuccessFlag; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Flag indicating if status code should be added to the serialized data. |
31
|
|
|
* |
32
|
|
|
* @var bool |
33
|
|
|
*/ |
34
|
|
|
protected $includeStatusCode; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The HTTP status code for the response. |
38
|
|
|
* |
39
|
|
|
* @var int |
40
|
|
|
*/ |
41
|
|
|
protected $statusCode; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Response factory used to generate JSON responses. |
45
|
|
|
* |
46
|
|
|
* @var \Illuminate\Contracts\Routing\ResponseFactory|\Laravel\Lumen\Http\ResponseFactory $responseFactory |
47
|
|
|
*/ |
48
|
|
|
protected $responseFactory; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Constructor. |
52
|
|
|
* |
53
|
|
|
* @param \Illuminate\Contracts\Routing\ResponseFactory|\Laravel\Lumen\Http\ResponseFactory $responseFactory |
54
|
|
|
*/ |
55
|
|
|
public function __construct($responseFactory) |
56
|
|
|
{ |
57
|
|
|
$this->responseFactory = $responseFactory; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Serialize the data and wrap it in a JSON response object. |
62
|
|
|
* |
63
|
|
|
* @param int|null $statusCode |
64
|
|
|
* @param array $headers |
65
|
|
|
* @return \Illuminate\Http\JsonResponse |
66
|
|
|
*/ |
67
|
|
|
public function respond(int $statusCode = null, array $headers = []):JsonResponse |
68
|
|
|
{ |
69
|
|
|
if (! is_null($statusCode)) { |
70
|
|
|
$this->setStatus($statusCode); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$data = $this->toArray(); |
74
|
|
|
$data = $this->includeStatusCode($data); |
75
|
|
|
$data = $this->includeSuccessFlag($data); |
76
|
|
|
|
77
|
|
|
return $this->responseFactory->json($data, $this->statusCode, $headers); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Set the HTTP status code for the response. |
82
|
|
|
* |
83
|
|
|
* @param int $statusCode |
84
|
|
|
* @return self |
85
|
|
|
*/ |
86
|
|
|
public function setStatus(int $statusCode):ResponseBuilder |
87
|
|
|
{ |
88
|
|
|
$this->statusCode = $statusCode; |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Return response success flag |
95
|
|
|
* |
96
|
|
|
* @return bool |
97
|
|
|
*/ |
98
|
|
|
abstract protected function isSuccessResponse():bool; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Set a flag indicating if success should be added to the response. |
102
|
|
|
* |
103
|
|
|
* @param bool $includeSuccessFlag |
104
|
|
|
* @return self |
105
|
|
|
*/ |
106
|
|
|
public function setIncludeSuccessFlag(bool $includeSuccessFlag):ResponseBuilder |
107
|
|
|
{ |
108
|
|
|
$this->includeSuccessFlag = $includeSuccessFlag; |
109
|
|
|
|
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Set a flag indicating if status code should be added to the response. |
115
|
|
|
* |
116
|
|
|
* @param bool $includeStatusCode |
117
|
|
|
* @return self |
118
|
|
|
*/ |
119
|
|
|
public function setIncludeStatusCode(bool $includeStatusCode):ResponseBuilder |
120
|
|
|
{ |
121
|
|
|
$this->includeStatusCode = $includeStatusCode; |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Convert the response to an Illuminate collection. |
128
|
|
|
* |
129
|
|
|
* @return \Illuminate\Support\Collection |
130
|
|
|
*/ |
131
|
|
|
public function toCollection():Collection |
132
|
|
|
{ |
133
|
|
|
return new Collection($this->toArray()); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Convert the response to JSON. |
138
|
|
|
* |
139
|
|
|
* @param int $options |
140
|
|
|
* @return string |
141
|
|
|
*/ |
142
|
|
|
public function toJson($options = 0) |
143
|
|
|
{ |
144
|
|
|
return json_encode($this->jsonSerialize(), $options); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Convert the object into something JSON serializable. |
149
|
|
|
* |
150
|
|
|
* @return array |
151
|
|
|
*/ |
152
|
|
|
public function jsonSerialize() |
153
|
|
|
{ |
154
|
|
|
return $this->toArray(); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Convert the response to an array. |
159
|
|
|
* |
160
|
|
|
* @return array |
161
|
|
|
*/ |
162
|
|
|
abstract public function toArray():array; |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Include a status code to the serialized data if enabled. |
166
|
|
|
* |
167
|
|
|
* @param array $data |
168
|
|
|
* @return array |
169
|
|
|
*/ |
170
|
|
|
protected function includeSuccessFlag(array $data):array |
171
|
|
|
{ |
172
|
|
|
if (! $this->includeSuccessFlag) { |
173
|
|
|
return $data; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return array_merge(['success' => $this->isSuccessResponse()], $data); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Include a status code to the serialized data if enabled. |
181
|
|
|
* |
182
|
|
|
* @param array $data |
183
|
|
|
* @return array |
184
|
|
|
*/ |
185
|
|
|
protected function includeStatusCode(array $data):array |
186
|
|
|
{ |
187
|
|
|
if (! $this->includeStatusCode) { |
188
|
|
|
return $data; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return array_merge(['status' => $this->statusCode], $data); |
192
|
|
|
} |
193
|
|
|
} |