1 | <?php |
||
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 success flag property |
||
38 | * |
||
39 | * @var bool |
||
40 | */ |
||
41 | protected $successFlag; |
||
42 | |||
43 | /** |
||
44 | * The HTTP status code for the response. |
||
45 | * |
||
46 | * @var int |
||
47 | */ |
||
48 | protected $statusCode; |
||
49 | |||
50 | /** |
||
51 | * Response factory used to generate JSON responses. |
||
52 | * |
||
53 | * @var \Illuminate\Contracts\Routing\ResponseFactory|\Laravel\Lumen\Http\ResponseFactory $responseFactory |
||
54 | */ |
||
55 | protected $responseFactory; |
||
56 | |||
57 | /** |
||
58 | * Constructor. |
||
59 | * |
||
60 | * @param \Illuminate\Contracts\Routing\ResponseFactory|\Laravel\Lumen\Http\ResponseFactory $responseFactory |
||
61 | */ |
||
62 | public function __construct($responseFactory) |
||
66 | |||
67 | /** |
||
68 | * Serialize the data and wrap it in a JSON response object. |
||
69 | * |
||
70 | * @param int|null $statusCode |
||
71 | * @param array $headers |
||
72 | * @return \Illuminate\Http\JsonResponse |
||
73 | */ |
||
74 | public function respond(int $statusCode = null, array $headers = []):JsonResponse |
||
86 | |||
87 | /** |
||
88 | * Set the HTTP status code for the response. |
||
89 | * |
||
90 | * @param int $statusCode |
||
91 | * @return self |
||
92 | */ |
||
93 | public function setStatus(int $statusCode):ResponseBuilder |
||
99 | |||
100 | /** |
||
101 | * Return response success flag |
||
102 | * |
||
103 | * @return bool |
||
104 | */ |
||
105 | abstract public function isSuccessResponse():bool; |
||
106 | |||
107 | /** |
||
108 | * Set a flag indicating if success should be added to the response. |
||
109 | * |
||
110 | * @param bool $includeSuccessFlag |
||
111 | * @return self |
||
112 | */ |
||
113 | public function setIncludeSuccessFlag(bool $includeSuccessFlag):ResponseBuilder |
||
119 | |||
120 | /** |
||
121 | * Set a flag indicating if status code should be added to the response. |
||
122 | * |
||
123 | * @param bool $includeStatusCode |
||
124 | * @return self |
||
125 | */ |
||
126 | public function setIncludeStatusCode(bool $includeStatusCode):ResponseBuilder |
||
132 | |||
133 | /** |
||
134 | * Convert the response to an Illuminate collection. |
||
135 | * |
||
136 | * @return \Illuminate\Support\Collection |
||
137 | */ |
||
138 | public function toCollection():Collection |
||
142 | |||
143 | /** |
||
144 | * Convert the response to JSON. |
||
145 | * |
||
146 | * @param int $options |
||
147 | * @return string |
||
148 | */ |
||
149 | public function toJson($options = 0) |
||
153 | |||
154 | /** |
||
155 | * Convert the object into something JSON serializable. |
||
156 | * |
||
157 | * @return array |
||
158 | */ |
||
159 | public function jsonSerialize() |
||
163 | |||
164 | /** |
||
165 | * Convert the response to an array. |
||
166 | * |
||
167 | * @return array |
||
168 | */ |
||
169 | abstract public function toArray():array; |
||
170 | |||
171 | /** |
||
172 | * Include a status code to the serialized data if enabled. |
||
173 | * |
||
174 | * @param array $data |
||
175 | * @return array |
||
176 | */ |
||
177 | protected function includeSuccessFlag(array $data):array |
||
185 | |||
186 | /** |
||
187 | * Include a status code to the serialized data if enabled. |
||
188 | * |
||
189 | * @param array $data |
||
190 | * @return array |
||
191 | */ |
||
192 | protected function includeStatusCode(array $data):array |
||
200 | } |