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 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) |
||
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 |
||
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 |
||
92 | |||
93 | /** |
||
94 | * Return response success flag |
||
95 | * |
||
96 | * @return bool |
||
97 | */ |
||
98 | abstract public 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 |
||
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 |
||
125 | |||
126 | /** |
||
127 | * Convert the response to an Illuminate collection. |
||
128 | * |
||
129 | * @return \Illuminate\Support\Collection |
||
130 | */ |
||
131 | public function toCollection():Collection |
||
135 | |||
136 | /** |
||
137 | * Convert the response to JSON. |
||
138 | * |
||
139 | * @param int $options |
||
140 | * @return string |
||
141 | */ |
||
142 | public function toJson($options = 0) |
||
146 | |||
147 | /** |
||
148 | * Convert the object into something JSON serializable. |
||
149 | * |
||
150 | * @return array |
||
151 | */ |
||
152 | public function jsonSerialize() |
||
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 |
||
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 |
||
193 | } |