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 | * @param bool $successFlag |
||
73 | * @return \Illuminate\Http\JsonResponse |
||
74 | */ |
||
75 | public function respond(int $statusCode = null, array $headers = [], bool $successFlag = true):JsonResponse |
||
88 | |||
89 | /** |
||
90 | * Set the HTTP status code for the response. |
||
91 | * |
||
92 | * @param int $statusCode |
||
93 | * @return self |
||
94 | */ |
||
95 | public function setStatus(int $statusCode):ResponseBuilder |
||
101 | |||
102 | /** |
||
103 | * Set a flag indicating if success should be added to the response. |
||
104 | * |
||
105 | * @param bool $includeSuccessFlag |
||
106 | * @return self |
||
107 | */ |
||
108 | public function setIncludeSuccessFlag(bool $includeSuccessFlag):ResponseBuilder |
||
114 | |||
115 | /** |
||
116 | * Set a flag indicating if status code should be added to the response. |
||
117 | * |
||
118 | * @param bool $includeStatusCode |
||
119 | * @return self |
||
120 | */ |
||
121 | public function setIncludeStatusCode(bool $includeStatusCode):ResponseBuilder |
||
127 | |||
128 | /** |
||
129 | * Convert the response to an Illuminate collection. |
||
130 | * |
||
131 | * @return \Illuminate\Support\Collection |
||
132 | */ |
||
133 | public function toCollection():Collection |
||
137 | |||
138 | /** |
||
139 | * Convert the response to JSON. |
||
140 | * |
||
141 | * @param int $options |
||
142 | * @return string |
||
143 | */ |
||
144 | public function toJson($options = 0) |
||
148 | |||
149 | /** |
||
150 | * Convert the object into something JSON serializable. |
||
151 | * |
||
152 | * @return array |
||
153 | */ |
||
154 | public function jsonSerialize() |
||
158 | |||
159 | /** |
||
160 | * Convert the response to an array. |
||
161 | * |
||
162 | * @return array |
||
163 | */ |
||
164 | abstract public function toArray():array; |
||
165 | |||
166 | /** |
||
167 | * Include a status code to the serialized data if enabled. |
||
168 | * |
||
169 | * @param array $data |
||
170 | * @return array |
||
171 | */ |
||
172 | protected function includeSuccessFlag(array $data):array |
||
180 | |||
181 | /** |
||
182 | * Include a status code to the serialized data if enabled. |
||
183 | * |
||
184 | * @param array $data |
||
185 | * @return array |
||
186 | */ |
||
187 | protected function includeStatusCode(array $data):array |
||
195 | } |