@@ -239,7 +239,7 @@ discard block |
||
239 | 239 | $data = (new Converter())->convert($data); |
240 | 240 | if ($data !== null && !is_object($data)) { |
241 | 241 | // ensure we get object in final JSON structure in data node |
242 | - $data = (object)$data; |
|
242 | + $data = (object) $data; |
|
243 | 243 | } |
244 | 244 | |
245 | 245 | // get human readable message for API code or use message string (if given instead of API code) |
@@ -261,7 +261,7 @@ discard block |
||
261 | 261 | |
262 | 262 | if ($debug_data !== null) { |
263 | 263 | $debug_key = Config::get(ResponseBuilder::CONF_KEY_DEBUG_DEBUG_KEY, ResponseBuilder::KEY_DEBUG); |
264 | - $response[ $debug_key ] = $debug_data; |
|
264 | + $response[$debug_key] = $debug_data; |
|
265 | 265 | } |
266 | 266 | |
267 | 267 | return $response; |
@@ -24,390 +24,390 @@ |
||
24 | 24 | */ |
25 | 25 | class ResponseBuilder extends ResponseBuilderBase |
26 | 26 | { |
27 | - /** @var bool */ |
|
28 | - protected $success = false; |
|
29 | - |
|
30 | - /** @var int */ |
|
31 | - protected $api_code; |
|
32 | - |
|
33 | - /** @var int|null */ |
|
34 | - protected $http_code = null; |
|
35 | - |
|
36 | - /** @var mixed */ |
|
37 | - protected $data = null; |
|
38 | - |
|
39 | - /** @var string */ |
|
40 | - protected $message = null; |
|
41 | - |
|
42 | - /** @var array */ |
|
43 | - protected $placeholders = []; |
|
44 | - |
|
45 | - /** @var int|null */ |
|
46 | - protected $json_opts = null; |
|
47 | - |
|
48 | - /** @var array */ |
|
49 | - protected $debug_data = []; |
|
50 | - |
|
51 | - /** @var array */ |
|
52 | - protected $http_headers = []; |
|
53 | - |
|
54 | - // ----------------------------------------------------------------------------------------------------------- |
|
55 | - |
|
56 | - /** |
|
57 | - * Private constructor. Use asSuccess() and asError() static methods to obtain instance of Builder. |
|
58 | - * |
|
59 | - * @param bool $success |
|
60 | - * @param int $api_code |
|
61 | - */ |
|
62 | - protected function __construct(bool $success, int $api_code) |
|
63 | - { |
|
64 | - $this->success = $success; |
|
65 | - $this->api_code = $api_code; |
|
66 | - } |
|
67 | - |
|
68 | - // ----------------------------------------------------------------------------------------------------------- |
|
69 | - |
|
70 | - /** |
|
71 | - * Returns success |
|
72 | - * |
|
73 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
74 | - * of the JSON response, single supported object or @null if there's no |
|
75 | - * to be returned. |
|
76 | - * @param integer|null $api_code API code to be returned or @null to use value of BaseApiCodes::OK(). |
|
77 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
78 | - * substitution or @null if none. |
|
79 | - * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
80 | - * for default DEFAULT_HTTP_CODE_OK. |
|
81 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
82 | - * options or pass @null to use value from your config (or defaults). |
|
83 | - * |
|
84 | - * @return HttpResponse |
|
85 | - */ |
|
86 | - public static function success($data = null, $api_code = null, array $placeholders = null, |
|
87 | - int $http_code = null, int $json_opts = null): HttpResponse |
|
88 | - { |
|
89 | - return ResponseBuilder::asSuccess($api_code) |
|
90 | - ->withData($data) |
|
91 | - ->withPlaceholders($placeholders) |
|
92 | - ->withHttpCode($http_code) |
|
93 | - ->withJsonOptions($json_opts) |
|
94 | - ->build(); |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * Builds error Response object. Supports optional arguments passed to Lang::get() if associated error |
|
99 | - * message uses placeholders as well as return data payload |
|
100 | - * |
|
101 | - * @param integer $api_code Your API code to be returned with the response object. |
|
102 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
103 | - * substitution or @null if none. |
|
104 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
105 | - * of the JSON response, single supported object or @null if there's no |
|
106 | - * to be returned. |
|
107 | - * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
108 | - * for default DEFAULT_HTTP_CODE_ERROR. |
|
109 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
110 | - * options or pass @null to use value from your config (or defaults). |
|
111 | - * |
|
112 | - * @return HttpResponse |
|
113 | - */ |
|
114 | - public static function error(int $api_code, array $placeholders = null, $data = null, int $http_code = null, |
|
115 | - int $json_opts = null): HttpResponse |
|
116 | - { |
|
117 | - return ResponseBuilder::asError($api_code) |
|
118 | - ->withPlaceholders($placeholders) |
|
119 | - ->withData($data) |
|
120 | - ->withHttpCode($http_code) |
|
121 | - ->withJsonOptions($json_opts) |
|
122 | - ->build(); |
|
123 | - } |
|
124 | - |
|
125 | - // ----------------------------------------------------------------------------------------------------------- |
|
126 | - |
|
127 | - /** |
|
128 | - * @param int|null $api_code |
|
129 | - * |
|
130 | - * @return \MarcinOrlowski\ResponseBuilder\ResponseBuilder |
|
131 | - */ |
|
132 | - public static function asSuccess(int $api_code = null): self |
|
133 | - { |
|
134 | - return new self(true, $api_code ?? BaseApiCodes::OK()); |
|
135 | - } |
|
136 | - |
|
137 | - /** |
|
138 | - * @param int $api_code |
|
139 | - * |
|
140 | - * @return \MarcinOrlowski\ResponseBuilder\ResponseBuilder |
|
141 | - */ |
|
142 | - public static function asError(int $api_code): self |
|
143 | - { |
|
144 | - $code_ok = BaseApiCodes::OK(); |
|
145 | - if ($api_code !== $code_ok) { |
|
146 | - Validator::assertIsIntRange('api_code', $api_code, BaseApiCodes::getMinCode(), BaseApiCodes::getMaxCode()); |
|
147 | - } |
|
148 | - if ($api_code === $code_ok) { |
|
149 | - throw new \InvalidArgumentException( |
|
150 | - "Error response cannot use api_code of value {$code_ok} which is reserved for OK"); |
|
151 | - } |
|
152 | - |
|
153 | - return new self(false, $api_code); |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * @param int|null $http_code |
|
158 | - * |
|
159 | - * @return $this |
|
160 | - */ |
|
161 | - public function withHttpCode(int $http_code = null): self |
|
162 | - { |
|
163 | - Validator::assertIsType('http_code', $http_code, [Validator::TYPE_INTEGER, |
|
164 | - Validator::TYPE_NULL]); |
|
165 | - $this->http_code = $http_code; |
|
166 | - |
|
167 | - return $this; |
|
168 | - } |
|
169 | - |
|
170 | - /** |
|
171 | - * @param null $data |
|
172 | - * |
|
173 | - * @return $this |
|
174 | - */ |
|
175 | - public function withData($data = null): self |
|
176 | - { |
|
177 | - Validator::assertIsType('data', $data, [Validator::TYPE_ARRAY, |
|
178 | - Validator::TYPE_OBJECT, |
|
179 | - Validator::TYPE_NULL]); |
|
180 | - $this->data = $data; |
|
181 | - |
|
182 | - return $this; |
|
183 | - } |
|
184 | - |
|
185 | - /** |
|
186 | - * @param int|null $json_opts |
|
187 | - * |
|
188 | - * @return $this |
|
189 | - */ |
|
190 | - public function withJsonOptions(int $json_opts = null): self |
|
191 | - { |
|
192 | - Validator::assertIsType('json_opts', $json_opts, [Validator::TYPE_INTEGER, |
|
193 | - Validator::TYPE_NULL]); |
|
194 | - $this->json_opts = $json_opts; |
|
195 | - |
|
196 | - return $this; |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * @param array|null $debug_data |
|
201 | - * |
|
202 | - * @return $this |
|
203 | - */ |
|
204 | - public function withDebugData(array $debug_data = null): self |
|
205 | - { |
|
206 | - Validator::assertIsType('$debug_data', $debug_data, [Validator::TYPE_ARRAY, |
|
207 | - Validator::TYPE_NULL]); |
|
208 | - $this->debug_data = $debug_data; |
|
209 | - |
|
210 | - return $this; |
|
211 | - } |
|
212 | - |
|
213 | - /** |
|
214 | - * @param string|null $msg |
|
215 | - * |
|
216 | - * @return $this |
|
217 | - */ |
|
218 | - public function withMessage(string $msg = null): self |
|
219 | - { |
|
220 | - Validator::assertIsType('message', $msg, [Validator::TYPE_STRING, |
|
221 | - Validator::TYPE_NULL]); |
|
222 | - $this->message = $msg; |
|
223 | - |
|
224 | - return $this; |
|
225 | - } |
|
226 | - |
|
227 | - /** |
|
228 | - * @param array|null $placeholders |
|
229 | - * |
|
230 | - * @return $this |
|
231 | - */ |
|
232 | - public function withPlaceholders(array $placeholders = null): self |
|
233 | - { |
|
234 | - $this->placeholders = $placeholders; |
|
235 | - |
|
236 | - return $this; |
|
237 | - } |
|
238 | - |
|
239 | - /** |
|
240 | - * @param array|null $http_headers |
|
241 | - * |
|
242 | - * @return $this |
|
243 | - */ |
|
244 | - public function withHttpHeaders(array $http_headers = null): self |
|
245 | - { |
|
246 | - $this->http_headers = $http_headers ?? []; |
|
247 | - |
|
248 | - return $this; |
|
249 | - } |
|
250 | - |
|
251 | - /** |
|
252 | - * Builds and returns final HttpResponse. It's safe to call build() as many times as needed, as no |
|
253 | - * internal state is changed. It's also safe to alter any parameter set previously and call build() |
|
254 | - * again to get new response object that includes new changes. |
|
255 | - * |
|
256 | - * @return \Symfony\Component\HttpFoundation\Response |
|
257 | - */ |
|
258 | - public function build(): HttpResponse |
|
259 | - { |
|
260 | - $api_code = $this->api_code; |
|
261 | - Validator::assertIsInt('api_code', $api_code); |
|
262 | - |
|
263 | - $msg_or_api_code = $this->message ?? $api_code; |
|
264 | - $http_headers = $this->http_headers ?? []; |
|
265 | - |
|
266 | - if ($this->success) { |
|
267 | - $api_code = $api_code ?? BaseApiCodes::OK(); |
|
268 | - $http_code = $this->http_code ?? ResponseBuilder::DEFAULT_HTTP_CODE_OK; |
|
269 | - |
|
270 | - Validator::assertOkHttpCode($http_code); |
|
271 | - |
|
272 | - $result = $this->make($this->success, $api_code, $msg_or_api_code, $this->data, $http_code, |
|
273 | - $this->placeholders, $http_headers, $this->json_opts); |
|
274 | - } else { |
|
275 | - $http_code = $this->http_code ?? ResponseBuilder::DEFAULT_HTTP_CODE_ERROR; |
|
276 | - |
|
277 | - Validator::assertErrorHttpCode($http_code); |
|
278 | - |
|
279 | - $result = $this->make(false, $api_code, $msg_or_api_code, $this->data, $http_code, $this->placeholders, |
|
280 | - $this->http_headers, $this->json_opts, $this->debug_data); |
|
281 | - } |
|
282 | - |
|
283 | - return $result; |
|
284 | - } |
|
285 | - |
|
286 | - /** |
|
287 | - * @param boolean $success @true if response reports successful operation, @false otherwise. |
|
288 | - * @param integer $api_code Your API code to be returned with the response object. |
|
289 | - * @param string|integer $msg_or_api_code message string or valid API code to get message for |
|
290 | - * @param object|array|null $data optional additional data to be included in response object |
|
291 | - * @param integer|null $http_code HTTP code for the HttpResponse or @null for either DEFAULT_HTTP_CODE_OK |
|
292 | - * or DEFAULT_HTTP_CODE_ERROR depending on the $success. |
|
293 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
294 | - * substitution or @null if none. |
|
295 | - * @param array|null $http_headers Optional HTTP headers to be returned in the response. |
|
296 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
297 | - * options or pass @null to use value from your config (or defaults). |
|
298 | - * @param array|null $debug_data Optional debug data array to be added to returned JSON. |
|
299 | - * |
|
300 | - * @return HttpResponse |
|
301 | - * |
|
302 | - * @throws \InvalidArgumentException If $api_code is neither a string nor valid integer code. |
|
303 | - * @throws \InvalidArgumentException if $data is an object of class that is not configured in "classes" mapping. |
|
304 | - * |
|
305 | - * @noinspection PhpTooManyParametersInspection |
|
306 | - */ |
|
307 | - protected function make(bool $success, int $api_code, $msg_or_api_code, $data = null, |
|
308 | - int $http_code = null, array $placeholders = null, array $http_headers = null, |
|
309 | - int $json_opts = null, array $debug_data = null): HttpResponse |
|
310 | - { |
|
311 | - $http_headers = $http_headers ?? []; |
|
312 | - $http_code = $http_code ?? ($success ? ResponseBuilder::DEFAULT_HTTP_CODE_OK : ResponseBuilder::DEFAULT_HTTP_CODE_ERROR); |
|
313 | - $json_opts = $json_opts ?? Config::get(ResponseBuilder::CONF_KEY_ENCODING_OPTIONS, ResponseBuilder::DEFAULT_ENCODING_OPTIONS); |
|
314 | - |
|
315 | - Validator::assertIsInt('encoding_options', $json_opts); |
|
316 | - |
|
317 | - Validator::assertIsInt('api_code', $api_code); |
|
318 | - if (!BaseApiCodes::isCodeValid($api_code)) { |
|
319 | - Validator::assertIsIntRange('api_code', $api_code, BaseApiCodes::getMinCode(), BaseApiCodes::getMaxCode()); |
|
320 | - } |
|
321 | - |
|
322 | - return Response::json( |
|
323 | - $this->buildResponse($success, $api_code, $msg_or_api_code, $placeholders, $data, $debug_data), |
|
324 | - $http_code, $http_headers, $json_opts); |
|
325 | - } |
|
326 | - |
|
327 | - /** |
|
328 | - * Creates standardised API response array. This is final method called in the whole pipeline before we |
|
329 | - * return final JSON back to client. If you want to manipulate your response, this is the place to do that. |
|
330 | - * If you set APP_DEBUG to true, 'code_hex' field will be additionally added to reported JSON for easier |
|
331 | - * manual debugging. |
|
332 | - * |
|
333 | - * @param boolean $success @true if response reports successful operation, @false otherwise. |
|
334 | - * @param integer $api_code Your API code to be returned with the response object. |
|
335 | - * @param string|integer $msg_or_api_code Message string or valid API code to get message for. |
|
336 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
337 | - * substitution or @null if none. |
|
338 | - * @param object|array|null $data API response data if any |
|
339 | - * @param array|null $debug_data optional debug data array to be added to returned JSON. |
|
340 | - * |
|
341 | - * @return array response ready to be encoded as json and sent back to client |
|
342 | - * |
|
343 | - * @throws \RuntimeException in case of missing or invalid "classes" mapping configuration |
|
344 | - * |
|
345 | - * @noinspection PhpTooManyParametersInspection |
|
346 | - */ |
|
347 | - protected function buildResponse(bool $success, int $api_code, |
|
348 | - $msg_or_api_code, array $placeholders = null, |
|
349 | - $data = null, array $debug_data = null): array |
|
350 | - { |
|
351 | - // ensure $data is either @null, array or object of class with configured mapping. |
|
352 | - $data = (new Converter())->convert($data); |
|
353 | - if ($data !== null && !is_object($data)) { |
|
354 | - // ensure we get object in final JSON structure in data node |
|
355 | - $data = (object)$data; |
|
356 | - } |
|
357 | - |
|
358 | - // get human readable message for API code or use message string (if given instead of API code) |
|
359 | - if (is_int($msg_or_api_code)) { |
|
360 | - $message = $this->getMessageForApiCode($success, $msg_or_api_code, $placeholders); |
|
361 | - } else { |
|
362 | - Validator::assertIsString('message', $msg_or_api_code); |
|
363 | - $message = $msg_or_api_code; |
|
364 | - } |
|
365 | - |
|
366 | - /** @noinspection PhpUndefinedClassInspection */ |
|
367 | - $response = [ |
|
368 | - ResponseBuilder::KEY_SUCCESS => $success, |
|
369 | - ResponseBuilder::KEY_CODE => $api_code, |
|
370 | - ResponseBuilder::KEY_LOCALE => \App::getLocale(), |
|
371 | - ResponseBuilder::KEY_MESSAGE => $message, |
|
372 | - ResponseBuilder::KEY_DATA => $data, |
|
373 | - ]; |
|
374 | - |
|
375 | - if ($debug_data !== null) { |
|
376 | - $debug_key = Config::get(ResponseBuilder::CONF_KEY_DEBUG_DEBUG_KEY, ResponseBuilder::KEY_DEBUG); |
|
377 | - $response[ $debug_key ] = $debug_data; |
|
378 | - } |
|
379 | - |
|
380 | - return $response; |
|
381 | - } |
|
382 | - |
|
383 | - /** |
|
384 | - * If $msg_or_api_code is integer value, returns human readable message associated with that code (with |
|
385 | - * fallback to built-in default string if no api code mapping is set. If $msg_or_api_code is a string, |
|
386 | - * returns it unaltered. |
|
387 | - * |
|
388 | - * @param boolean $success @true if response reports successful operation, @false otherwise. |
|
389 | - * @param integer $api_code Your API code to be returned with the response object. |
|
390 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
391 | - * substitution or @null if none. |
|
392 | - * |
|
393 | - * @return string |
|
394 | - */ |
|
395 | - protected function getMessageForApiCode(bool $success, int $api_code, array $placeholders = null): string |
|
396 | - { |
|
397 | - // We got integer value here not a message string, so we need to check if we have the mapping for |
|
398 | - // this string already configured. |
|
399 | - $key = BaseApiCodes::getCodeMessageKey($api_code); |
|
400 | - if ($key === null) { |
|
401 | - // nope, let's get the default one instead, based of |
|
402 | - $fallback_code = $success ? BaseApiCodes::OK() : BaseApiCodes::NO_ERROR_MESSAGE(); |
|
403 | - $key = BaseApiCodes::getCodeMessageKey($fallback_code); |
|
404 | - } |
|
405 | - |
|
406 | - $placeholders = $placeholders ?? []; |
|
407 | - if (!array_key_exists('api_code', $placeholders)) { |
|
408 | - $placeholders['api_code'] = $api_code; |
|
409 | - } |
|
410 | - |
|
411 | - return \Lang::get($key, $placeholders); |
|
412 | - } |
|
27 | + /** @var bool */ |
|
28 | + protected $success = false; |
|
29 | + |
|
30 | + /** @var int */ |
|
31 | + protected $api_code; |
|
32 | + |
|
33 | + /** @var int|null */ |
|
34 | + protected $http_code = null; |
|
35 | + |
|
36 | + /** @var mixed */ |
|
37 | + protected $data = null; |
|
38 | + |
|
39 | + /** @var string */ |
|
40 | + protected $message = null; |
|
41 | + |
|
42 | + /** @var array */ |
|
43 | + protected $placeholders = []; |
|
44 | + |
|
45 | + /** @var int|null */ |
|
46 | + protected $json_opts = null; |
|
47 | + |
|
48 | + /** @var array */ |
|
49 | + protected $debug_data = []; |
|
50 | + |
|
51 | + /** @var array */ |
|
52 | + protected $http_headers = []; |
|
53 | + |
|
54 | + // ----------------------------------------------------------------------------------------------------------- |
|
55 | + |
|
56 | + /** |
|
57 | + * Private constructor. Use asSuccess() and asError() static methods to obtain instance of Builder. |
|
58 | + * |
|
59 | + * @param bool $success |
|
60 | + * @param int $api_code |
|
61 | + */ |
|
62 | + protected function __construct(bool $success, int $api_code) |
|
63 | + { |
|
64 | + $this->success = $success; |
|
65 | + $this->api_code = $api_code; |
|
66 | + } |
|
67 | + |
|
68 | + // ----------------------------------------------------------------------------------------------------------- |
|
69 | + |
|
70 | + /** |
|
71 | + * Returns success |
|
72 | + * |
|
73 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
74 | + * of the JSON response, single supported object or @null if there's no |
|
75 | + * to be returned. |
|
76 | + * @param integer|null $api_code API code to be returned or @null to use value of BaseApiCodes::OK(). |
|
77 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
78 | + * substitution or @null if none. |
|
79 | + * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
80 | + * for default DEFAULT_HTTP_CODE_OK. |
|
81 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
82 | + * options or pass @null to use value from your config (or defaults). |
|
83 | + * |
|
84 | + * @return HttpResponse |
|
85 | + */ |
|
86 | + public static function success($data = null, $api_code = null, array $placeholders = null, |
|
87 | + int $http_code = null, int $json_opts = null): HttpResponse |
|
88 | + { |
|
89 | + return ResponseBuilder::asSuccess($api_code) |
|
90 | + ->withData($data) |
|
91 | + ->withPlaceholders($placeholders) |
|
92 | + ->withHttpCode($http_code) |
|
93 | + ->withJsonOptions($json_opts) |
|
94 | + ->build(); |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * Builds error Response object. Supports optional arguments passed to Lang::get() if associated error |
|
99 | + * message uses placeholders as well as return data payload |
|
100 | + * |
|
101 | + * @param integer $api_code Your API code to be returned with the response object. |
|
102 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
103 | + * substitution or @null if none. |
|
104 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
105 | + * of the JSON response, single supported object or @null if there's no |
|
106 | + * to be returned. |
|
107 | + * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
108 | + * for default DEFAULT_HTTP_CODE_ERROR. |
|
109 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
110 | + * options or pass @null to use value from your config (or defaults). |
|
111 | + * |
|
112 | + * @return HttpResponse |
|
113 | + */ |
|
114 | + public static function error(int $api_code, array $placeholders = null, $data = null, int $http_code = null, |
|
115 | + int $json_opts = null): HttpResponse |
|
116 | + { |
|
117 | + return ResponseBuilder::asError($api_code) |
|
118 | + ->withPlaceholders($placeholders) |
|
119 | + ->withData($data) |
|
120 | + ->withHttpCode($http_code) |
|
121 | + ->withJsonOptions($json_opts) |
|
122 | + ->build(); |
|
123 | + } |
|
124 | + |
|
125 | + // ----------------------------------------------------------------------------------------------------------- |
|
126 | + |
|
127 | + /** |
|
128 | + * @param int|null $api_code |
|
129 | + * |
|
130 | + * @return \MarcinOrlowski\ResponseBuilder\ResponseBuilder |
|
131 | + */ |
|
132 | + public static function asSuccess(int $api_code = null): self |
|
133 | + { |
|
134 | + return new self(true, $api_code ?? BaseApiCodes::OK()); |
|
135 | + } |
|
136 | + |
|
137 | + /** |
|
138 | + * @param int $api_code |
|
139 | + * |
|
140 | + * @return \MarcinOrlowski\ResponseBuilder\ResponseBuilder |
|
141 | + */ |
|
142 | + public static function asError(int $api_code): self |
|
143 | + { |
|
144 | + $code_ok = BaseApiCodes::OK(); |
|
145 | + if ($api_code !== $code_ok) { |
|
146 | + Validator::assertIsIntRange('api_code', $api_code, BaseApiCodes::getMinCode(), BaseApiCodes::getMaxCode()); |
|
147 | + } |
|
148 | + if ($api_code === $code_ok) { |
|
149 | + throw new \InvalidArgumentException( |
|
150 | + "Error response cannot use api_code of value {$code_ok} which is reserved for OK"); |
|
151 | + } |
|
152 | + |
|
153 | + return new self(false, $api_code); |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * @param int|null $http_code |
|
158 | + * |
|
159 | + * @return $this |
|
160 | + */ |
|
161 | + public function withHttpCode(int $http_code = null): self |
|
162 | + { |
|
163 | + Validator::assertIsType('http_code', $http_code, [Validator::TYPE_INTEGER, |
|
164 | + Validator::TYPE_NULL]); |
|
165 | + $this->http_code = $http_code; |
|
166 | + |
|
167 | + return $this; |
|
168 | + } |
|
169 | + |
|
170 | + /** |
|
171 | + * @param null $data |
|
172 | + * |
|
173 | + * @return $this |
|
174 | + */ |
|
175 | + public function withData($data = null): self |
|
176 | + { |
|
177 | + Validator::assertIsType('data', $data, [Validator::TYPE_ARRAY, |
|
178 | + Validator::TYPE_OBJECT, |
|
179 | + Validator::TYPE_NULL]); |
|
180 | + $this->data = $data; |
|
181 | + |
|
182 | + return $this; |
|
183 | + } |
|
184 | + |
|
185 | + /** |
|
186 | + * @param int|null $json_opts |
|
187 | + * |
|
188 | + * @return $this |
|
189 | + */ |
|
190 | + public function withJsonOptions(int $json_opts = null): self |
|
191 | + { |
|
192 | + Validator::assertIsType('json_opts', $json_opts, [Validator::TYPE_INTEGER, |
|
193 | + Validator::TYPE_NULL]); |
|
194 | + $this->json_opts = $json_opts; |
|
195 | + |
|
196 | + return $this; |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * @param array|null $debug_data |
|
201 | + * |
|
202 | + * @return $this |
|
203 | + */ |
|
204 | + public function withDebugData(array $debug_data = null): self |
|
205 | + { |
|
206 | + Validator::assertIsType('$debug_data', $debug_data, [Validator::TYPE_ARRAY, |
|
207 | + Validator::TYPE_NULL]); |
|
208 | + $this->debug_data = $debug_data; |
|
209 | + |
|
210 | + return $this; |
|
211 | + } |
|
212 | + |
|
213 | + /** |
|
214 | + * @param string|null $msg |
|
215 | + * |
|
216 | + * @return $this |
|
217 | + */ |
|
218 | + public function withMessage(string $msg = null): self |
|
219 | + { |
|
220 | + Validator::assertIsType('message', $msg, [Validator::TYPE_STRING, |
|
221 | + Validator::TYPE_NULL]); |
|
222 | + $this->message = $msg; |
|
223 | + |
|
224 | + return $this; |
|
225 | + } |
|
226 | + |
|
227 | + /** |
|
228 | + * @param array|null $placeholders |
|
229 | + * |
|
230 | + * @return $this |
|
231 | + */ |
|
232 | + public function withPlaceholders(array $placeholders = null): self |
|
233 | + { |
|
234 | + $this->placeholders = $placeholders; |
|
235 | + |
|
236 | + return $this; |
|
237 | + } |
|
238 | + |
|
239 | + /** |
|
240 | + * @param array|null $http_headers |
|
241 | + * |
|
242 | + * @return $this |
|
243 | + */ |
|
244 | + public function withHttpHeaders(array $http_headers = null): self |
|
245 | + { |
|
246 | + $this->http_headers = $http_headers ?? []; |
|
247 | + |
|
248 | + return $this; |
|
249 | + } |
|
250 | + |
|
251 | + /** |
|
252 | + * Builds and returns final HttpResponse. It's safe to call build() as many times as needed, as no |
|
253 | + * internal state is changed. It's also safe to alter any parameter set previously and call build() |
|
254 | + * again to get new response object that includes new changes. |
|
255 | + * |
|
256 | + * @return \Symfony\Component\HttpFoundation\Response |
|
257 | + */ |
|
258 | + public function build(): HttpResponse |
|
259 | + { |
|
260 | + $api_code = $this->api_code; |
|
261 | + Validator::assertIsInt('api_code', $api_code); |
|
262 | + |
|
263 | + $msg_or_api_code = $this->message ?? $api_code; |
|
264 | + $http_headers = $this->http_headers ?? []; |
|
265 | + |
|
266 | + if ($this->success) { |
|
267 | + $api_code = $api_code ?? BaseApiCodes::OK(); |
|
268 | + $http_code = $this->http_code ?? ResponseBuilder::DEFAULT_HTTP_CODE_OK; |
|
269 | + |
|
270 | + Validator::assertOkHttpCode($http_code); |
|
271 | + |
|
272 | + $result = $this->make($this->success, $api_code, $msg_or_api_code, $this->data, $http_code, |
|
273 | + $this->placeholders, $http_headers, $this->json_opts); |
|
274 | + } else { |
|
275 | + $http_code = $this->http_code ?? ResponseBuilder::DEFAULT_HTTP_CODE_ERROR; |
|
276 | + |
|
277 | + Validator::assertErrorHttpCode($http_code); |
|
278 | + |
|
279 | + $result = $this->make(false, $api_code, $msg_or_api_code, $this->data, $http_code, $this->placeholders, |
|
280 | + $this->http_headers, $this->json_opts, $this->debug_data); |
|
281 | + } |
|
282 | + |
|
283 | + return $result; |
|
284 | + } |
|
285 | + |
|
286 | + /** |
|
287 | + * @param boolean $success @true if response reports successful operation, @false otherwise. |
|
288 | + * @param integer $api_code Your API code to be returned with the response object. |
|
289 | + * @param string|integer $msg_or_api_code message string or valid API code to get message for |
|
290 | + * @param object|array|null $data optional additional data to be included in response object |
|
291 | + * @param integer|null $http_code HTTP code for the HttpResponse or @null for either DEFAULT_HTTP_CODE_OK |
|
292 | + * or DEFAULT_HTTP_CODE_ERROR depending on the $success. |
|
293 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
294 | + * substitution or @null if none. |
|
295 | + * @param array|null $http_headers Optional HTTP headers to be returned in the response. |
|
296 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
297 | + * options or pass @null to use value from your config (or defaults). |
|
298 | + * @param array|null $debug_data Optional debug data array to be added to returned JSON. |
|
299 | + * |
|
300 | + * @return HttpResponse |
|
301 | + * |
|
302 | + * @throws \InvalidArgumentException If $api_code is neither a string nor valid integer code. |
|
303 | + * @throws \InvalidArgumentException if $data is an object of class that is not configured in "classes" mapping. |
|
304 | + * |
|
305 | + * @noinspection PhpTooManyParametersInspection |
|
306 | + */ |
|
307 | + protected function make(bool $success, int $api_code, $msg_or_api_code, $data = null, |
|
308 | + int $http_code = null, array $placeholders = null, array $http_headers = null, |
|
309 | + int $json_opts = null, array $debug_data = null): HttpResponse |
|
310 | + { |
|
311 | + $http_headers = $http_headers ?? []; |
|
312 | + $http_code = $http_code ?? ($success ? ResponseBuilder::DEFAULT_HTTP_CODE_OK : ResponseBuilder::DEFAULT_HTTP_CODE_ERROR); |
|
313 | + $json_opts = $json_opts ?? Config::get(ResponseBuilder::CONF_KEY_ENCODING_OPTIONS, ResponseBuilder::DEFAULT_ENCODING_OPTIONS); |
|
314 | + |
|
315 | + Validator::assertIsInt('encoding_options', $json_opts); |
|
316 | + |
|
317 | + Validator::assertIsInt('api_code', $api_code); |
|
318 | + if (!BaseApiCodes::isCodeValid($api_code)) { |
|
319 | + Validator::assertIsIntRange('api_code', $api_code, BaseApiCodes::getMinCode(), BaseApiCodes::getMaxCode()); |
|
320 | + } |
|
321 | + |
|
322 | + return Response::json( |
|
323 | + $this->buildResponse($success, $api_code, $msg_or_api_code, $placeholders, $data, $debug_data), |
|
324 | + $http_code, $http_headers, $json_opts); |
|
325 | + } |
|
326 | + |
|
327 | + /** |
|
328 | + * Creates standardised API response array. This is final method called in the whole pipeline before we |
|
329 | + * return final JSON back to client. If you want to manipulate your response, this is the place to do that. |
|
330 | + * If you set APP_DEBUG to true, 'code_hex' field will be additionally added to reported JSON for easier |
|
331 | + * manual debugging. |
|
332 | + * |
|
333 | + * @param boolean $success @true if response reports successful operation, @false otherwise. |
|
334 | + * @param integer $api_code Your API code to be returned with the response object. |
|
335 | + * @param string|integer $msg_or_api_code Message string or valid API code to get message for. |
|
336 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
337 | + * substitution or @null if none. |
|
338 | + * @param object|array|null $data API response data if any |
|
339 | + * @param array|null $debug_data optional debug data array to be added to returned JSON. |
|
340 | + * |
|
341 | + * @return array response ready to be encoded as json and sent back to client |
|
342 | + * |
|
343 | + * @throws \RuntimeException in case of missing or invalid "classes" mapping configuration |
|
344 | + * |
|
345 | + * @noinspection PhpTooManyParametersInspection |
|
346 | + */ |
|
347 | + protected function buildResponse(bool $success, int $api_code, |
|
348 | + $msg_or_api_code, array $placeholders = null, |
|
349 | + $data = null, array $debug_data = null): array |
|
350 | + { |
|
351 | + // ensure $data is either @null, array or object of class with configured mapping. |
|
352 | + $data = (new Converter())->convert($data); |
|
353 | + if ($data !== null && !is_object($data)) { |
|
354 | + // ensure we get object in final JSON structure in data node |
|
355 | + $data = (object)$data; |
|
356 | + } |
|
357 | + |
|
358 | + // get human readable message for API code or use message string (if given instead of API code) |
|
359 | + if (is_int($msg_or_api_code)) { |
|
360 | + $message = $this->getMessageForApiCode($success, $msg_or_api_code, $placeholders); |
|
361 | + } else { |
|
362 | + Validator::assertIsString('message', $msg_or_api_code); |
|
363 | + $message = $msg_or_api_code; |
|
364 | + } |
|
365 | + |
|
366 | + /** @noinspection PhpUndefinedClassInspection */ |
|
367 | + $response = [ |
|
368 | + ResponseBuilder::KEY_SUCCESS => $success, |
|
369 | + ResponseBuilder::KEY_CODE => $api_code, |
|
370 | + ResponseBuilder::KEY_LOCALE => \App::getLocale(), |
|
371 | + ResponseBuilder::KEY_MESSAGE => $message, |
|
372 | + ResponseBuilder::KEY_DATA => $data, |
|
373 | + ]; |
|
374 | + |
|
375 | + if ($debug_data !== null) { |
|
376 | + $debug_key = Config::get(ResponseBuilder::CONF_KEY_DEBUG_DEBUG_KEY, ResponseBuilder::KEY_DEBUG); |
|
377 | + $response[ $debug_key ] = $debug_data; |
|
378 | + } |
|
379 | + |
|
380 | + return $response; |
|
381 | + } |
|
382 | + |
|
383 | + /** |
|
384 | + * If $msg_or_api_code is integer value, returns human readable message associated with that code (with |
|
385 | + * fallback to built-in default string if no api code mapping is set. If $msg_or_api_code is a string, |
|
386 | + * returns it unaltered. |
|
387 | + * |
|
388 | + * @param boolean $success @true if response reports successful operation, @false otherwise. |
|
389 | + * @param integer $api_code Your API code to be returned with the response object. |
|
390 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
391 | + * substitution or @null if none. |
|
392 | + * |
|
393 | + * @return string |
|
394 | + */ |
|
395 | + protected function getMessageForApiCode(bool $success, int $api_code, array $placeholders = null): string |
|
396 | + { |
|
397 | + // We got integer value here not a message string, so we need to check if we have the mapping for |
|
398 | + // this string already configured. |
|
399 | + $key = BaseApiCodes::getCodeMessageKey($api_code); |
|
400 | + if ($key === null) { |
|
401 | + // nope, let's get the default one instead, based of |
|
402 | + $fallback_code = $success ? BaseApiCodes::OK() : BaseApiCodes::NO_ERROR_MESSAGE(); |
|
403 | + $key = BaseApiCodes::getCodeMessageKey($fallback_code); |
|
404 | + } |
|
405 | + |
|
406 | + $placeholders = $placeholders ?? []; |
|
407 | + if (!array_key_exists('api_code', $placeholders)) { |
|
408 | + $placeholders['api_code'] = $api_code; |
|
409 | + } |
|
410 | + |
|
411 | + return \Lang::get($key, $placeholders); |
|
412 | + } |
|
413 | 413 | } |
@@ -21,194 +21,194 @@ |
||
21 | 21 | */ |
22 | 22 | class BaseApiCodes |
23 | 23 | { |
24 | - use ApiCodesHelpers; |
|
25 | - |
|
26 | - /** |
|
27 | - * protected code range - lowest code for reserved range. |
|
28 | - * |
|
29 | - * @var int |
|
30 | - */ |
|
31 | - public const RESERVED_MIN_API_CODE_OFFSET = 0; |
|
32 | - |
|
33 | - /** |
|
34 | - * protected code range - highest code for reserved range |
|
35 | - * |
|
36 | - * @var int |
|
37 | - */ |
|
38 | - public const RESERVED_MAX_API_CODE_OFFSET = 19; |
|
39 | - |
|
40 | - /** |
|
41 | - * built-in codes: OK |
|
42 | - * |
|
43 | - * @var int |
|
44 | - */ |
|
45 | - protected const OK_OFFSET = 0; |
|
46 | - /** |
|
47 | - * built-in code for fallback message mapping |
|
48 | - * |
|
49 | - * @var int |
|
50 | - */ |
|
51 | - protected const NO_ERROR_MESSAGE_OFFSET = 1; |
|
52 | - /** |
|
53 | - * built-in error code for HTTP_NOT_FOUND exception |
|
54 | - * |
|
55 | - * @var int |
|
56 | - */ |
|
57 | - protected const EX_HTTP_NOT_FOUND_OFFSET = 10; |
|
58 | - /** |
|
59 | - * built-in error code for HTTP_SERVICE_UNAVAILABLE exception |
|
60 | - * |
|
61 | - * @var int |
|
62 | - */ |
|
63 | - protected const EX_HTTP_SERVICE_UNAVAILABLE_OFFSET = 11; |
|
64 | - /** |
|
65 | - * built-in error code for HTTP_EXCEPTION |
|
66 | - * |
|
67 | - * @var int |
|
68 | - */ |
|
69 | - protected const EX_HTTP_EXCEPTION_OFFSET = 12; |
|
70 | - /** |
|
71 | - * built-in error code for UNCAUGHT_EXCEPTION |
|
72 | - * |
|
73 | - * @var int |
|
74 | - */ |
|
75 | - protected const EX_UNCAUGHT_EXCEPTION_OFFSET = 13; |
|
76 | - |
|
77 | - /** |
|
78 | - * built-in error code for \Illuminate\Auth\AuthenticationException |
|
79 | - * |
|
80 | - * @var int |
|
81 | - */ |
|
82 | - protected const EX_AUTHENTICATION_EXCEPTION_OFFSET = 14; |
|
83 | - |
|
84 | - /** |
|
85 | - * built-in error code for \Illuminate\Auth\AuthenticationException |
|
86 | - * |
|
87 | - * @var int |
|
88 | - */ |
|
89 | - protected const EX_VALIDATION_EXCEPTION_OFFSET = 15; |
|
90 | - |
|
91 | - /** |
|
92 | - * Returns base code mapping array |
|
93 | - * |
|
94 | - * @return array |
|
95 | - */ |
|
96 | - protected static function getBaseMap(): array |
|
97 | - { |
|
98 | - $tpl = 'response-builder::builder.http_%d'; |
|
99 | - |
|
100 | - return [ |
|
101 | - /** @scrutinizer ignore-deprecated */ |
|
102 | - self::OK() => 'response-builder::builder.ok', |
|
103 | - /** @scrutinizer ignore-deprecated */ |
|
104 | - self::NO_ERROR_MESSAGE() => 'response-builder::builder.no_error_message', |
|
105 | - /** @scrutinizer ignore-deprecated */ |
|
106 | - self::EX_HTTP_EXCEPTION() => 'response-builder::builder.http_exception', |
|
107 | - /** @scrutinizer ignore-deprecated */ |
|
108 | - self::EX_UNCAUGHT_EXCEPTION() => 'response-builder::builder.uncaught_exception', |
|
109 | - /** @scrutinizer ignore-deprecated */ |
|
110 | - self::EX_HTTP_NOT_FOUND() => sprintf($tpl, HttpResponse::HTTP_NOT_FOUND), |
|
111 | - /** @scrutinizer ignore-deprecated */ |
|
112 | - self::EX_HTTP_SERVICE_UNAVAILABLE() => sprintf($tpl, HttpResponse::HTTP_SERVICE_UNAVAILABLE), |
|
113 | - /** @scrutinizer ignore-deprecated */ |
|
114 | - self::EX_AUTHENTICATION_EXCEPTION() => sprintf($tpl, HttpResponse::HTTP_UNAUTHORIZED), |
|
115 | - /** @scrutinizer ignore-deprecated */ |
|
116 | - self::EX_VALIDATION_EXCEPTION() => sprintf($tpl, HttpResponse::HTTP_UNPROCESSABLE_ENTITY), |
|
117 | - ]; |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * Returns API code for internal code OK |
|
122 | - * |
|
123 | - * @return int valid API code in current range |
|
124 | - * |
|
125 | - * @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8. |
|
126 | - */ |
|
127 | - public static function OK(): int |
|
128 | - { |
|
129 | - return static::getCodeForInternalOffset(static::OK_OFFSET); |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Returns API code for internal code NO_ERROR_MESSAGE |
|
134 | - * |
|
135 | - * @return int valid API code in current range |
|
136 | - */ |
|
137 | - public static function NO_ERROR_MESSAGE(): int |
|
138 | - { |
|
139 | - return static::getCodeForInternalOffset(static::NO_ERROR_MESSAGE_OFFSET); |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * Returns API code for internal code EX_HTTP_NOT_FOUND |
|
144 | - * |
|
145 | - * @return int valid API code in current range |
|
146 | - * |
|
147 | - * @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8. |
|
148 | - */ |
|
149 | - public static function EX_HTTP_NOT_FOUND(): int |
|
150 | - { |
|
151 | - return static::getCodeForInternalOffset(static::EX_HTTP_NOT_FOUND_OFFSET); |
|
152 | - } |
|
153 | - |
|
154 | - /** |
|
155 | - * Returns API code for internal code EX_HTTP_EXCEPTION |
|
156 | - * |
|
157 | - * @return int valid API code in current range |
|
158 | - * |
|
159 | - * @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8. |
|
160 | - */ |
|
161 | - public static function EX_HTTP_EXCEPTION(): int |
|
162 | - { |
|
163 | - return static::getCodeForInternalOffset(static::EX_HTTP_EXCEPTION_OFFSET); |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * Returns API code for internal code EX_UNCAUGHT_EXCEPTION |
|
168 | - * |
|
169 | - * @return int valid API code in current range |
|
170 | - * |
|
171 | - * @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8. |
|
172 | - */ |
|
173 | - public static function EX_UNCAUGHT_EXCEPTION(): int |
|
174 | - { |
|
175 | - return static::getCodeForInternalOffset(static::EX_UNCAUGHT_EXCEPTION_OFFSET); |
|
176 | - } |
|
177 | - |
|
178 | - /** |
|
179 | - * Returns API code for internal code EX_AUTHENTICATION_EXCEPTION |
|
180 | - * |
|
181 | - * @return int valid API code in current range |
|
182 | - * |
|
183 | - * @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8. |
|
184 | - */ |
|
185 | - public static function EX_AUTHENTICATION_EXCEPTION(): int |
|
186 | - { |
|
187 | - return static::getCodeForInternalOffset(static::EX_AUTHENTICATION_EXCEPTION_OFFSET); |
|
188 | - } |
|
189 | - |
|
190 | - /** |
|
191 | - * Returns API code for internal code EX_VALIDATION_EXCEPTION |
|
192 | - * |
|
193 | - * @return int valid API code in current range |
|
194 | - * |
|
195 | - * @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8. |
|
196 | - */ |
|
197 | - public static function EX_VALIDATION_EXCEPTION(): int |
|
198 | - { |
|
199 | - return static::getCodeForInternalOffset(static::EX_VALIDATION_EXCEPTION_OFFSET); |
|
200 | - } |
|
201 | - |
|
202 | - /** |
|
203 | - * Returns API code for internal code EX_HTTP_SERVICE_UNAVAILABLE |
|
204 | - * |
|
205 | - * @return int valid API code in current range |
|
206 | - * |
|
207 | - * @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8. |
|
208 | - */ |
|
209 | - public static function EX_HTTP_SERVICE_UNAVAILABLE(): int |
|
210 | - { |
|
211 | - return static::getCodeForInternalOffset(static::EX_HTTP_SERVICE_UNAVAILABLE_OFFSET); |
|
212 | - } |
|
24 | + use ApiCodesHelpers; |
|
25 | + |
|
26 | + /** |
|
27 | + * protected code range - lowest code for reserved range. |
|
28 | + * |
|
29 | + * @var int |
|
30 | + */ |
|
31 | + public const RESERVED_MIN_API_CODE_OFFSET = 0; |
|
32 | + |
|
33 | + /** |
|
34 | + * protected code range - highest code for reserved range |
|
35 | + * |
|
36 | + * @var int |
|
37 | + */ |
|
38 | + public const RESERVED_MAX_API_CODE_OFFSET = 19; |
|
39 | + |
|
40 | + /** |
|
41 | + * built-in codes: OK |
|
42 | + * |
|
43 | + * @var int |
|
44 | + */ |
|
45 | + protected const OK_OFFSET = 0; |
|
46 | + /** |
|
47 | + * built-in code for fallback message mapping |
|
48 | + * |
|
49 | + * @var int |
|
50 | + */ |
|
51 | + protected const NO_ERROR_MESSAGE_OFFSET = 1; |
|
52 | + /** |
|
53 | + * built-in error code for HTTP_NOT_FOUND exception |
|
54 | + * |
|
55 | + * @var int |
|
56 | + */ |
|
57 | + protected const EX_HTTP_NOT_FOUND_OFFSET = 10; |
|
58 | + /** |
|
59 | + * built-in error code for HTTP_SERVICE_UNAVAILABLE exception |
|
60 | + * |
|
61 | + * @var int |
|
62 | + */ |
|
63 | + protected const EX_HTTP_SERVICE_UNAVAILABLE_OFFSET = 11; |
|
64 | + /** |
|
65 | + * built-in error code for HTTP_EXCEPTION |
|
66 | + * |
|
67 | + * @var int |
|
68 | + */ |
|
69 | + protected const EX_HTTP_EXCEPTION_OFFSET = 12; |
|
70 | + /** |
|
71 | + * built-in error code for UNCAUGHT_EXCEPTION |
|
72 | + * |
|
73 | + * @var int |
|
74 | + */ |
|
75 | + protected const EX_UNCAUGHT_EXCEPTION_OFFSET = 13; |
|
76 | + |
|
77 | + /** |
|
78 | + * built-in error code for \Illuminate\Auth\AuthenticationException |
|
79 | + * |
|
80 | + * @var int |
|
81 | + */ |
|
82 | + protected const EX_AUTHENTICATION_EXCEPTION_OFFSET = 14; |
|
83 | + |
|
84 | + /** |
|
85 | + * built-in error code for \Illuminate\Auth\AuthenticationException |
|
86 | + * |
|
87 | + * @var int |
|
88 | + */ |
|
89 | + protected const EX_VALIDATION_EXCEPTION_OFFSET = 15; |
|
90 | + |
|
91 | + /** |
|
92 | + * Returns base code mapping array |
|
93 | + * |
|
94 | + * @return array |
|
95 | + */ |
|
96 | + protected static function getBaseMap(): array |
|
97 | + { |
|
98 | + $tpl = 'response-builder::builder.http_%d'; |
|
99 | + |
|
100 | + return [ |
|
101 | + /** @scrutinizer ignore-deprecated */ |
|
102 | + self::OK() => 'response-builder::builder.ok', |
|
103 | + /** @scrutinizer ignore-deprecated */ |
|
104 | + self::NO_ERROR_MESSAGE() => 'response-builder::builder.no_error_message', |
|
105 | + /** @scrutinizer ignore-deprecated */ |
|
106 | + self::EX_HTTP_EXCEPTION() => 'response-builder::builder.http_exception', |
|
107 | + /** @scrutinizer ignore-deprecated */ |
|
108 | + self::EX_UNCAUGHT_EXCEPTION() => 'response-builder::builder.uncaught_exception', |
|
109 | + /** @scrutinizer ignore-deprecated */ |
|
110 | + self::EX_HTTP_NOT_FOUND() => sprintf($tpl, HttpResponse::HTTP_NOT_FOUND), |
|
111 | + /** @scrutinizer ignore-deprecated */ |
|
112 | + self::EX_HTTP_SERVICE_UNAVAILABLE() => sprintf($tpl, HttpResponse::HTTP_SERVICE_UNAVAILABLE), |
|
113 | + /** @scrutinizer ignore-deprecated */ |
|
114 | + self::EX_AUTHENTICATION_EXCEPTION() => sprintf($tpl, HttpResponse::HTTP_UNAUTHORIZED), |
|
115 | + /** @scrutinizer ignore-deprecated */ |
|
116 | + self::EX_VALIDATION_EXCEPTION() => sprintf($tpl, HttpResponse::HTTP_UNPROCESSABLE_ENTITY), |
|
117 | + ]; |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * Returns API code for internal code OK |
|
122 | + * |
|
123 | + * @return int valid API code in current range |
|
124 | + * |
|
125 | + * @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8. |
|
126 | + */ |
|
127 | + public static function OK(): int |
|
128 | + { |
|
129 | + return static::getCodeForInternalOffset(static::OK_OFFSET); |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Returns API code for internal code NO_ERROR_MESSAGE |
|
134 | + * |
|
135 | + * @return int valid API code in current range |
|
136 | + */ |
|
137 | + public static function NO_ERROR_MESSAGE(): int |
|
138 | + { |
|
139 | + return static::getCodeForInternalOffset(static::NO_ERROR_MESSAGE_OFFSET); |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * Returns API code for internal code EX_HTTP_NOT_FOUND |
|
144 | + * |
|
145 | + * @return int valid API code in current range |
|
146 | + * |
|
147 | + * @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8. |
|
148 | + */ |
|
149 | + public static function EX_HTTP_NOT_FOUND(): int |
|
150 | + { |
|
151 | + return static::getCodeForInternalOffset(static::EX_HTTP_NOT_FOUND_OFFSET); |
|
152 | + } |
|
153 | + |
|
154 | + /** |
|
155 | + * Returns API code for internal code EX_HTTP_EXCEPTION |
|
156 | + * |
|
157 | + * @return int valid API code in current range |
|
158 | + * |
|
159 | + * @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8. |
|
160 | + */ |
|
161 | + public static function EX_HTTP_EXCEPTION(): int |
|
162 | + { |
|
163 | + return static::getCodeForInternalOffset(static::EX_HTTP_EXCEPTION_OFFSET); |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * Returns API code for internal code EX_UNCAUGHT_EXCEPTION |
|
168 | + * |
|
169 | + * @return int valid API code in current range |
|
170 | + * |
|
171 | + * @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8. |
|
172 | + */ |
|
173 | + public static function EX_UNCAUGHT_EXCEPTION(): int |
|
174 | + { |
|
175 | + return static::getCodeForInternalOffset(static::EX_UNCAUGHT_EXCEPTION_OFFSET); |
|
176 | + } |
|
177 | + |
|
178 | + /** |
|
179 | + * Returns API code for internal code EX_AUTHENTICATION_EXCEPTION |
|
180 | + * |
|
181 | + * @return int valid API code in current range |
|
182 | + * |
|
183 | + * @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8. |
|
184 | + */ |
|
185 | + public static function EX_AUTHENTICATION_EXCEPTION(): int |
|
186 | + { |
|
187 | + return static::getCodeForInternalOffset(static::EX_AUTHENTICATION_EXCEPTION_OFFSET); |
|
188 | + } |
|
189 | + |
|
190 | + /** |
|
191 | + * Returns API code for internal code EX_VALIDATION_EXCEPTION |
|
192 | + * |
|
193 | + * @return int valid API code in current range |
|
194 | + * |
|
195 | + * @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8. |
|
196 | + */ |
|
197 | + public static function EX_VALIDATION_EXCEPTION(): int |
|
198 | + { |
|
199 | + return static::getCodeForInternalOffset(static::EX_VALIDATION_EXCEPTION_OFFSET); |
|
200 | + } |
|
201 | + |
|
202 | + /** |
|
203 | + * Returns API code for internal code EX_HTTP_SERVICE_UNAVAILABLE |
|
204 | + * |
|
205 | + * @return int valid API code in current range |
|
206 | + * |
|
207 | + * @deprecated Configure Exception Handler to use your own API code. This method will be removed in v8. |
|
208 | + */ |
|
209 | + public static function EX_HTTP_SERVICE_UNAVAILABLE(): int |
|
210 | + { |
|
211 | + return static::getCodeForInternalOffset(static::EX_HTTP_SERVICE_UNAVAILABLE_OFFSET); |
|
212 | + } |
|
213 | 213 | |
214 | 214 | } |
@@ -27,228 +27,228 @@ |
||
27 | 27 | */ |
28 | 28 | class ExceptionHandlerHelper |
29 | 29 | { |
30 | - /** |
|
31 | - * Render an exception into valid API response. |
|
32 | - * |
|
33 | - * @param \Illuminate\Http\Request $request Request object |
|
34 | - * @param \Exception $ex Exception |
|
35 | - * |
|
36 | - * @return HttpResponse |
|
37 | - */ |
|
38 | - public static function render(/** @scrutinizer ignore-unused */ $request, Exception $ex): HttpResponse |
|
39 | - { |
|
40 | - $result = null; |
|
41 | - $cfg = static::getExceptionHandlerConfig()['map']; |
|
42 | - |
|
43 | - if ($ex instanceof HttpException) { |
|
44 | - // Check if we have any exception configuration for this particular HTTP status code. |
|
45 | - // This confing entry is guaranted to exist (at least 'default'). Enforced by tests. |
|
46 | - $http_code = $ex->getStatusCode(); |
|
47 | - $ex_cfg = $cfg[ HttpException::class ][ $http_code ] ?? null; |
|
48 | - $ex_cfg = $ex_cfg ?? $cfg[ HttpException::class ]['default']; |
|
49 | - $result = self::processException($ex, /** @scrutinizer ignore-type */ $ex_cfg, $http_code); |
|
50 | - } elseif ($ex instanceof ValidationException) { |
|
51 | - // This entry is guaranted to exist. Enforced by tests. |
|
52 | - $http_code = HttpResponse::HTTP_UNPROCESSABLE_ENTITY; |
|
53 | - $result = self::processException($ex, $cfg[ HttpException::class ][ $http_code ], $http_code); |
|
54 | - } |
|
55 | - |
|
56 | - if ($result === null) { |
|
57 | - // This entry is guaranted to exist. Enforced by tests. |
|
58 | - $result = self::processException($ex, $cfg['default'], HttpResponse::HTTP_INTERNAL_SERVER_ERROR); |
|
59 | - } |
|
60 | - |
|
61 | - return $result; |
|
62 | - } |
|
63 | - |
|
64 | - /** |
|
65 | - * Handles given exception and produces valid HTTP response object. |
|
66 | - * |
|
67 | - * @param \Exception $ex Exception to be handled. |
|
68 | - * @param array $ex_cfg ExceptionHandler's config excerpt related to $ex exception type. |
|
69 | - * @param int $fallback_http_code HTTP code to be assigned to produced $ex related response in |
|
70 | - * case configuration array lacks own `http_code` value. |
|
71 | - * |
|
72 | - * @return \Symfony\Component\HttpFoundation\Response |
|
73 | - */ |
|
74 | - protected static function processException(\Exception $ex, array $ex_cfg, int $fallback_http_code) |
|
75 | - { |
|
76 | - $api_code = $ex_cfg['api_code']; |
|
77 | - $http_code = $ex_cfg['http_code'] ?? $fallback_http_code; |
|
78 | - $msg_key = $ex_cfg['msg_key'] ?? null; |
|
79 | - $msg_enforce = $ex_cfg['msg_enforce'] ?? false; |
|
80 | - |
|
81 | - // No message key, let's get exception message and if there's nothing useful, fallback to built-in one. |
|
82 | - $msg = $ex->getMessage(); |
|
83 | - $placeholders = [ |
|
84 | - 'api_code' => $api_code, |
|
85 | - 'message' => ($msg !== '') ? $msg : '???', |
|
86 | - ]; |
|
87 | - |
|
88 | - // shall we enforce error message? |
|
89 | - if ($msg_enforce) { |
|
90 | - // yes, please. |
|
91 | - if ($msg_key === null) { |
|
92 | - // there's no msg_key configured for this exact code, so let's obtain our default message |
|
93 | - $msg = ($msg_key === null) ? static::getErrorMessageForException($ex, $http_code, $placeholders) |
|
94 | - : Lang::get($msg_key, $placeholders); |
|
95 | - } |
|
96 | - } else { |
|
97 | - // nothing enforced, handling pipeline: ex_message -> user_defined_msg -> http_ex -> default |
|
98 | - if ($msg === '') { |
|
99 | - $msg = ($msg_key === null) ? static::getErrorMessageForException($ex, $http_code, $placeholders) |
|
100 | - : Lang::get($msg_key, $placeholders); |
|
101 | - } |
|
102 | - } |
|
103 | - |
|
104 | - // Lets' try to build the error response with what we have now |
|
105 | - return static::error($ex, $api_code, $http_code, $msg); |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * Returns error message for given exception. If exception message is empty, then falls back to |
|
110 | - * `default` handler either for HttpException (if $ex is instance of it), or generic `default` |
|
111 | - * config. |
|
112 | - * |
|
113 | - * @param \Exception $ex |
|
114 | - * @param int $http_code |
|
115 | - * @param array $placeholders |
|
116 | - * |
|
117 | - * @return string |
|
118 | - */ |
|
119 | - protected static function getErrorMessageForException(\Exception $ex, int $http_code, array $placeholders): string |
|
120 | - { |
|
121 | - // exception message is uselss, lets go deeper |
|
122 | - if ($ex instanceof HttpException) { |
|
123 | - $error_message = Lang::get("response-builder::builder.http_{$http_code}", $placeholders); |
|
124 | - } else { |
|
125 | - // Still got nothing? Fall back to built-in generic message for this type of exception. |
|
126 | - $key = BaseApiCodes::getCodeMessageKey(($ex instanceof HttpException) |
|
127 | - ? /** @scrutinizer ignore-deprecated */ BaseApiCodes::EX_HTTP_EXCEPTION() |
|
128 | - : /** @scrutinizer ignore-deprecated */ BaseApiCodes::NO_ERROR_MESSAGE()); |
|
129 | - $error_message = Lang::get($key, $placeholders); |
|
130 | - } |
|
131 | - |
|
132 | - return $error_message; |
|
133 | - } |
|
134 | - |
|
135 | - /** |
|
136 | - * Convert an authentication exception into an unauthenticated response. |
|
137 | - * |
|
138 | - * @param \Illuminate\Http\Request $request |
|
139 | - * @param \Illuminate\Auth\AuthenticationException $exception |
|
140 | - * |
|
141 | - * @return HttpResponse |
|
142 | - */ |
|
143 | - protected function unauthenticated(/** @scrutinizer ignore-unused */ $request, |
|
144 | - AuthException $exception): HttpResponse |
|
145 | - { |
|
146 | - // This entry is guaranted to exist. Enforced by tests. |
|
147 | - $http_code = HttpResponse::HTTP_UNAUTHORIZED; |
|
148 | - $cfg = static::getExceptionHandlerConfig()['map'][ HttpException::class ][ $http_code ]; |
|
149 | - |
|
150 | - return static::processException($exception, $cfg, $http_code); |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * Process single error and produce valid API response. |
|
155 | - * |
|
156 | - * @param Exception $ex Exception to be handled. |
|
157 | - * @param integer $api_code |
|
158 | - * @param integer $http_code |
|
159 | - * |
|
160 | - * @return HttpResponse |
|
161 | - */ |
|
162 | - protected static function error(Exception $ex, |
|
163 | - int $api_code, int $http_code = null, string $error_message): HttpResponse |
|
164 | - { |
|
165 | - $ex_http_code = ($ex instanceof HttpException) ? $ex->getStatusCode() : $ex->getCode(); |
|
166 | - $http_code = $http_code ?? $ex_http_code; |
|
167 | - |
|
168 | - // Check if we now have valid HTTP error code for this case or need to make one up. |
|
169 | - // We cannot throw any exception if codes are invalid because we are in Exception Handler already. |
|
170 | - if ($http_code < ResponseBuilder::ERROR_HTTP_CODE_MIN) { |
|
171 | - // Not a valid code, let's try to get the exception status. |
|
172 | - $http_code = $ex_http_code; |
|
173 | - } |
|
174 | - // Can it be considered a valid HTTP error code? |
|
175 | - if ($http_code < ResponseBuilder::ERROR_HTTP_CODE_MIN) { |
|
176 | - // We now handle uncaught exception, so we cannot throw another one if there's |
|
177 | - // something wrong with the configuration, so we try to recover and use built-in |
|
178 | - // codes instead. |
|
179 | - // FIXME: We should log this event as (warning or error?) |
|
180 | - $http_code = ResponseBuilder::DEFAULT_HTTP_CODE_ERROR; |
|
181 | - } |
|
182 | - |
|
183 | - // If we have trace data debugging enabled, let's gather some debug info and add to the response. |
|
184 | - $debug_data = null; |
|
185 | - if (Config::get(ResponseBuilder::CONF_KEY_DEBUG_EX_TRACE_ENABLED, false)) { |
|
186 | - $debug_data = [ |
|
187 | - Config::get(ResponseBuilder::CONF_KEY_DEBUG_EX_TRACE_KEY, ResponseBuilder::KEY_TRACE) => [ |
|
188 | - ResponseBuilder::KEY_CLASS => get_class($ex), |
|
189 | - ResponseBuilder::KEY_FILE => $ex->getFile(), |
|
190 | - ResponseBuilder::KEY_LINE => $ex->getLine(), |
|
191 | - ], |
|
192 | - ]; |
|
193 | - } |
|
194 | - |
|
195 | - // If this is ValidationException, add all the messages from MessageBag to the data node. |
|
196 | - $data = null; |
|
197 | - if ($ex instanceof ValidationException) { |
|
198 | - /** @var ValidationException $ex */ |
|
199 | - $data = [ResponseBuilder::KEY_MESSAGES => $ex->validator->errors()->messages()]; |
|
200 | - } |
|
201 | - |
|
202 | - return ResponseBuilder::asError($api_code) |
|
203 | - ->withMessage($error_message) |
|
204 | - ->withHttpCode($http_code) |
|
205 | - ->withData($data) |
|
206 | - ->withDebugData($debug_data) |
|
207 | - ->build(); |
|
208 | - } |
|
209 | - |
|
210 | - /** |
|
211 | - * Returns default (built-in) exception handler config array. |
|
212 | - * |
|
213 | - * @return array |
|
214 | - */ |
|
215 | - protected static function getExceptionHandlerDefaultConfig(): array |
|
216 | - { |
|
217 | - return [ |
|
218 | - 'map' => [ |
|
219 | - HttpException::class => [ |
|
220 | - // used by unauthenticated() to obtain api and http code for the exception |
|
221 | - HttpResponse::HTTP_UNAUTHORIZED => [ |
|
222 | - 'api_code' => /** @scrutinizer ignore-deprecated */ BaseApiCodes::EX_AUTHENTICATION_EXCEPTION(), |
|
223 | - ], |
|
224 | - // Required by ValidationException handler |
|
225 | - HttpResponse::HTTP_UNPROCESSABLE_ENTITY => [ |
|
226 | - 'api_code' => /** @scrutinizer ignore-deprecated */ BaseApiCodes::EX_VALIDATION_EXCEPTION(), |
|
227 | - ], |
|
228 | - // default handler is mandatory. `default` entry MUST have both `api_code` and `http_code` set. |
|
229 | - 'default' => [ |
|
230 | - 'api_code' => /** @scrutinizer ignore-deprecated */ BaseApiCodes::EX_HTTP_EXCEPTION(), |
|
231 | - 'http_code' => HttpResponse::HTTP_BAD_REQUEST, |
|
232 | - ], |
|
233 | - ], |
|
234 | - // default handler is mandatory. `default` entry MUST have both `api_code` and `http_code` set. |
|
235 | - 'default' => [ |
|
236 | - 'api_code' => /** @scrutinizer ignore-deprecated */ BaseApiCodes::EX_UNCAUGHT_EXCEPTION(), |
|
237 | - 'http_code' => HttpResponse::HTTP_INTERNAL_SERVER_ERROR, |
|
238 | - ], |
|
239 | - ], |
|
240 | - ]; |
|
241 | - } |
|
242 | - |
|
243 | - /** |
|
244 | - * Returns ExceptionHandlerHelper configration array with user configuration merged into built-in defaults. |
|
245 | - * |
|
246 | - * @return array |
|
247 | - */ |
|
248 | - protected static function getExceptionHandlerConfig(): array |
|
249 | - { |
|
250 | - return Util::mergeConfig(static::getExceptionHandlerDefaultConfig(), |
|
251 | - \Config::get(ResponseBuilder::CONF_KEY_EXCEPTION_HANDLER, [])); |
|
252 | - } |
|
30 | + /** |
|
31 | + * Render an exception into valid API response. |
|
32 | + * |
|
33 | + * @param \Illuminate\Http\Request $request Request object |
|
34 | + * @param \Exception $ex Exception |
|
35 | + * |
|
36 | + * @return HttpResponse |
|
37 | + */ |
|
38 | + public static function render(/** @scrutinizer ignore-unused */ $request, Exception $ex): HttpResponse |
|
39 | + { |
|
40 | + $result = null; |
|
41 | + $cfg = static::getExceptionHandlerConfig()['map']; |
|
42 | + |
|
43 | + if ($ex instanceof HttpException) { |
|
44 | + // Check if we have any exception configuration for this particular HTTP status code. |
|
45 | + // This confing entry is guaranted to exist (at least 'default'). Enforced by tests. |
|
46 | + $http_code = $ex->getStatusCode(); |
|
47 | + $ex_cfg = $cfg[ HttpException::class ][ $http_code ] ?? null; |
|
48 | + $ex_cfg = $ex_cfg ?? $cfg[ HttpException::class ]['default']; |
|
49 | + $result = self::processException($ex, /** @scrutinizer ignore-type */ $ex_cfg, $http_code); |
|
50 | + } elseif ($ex instanceof ValidationException) { |
|
51 | + // This entry is guaranted to exist. Enforced by tests. |
|
52 | + $http_code = HttpResponse::HTTP_UNPROCESSABLE_ENTITY; |
|
53 | + $result = self::processException($ex, $cfg[ HttpException::class ][ $http_code ], $http_code); |
|
54 | + } |
|
55 | + |
|
56 | + if ($result === null) { |
|
57 | + // This entry is guaranted to exist. Enforced by tests. |
|
58 | + $result = self::processException($ex, $cfg['default'], HttpResponse::HTTP_INTERNAL_SERVER_ERROR); |
|
59 | + } |
|
60 | + |
|
61 | + return $result; |
|
62 | + } |
|
63 | + |
|
64 | + /** |
|
65 | + * Handles given exception and produces valid HTTP response object. |
|
66 | + * |
|
67 | + * @param \Exception $ex Exception to be handled. |
|
68 | + * @param array $ex_cfg ExceptionHandler's config excerpt related to $ex exception type. |
|
69 | + * @param int $fallback_http_code HTTP code to be assigned to produced $ex related response in |
|
70 | + * case configuration array lacks own `http_code` value. |
|
71 | + * |
|
72 | + * @return \Symfony\Component\HttpFoundation\Response |
|
73 | + */ |
|
74 | + protected static function processException(\Exception $ex, array $ex_cfg, int $fallback_http_code) |
|
75 | + { |
|
76 | + $api_code = $ex_cfg['api_code']; |
|
77 | + $http_code = $ex_cfg['http_code'] ?? $fallback_http_code; |
|
78 | + $msg_key = $ex_cfg['msg_key'] ?? null; |
|
79 | + $msg_enforce = $ex_cfg['msg_enforce'] ?? false; |
|
80 | + |
|
81 | + // No message key, let's get exception message and if there's nothing useful, fallback to built-in one. |
|
82 | + $msg = $ex->getMessage(); |
|
83 | + $placeholders = [ |
|
84 | + 'api_code' => $api_code, |
|
85 | + 'message' => ($msg !== '') ? $msg : '???', |
|
86 | + ]; |
|
87 | + |
|
88 | + // shall we enforce error message? |
|
89 | + if ($msg_enforce) { |
|
90 | + // yes, please. |
|
91 | + if ($msg_key === null) { |
|
92 | + // there's no msg_key configured for this exact code, so let's obtain our default message |
|
93 | + $msg = ($msg_key === null) ? static::getErrorMessageForException($ex, $http_code, $placeholders) |
|
94 | + : Lang::get($msg_key, $placeholders); |
|
95 | + } |
|
96 | + } else { |
|
97 | + // nothing enforced, handling pipeline: ex_message -> user_defined_msg -> http_ex -> default |
|
98 | + if ($msg === '') { |
|
99 | + $msg = ($msg_key === null) ? static::getErrorMessageForException($ex, $http_code, $placeholders) |
|
100 | + : Lang::get($msg_key, $placeholders); |
|
101 | + } |
|
102 | + } |
|
103 | + |
|
104 | + // Lets' try to build the error response with what we have now |
|
105 | + return static::error($ex, $api_code, $http_code, $msg); |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * Returns error message for given exception. If exception message is empty, then falls back to |
|
110 | + * `default` handler either for HttpException (if $ex is instance of it), or generic `default` |
|
111 | + * config. |
|
112 | + * |
|
113 | + * @param \Exception $ex |
|
114 | + * @param int $http_code |
|
115 | + * @param array $placeholders |
|
116 | + * |
|
117 | + * @return string |
|
118 | + */ |
|
119 | + protected static function getErrorMessageForException(\Exception $ex, int $http_code, array $placeholders): string |
|
120 | + { |
|
121 | + // exception message is uselss, lets go deeper |
|
122 | + if ($ex instanceof HttpException) { |
|
123 | + $error_message = Lang::get("response-builder::builder.http_{$http_code}", $placeholders); |
|
124 | + } else { |
|
125 | + // Still got nothing? Fall back to built-in generic message for this type of exception. |
|
126 | + $key = BaseApiCodes::getCodeMessageKey(($ex instanceof HttpException) |
|
127 | + ? /** @scrutinizer ignore-deprecated */ BaseApiCodes::EX_HTTP_EXCEPTION() |
|
128 | + : /** @scrutinizer ignore-deprecated */ BaseApiCodes::NO_ERROR_MESSAGE()); |
|
129 | + $error_message = Lang::get($key, $placeholders); |
|
130 | + } |
|
131 | + |
|
132 | + return $error_message; |
|
133 | + } |
|
134 | + |
|
135 | + /** |
|
136 | + * Convert an authentication exception into an unauthenticated response. |
|
137 | + * |
|
138 | + * @param \Illuminate\Http\Request $request |
|
139 | + * @param \Illuminate\Auth\AuthenticationException $exception |
|
140 | + * |
|
141 | + * @return HttpResponse |
|
142 | + */ |
|
143 | + protected function unauthenticated(/** @scrutinizer ignore-unused */ $request, |
|
144 | + AuthException $exception): HttpResponse |
|
145 | + { |
|
146 | + // This entry is guaranted to exist. Enforced by tests. |
|
147 | + $http_code = HttpResponse::HTTP_UNAUTHORIZED; |
|
148 | + $cfg = static::getExceptionHandlerConfig()['map'][ HttpException::class ][ $http_code ]; |
|
149 | + |
|
150 | + return static::processException($exception, $cfg, $http_code); |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * Process single error and produce valid API response. |
|
155 | + * |
|
156 | + * @param Exception $ex Exception to be handled. |
|
157 | + * @param integer $api_code |
|
158 | + * @param integer $http_code |
|
159 | + * |
|
160 | + * @return HttpResponse |
|
161 | + */ |
|
162 | + protected static function error(Exception $ex, |
|
163 | + int $api_code, int $http_code = null, string $error_message): HttpResponse |
|
164 | + { |
|
165 | + $ex_http_code = ($ex instanceof HttpException) ? $ex->getStatusCode() : $ex->getCode(); |
|
166 | + $http_code = $http_code ?? $ex_http_code; |
|
167 | + |
|
168 | + // Check if we now have valid HTTP error code for this case or need to make one up. |
|
169 | + // We cannot throw any exception if codes are invalid because we are in Exception Handler already. |
|
170 | + if ($http_code < ResponseBuilder::ERROR_HTTP_CODE_MIN) { |
|
171 | + // Not a valid code, let's try to get the exception status. |
|
172 | + $http_code = $ex_http_code; |
|
173 | + } |
|
174 | + // Can it be considered a valid HTTP error code? |
|
175 | + if ($http_code < ResponseBuilder::ERROR_HTTP_CODE_MIN) { |
|
176 | + // We now handle uncaught exception, so we cannot throw another one if there's |
|
177 | + // something wrong with the configuration, so we try to recover and use built-in |
|
178 | + // codes instead. |
|
179 | + // FIXME: We should log this event as (warning or error?) |
|
180 | + $http_code = ResponseBuilder::DEFAULT_HTTP_CODE_ERROR; |
|
181 | + } |
|
182 | + |
|
183 | + // If we have trace data debugging enabled, let's gather some debug info and add to the response. |
|
184 | + $debug_data = null; |
|
185 | + if (Config::get(ResponseBuilder::CONF_KEY_DEBUG_EX_TRACE_ENABLED, false)) { |
|
186 | + $debug_data = [ |
|
187 | + Config::get(ResponseBuilder::CONF_KEY_DEBUG_EX_TRACE_KEY, ResponseBuilder::KEY_TRACE) => [ |
|
188 | + ResponseBuilder::KEY_CLASS => get_class($ex), |
|
189 | + ResponseBuilder::KEY_FILE => $ex->getFile(), |
|
190 | + ResponseBuilder::KEY_LINE => $ex->getLine(), |
|
191 | + ], |
|
192 | + ]; |
|
193 | + } |
|
194 | + |
|
195 | + // If this is ValidationException, add all the messages from MessageBag to the data node. |
|
196 | + $data = null; |
|
197 | + if ($ex instanceof ValidationException) { |
|
198 | + /** @var ValidationException $ex */ |
|
199 | + $data = [ResponseBuilder::KEY_MESSAGES => $ex->validator->errors()->messages()]; |
|
200 | + } |
|
201 | + |
|
202 | + return ResponseBuilder::asError($api_code) |
|
203 | + ->withMessage($error_message) |
|
204 | + ->withHttpCode($http_code) |
|
205 | + ->withData($data) |
|
206 | + ->withDebugData($debug_data) |
|
207 | + ->build(); |
|
208 | + } |
|
209 | + |
|
210 | + /** |
|
211 | + * Returns default (built-in) exception handler config array. |
|
212 | + * |
|
213 | + * @return array |
|
214 | + */ |
|
215 | + protected static function getExceptionHandlerDefaultConfig(): array |
|
216 | + { |
|
217 | + return [ |
|
218 | + 'map' => [ |
|
219 | + HttpException::class => [ |
|
220 | + // used by unauthenticated() to obtain api and http code for the exception |
|
221 | + HttpResponse::HTTP_UNAUTHORIZED => [ |
|
222 | + 'api_code' => /** @scrutinizer ignore-deprecated */ BaseApiCodes::EX_AUTHENTICATION_EXCEPTION(), |
|
223 | + ], |
|
224 | + // Required by ValidationException handler |
|
225 | + HttpResponse::HTTP_UNPROCESSABLE_ENTITY => [ |
|
226 | + 'api_code' => /** @scrutinizer ignore-deprecated */ BaseApiCodes::EX_VALIDATION_EXCEPTION(), |
|
227 | + ], |
|
228 | + // default handler is mandatory. `default` entry MUST have both `api_code` and `http_code` set. |
|
229 | + 'default' => [ |
|
230 | + 'api_code' => /** @scrutinizer ignore-deprecated */ BaseApiCodes::EX_HTTP_EXCEPTION(), |
|
231 | + 'http_code' => HttpResponse::HTTP_BAD_REQUEST, |
|
232 | + ], |
|
233 | + ], |
|
234 | + // default handler is mandatory. `default` entry MUST have both `api_code` and `http_code` set. |
|
235 | + 'default' => [ |
|
236 | + 'api_code' => /** @scrutinizer ignore-deprecated */ BaseApiCodes::EX_UNCAUGHT_EXCEPTION(), |
|
237 | + 'http_code' => HttpResponse::HTTP_INTERNAL_SERVER_ERROR, |
|
238 | + ], |
|
239 | + ], |
|
240 | + ]; |
|
241 | + } |
|
242 | + |
|
243 | + /** |
|
244 | + * Returns ExceptionHandlerHelper configration array with user configuration merged into built-in defaults. |
|
245 | + * |
|
246 | + * @return array |
|
247 | + */ |
|
248 | + protected static function getExceptionHandlerConfig(): array |
|
249 | + { |
|
250 | + return Util::mergeConfig(static::getExceptionHandlerDefaultConfig(), |
|
251 | + \Config::get(ResponseBuilder::CONF_KEY_EXCEPTION_HANDLER, [])); |
|
252 | + } |
|
253 | 253 | |
254 | 254 | } |
@@ -44,13 +44,13 @@ discard block |
||
44 | 44 | // Check if we have any exception configuration for this particular HTTP status code. |
45 | 45 | // This confing entry is guaranted to exist (at least 'default'). Enforced by tests. |
46 | 46 | $http_code = $ex->getStatusCode(); |
47 | - $ex_cfg = $cfg[ HttpException::class ][ $http_code ] ?? null; |
|
48 | - $ex_cfg = $ex_cfg ?? $cfg[ HttpException::class ]['default']; |
|
47 | + $ex_cfg = $cfg[HttpException::class][$http_code] ?? null; |
|
48 | + $ex_cfg = $ex_cfg ?? $cfg[HttpException::class]['default']; |
|
49 | 49 | $result = self::processException($ex, /** @scrutinizer ignore-type */ $ex_cfg, $http_code); |
50 | 50 | } elseif ($ex instanceof ValidationException) { |
51 | 51 | // This entry is guaranted to exist. Enforced by tests. |
52 | 52 | $http_code = HttpResponse::HTTP_UNPROCESSABLE_ENTITY; |
53 | - $result = self::processException($ex, $cfg[ HttpException::class ][ $http_code ], $http_code); |
|
53 | + $result = self::processException($ex, $cfg[HttpException::class][$http_code], $http_code); |
|
54 | 54 | } |
55 | 55 | |
56 | 56 | if ($result === null) { |
@@ -145,7 +145,7 @@ discard block |
||
145 | 145 | { |
146 | 146 | // This entry is guaranted to exist. Enforced by tests. |
147 | 147 | $http_code = HttpResponse::HTTP_UNAUTHORIZED; |
148 | - $cfg = static::getExceptionHandlerConfig()['map'][ HttpException::class ][ $http_code ]; |
|
148 | + $cfg = static::getExceptionHandlerConfig()['map'][HttpException::class][$http_code]; |
|
149 | 149 | |
150 | 150 | return static::processException($exception, $cfg, $http_code); |
151 | 151 | } |
@@ -24,274 +24,274 @@ |
||
24 | 24 | */ |
25 | 25 | class ResponseBuilderLegacy extends ResponseBuilderBase |
26 | 26 | { |
27 | - /** |
|
28 | - * Returns success |
|
29 | - * |
|
30 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
31 | - * of the JSON response, single supported object or @null if there's no |
|
32 | - * to be returned. |
|
33 | - * @param integer|null $api_code API code to be returned or @null to use value of BaseApiCodes::OK(). |
|
34 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
35 | - * substitution or @null if none. |
|
36 | - * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
37 | - * for default DEFAULT_HTTP_CODE_OK. |
|
38 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
39 | - * options or pass @null to use value from your config (or defaults). |
|
40 | - * |
|
41 | - * @return HttpResponse |
|
42 | - */ |
|
43 | - public static function success($data = null, $api_code = null, array $placeholders = null, |
|
44 | - int $http_code = null, int $json_opts = null): HttpResponse |
|
45 | - { |
|
46 | - return ResponseBuilder::asSuccess($api_code) |
|
47 | - ->withData($data) |
|
48 | - ->withPlaceholders($placeholders) |
|
49 | - ->withHttpCode($http_code) |
|
50 | - ->withJsonOptions($json_opts) |
|
51 | - ->build(); |
|
52 | - } |
|
27 | + /** |
|
28 | + * Returns success |
|
29 | + * |
|
30 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
31 | + * of the JSON response, single supported object or @null if there's no |
|
32 | + * to be returned. |
|
33 | + * @param integer|null $api_code API code to be returned or @null to use value of BaseApiCodes::OK(). |
|
34 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
35 | + * substitution or @null if none. |
|
36 | + * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
37 | + * for default DEFAULT_HTTP_CODE_OK. |
|
38 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
39 | + * options or pass @null to use value from your config (or defaults). |
|
40 | + * |
|
41 | + * @return HttpResponse |
|
42 | + */ |
|
43 | + public static function success($data = null, $api_code = null, array $placeholders = null, |
|
44 | + int $http_code = null, int $json_opts = null): HttpResponse |
|
45 | + { |
|
46 | + return ResponseBuilder::asSuccess($api_code) |
|
47 | + ->withData($data) |
|
48 | + ->withPlaceholders($placeholders) |
|
49 | + ->withHttpCode($http_code) |
|
50 | + ->withJsonOptions($json_opts) |
|
51 | + ->build(); |
|
52 | + } |
|
53 | 53 | |
54 | - /** |
|
55 | - * Returns success |
|
56 | - * |
|
57 | - * @param integer|null $api_code API code to be returned or @null to use value of BaseApiCodes::OK(). |
|
58 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
59 | - * substitution or @null if none. |
|
60 | - * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
61 | - * for default DEFAULT_HTTP_CODE_OK. |
|
62 | - * |
|
63 | - * @return HttpResponse |
|
64 | - * |
|
65 | - * @deprecated Please use Builder class. |
|
66 | - */ |
|
67 | - public static function successWithCode(int $api_code = null, array $placeholders = null, |
|
68 | - int $http_code = null): HttpResponse |
|
69 | - { |
|
70 | - return ResponseBuilder::asSuccess($api_code) |
|
71 | - ->withPlaceholders($placeholders) |
|
72 | - ->withHttpCode($http_code) |
|
73 | - ->build(); |
|
74 | - } |
|
54 | + /** |
|
55 | + * Returns success |
|
56 | + * |
|
57 | + * @param integer|null $api_code API code to be returned or @null to use value of BaseApiCodes::OK(). |
|
58 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
59 | + * substitution or @null if none. |
|
60 | + * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
61 | + * for default DEFAULT_HTTP_CODE_OK. |
|
62 | + * |
|
63 | + * @return HttpResponse |
|
64 | + * |
|
65 | + * @deprecated Please use Builder class. |
|
66 | + */ |
|
67 | + public static function successWithCode(int $api_code = null, array $placeholders = null, |
|
68 | + int $http_code = null): HttpResponse |
|
69 | + { |
|
70 | + return ResponseBuilder::asSuccess($api_code) |
|
71 | + ->withPlaceholders($placeholders) |
|
72 | + ->withHttpCode($http_code) |
|
73 | + ->build(); |
|
74 | + } |
|
75 | 75 | |
76 | - /** |
|
77 | - * @param string $message Custom message to be returned as part of the response. |
|
78 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
79 | - * of the JSON response, single supported object or @null if there's no |
|
80 | - * to be returned. |
|
81 | - * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
82 | - * for default DEFAULT_HTTP_CODE_OK. |
|
83 | - * |
|
84 | - * @return HttpResponse |
|
85 | - * |
|
86 | - * @deprecated Please use Builder class. |
|
87 | - */ |
|
88 | - public static function successWithMessage(string $message, $data = null, int $api_code = null, |
|
89 | - int $http_code = null): HttpResponse |
|
90 | - { |
|
91 | - return ResponseBuilder::asSuccess($api_code) |
|
92 | - ->withMessage($message) |
|
93 | - ->withData($data) |
|
94 | - ->withHttpCode($http_code) |
|
95 | - ->build(); |
|
96 | - } |
|
76 | + /** |
|
77 | + * @param string $message Custom message to be returned as part of the response. |
|
78 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
79 | + * of the JSON response, single supported object or @null if there's no |
|
80 | + * to be returned. |
|
81 | + * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
82 | + * for default DEFAULT_HTTP_CODE_OK. |
|
83 | + * |
|
84 | + * @return HttpResponse |
|
85 | + * |
|
86 | + * @deprecated Please use Builder class. |
|
87 | + */ |
|
88 | + public static function successWithMessage(string $message, $data = null, int $api_code = null, |
|
89 | + int $http_code = null): HttpResponse |
|
90 | + { |
|
91 | + return ResponseBuilder::asSuccess($api_code) |
|
92 | + ->withMessage($message) |
|
93 | + ->withData($data) |
|
94 | + ->withHttpCode($http_code) |
|
95 | + ->build(); |
|
96 | + } |
|
97 | 97 | |
98 | - /** |
|
99 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node. |
|
100 | - * of the JSON response, single supported object or @null if there's no |
|
101 | - * to be returned. |
|
102 | - * @param integer|null $api_code API code to be returned or @null to use value of BaseApiCodes::OK(). |
|
103 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
104 | - * substitution or @null if none. |
|
105 | - * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
106 | - * for default DEFAULT_HTTP_CODE_OK. |
|
107 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
108 | - * options or pass @null to use value from your config (or defaults). |
|
109 | - * |
|
110 | - * @return HttpResponse |
|
111 | - * |
|
112 | - * @deprecated Please use Builder class. |
|
113 | - */ |
|
114 | - public static function successWithHttpCode(int $http_code = null): HttpResponse |
|
115 | - { |
|
116 | - return ResponseBuilder::asSuccess() |
|
117 | - ->withHttpCode($http_code) |
|
118 | - ->build(); |
|
119 | - } |
|
98 | + /** |
|
99 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node. |
|
100 | + * of the JSON response, single supported object or @null if there's no |
|
101 | + * to be returned. |
|
102 | + * @param integer|null $api_code API code to be returned or @null to use value of BaseApiCodes::OK(). |
|
103 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
104 | + * substitution or @null if none. |
|
105 | + * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
106 | + * for default DEFAULT_HTTP_CODE_OK. |
|
107 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
108 | + * options or pass @null to use value from your config (or defaults). |
|
109 | + * |
|
110 | + * @return HttpResponse |
|
111 | + * |
|
112 | + * @deprecated Please use Builder class. |
|
113 | + */ |
|
114 | + public static function successWithHttpCode(int $http_code = null): HttpResponse |
|
115 | + { |
|
116 | + return ResponseBuilder::asSuccess() |
|
117 | + ->withHttpCode($http_code) |
|
118 | + ->build(); |
|
119 | + } |
|
120 | 120 | |
121 | - /** |
|
122 | - * Builds error Response object. Supports optional arguments passed to Lang::get() if associated error |
|
123 | - * message uses placeholders as well as return data payload |
|
124 | - * |
|
125 | - * @param integer $api_code Your API code to be returned with the response object. |
|
126 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
127 | - * substitution or @null if none. |
|
128 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
129 | - * of the JSON response, single supported object or @null if there's no |
|
130 | - * to be returned. |
|
131 | - * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
132 | - * for default DEFAULT_HTTP_CODE_ERROR. |
|
133 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
134 | - * options or pass @null to use value from your config (or defaults). |
|
135 | - * |
|
136 | - * @return HttpResponse |
|
137 | - */ |
|
138 | - public static function error(int $api_code, array $placeholders = null, $data = null, int $http_code = null, |
|
139 | - int $json_opts = null): HttpResponse |
|
140 | - { |
|
141 | - return ResponseBuilder::asError($api_code) |
|
142 | - ->withPlaceholders($placeholders) |
|
143 | - ->withData($data) |
|
144 | - ->withHttpCode($http_code) |
|
145 | - ->withJsonOptions($json_opts) |
|
146 | - ->build(); |
|
147 | - } |
|
121 | + /** |
|
122 | + * Builds error Response object. Supports optional arguments passed to Lang::get() if associated error |
|
123 | + * message uses placeholders as well as return data payload |
|
124 | + * |
|
125 | + * @param integer $api_code Your API code to be returned with the response object. |
|
126 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
127 | + * substitution or @null if none. |
|
128 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
129 | + * of the JSON response, single supported object or @null if there's no |
|
130 | + * to be returned. |
|
131 | + * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
132 | + * for default DEFAULT_HTTP_CODE_ERROR. |
|
133 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
134 | + * options or pass @null to use value from your config (or defaults). |
|
135 | + * |
|
136 | + * @return HttpResponse |
|
137 | + */ |
|
138 | + public static function error(int $api_code, array $placeholders = null, $data = null, int $http_code = null, |
|
139 | + int $json_opts = null): HttpResponse |
|
140 | + { |
|
141 | + return ResponseBuilder::asError($api_code) |
|
142 | + ->withPlaceholders($placeholders) |
|
143 | + ->withData($data) |
|
144 | + ->withHttpCode($http_code) |
|
145 | + ->withJsonOptions($json_opts) |
|
146 | + ->build(); |
|
147 | + } |
|
148 | 148 | |
149 | - /** |
|
150 | - * @param integer $api_code Your API code to be returned with the response object. |
|
151 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
152 | - * of the JSON response, single supported object or @null if there's no |
|
153 | - * to be returned. |
|
154 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
155 | - * substitution or @null if none. |
|
156 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
157 | - * options or pass @null to use value from your config (or defaults). |
|
158 | - * |
|
159 | - * @return HttpResponse |
|
160 | - * |
|
161 | - * @deprecated Please use Builder class. |
|
162 | - */ |
|
163 | - public static function errorWithData(int $api_code, $data, array $placeholders = null, |
|
164 | - int $json_opts = null): HttpResponse |
|
165 | - { |
|
166 | - return ResponseBuilder::asError($api_code) |
|
167 | - ->withData($data) |
|
168 | - ->withPlaceholders($placeholders) |
|
169 | - ->withJsonOptions($json_opts) |
|
170 | - ->build(); |
|
171 | - } |
|
149 | + /** |
|
150 | + * @param integer $api_code Your API code to be returned with the response object. |
|
151 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
152 | + * of the JSON response, single supported object or @null if there's no |
|
153 | + * to be returned. |
|
154 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
155 | + * substitution or @null if none. |
|
156 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
157 | + * options or pass @null to use value from your config (or defaults). |
|
158 | + * |
|
159 | + * @return HttpResponse |
|
160 | + * |
|
161 | + * @deprecated Please use Builder class. |
|
162 | + */ |
|
163 | + public static function errorWithData(int $api_code, $data, array $placeholders = null, |
|
164 | + int $json_opts = null): HttpResponse |
|
165 | + { |
|
166 | + return ResponseBuilder::asError($api_code) |
|
167 | + ->withData($data) |
|
168 | + ->withPlaceholders($placeholders) |
|
169 | + ->withJsonOptions($json_opts) |
|
170 | + ->build(); |
|
171 | + } |
|
172 | 172 | |
173 | - /** |
|
174 | - * @param integer $api_code Your API code to be returned with the response object. |
|
175 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
176 | - * of the JSON response, single supported object or @null if there's no |
|
177 | - * to be returned. |
|
178 | - * @param integer $http_code HTTP code to be used for HttpResponse sent. |
|
179 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
180 | - * substitution or @null if none. |
|
181 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
182 | - * options or pass @null to use value from your config (or defaults). |
|
183 | - * |
|
184 | - * @return HttpResponse |
|
185 | - * |
|
186 | - * @throws \InvalidArgumentException if http_code is @null |
|
187 | - * |
|
188 | - * @deprecated Please use Builder class. |
|
189 | - */ |
|
190 | - public static function errorWithDataAndHttpCode(int $api_code, $data, int $http_code, array $placeholders = null, |
|
191 | - int $json_opts = null): HttpResponse |
|
192 | - { |
|
193 | - return ResponseBuilder::asError($api_code) |
|
194 | - ->withData($data) |
|
195 | - ->withHttpCode($http_code) |
|
196 | - ->withPlaceholders($placeholders) |
|
197 | - ->withJsonOptions($json_opts) |
|
198 | - ->build(); |
|
199 | - } |
|
173 | + /** |
|
174 | + * @param integer $api_code Your API code to be returned with the response object. |
|
175 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
176 | + * of the JSON response, single supported object or @null if there's no |
|
177 | + * to be returned. |
|
178 | + * @param integer $http_code HTTP code to be used for HttpResponse sent. |
|
179 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
180 | + * substitution or @null if none. |
|
181 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
182 | + * options or pass @null to use value from your config (or defaults). |
|
183 | + * |
|
184 | + * @return HttpResponse |
|
185 | + * |
|
186 | + * @throws \InvalidArgumentException if http_code is @null |
|
187 | + * |
|
188 | + * @deprecated Please use Builder class. |
|
189 | + */ |
|
190 | + public static function errorWithDataAndHttpCode(int $api_code, $data, int $http_code, array $placeholders = null, |
|
191 | + int $json_opts = null): HttpResponse |
|
192 | + { |
|
193 | + return ResponseBuilder::asError($api_code) |
|
194 | + ->withData($data) |
|
195 | + ->withHttpCode($http_code) |
|
196 | + ->withPlaceholders($placeholders) |
|
197 | + ->withJsonOptions($json_opts) |
|
198 | + ->build(); |
|
199 | + } |
|
200 | 200 | |
201 | - /** |
|
202 | - * @param integer $api_code Your API code to be returned with the response object. |
|
203 | - * @param integer $http_code HTTP code to be used for HttpResponse sent or @null |
|
204 | - * for default DEFAULT_HTTP_CODE_ERROR. |
|
205 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
206 | - * substitution or @null if none. |
|
207 | - * |
|
208 | - * @return HttpResponse |
|
209 | - * |
|
210 | - * @throws \InvalidArgumentException if http_code is @null |
|
211 | - * |
|
212 | - * @deprecated Please use Builder class. |
|
213 | - */ |
|
214 | - public static function errorWithHttpCode(int $api_code, int $http_code, array $placeholders = null): HttpResponse |
|
215 | - { |
|
216 | - return ResponseBuilder::asError($api_code) |
|
217 | - ->withHttpCode($http_code) |
|
218 | - ->withPlaceholders($placeholders) |
|
219 | - ->build(); |
|
220 | - } |
|
201 | + /** |
|
202 | + * @param integer $api_code Your API code to be returned with the response object. |
|
203 | + * @param integer $http_code HTTP code to be used for HttpResponse sent or @null |
|
204 | + * for default DEFAULT_HTTP_CODE_ERROR. |
|
205 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
206 | + * substitution or @null if none. |
|
207 | + * |
|
208 | + * @return HttpResponse |
|
209 | + * |
|
210 | + * @throws \InvalidArgumentException if http_code is @null |
|
211 | + * |
|
212 | + * @deprecated Please use Builder class. |
|
213 | + */ |
|
214 | + public static function errorWithHttpCode(int $api_code, int $http_code, array $placeholders = null): HttpResponse |
|
215 | + { |
|
216 | + return ResponseBuilder::asError($api_code) |
|
217 | + ->withHttpCode($http_code) |
|
218 | + ->withPlaceholders($placeholders) |
|
219 | + ->build(); |
|
220 | + } |
|
221 | 221 | |
222 | - /** |
|
223 | - * @param integer $api_code Your API code to be returned with the response object. |
|
224 | - * @param string $message Custom message to be returned as part of error response |
|
225 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
226 | - * of the JSON response, single supported object or @null if there's no |
|
227 | - * to be returned. |
|
228 | - * @param integer|null $http_code Optional HTTP status code to be used for HttpResponse sent |
|
229 | - * or @null for DEFAULT_HTTP_CODE_ERROR |
|
230 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
231 | - * options or pass @null to use value from your config (or defaults). |
|
232 | - * |
|
233 | - * @return HttpResponse |
|
234 | - * |
|
235 | - * @deprecated Please use Builder class. |
|
236 | - */ |
|
237 | - public static function errorWithMessageAndData(int $api_code, string $message, $data, |
|
238 | - int $http_code = null, int $json_opts = null): HttpResponse |
|
239 | - { |
|
240 | - return ResponseBuilder::asError($api_code) |
|
241 | - ->withMessage($message) |
|
242 | - ->withData($data) |
|
243 | - ->withHttpCode($http_code) |
|
244 | - ->withJsonOptions($json_opts) |
|
245 | - ->build(); |
|
246 | - } |
|
222 | + /** |
|
223 | + * @param integer $api_code Your API code to be returned with the response object. |
|
224 | + * @param string $message Custom message to be returned as part of error response |
|
225 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
226 | + * of the JSON response, single supported object or @null if there's no |
|
227 | + * to be returned. |
|
228 | + * @param integer|null $http_code Optional HTTP status code to be used for HttpResponse sent |
|
229 | + * or @null for DEFAULT_HTTP_CODE_ERROR |
|
230 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
231 | + * options or pass @null to use value from your config (or defaults). |
|
232 | + * |
|
233 | + * @return HttpResponse |
|
234 | + * |
|
235 | + * @deprecated Please use Builder class. |
|
236 | + */ |
|
237 | + public static function errorWithMessageAndData(int $api_code, string $message, $data, |
|
238 | + int $http_code = null, int $json_opts = null): HttpResponse |
|
239 | + { |
|
240 | + return ResponseBuilder::asError($api_code) |
|
241 | + ->withMessage($message) |
|
242 | + ->withData($data) |
|
243 | + ->withHttpCode($http_code) |
|
244 | + ->withJsonOptions($json_opts) |
|
245 | + ->build(); |
|
246 | + } |
|
247 | 247 | |
248 | - /** |
|
249 | - * @param integer $api_code Your API code to be returned with the response object. |
|
250 | - * @param string $message custom message to be returned as part of error response |
|
251 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
252 | - * of the JSON response, single supported object or @null if there's no |
|
253 | - * to be returned. |
|
254 | - * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
255 | - * for default DEFAULT_HTTP_CODE_ERROR. |
|
256 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
257 | - * options or pass @null to use value from your config (or defaults). |
|
258 | - * @param array|null $debug_data optional debug data array to be added to returned JSON. |
|
259 | - * |
|
260 | - * @return HttpResponse |
|
261 | - * |
|
262 | - * @deprecated Please use Builder class. |
|
263 | - * |
|
264 | - * @noinspection PhpTooManyParametersInspection |
|
265 | - */ |
|
266 | - public static function errorWithMessageAndDataAndDebug(int $api_code, string $message, $data, |
|
267 | - int $http_code = null, int $json_opts = null, |
|
268 | - array $debug_data = null): HttpResponse |
|
269 | - { |
|
270 | - return ResponseBuilder::asError($api_code) |
|
271 | - ->withMessage($message) |
|
272 | - ->withData($data) |
|
273 | - ->withHttpCode($http_code) |
|
274 | - ->withJsonOptions($json_opts) |
|
275 | - ->withDebugData($debug_data) |
|
276 | - ->build(); |
|
277 | - } |
|
248 | + /** |
|
249 | + * @param integer $api_code Your API code to be returned with the response object. |
|
250 | + * @param string $message custom message to be returned as part of error response |
|
251 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
252 | + * of the JSON response, single supported object or @null if there's no |
|
253 | + * to be returned. |
|
254 | + * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
255 | + * for default DEFAULT_HTTP_CODE_ERROR. |
|
256 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
257 | + * options or pass @null to use value from your config (or defaults). |
|
258 | + * @param array|null $debug_data optional debug data array to be added to returned JSON. |
|
259 | + * |
|
260 | + * @return HttpResponse |
|
261 | + * |
|
262 | + * @deprecated Please use Builder class. |
|
263 | + * |
|
264 | + * @noinspection PhpTooManyParametersInspection |
|
265 | + */ |
|
266 | + public static function errorWithMessageAndDataAndDebug(int $api_code, string $message, $data, |
|
267 | + int $http_code = null, int $json_opts = null, |
|
268 | + array $debug_data = null): HttpResponse |
|
269 | + { |
|
270 | + return ResponseBuilder::asError($api_code) |
|
271 | + ->withMessage($message) |
|
272 | + ->withData($data) |
|
273 | + ->withHttpCode($http_code) |
|
274 | + ->withJsonOptions($json_opts) |
|
275 | + ->withDebugData($debug_data) |
|
276 | + ->build(); |
|
277 | + } |
|
278 | 278 | |
279 | - /** |
|
280 | - * @param integer $api_code Your API code to be returned with the response object. |
|
281 | - * @param string $message Custom message to be returned as part of error response |
|
282 | - * @param integer|null $http_code HTTP code to be used with final response sent or @null |
|
283 | - * for default DEFAULT_HTTP_CODE_ERROR. |
|
284 | - * |
|
285 | - * @return HttpResponse |
|
286 | - * |
|
287 | - * @deprecated Please use Builder class. |
|
288 | - */ |
|
289 | - public static function errorWithMessage(int $api_code, string $message, int $http_code = null): HttpResponse |
|
290 | - { |
|
291 | - return ResponseBuilder::asError($api_code) |
|
292 | - ->withMessage($message) |
|
293 | - ->withHttpCode($http_code) |
|
294 | - ->build(); |
|
295 | - } |
|
279 | + /** |
|
280 | + * @param integer $api_code Your API code to be returned with the response object. |
|
281 | + * @param string $message Custom message to be returned as part of error response |
|
282 | + * @param integer|null $http_code HTTP code to be used with final response sent or @null |
|
283 | + * for default DEFAULT_HTTP_CODE_ERROR. |
|
284 | + * |
|
285 | + * @return HttpResponse |
|
286 | + * |
|
287 | + * @deprecated Please use Builder class. |
|
288 | + */ |
|
289 | + public static function errorWithMessage(int $api_code, string $message, int $http_code = null): HttpResponse |
|
290 | + { |
|
291 | + return ResponseBuilder::asError($api_code) |
|
292 | + ->withMessage($message) |
|
293 | + ->withHttpCode($http_code) |
|
294 | + ->build(); |
|
295 | + } |
|
296 | 296 | |
297 | 297 | } |