@@ -505,7 +505,7 @@ discard block |
||
505 | 505 | $data = $converter->convert($data); |
506 | 506 | if ($data !== null && !is_object($data)) { |
507 | 507 | // ensure we get object in final JSON structure in data node |
508 | - $data = (object)$data; |
|
508 | + $data = (object) $data; |
|
509 | 509 | } |
510 | 510 | |
511 | 511 | // get human readable message for API code or use message string (if given instead of API code) |
@@ -527,7 +527,7 @@ discard block |
||
527 | 527 | |
528 | 528 | if ($debug_data !== null) { |
529 | 529 | $debug_key = Config::get(static::CONF_KEY_DEBUG_DEBUG_KEY, self::KEY_DEBUG); |
530 | - $response[ $debug_key ] = $debug_data; |
|
530 | + $response[$debug_key] = $debug_data; |
|
531 | 531 | } |
532 | 532 | |
533 | 533 | return $response; |
@@ -24,499 +24,499 @@ |
||
24 | 24 | */ |
25 | 25 | class ResponseBuilder |
26 | 26 | { |
27 | - /** |
|
28 | - * Default HTTP code to be used with success responses |
|
29 | - * |
|
30 | - * @var int |
|
31 | - */ |
|
32 | - public const DEFAULT_HTTP_CODE_OK = HttpResponse::HTTP_OK; |
|
33 | - |
|
34 | - /** |
|
35 | - * Default HTTP code to be used with error responses |
|
36 | - * |
|
37 | - * @var int |
|
38 | - */ |
|
39 | - public const DEFAULT_HTTP_CODE_ERROR = HttpResponse::HTTP_BAD_REQUEST; |
|
40 | - |
|
41 | - /** |
|
42 | - * Min allowed HTTP code for errorXXX() |
|
43 | - * |
|
44 | - * @var int |
|
45 | - */ |
|
46 | - public const ERROR_HTTP_CODE_MIN = 400; |
|
47 | - |
|
48 | - /** |
|
49 | - * Max allowed HTTP code for errorXXX() |
|
50 | - * |
|
51 | - * @var int |
|
52 | - */ |
|
53 | - public const ERROR_HTTP_CODE_MAX = 599; |
|
54 | - |
|
55 | - /** |
|
56 | - * Configuration keys |
|
57 | - */ |
|
58 | - public const CONF_KEY_DEBUG_DEBUG_KEY = 'response_builder.debug.debug_key'; |
|
59 | - public const CONF_KEY_DEBUG_EX_TRACE_ENABLED = 'response_builder.debug.exception_handler.trace_enabled'; |
|
60 | - public const CONF_KEY_DEBUG_EX_TRACE_KEY = 'response_builder.debug.exception_handler.trace_key'; |
|
61 | - public const CONF_KEY_MAP = 'response_builder.map'; |
|
62 | - public const CONF_KEY_ENCODING_OPTIONS = 'response_builder.encoding_options'; |
|
63 | - public const CONF_KEY_CLASSES = 'response_builder.classes'; |
|
64 | - public const CONF_KEY_MIN_CODE = 'response_builder.min_code'; |
|
65 | - public const CONF_KEY_MAX_CODE = 'response_builder.max_code'; |
|
66 | - public const CONF_EXCEPTION_HANDLER_KEY = 'response_builder.exception_handler'; |
|
67 | - |
|
68 | - /** |
|
69 | - * Default keys to be used by exception handler while adding debug information |
|
70 | - */ |
|
71 | - public const KEY_DEBUG = 'debug'; |
|
72 | - public const KEY_TRACE = 'trace'; |
|
73 | - public const KEY_CLASS = 'class'; |
|
74 | - public const KEY_FILE = 'file'; |
|
75 | - public const KEY_LINE = 'line'; |
|
76 | - public const KEY_KEY = 'key'; |
|
77 | - public const KEY_METHOD = 'method'; |
|
78 | - public const KEY_SUCCESS = 'success'; |
|
79 | - public const KEY_CODE = 'code'; |
|
80 | - public const KEY_LOCALE = 'locale'; |
|
81 | - public const KEY_MESSAGE = 'message'; |
|
82 | - public const KEY_DATA = 'data'; |
|
83 | - |
|
84 | - /** |
|
85 | - * Default key to be used by exception handler while processing ValidationException |
|
86 | - * to return all the error messages |
|
87 | - * |
|
88 | - * @var string |
|
89 | - */ |
|
90 | - public const KEY_MESSAGES = 'messages'; |
|
91 | - |
|
92 | - /** |
|
93 | - * Default JSON encoding options. Must be specified as final value (i.e. 271) and NOT |
|
94 | - * PHP expression i.e. `JSON_HEX_TAG|JSON_HEX_APOS|...` as such syntax is not yet supported |
|
95 | - * by PHP. |
|
96 | - * |
|
97 | - * 271 = JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_AMP|JSON_HEX_QUOT|JSON_UNESCAPED_UNICODE |
|
98 | - * |
|
99 | - * @var int |
|
100 | - */ |
|
101 | - public const DEFAULT_ENCODING_OPTIONS = 271; |
|
102 | - |
|
103 | - |
|
104 | - |
|
105 | - /** |
|
106 | - * Returns success |
|
107 | - * |
|
108 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
109 | - * of the JSON response, single supported object or @null if there's no |
|
110 | - * to be returned. |
|
111 | - * @param integer|null $api_code API code to be returned or @null to use value of BaseApiCodes::OK(). |
|
112 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
113 | - * substitution or @null if none. |
|
114 | - * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
115 | - * for default DEFAULT_HTTP_CODE_OK. |
|
116 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
117 | - * options or pass @null to use value from your config (or defaults). |
|
118 | - * |
|
119 | - * @return HttpResponse |
|
120 | - */ |
|
121 | - public static function success($data = null, $api_code = null, array $placeholders = null, |
|
122 | - int $http_code = null, int $json_opts = null): HttpResponse |
|
123 | - { |
|
124 | - return static::buildSuccessResponse($data, $api_code, $placeholders, $http_code, $json_opts); |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * Returns success |
|
129 | - * |
|
130 | - * @param integer|null $api_code API code to be returned or @null to use value of BaseApiCodes::OK(). |
|
131 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
132 | - * substitution or @null if none. |
|
133 | - * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
134 | - * for default DEFAULT_HTTP_CODE_OK. |
|
135 | - * |
|
136 | - * @return HttpResponse |
|
137 | - */ |
|
138 | - public static function successWithCode(int $api_code = null, array $placeholders = null, |
|
139 | - int $http_code = null): HttpResponse |
|
140 | - { |
|
141 | - return static::success(null, $api_code, $placeholders, $http_code); |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * Returns success with custom HTTP code |
|
146 | - * |
|
147 | - * @param integer|null $http_code HTTP return code to be set for this response. If @null is passed, falls back |
|
148 | - * to DEFAULT_HTTP_CODE_OK. |
|
149 | - * |
|
150 | - * @return HttpResponse |
|
151 | - */ |
|
152 | - public static function successWithHttpCode(int $http_code = null): HttpResponse |
|
153 | - { |
|
154 | - return static::buildSuccessResponse(null, BaseApiCodes::OK(), [], $http_code); |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * @param string $message Custom message to be returned as part of the response. |
|
159 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
160 | - * of the JSON response, single supported object or @null if there's no |
|
161 | - * to be returned. |
|
162 | - * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
163 | - * for default DEFAULT_HTTP_CODE_OK. |
|
164 | - * |
|
165 | - * @return \Symfony\Component\HttpFoundation\Response |
|
166 | - */ |
|
167 | - public static function successWithMessage(string $message, $data = null, int $api_code = null, |
|
168 | - int $http_code = null): HttpResponse |
|
169 | - { |
|
170 | - return static::buildSuccessResponse($data, $api_code, [], $http_code, null, $message); |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node. |
|
175 | - * of the JSON response, single supported object or @null if there's no |
|
176 | - * to be returned. |
|
177 | - * @param integer|null $api_code API code to be returned or @null to use value of BaseApiCodes::OK(). |
|
178 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
179 | - * substitution or @null if none. |
|
180 | - * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
181 | - * for default DEFAULT_HTTP_CODE_OK. |
|
182 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
183 | - * options or pass @null to use value from your config (or defaults). |
|
184 | - * |
|
185 | - * @return HttpResponse |
|
186 | - * |
|
187 | - * @throws \InvalidArgumentException Thrown when provided arguments are invalid. |
|
188 | - */ |
|
189 | - protected static function buildSuccessResponse($data = null, int $api_code = null, array $placeholders = null, |
|
190 | - int $http_code = null, int $json_opts = null, |
|
191 | - $msg_or_api_code = null): HttpResponse |
|
192 | - { |
|
193 | - $http_code = $http_code ?? static::DEFAULT_HTTP_CODE_OK; |
|
194 | - $api_code = $api_code ?? BaseApiCodes::OK(); |
|
195 | - $msg_or_api_code = $msg_or_api_code ?? $api_code; |
|
196 | - |
|
197 | - Validator::assertInt('api_code', $api_code); |
|
198 | - Validator::assertInt('http_code', $http_code); |
|
199 | - Validator::assertIntRange('http_code', $http_code, 200, 299); |
|
200 | - Validator::assertType('msg_or_api_code', $msg_or_api_code, [Validator::TYPE_STRING, |
|
201 | - Validator::TYPE_INTEGER]); |
|
202 | - |
|
203 | - return static::make(true, $api_code, $msg_or_api_code, $data, $http_code, $placeholders, null, $json_opts); |
|
204 | - } |
|
205 | - |
|
206 | - /** |
|
207 | - * Builds error Response object. Supports optional arguments passed to Lang::get() if associated error |
|
208 | - * message uses placeholders as well as return data payload |
|
209 | - * |
|
210 | - * @param integer $api_code Your API code to be returned with the response object. |
|
211 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
212 | - * substitution or @null if none. |
|
213 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
214 | - * of the JSON response, single supported object or @null if there's no |
|
215 | - * to be returned. |
|
216 | - * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
217 | - * for default DEFAULT_HTTP_CODE_ERROR. |
|
218 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
219 | - * options or pass @null to use value from your config (or defaults). |
|
220 | - * |
|
221 | - * @return HttpResponse |
|
222 | - */ |
|
223 | - public static function error(int $api_code, array $placeholders = null, $data = null, int $http_code = null, |
|
224 | - int $encoding_options = null): HttpResponse |
|
225 | - { |
|
226 | - return static::buildErrorResponse($data, $api_code, $http_code, $placeholders, $encoding_options); |
|
227 | - } |
|
228 | - |
|
229 | - /** |
|
230 | - * @param integer $api_code Your API code to be returned with the response object. |
|
231 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
232 | - * of the JSON response, single supported object or @null if there's no |
|
233 | - * to be returned. |
|
234 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
235 | - * substitution or @null if none. |
|
236 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
237 | - * options or pass @null to use value from your config (or defaults). |
|
238 | - * |
|
239 | - * @return HttpResponse |
|
240 | - */ |
|
241 | - public static function errorWithData(int $api_code, $data, array $placeholders = null, |
|
242 | - int $json_opts = null): HttpResponse |
|
243 | - { |
|
244 | - return static::buildErrorResponse($data, $api_code, null, $placeholders, $json_opts); |
|
245 | - } |
|
246 | - |
|
247 | - /** |
|
248 | - * @param integer $api_code Your API code to be returned with the response object. |
|
249 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
250 | - * of the JSON response, single supported object or @null if there's no |
|
251 | - * to be returned. |
|
252 | - * @param integer $http_code HTTP code to be used for HttpResponse sent. |
|
253 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
254 | - * substitution or @null if none. |
|
255 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
256 | - * options or pass @null to use value from your config (or defaults). |
|
257 | - * |
|
258 | - * @return HttpResponse |
|
259 | - * |
|
260 | - * @throws \InvalidArgumentException if http_code is @null |
|
261 | - */ |
|
262 | - public static function errorWithDataAndHttpCode(int $api_code, $data, int $http_code, array $placeholders = null, |
|
263 | - int $json_opts = null): HttpResponse |
|
264 | - { |
|
265 | - return static::buildErrorResponse($data, $api_code, $http_code, $placeholders, $json_opts); |
|
266 | - } |
|
267 | - |
|
268 | - /** |
|
269 | - * @param integer $api_code Your API code to be returned with the response object. |
|
270 | - * @param integer $http_code HTTP code to be used for HttpResponse sent or @null |
|
271 | - * for default DEFAULT_HTTP_CODE_ERROR. |
|
272 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
273 | - * substitution or @null if none. |
|
274 | - * |
|
275 | - * @return HttpResponse |
|
276 | - * |
|
277 | - * @throws \InvalidArgumentException if http_code is @null |
|
278 | - */ |
|
279 | - public static function errorWithHttpCode(int $api_code, int $http_code, array $placeholders = null): HttpResponse |
|
280 | - { |
|
281 | - return static::buildErrorResponse(null, $api_code, $http_code, $placeholders); |
|
282 | - } |
|
283 | - |
|
284 | - /** |
|
285 | - * @param integer $api_code Your API code to be returned with the response object. |
|
286 | - * @param string $message Custom message to be returned as part of error response |
|
287 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
288 | - * of the JSON response, single supported object or @null if there's no |
|
289 | - * to be returned. |
|
290 | - * @param integer|null $http_code Optional HTTP status code to be used for HttpResponse sent |
|
291 | - * or @null for DEFAULT_HTTP_CODE_ERROR |
|
292 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
293 | - * options or pass @null to use value from your config (or defaults). |
|
294 | - * |
|
295 | - * @return HttpResponse |
|
296 | - */ |
|
297 | - public static function errorWithMessageAndData(int $api_code, string $message, $data, |
|
298 | - int $http_code = null, int $json_opts = null): HttpResponse |
|
299 | - { |
|
300 | - return static::buildErrorResponse($data, $api_code, $http_code, null, |
|
301 | - $message, null, $json_opts); |
|
302 | - } |
|
303 | - |
|
304 | - /** |
|
305 | - * @param integer $api_code Your API code to be returned with the response object. |
|
306 | - * @param string $message custom message to be returned as part of error response |
|
307 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
308 | - * of the JSON response, single supported object or @null if there's no |
|
309 | - * to be returned. |
|
310 | - * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
311 | - * for default DEFAULT_HTTP_CODE_ERROR. |
|
312 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
313 | - * options or pass @null to use value from your config (or defaults). |
|
314 | - * @param array|null $debug_data optional debug data array to be added to returned JSON. |
|
315 | - * |
|
316 | - * @return HttpResponse |
|
317 | - */ |
|
318 | - public static function errorWithMessageAndDataAndDebug(int $api_code, string $message, $data, |
|
319 | - int $http_code = null, int $json_opts = null, |
|
320 | - array $debug_data = null): HttpResponse |
|
321 | - { |
|
322 | - return static::buildErrorResponse($data, $api_code, $http_code, null, |
|
323 | - $message, null, $json_opts, $debug_data); |
|
324 | - } |
|
325 | - |
|
326 | - /** |
|
327 | - * @param integer $api_code Your API code to be returned with the response object. |
|
328 | - * @param string $message Custom message to be returned as part of error response |
|
329 | - * @param integer|null $http_code HTTP code to be used with final response sent or @null |
|
330 | - * for default DEFAULT_HTTP_CODE_ERROR. |
|
331 | - * |
|
332 | - * @return HttpResponse |
|
333 | - */ |
|
334 | - public static function errorWithMessage(int $api_code, string $message, int $http_code = null): HttpResponse |
|
335 | - { |
|
336 | - return static::buildErrorResponse(null, $api_code, $http_code, null, $message); |
|
337 | - } |
|
338 | - |
|
339 | - /** |
|
340 | - * Builds error Response object. Supports optional arguments passed to Lang::get() if associated error message |
|
341 | - * uses placeholders as well as return data payload |
|
342 | - * |
|
343 | - * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
344 | - * of the JSON response, single supported object or @null if there's no |
|
345 | - * to be returned. |
|
346 | - * @param integer $api_code Your API code to be returned with the response object. |
|
347 | - * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
348 | - * for default DEFAULT_HTTP_CODE_ERROR. |
|
349 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
350 | - * substitution or @null if none. |
|
351 | - * @param string|null $message custom message to be returned as part of error response |
|
352 | - * @param array|null $headers optional HTTP headers to be returned in error response |
|
353 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
354 | - * options or pass @null to use value from your config (or defaults). |
|
355 | - * @param array|null $debug_data optional debug data array to be added to returned JSON. |
|
356 | - * |
|
357 | - * @return HttpResponse |
|
358 | - * |
|
359 | - * @throws \InvalidArgumentException Thrown if $code is not correct, outside the range, equals OK code etc. |
|
360 | - * |
|
361 | - * @noinspection MoreThanThreeArgumentsInspection |
|
362 | - */ |
|
363 | - protected static function buildErrorResponse($data, int $api_code, int $http_code = null, |
|
364 | - array $placeholders = null, |
|
365 | - string $message = null, array $headers = null, |
|
366 | - int $json_opts = null, |
|
367 | - array $debug_data = null): HttpResponse |
|
368 | - { |
|
369 | - $http_code = $http_code ?? static::DEFAULT_HTTP_CODE_ERROR; |
|
370 | - $headers = $headers ?? []; |
|
371 | - |
|
372 | - Validator::assertInt('api_code', $api_code); |
|
373 | - |
|
374 | - $code_ok = BaseApiCodes::OK(); |
|
375 | - if ($api_code !== $code_ok) { |
|
376 | - Validator::assertIntRange('api_code', $api_code, BaseApiCodes::getMinCode(), BaseApiCodes::getMaxCode()); |
|
377 | - } |
|
378 | - if ($api_code === $code_ok) { |
|
379 | - throw new \InvalidArgumentException( |
|
380 | - "Error response cannot use api_code of value {$code_ok} which is reserved for OK"); |
|
381 | - } |
|
382 | - |
|
383 | - Validator::assertInt('http_code', $http_code); |
|
384 | - Validator::assertIntRange('http_code', $http_code, static::ERROR_HTTP_CODE_MIN, static::ERROR_HTTP_CODE_MAX); |
|
385 | - |
|
386 | - $msg_or_api_code = $message ?? $api_code; |
|
387 | - |
|
388 | - return static::make(false, $api_code, $msg_or_api_code, $data, $http_code, |
|
389 | - $placeholders, $headers, $json_opts, $debug_data); |
|
390 | - } |
|
391 | - |
|
392 | - /** |
|
393 | - * @param boolean $success @true if response reports successful operation, @false otherwise. |
|
394 | - * @param integer $api_code Your API code to be returned with the response object. |
|
395 | - * @param string|integer $msg_or_api_code message string or valid API code to get message for |
|
396 | - * @param object|array|null $data optional additional data to be included in response object |
|
397 | - * @param integer|null $http_code HTTP code for the HttpResponse or @null for either DEFAULT_HTTP_CODE_OK |
|
398 | - * or DEFAULT_HTTP_CODE_ERROR depending on the $success. |
|
399 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
400 | - * substitution or @null if none. |
|
401 | - * @param array|null $headers Optional HTTP headers to be returned in the response. |
|
402 | - * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
403 | - * options or pass @null to use value from your config (or defaults). |
|
404 | - * @param array|null $debug_data Optional debug data array to be added to returned JSON. |
|
405 | - * |
|
406 | - * @return HttpResponse |
|
407 | - * |
|
408 | - * @throws \InvalidArgumentException If $api_code is neither a string nor valid integer code. |
|
409 | - * @throws \InvalidArgumentException if $data is an object of class that is not configured in "classes" mapping. |
|
410 | - * |
|
411 | - * @noinspection MoreThanThreeArgumentsInspection |
|
412 | - */ |
|
413 | - protected static function make(bool $success, int $api_code, $msg_or_api_code, $data = null, |
|
414 | - int $http_code = null, array $placeholders = null, array $headers = null, |
|
415 | - int $json_opts = null, array $debug_data = null): HttpResponse |
|
416 | - { |
|
417 | - $headers = $headers ?? []; |
|
418 | - $http_code = $http_code ?? ($success ? static::DEFAULT_HTTP_CODE_OK : static::DEFAULT_HTTP_CODE_ERROR); |
|
419 | - $json_opts = $json_opts ?? Config::get(self::CONF_KEY_ENCODING_OPTIONS, static::DEFAULT_ENCODING_OPTIONS); |
|
420 | - |
|
421 | - Validator::assertInt('encoding_options', $json_opts); |
|
422 | - |
|
423 | - Validator::assertInt('api_code', $api_code); |
|
424 | - if (!BaseApiCodes::isCodeValid($api_code)) { |
|
425 | - $min = BaseApiCodes::getMinCode(); |
|
426 | - $max = BaseApiCodes::getMaxCode(); |
|
427 | - throw new \InvalidArgumentException("API code value ({$api_code}) is out of allowed range {$min}-{$max}"); |
|
428 | - } |
|
429 | - |
|
430 | - return Response::json( |
|
431 | - static::buildResponse($success, $api_code, $msg_or_api_code, $placeholders, $data, $debug_data), |
|
432 | - $http_code, $headers, $json_opts); |
|
433 | - } |
|
434 | - |
|
435 | - /** |
|
436 | - * If $msg_or_api_code is integer value, returns human readable message associated with that code (with |
|
437 | - * fallback to built-in default string if no api code mapping is set. If $msg_or_api_code is a string, |
|
438 | - * returns it unaltered. |
|
439 | - * |
|
440 | - * @param boolean $success @true if response reports successful operation, @false otherwise. |
|
441 | - * @param integer $api_code Your API code to be returned with the response object. |
|
442 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
443 | - * substitution or @null if none. |
|
444 | - * |
|
445 | - * @return string |
|
446 | - */ |
|
447 | - protected static function getMessageForApiCode(bool $success, int $api_code, array $placeholders = null): string |
|
448 | - { |
|
449 | - // We got integer value here not a message string, so we need to check if we have the mapping for |
|
450 | - // this string already configured. |
|
451 | - $key = BaseApiCodes::getCodeMessageKey($api_code); |
|
452 | - if ($key === null) { |
|
453 | - // nope, let's get the default one instead, based of |
|
454 | - $fallback_code = $success ? BaseApiCodes::OK() : BaseApiCodes::NO_ERROR_MESSAGE(); |
|
455 | - $key = BaseApiCodes::getCodeMessageKey($fallback_code); |
|
456 | - } |
|
457 | - |
|
458 | - $placeholders = $placeholders ?? []; |
|
459 | - if (!array_key_exists('api_code', $placeholders)) { |
|
460 | - $placeholders['api_code'] = $api_code; |
|
461 | - } |
|
462 | - |
|
463 | - return \Lang::get($key, $placeholders); |
|
464 | - } |
|
465 | - |
|
466 | - /** |
|
467 | - * Creates standardised API response array. This is final method called in the whole pipeline before we |
|
468 | - * return final JSON back to client. If you want to manipulate your response, this is the place to do that. |
|
469 | - * If you set APP_DEBUG to true, 'code_hex' field will be additionally added to reported JSON for easier |
|
470 | - * manual debugging. |
|
471 | - * |
|
472 | - * @param boolean $success @true if response reports successful operation, @false otherwise. |
|
473 | - * @param integer $api_code Your API code to be returned with the response object. |
|
474 | - * @param string|integer $msg_or_api_code Message string or valid API code to get message for. |
|
475 | - * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
476 | - * substitution or @null if none. |
|
477 | - * @param object|array|null $data API response data if any |
|
478 | - * @param array|null $debug_data optional debug data array to be added to returned JSON. |
|
479 | - * |
|
480 | - * @return array response ready to be encoded as json and sent back to client |
|
481 | - * |
|
482 | - * @throws \RuntimeException in case of missing or invalid "classes" mapping configuration |
|
483 | - */ |
|
484 | - protected static function buildResponse(bool $success, int $api_code, |
|
485 | - $msg_or_api_code, array $placeholders = null, |
|
486 | - $data = null, array $debug_data = null): array |
|
487 | - { |
|
488 | - // ensure $data is either @null, array or object of class with configured mapping. |
|
489 | - $converter = new Converter(); |
|
490 | - |
|
491 | - $data = $converter->convert($data); |
|
492 | - if ($data !== null && !is_object($data)) { |
|
493 | - // ensure we get object in final JSON structure in data node |
|
494 | - $data = (object)$data; |
|
495 | - } |
|
496 | - |
|
497 | - // get human readable message for API code or use message string (if given instead of API code) |
|
498 | - if (is_int($msg_or_api_code)) { |
|
499 | - $message = self::getMessageForApiCode($success, $msg_or_api_code, $placeholders); |
|
500 | - } else { |
|
501 | - Validator::assertString('message', $msg_or_api_code); |
|
502 | - $message = $msg_or_api_code; |
|
503 | - } |
|
504 | - |
|
505 | - /** @noinspection PhpUndefinedClassInspection */ |
|
506 | - $response = [ |
|
507 | - static::KEY_SUCCESS => $success, |
|
508 | - static::KEY_CODE => $api_code, |
|
509 | - static::KEY_LOCALE => \App::getLocale(), |
|
510 | - static::KEY_MESSAGE => $message, |
|
511 | - static::KEY_DATA => $data, |
|
512 | - ]; |
|
513 | - |
|
514 | - if ($debug_data !== null) { |
|
515 | - $debug_key = Config::get(static::CONF_KEY_DEBUG_DEBUG_KEY, self::KEY_DEBUG); |
|
516 | - $response[ $debug_key ] = $debug_data; |
|
517 | - } |
|
518 | - |
|
519 | - return $response; |
|
520 | - } |
|
27 | + /** |
|
28 | + * Default HTTP code to be used with success responses |
|
29 | + * |
|
30 | + * @var int |
|
31 | + */ |
|
32 | + public const DEFAULT_HTTP_CODE_OK = HttpResponse::HTTP_OK; |
|
33 | + |
|
34 | + /** |
|
35 | + * Default HTTP code to be used with error responses |
|
36 | + * |
|
37 | + * @var int |
|
38 | + */ |
|
39 | + public const DEFAULT_HTTP_CODE_ERROR = HttpResponse::HTTP_BAD_REQUEST; |
|
40 | + |
|
41 | + /** |
|
42 | + * Min allowed HTTP code for errorXXX() |
|
43 | + * |
|
44 | + * @var int |
|
45 | + */ |
|
46 | + public const ERROR_HTTP_CODE_MIN = 400; |
|
47 | + |
|
48 | + /** |
|
49 | + * Max allowed HTTP code for errorXXX() |
|
50 | + * |
|
51 | + * @var int |
|
52 | + */ |
|
53 | + public const ERROR_HTTP_CODE_MAX = 599; |
|
54 | + |
|
55 | + /** |
|
56 | + * Configuration keys |
|
57 | + */ |
|
58 | + public const CONF_KEY_DEBUG_DEBUG_KEY = 'response_builder.debug.debug_key'; |
|
59 | + public const CONF_KEY_DEBUG_EX_TRACE_ENABLED = 'response_builder.debug.exception_handler.trace_enabled'; |
|
60 | + public const CONF_KEY_DEBUG_EX_TRACE_KEY = 'response_builder.debug.exception_handler.trace_key'; |
|
61 | + public const CONF_KEY_MAP = 'response_builder.map'; |
|
62 | + public const CONF_KEY_ENCODING_OPTIONS = 'response_builder.encoding_options'; |
|
63 | + public const CONF_KEY_CLASSES = 'response_builder.classes'; |
|
64 | + public const CONF_KEY_MIN_CODE = 'response_builder.min_code'; |
|
65 | + public const CONF_KEY_MAX_CODE = 'response_builder.max_code'; |
|
66 | + public const CONF_EXCEPTION_HANDLER_KEY = 'response_builder.exception_handler'; |
|
67 | + |
|
68 | + /** |
|
69 | + * Default keys to be used by exception handler while adding debug information |
|
70 | + */ |
|
71 | + public const KEY_DEBUG = 'debug'; |
|
72 | + public const KEY_TRACE = 'trace'; |
|
73 | + public const KEY_CLASS = 'class'; |
|
74 | + public const KEY_FILE = 'file'; |
|
75 | + public const KEY_LINE = 'line'; |
|
76 | + public const KEY_KEY = 'key'; |
|
77 | + public const KEY_METHOD = 'method'; |
|
78 | + public const KEY_SUCCESS = 'success'; |
|
79 | + public const KEY_CODE = 'code'; |
|
80 | + public const KEY_LOCALE = 'locale'; |
|
81 | + public const KEY_MESSAGE = 'message'; |
|
82 | + public const KEY_DATA = 'data'; |
|
83 | + |
|
84 | + /** |
|
85 | + * Default key to be used by exception handler while processing ValidationException |
|
86 | + * to return all the error messages |
|
87 | + * |
|
88 | + * @var string |
|
89 | + */ |
|
90 | + public const KEY_MESSAGES = 'messages'; |
|
91 | + |
|
92 | + /** |
|
93 | + * Default JSON encoding options. Must be specified as final value (i.e. 271) and NOT |
|
94 | + * PHP expression i.e. `JSON_HEX_TAG|JSON_HEX_APOS|...` as such syntax is not yet supported |
|
95 | + * by PHP. |
|
96 | + * |
|
97 | + * 271 = JSON_HEX_TAG|JSON_HEX_APOS|JSON_HEX_AMP|JSON_HEX_QUOT|JSON_UNESCAPED_UNICODE |
|
98 | + * |
|
99 | + * @var int |
|
100 | + */ |
|
101 | + public const DEFAULT_ENCODING_OPTIONS = 271; |
|
102 | + |
|
103 | + |
|
104 | + |
|
105 | + /** |
|
106 | + * Returns success |
|
107 | + * |
|
108 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
109 | + * of the JSON response, single supported object or @null if there's no |
|
110 | + * to be returned. |
|
111 | + * @param integer|null $api_code API code to be returned or @null to use value of BaseApiCodes::OK(). |
|
112 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
113 | + * substitution or @null if none. |
|
114 | + * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
115 | + * for default DEFAULT_HTTP_CODE_OK. |
|
116 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
117 | + * options or pass @null to use value from your config (or defaults). |
|
118 | + * |
|
119 | + * @return HttpResponse |
|
120 | + */ |
|
121 | + public static function success($data = null, $api_code = null, array $placeholders = null, |
|
122 | + int $http_code = null, int $json_opts = null): HttpResponse |
|
123 | + { |
|
124 | + return static::buildSuccessResponse($data, $api_code, $placeholders, $http_code, $json_opts); |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * Returns success |
|
129 | + * |
|
130 | + * @param integer|null $api_code API code to be returned or @null to use value of BaseApiCodes::OK(). |
|
131 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
132 | + * substitution or @null if none. |
|
133 | + * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
134 | + * for default DEFAULT_HTTP_CODE_OK. |
|
135 | + * |
|
136 | + * @return HttpResponse |
|
137 | + */ |
|
138 | + public static function successWithCode(int $api_code = null, array $placeholders = null, |
|
139 | + int $http_code = null): HttpResponse |
|
140 | + { |
|
141 | + return static::success(null, $api_code, $placeholders, $http_code); |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * Returns success with custom HTTP code |
|
146 | + * |
|
147 | + * @param integer|null $http_code HTTP return code to be set for this response. If @null is passed, falls back |
|
148 | + * to DEFAULT_HTTP_CODE_OK. |
|
149 | + * |
|
150 | + * @return HttpResponse |
|
151 | + */ |
|
152 | + public static function successWithHttpCode(int $http_code = null): HttpResponse |
|
153 | + { |
|
154 | + return static::buildSuccessResponse(null, BaseApiCodes::OK(), [], $http_code); |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * @param string $message Custom message to be returned as part of the response. |
|
159 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
160 | + * of the JSON response, single supported object or @null if there's no |
|
161 | + * to be returned. |
|
162 | + * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
163 | + * for default DEFAULT_HTTP_CODE_OK. |
|
164 | + * |
|
165 | + * @return \Symfony\Component\HttpFoundation\Response |
|
166 | + */ |
|
167 | + public static function successWithMessage(string $message, $data = null, int $api_code = null, |
|
168 | + int $http_code = null): HttpResponse |
|
169 | + { |
|
170 | + return static::buildSuccessResponse($data, $api_code, [], $http_code, null, $message); |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node. |
|
175 | + * of the JSON response, single supported object or @null if there's no |
|
176 | + * to be returned. |
|
177 | + * @param integer|null $api_code API code to be returned or @null to use value of BaseApiCodes::OK(). |
|
178 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
179 | + * substitution or @null if none. |
|
180 | + * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
181 | + * for default DEFAULT_HTTP_CODE_OK. |
|
182 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
183 | + * options or pass @null to use value from your config (or defaults). |
|
184 | + * |
|
185 | + * @return HttpResponse |
|
186 | + * |
|
187 | + * @throws \InvalidArgumentException Thrown when provided arguments are invalid. |
|
188 | + */ |
|
189 | + protected static function buildSuccessResponse($data = null, int $api_code = null, array $placeholders = null, |
|
190 | + int $http_code = null, int $json_opts = null, |
|
191 | + $msg_or_api_code = null): HttpResponse |
|
192 | + { |
|
193 | + $http_code = $http_code ?? static::DEFAULT_HTTP_CODE_OK; |
|
194 | + $api_code = $api_code ?? BaseApiCodes::OK(); |
|
195 | + $msg_or_api_code = $msg_or_api_code ?? $api_code; |
|
196 | + |
|
197 | + Validator::assertInt('api_code', $api_code); |
|
198 | + Validator::assertInt('http_code', $http_code); |
|
199 | + Validator::assertIntRange('http_code', $http_code, 200, 299); |
|
200 | + Validator::assertType('msg_or_api_code', $msg_or_api_code, [Validator::TYPE_STRING, |
|
201 | + Validator::TYPE_INTEGER]); |
|
202 | + |
|
203 | + return static::make(true, $api_code, $msg_or_api_code, $data, $http_code, $placeholders, null, $json_opts); |
|
204 | + } |
|
205 | + |
|
206 | + /** |
|
207 | + * Builds error Response object. Supports optional arguments passed to Lang::get() if associated error |
|
208 | + * message uses placeholders as well as return data payload |
|
209 | + * |
|
210 | + * @param integer $api_code Your API code to be returned with the response object. |
|
211 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
212 | + * substitution or @null if none. |
|
213 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
214 | + * of the JSON response, single supported object or @null if there's no |
|
215 | + * to be returned. |
|
216 | + * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
217 | + * for default DEFAULT_HTTP_CODE_ERROR. |
|
218 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
219 | + * options or pass @null to use value from your config (or defaults). |
|
220 | + * |
|
221 | + * @return HttpResponse |
|
222 | + */ |
|
223 | + public static function error(int $api_code, array $placeholders = null, $data = null, int $http_code = null, |
|
224 | + int $encoding_options = null): HttpResponse |
|
225 | + { |
|
226 | + return static::buildErrorResponse($data, $api_code, $http_code, $placeholders, $encoding_options); |
|
227 | + } |
|
228 | + |
|
229 | + /** |
|
230 | + * @param integer $api_code Your API code to be returned with the response object. |
|
231 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
232 | + * of the JSON response, single supported object or @null if there's no |
|
233 | + * to be returned. |
|
234 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
235 | + * substitution or @null if none. |
|
236 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
237 | + * options or pass @null to use value from your config (or defaults). |
|
238 | + * |
|
239 | + * @return HttpResponse |
|
240 | + */ |
|
241 | + public static function errorWithData(int $api_code, $data, array $placeholders = null, |
|
242 | + int $json_opts = null): HttpResponse |
|
243 | + { |
|
244 | + return static::buildErrorResponse($data, $api_code, null, $placeholders, $json_opts); |
|
245 | + } |
|
246 | + |
|
247 | + /** |
|
248 | + * @param integer $api_code Your API code to be returned with the response object. |
|
249 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
250 | + * of the JSON response, single supported object or @null if there's no |
|
251 | + * to be returned. |
|
252 | + * @param integer $http_code HTTP code to be used for HttpResponse sent. |
|
253 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
254 | + * substitution or @null if none. |
|
255 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
256 | + * options or pass @null to use value from your config (or defaults). |
|
257 | + * |
|
258 | + * @return HttpResponse |
|
259 | + * |
|
260 | + * @throws \InvalidArgumentException if http_code is @null |
|
261 | + */ |
|
262 | + public static function errorWithDataAndHttpCode(int $api_code, $data, int $http_code, array $placeholders = null, |
|
263 | + int $json_opts = null): HttpResponse |
|
264 | + { |
|
265 | + return static::buildErrorResponse($data, $api_code, $http_code, $placeholders, $json_opts); |
|
266 | + } |
|
267 | + |
|
268 | + /** |
|
269 | + * @param integer $api_code Your API code to be returned with the response object. |
|
270 | + * @param integer $http_code HTTP code to be used for HttpResponse sent or @null |
|
271 | + * for default DEFAULT_HTTP_CODE_ERROR. |
|
272 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
273 | + * substitution or @null if none. |
|
274 | + * |
|
275 | + * @return HttpResponse |
|
276 | + * |
|
277 | + * @throws \InvalidArgumentException if http_code is @null |
|
278 | + */ |
|
279 | + public static function errorWithHttpCode(int $api_code, int $http_code, array $placeholders = null): HttpResponse |
|
280 | + { |
|
281 | + return static::buildErrorResponse(null, $api_code, $http_code, $placeholders); |
|
282 | + } |
|
283 | + |
|
284 | + /** |
|
285 | + * @param integer $api_code Your API code to be returned with the response object. |
|
286 | + * @param string $message Custom message to be returned as part of error response |
|
287 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
288 | + * of the JSON response, single supported object or @null if there's no |
|
289 | + * to be returned. |
|
290 | + * @param integer|null $http_code Optional HTTP status code to be used for HttpResponse sent |
|
291 | + * or @null for DEFAULT_HTTP_CODE_ERROR |
|
292 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
293 | + * options or pass @null to use value from your config (or defaults). |
|
294 | + * |
|
295 | + * @return HttpResponse |
|
296 | + */ |
|
297 | + public static function errorWithMessageAndData(int $api_code, string $message, $data, |
|
298 | + int $http_code = null, int $json_opts = null): HttpResponse |
|
299 | + { |
|
300 | + return static::buildErrorResponse($data, $api_code, $http_code, null, |
|
301 | + $message, null, $json_opts); |
|
302 | + } |
|
303 | + |
|
304 | + /** |
|
305 | + * @param integer $api_code Your API code to be returned with the response object. |
|
306 | + * @param string $message custom message to be returned as part of error response |
|
307 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
308 | + * of the JSON response, single supported object or @null if there's no |
|
309 | + * to be returned. |
|
310 | + * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
311 | + * for default DEFAULT_HTTP_CODE_ERROR. |
|
312 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
313 | + * options or pass @null to use value from your config (or defaults). |
|
314 | + * @param array|null $debug_data optional debug data array to be added to returned JSON. |
|
315 | + * |
|
316 | + * @return HttpResponse |
|
317 | + */ |
|
318 | + public static function errorWithMessageAndDataAndDebug(int $api_code, string $message, $data, |
|
319 | + int $http_code = null, int $json_opts = null, |
|
320 | + array $debug_data = null): HttpResponse |
|
321 | + { |
|
322 | + return static::buildErrorResponse($data, $api_code, $http_code, null, |
|
323 | + $message, null, $json_opts, $debug_data); |
|
324 | + } |
|
325 | + |
|
326 | + /** |
|
327 | + * @param integer $api_code Your API code to be returned with the response object. |
|
328 | + * @param string $message Custom message to be returned as part of error response |
|
329 | + * @param integer|null $http_code HTTP code to be used with final response sent or @null |
|
330 | + * for default DEFAULT_HTTP_CODE_ERROR. |
|
331 | + * |
|
332 | + * @return HttpResponse |
|
333 | + */ |
|
334 | + public static function errorWithMessage(int $api_code, string $message, int $http_code = null): HttpResponse |
|
335 | + { |
|
336 | + return static::buildErrorResponse(null, $api_code, $http_code, null, $message); |
|
337 | + } |
|
338 | + |
|
339 | + /** |
|
340 | + * Builds error Response object. Supports optional arguments passed to Lang::get() if associated error message |
|
341 | + * uses placeholders as well as return data payload |
|
342 | + * |
|
343 | + * @param object|array|null $data Array of primitives and supported objects to be returned in 'data' node |
|
344 | + * of the JSON response, single supported object or @null if there's no |
|
345 | + * to be returned. |
|
346 | + * @param integer $api_code Your API code to be returned with the response object. |
|
347 | + * @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null |
|
348 | + * for default DEFAULT_HTTP_CODE_ERROR. |
|
349 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
350 | + * substitution or @null if none. |
|
351 | + * @param string|null $message custom message to be returned as part of error response |
|
352 | + * @param array|null $headers optional HTTP headers to be returned in error response |
|
353 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
354 | + * options or pass @null to use value from your config (or defaults). |
|
355 | + * @param array|null $debug_data optional debug data array to be added to returned JSON. |
|
356 | + * |
|
357 | + * @return HttpResponse |
|
358 | + * |
|
359 | + * @throws \InvalidArgumentException Thrown if $code is not correct, outside the range, equals OK code etc. |
|
360 | + * |
|
361 | + * @noinspection MoreThanThreeArgumentsInspection |
|
362 | + */ |
|
363 | + protected static function buildErrorResponse($data, int $api_code, int $http_code = null, |
|
364 | + array $placeholders = null, |
|
365 | + string $message = null, array $headers = null, |
|
366 | + int $json_opts = null, |
|
367 | + array $debug_data = null): HttpResponse |
|
368 | + { |
|
369 | + $http_code = $http_code ?? static::DEFAULT_HTTP_CODE_ERROR; |
|
370 | + $headers = $headers ?? []; |
|
371 | + |
|
372 | + Validator::assertInt('api_code', $api_code); |
|
373 | + |
|
374 | + $code_ok = BaseApiCodes::OK(); |
|
375 | + if ($api_code !== $code_ok) { |
|
376 | + Validator::assertIntRange('api_code', $api_code, BaseApiCodes::getMinCode(), BaseApiCodes::getMaxCode()); |
|
377 | + } |
|
378 | + if ($api_code === $code_ok) { |
|
379 | + throw new \InvalidArgumentException( |
|
380 | + "Error response cannot use api_code of value {$code_ok} which is reserved for OK"); |
|
381 | + } |
|
382 | + |
|
383 | + Validator::assertInt('http_code', $http_code); |
|
384 | + Validator::assertIntRange('http_code', $http_code, static::ERROR_HTTP_CODE_MIN, static::ERROR_HTTP_CODE_MAX); |
|
385 | + |
|
386 | + $msg_or_api_code = $message ?? $api_code; |
|
387 | + |
|
388 | + return static::make(false, $api_code, $msg_or_api_code, $data, $http_code, |
|
389 | + $placeholders, $headers, $json_opts, $debug_data); |
|
390 | + } |
|
391 | + |
|
392 | + /** |
|
393 | + * @param boolean $success @true if response reports successful operation, @false otherwise. |
|
394 | + * @param integer $api_code Your API code to be returned with the response object. |
|
395 | + * @param string|integer $msg_or_api_code message string or valid API code to get message for |
|
396 | + * @param object|array|null $data optional additional data to be included in response object |
|
397 | + * @param integer|null $http_code HTTP code for the HttpResponse or @null for either DEFAULT_HTTP_CODE_OK |
|
398 | + * or DEFAULT_HTTP_CODE_ERROR depending on the $success. |
|
399 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
400 | + * substitution or @null if none. |
|
401 | + * @param array|null $headers Optional HTTP headers to be returned in the response. |
|
402 | + * @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for supported |
|
403 | + * options or pass @null to use value from your config (or defaults). |
|
404 | + * @param array|null $debug_data Optional debug data array to be added to returned JSON. |
|
405 | + * |
|
406 | + * @return HttpResponse |
|
407 | + * |
|
408 | + * @throws \InvalidArgumentException If $api_code is neither a string nor valid integer code. |
|
409 | + * @throws \InvalidArgumentException if $data is an object of class that is not configured in "classes" mapping. |
|
410 | + * |
|
411 | + * @noinspection MoreThanThreeArgumentsInspection |
|
412 | + */ |
|
413 | + protected static function make(bool $success, int $api_code, $msg_or_api_code, $data = null, |
|
414 | + int $http_code = null, array $placeholders = null, array $headers = null, |
|
415 | + int $json_opts = null, array $debug_data = null): HttpResponse |
|
416 | + { |
|
417 | + $headers = $headers ?? []; |
|
418 | + $http_code = $http_code ?? ($success ? static::DEFAULT_HTTP_CODE_OK : static::DEFAULT_HTTP_CODE_ERROR); |
|
419 | + $json_opts = $json_opts ?? Config::get(self::CONF_KEY_ENCODING_OPTIONS, static::DEFAULT_ENCODING_OPTIONS); |
|
420 | + |
|
421 | + Validator::assertInt('encoding_options', $json_opts); |
|
422 | + |
|
423 | + Validator::assertInt('api_code', $api_code); |
|
424 | + if (!BaseApiCodes::isCodeValid($api_code)) { |
|
425 | + $min = BaseApiCodes::getMinCode(); |
|
426 | + $max = BaseApiCodes::getMaxCode(); |
|
427 | + throw new \InvalidArgumentException("API code value ({$api_code}) is out of allowed range {$min}-{$max}"); |
|
428 | + } |
|
429 | + |
|
430 | + return Response::json( |
|
431 | + static::buildResponse($success, $api_code, $msg_or_api_code, $placeholders, $data, $debug_data), |
|
432 | + $http_code, $headers, $json_opts); |
|
433 | + } |
|
434 | + |
|
435 | + /** |
|
436 | + * If $msg_or_api_code is integer value, returns human readable message associated with that code (with |
|
437 | + * fallback to built-in default string if no api code mapping is set. If $msg_or_api_code is a string, |
|
438 | + * returns it unaltered. |
|
439 | + * |
|
440 | + * @param boolean $success @true if response reports successful operation, @false otherwise. |
|
441 | + * @param integer $api_code Your API code to be returned with the response object. |
|
442 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
443 | + * substitution or @null if none. |
|
444 | + * |
|
445 | + * @return string |
|
446 | + */ |
|
447 | + protected static function getMessageForApiCode(bool $success, int $api_code, array $placeholders = null): string |
|
448 | + { |
|
449 | + // We got integer value here not a message string, so we need to check if we have the mapping for |
|
450 | + // this string already configured. |
|
451 | + $key = BaseApiCodes::getCodeMessageKey($api_code); |
|
452 | + if ($key === null) { |
|
453 | + // nope, let's get the default one instead, based of |
|
454 | + $fallback_code = $success ? BaseApiCodes::OK() : BaseApiCodes::NO_ERROR_MESSAGE(); |
|
455 | + $key = BaseApiCodes::getCodeMessageKey($fallback_code); |
|
456 | + } |
|
457 | + |
|
458 | + $placeholders = $placeholders ?? []; |
|
459 | + if (!array_key_exists('api_code', $placeholders)) { |
|
460 | + $placeholders['api_code'] = $api_code; |
|
461 | + } |
|
462 | + |
|
463 | + return \Lang::get($key, $placeholders); |
|
464 | + } |
|
465 | + |
|
466 | + /** |
|
467 | + * Creates standardised API response array. This is final method called in the whole pipeline before we |
|
468 | + * return final JSON back to client. If you want to manipulate your response, this is the place to do that. |
|
469 | + * If you set APP_DEBUG to true, 'code_hex' field will be additionally added to reported JSON for easier |
|
470 | + * manual debugging. |
|
471 | + * |
|
472 | + * @param boolean $success @true if response reports successful operation, @false otherwise. |
|
473 | + * @param integer $api_code Your API code to be returned with the response object. |
|
474 | + * @param string|integer $msg_or_api_code Message string or valid API code to get message for. |
|
475 | + * @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders |
|
476 | + * substitution or @null if none. |
|
477 | + * @param object|array|null $data API response data if any |
|
478 | + * @param array|null $debug_data optional debug data array to be added to returned JSON. |
|
479 | + * |
|
480 | + * @return array response ready to be encoded as json and sent back to client |
|
481 | + * |
|
482 | + * @throws \RuntimeException in case of missing or invalid "classes" mapping configuration |
|
483 | + */ |
|
484 | + protected static function buildResponse(bool $success, int $api_code, |
|
485 | + $msg_or_api_code, array $placeholders = null, |
|
486 | + $data = null, array $debug_data = null): array |
|
487 | + { |
|
488 | + // ensure $data is either @null, array or object of class with configured mapping. |
|
489 | + $converter = new Converter(); |
|
490 | + |
|
491 | + $data = $converter->convert($data); |
|
492 | + if ($data !== null && !is_object($data)) { |
|
493 | + // ensure we get object in final JSON structure in data node |
|
494 | + $data = (object)$data; |
|
495 | + } |
|
496 | + |
|
497 | + // get human readable message for API code or use message string (if given instead of API code) |
|
498 | + if (is_int($msg_or_api_code)) { |
|
499 | + $message = self::getMessageForApiCode($success, $msg_or_api_code, $placeholders); |
|
500 | + } else { |
|
501 | + Validator::assertString('message', $msg_or_api_code); |
|
502 | + $message = $msg_or_api_code; |
|
503 | + } |
|
504 | + |
|
505 | + /** @noinspection PhpUndefinedClassInspection */ |
|
506 | + $response = [ |
|
507 | + static::KEY_SUCCESS => $success, |
|
508 | + static::KEY_CODE => $api_code, |
|
509 | + static::KEY_LOCALE => \App::getLocale(), |
|
510 | + static::KEY_MESSAGE => $message, |
|
511 | + static::KEY_DATA => $data, |
|
512 | + ]; |
|
513 | + |
|
514 | + if ($debug_data !== null) { |
|
515 | + $debug_key = Config::get(static::CONF_KEY_DEBUG_DEBUG_KEY, self::KEY_DEBUG); |
|
516 | + $response[ $debug_key ] = $debug_data; |
|
517 | + } |
|
518 | + |
|
519 | + return $response; |
|
520 | + } |
|
521 | 521 | |
522 | 522 | } |
@@ -72,12 +72,12 @@ discard block |
||
72 | 72 | // check for exact class name match... |
73 | 73 | $cls = get_class($data); |
74 | 74 | if (array_key_exists($cls, $this->classes)) { |
75 | - $result = $this->classes[ $cls ]; |
|
75 | + $result = $this->classes[$cls]; |
|
76 | 76 | } else { |
77 | 77 | // no exact match, then lets try with `instanceof` |
78 | 78 | foreach (array_keys($this->classes) as $class_name) { |
79 | 79 | if ($data instanceof $class_name) { |
80 | - $result = $this->classes[ $class_name ]; |
|
80 | + $result = $this->classes[$class_name]; |
|
81 | 81 | break; |
82 | 82 | } |
83 | 83 | } |
@@ -107,7 +107,7 @@ discard block |
||
107 | 107 | |
108 | 108 | if (is_object($data)) { |
109 | 109 | $cfg = $this->getClassMappingConfigOrThrow($data); |
110 | - $data = [$cfg[ ResponseBuilder::KEY_KEY ] => $data->{$cfg[ ResponseBuilder::KEY_METHOD ]}()]; |
|
110 | + $data = [$cfg[ResponseBuilder::KEY_KEY] => $data->{$cfg[ResponseBuilder::KEY_METHOD]}()]; |
|
111 | 111 | } elseif (!is_array($data)) { |
112 | 112 | throw new \InvalidArgumentException( |
113 | 113 | sprintf('Payload must be null, array or object with mapping ("%s" given).', gettype($data))); |
@@ -149,13 +149,13 @@ discard block |
||
149 | 149 | |
150 | 150 | foreach ($data as $key => $val) { |
151 | 151 | if (is_array($val)) { |
152 | - $data[ $key ] = $this->convertArray($val); |
|
152 | + $data[$key] = $this->convertArray($val); |
|
153 | 153 | } elseif (is_object($val)) { |
154 | 154 | $cls = get_class($val); |
155 | 155 | if (array_key_exists($cls, $this->classes)) { |
156 | - $conversion_method = $this->classes[ $cls ][ ResponseBuilder::KEY_METHOD ]; |
|
156 | + $conversion_method = $this->classes[$cls][ResponseBuilder::KEY_METHOD]; |
|
157 | 157 | $converted_data = $val->$conversion_method(); |
158 | - $data[ $key ] = $converted_data; |
|
158 | + $data[$key] = $converted_data; |
|
159 | 159 | } |
160 | 160 | } |
161 | 161 | } |
@@ -23,179 +23,179 @@ |
||
23 | 23 | */ |
24 | 24 | class Converter |
25 | 25 | { |
26 | - /** |
|
27 | - * @var array |
|
28 | - */ |
|
29 | - protected $classes = []; |
|
30 | - |
|
31 | - /** |
|
32 | - * Converter constructor. |
|
33 | - * |
|
34 | - * @throws \RuntimeException |
|
35 | - */ |
|
36 | - public function __construct() |
|
37 | - { |
|
38 | - $classes = static::getClassesMapping() ?? []; |
|
39 | - if (!is_array($classes)) { |
|
40 | - throw new \RuntimeException( |
|
41 | - sprintf('CONFIG: "classes" mapping must be an array (%s given)', gettype($classes))); |
|
42 | - } |
|
43 | - |
|
44 | - $this->classes = $classes; |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * Returns local copy of configuration mapping for the classes. |
|
49 | - * |
|
50 | - * @return array |
|
51 | - */ |
|
52 | - public function getClasses(): array |
|
53 | - { |
|
54 | - return $this->classes; |
|
55 | - } |
|
56 | - |
|
57 | - /** |
|
58 | - * Checks if we have "classes" mapping configured for $data object class. |
|
59 | - * Returns @true if there's valid config for this class. |
|
60 | - * Throws \RuntimeException if there's no config "classes" mapping entry for this object configured. |
|
61 | - * Throws \InvalidArgumentException if No data conversion mapping configured for given class. |
|
62 | - * |
|
63 | - * @param object $data Object to check mapping for. |
|
64 | - * |
|
65 | - * @return array |
|
66 | - * |
|
67 | - * @throws \InvalidArgumentException |
|
68 | - */ |
|
69 | - protected function getClassMappingConfigOrThrow(object $data): array |
|
70 | - { |
|
71 | - $result = null; |
|
72 | - |
|
73 | - // check for exact class name match... |
|
74 | - $cls = get_class($data); |
|
75 | - if (array_key_exists($cls, $this->classes)) { |
|
76 | - $result = $this->classes[ $cls ]; |
|
77 | - } else { |
|
78 | - // no exact match, then lets try with `instanceof` |
|
79 | - foreach (array_keys($this->classes) as $class_name) { |
|
80 | - if ($data instanceof $class_name) { |
|
81 | - $result = $this->classes[ $class_name ]; |
|
82 | - break; |
|
83 | - } |
|
84 | - } |
|
85 | - } |
|
86 | - |
|
87 | - if ($result === null) { |
|
88 | - throw new \InvalidArgumentException(sprintf('No data conversion mapping configured for "%s" class.', $cls)); |
|
89 | - } |
|
90 | - |
|
91 | - return $result; |
|
92 | - } |
|
93 | - |
|
94 | - /** |
|
95 | - * We need to prepare source data |
|
96 | - * |
|
97 | - * @param object|array|null $data |
|
98 | - * |
|
99 | - * @return array|null |
|
100 | - * |
|
101 | - * @throws \InvalidArgumentException |
|
102 | - */ |
|
103 | - public function convert($data = null): ?array |
|
104 | - { |
|
105 | - if ($data === null) { |
|
106 | - return null; |
|
107 | - } |
|
108 | - |
|
109 | - if (is_object($data)) { |
|
110 | - $cfg = $this->getClassMappingConfigOrThrow($data); |
|
111 | - $data = [$cfg[ ResponseBuilder::KEY_KEY ] => $data->{$cfg[ ResponseBuilder::KEY_METHOD ]}()]; |
|
112 | - } elseif (!is_array($data)) { |
|
113 | - throw new \InvalidArgumentException( |
|
114 | - sprintf('Payload must be null, array or object with mapping ("%s" given).', gettype($data))); |
|
115 | - } |
|
116 | - |
|
117 | - return $this->convertArray($data); |
|
118 | - } |
|
119 | - |
|
120 | - /** |
|
121 | - * Recursively walks $data array and converts all known objects if found. Note |
|
122 | - * $data array is passed by reference so source $data array may be modified. |
|
123 | - * |
|
124 | - * @param array $data array to recursively convert known elements of |
|
125 | - * |
|
126 | - * @return array |
|
127 | - * |
|
128 | - * @throws \RuntimeException |
|
129 | - */ |
|
130 | - protected function convertArray(array $data): array |
|
131 | - { |
|
132 | - // This is to ensure that we either have array with user provided keys i.e. ['foo'=>'bar'], which will then |
|
133 | - // be turned into JSON object or array without user specified keys (['bar']) which we would return as JSON |
|
134 | - // array. But you can't mix these two as the final JSON would not produce predictable results. |
|
135 | - $string_keys_cnt = 0; |
|
136 | - $int_keys_cnt = 0; |
|
137 | - foreach ($data as $key => $val) { |
|
138 | - if (is_int($key)) { |
|
139 | - $int_keys_cnt++; |
|
140 | - } else { |
|
141 | - $string_keys_cnt++; |
|
142 | - } |
|
143 | - |
|
144 | - if (($string_keys_cnt > 0) && ($int_keys_cnt > 0)) { |
|
145 | - throw new \RuntimeException( |
|
146 | - 'Invalid data array. Either set own keys for all the items or do not specify any keys at all. ' . |
|
147 | - 'Arrays with mixed keys are not supported by design.'); |
|
148 | - } |
|
149 | - } |
|
150 | - |
|
151 | - foreach ($data as $key => $val) { |
|
152 | - if (is_array($val)) { |
|
153 | - $data[ $key ] = $this->convertArray($val); |
|
154 | - } elseif (is_object($val)) { |
|
155 | - $cls = get_class($val); |
|
156 | - if (array_key_exists($cls, $this->classes)) { |
|
157 | - $conversion_method = $this->classes[ $cls ][ ResponseBuilder::KEY_METHOD ]; |
|
158 | - $converted_data = $val->$conversion_method(); |
|
159 | - $data[ $key ] = $converted_data; |
|
160 | - } |
|
161 | - } |
|
162 | - } |
|
163 | - |
|
164 | - return $data; |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * Reads and validates "classes" config mapping |
|
169 | - * |
|
170 | - * @return array Classes mapping as specified in configuration or empty array if configuration found |
|
171 | - * |
|
172 | - * @throws \RuntimeException if "classes" mapping is technically invalid (i.e. not array etc). |
|
173 | - */ |
|
174 | - protected static function getClassesMapping(): ?array |
|
175 | - { |
|
176 | - $classes = Config::get(ResponseBuilder::CONF_KEY_CLASSES); |
|
177 | - |
|
178 | - if ($classes !== null) { |
|
179 | - if (!is_array($classes)) { |
|
180 | - throw new \RuntimeException( |
|
181 | - sprintf('CONFIG: "classes" mapping must be an array (%s given)', gettype($classes))); |
|
182 | - } |
|
183 | - |
|
184 | - $mandatory_keys = [ |
|
185 | - ResponseBuilder::KEY_KEY, |
|
186 | - ResponseBuilder::KEY_METHOD, |
|
187 | - ]; |
|
188 | - foreach ($classes as $class_name => $class_config) { |
|
189 | - foreach ($mandatory_keys as $key_name) { |
|
190 | - if (!array_key_exists($key_name, $class_config)) { |
|
191 | - throw new \RuntimeException("CONFIG: Missing '{$key_name}' for '{$class_name}' class mapping"); |
|
192 | - } |
|
193 | - } |
|
194 | - } |
|
195 | - } else { |
|
196 | - $classes = []; |
|
197 | - } |
|
198 | - |
|
199 | - return $classes; |
|
200 | - } |
|
26 | + /** |
|
27 | + * @var array |
|
28 | + */ |
|
29 | + protected $classes = []; |
|
30 | + |
|
31 | + /** |
|
32 | + * Converter constructor. |
|
33 | + * |
|
34 | + * @throws \RuntimeException |
|
35 | + */ |
|
36 | + public function __construct() |
|
37 | + { |
|
38 | + $classes = static::getClassesMapping() ?? []; |
|
39 | + if (!is_array($classes)) { |
|
40 | + throw new \RuntimeException( |
|
41 | + sprintf('CONFIG: "classes" mapping must be an array (%s given)', gettype($classes))); |
|
42 | + } |
|
43 | + |
|
44 | + $this->classes = $classes; |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * Returns local copy of configuration mapping for the classes. |
|
49 | + * |
|
50 | + * @return array |
|
51 | + */ |
|
52 | + public function getClasses(): array |
|
53 | + { |
|
54 | + return $this->classes; |
|
55 | + } |
|
56 | + |
|
57 | + /** |
|
58 | + * Checks if we have "classes" mapping configured for $data object class. |
|
59 | + * Returns @true if there's valid config for this class. |
|
60 | + * Throws \RuntimeException if there's no config "classes" mapping entry for this object configured. |
|
61 | + * Throws \InvalidArgumentException if No data conversion mapping configured for given class. |
|
62 | + * |
|
63 | + * @param object $data Object to check mapping for. |
|
64 | + * |
|
65 | + * @return array |
|
66 | + * |
|
67 | + * @throws \InvalidArgumentException |
|
68 | + */ |
|
69 | + protected function getClassMappingConfigOrThrow(object $data): array |
|
70 | + { |
|
71 | + $result = null; |
|
72 | + |
|
73 | + // check for exact class name match... |
|
74 | + $cls = get_class($data); |
|
75 | + if (array_key_exists($cls, $this->classes)) { |
|
76 | + $result = $this->classes[ $cls ]; |
|
77 | + } else { |
|
78 | + // no exact match, then lets try with `instanceof` |
|
79 | + foreach (array_keys($this->classes) as $class_name) { |
|
80 | + if ($data instanceof $class_name) { |
|
81 | + $result = $this->classes[ $class_name ]; |
|
82 | + break; |
|
83 | + } |
|
84 | + } |
|
85 | + } |
|
86 | + |
|
87 | + if ($result === null) { |
|
88 | + throw new \InvalidArgumentException(sprintf('No data conversion mapping configured for "%s" class.', $cls)); |
|
89 | + } |
|
90 | + |
|
91 | + return $result; |
|
92 | + } |
|
93 | + |
|
94 | + /** |
|
95 | + * We need to prepare source data |
|
96 | + * |
|
97 | + * @param object|array|null $data |
|
98 | + * |
|
99 | + * @return array|null |
|
100 | + * |
|
101 | + * @throws \InvalidArgumentException |
|
102 | + */ |
|
103 | + public function convert($data = null): ?array |
|
104 | + { |
|
105 | + if ($data === null) { |
|
106 | + return null; |
|
107 | + } |
|
108 | + |
|
109 | + if (is_object($data)) { |
|
110 | + $cfg = $this->getClassMappingConfigOrThrow($data); |
|
111 | + $data = [$cfg[ ResponseBuilder::KEY_KEY ] => $data->{$cfg[ ResponseBuilder::KEY_METHOD ]}()]; |
|
112 | + } elseif (!is_array($data)) { |
|
113 | + throw new \InvalidArgumentException( |
|
114 | + sprintf('Payload must be null, array or object with mapping ("%s" given).', gettype($data))); |
|
115 | + } |
|
116 | + |
|
117 | + return $this->convertArray($data); |
|
118 | + } |
|
119 | + |
|
120 | + /** |
|
121 | + * Recursively walks $data array and converts all known objects if found. Note |
|
122 | + * $data array is passed by reference so source $data array may be modified. |
|
123 | + * |
|
124 | + * @param array $data array to recursively convert known elements of |
|
125 | + * |
|
126 | + * @return array |
|
127 | + * |
|
128 | + * @throws \RuntimeException |
|
129 | + */ |
|
130 | + protected function convertArray(array $data): array |
|
131 | + { |
|
132 | + // This is to ensure that we either have array with user provided keys i.e. ['foo'=>'bar'], which will then |
|
133 | + // be turned into JSON object or array without user specified keys (['bar']) which we would return as JSON |
|
134 | + // array. But you can't mix these two as the final JSON would not produce predictable results. |
|
135 | + $string_keys_cnt = 0; |
|
136 | + $int_keys_cnt = 0; |
|
137 | + foreach ($data as $key => $val) { |
|
138 | + if (is_int($key)) { |
|
139 | + $int_keys_cnt++; |
|
140 | + } else { |
|
141 | + $string_keys_cnt++; |
|
142 | + } |
|
143 | + |
|
144 | + if (($string_keys_cnt > 0) && ($int_keys_cnt > 0)) { |
|
145 | + throw new \RuntimeException( |
|
146 | + 'Invalid data array. Either set own keys for all the items or do not specify any keys at all. ' . |
|
147 | + 'Arrays with mixed keys are not supported by design.'); |
|
148 | + } |
|
149 | + } |
|
150 | + |
|
151 | + foreach ($data as $key => $val) { |
|
152 | + if (is_array($val)) { |
|
153 | + $data[ $key ] = $this->convertArray($val); |
|
154 | + } elseif (is_object($val)) { |
|
155 | + $cls = get_class($val); |
|
156 | + if (array_key_exists($cls, $this->classes)) { |
|
157 | + $conversion_method = $this->classes[ $cls ][ ResponseBuilder::KEY_METHOD ]; |
|
158 | + $converted_data = $val->$conversion_method(); |
|
159 | + $data[ $key ] = $converted_data; |
|
160 | + } |
|
161 | + } |
|
162 | + } |
|
163 | + |
|
164 | + return $data; |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * Reads and validates "classes" config mapping |
|
169 | + * |
|
170 | + * @return array Classes mapping as specified in configuration or empty array if configuration found |
|
171 | + * |
|
172 | + * @throws \RuntimeException if "classes" mapping is technically invalid (i.e. not array etc). |
|
173 | + */ |
|
174 | + protected static function getClassesMapping(): ?array |
|
175 | + { |
|
176 | + $classes = Config::get(ResponseBuilder::CONF_KEY_CLASSES); |
|
177 | + |
|
178 | + if ($classes !== null) { |
|
179 | + if (!is_array($classes)) { |
|
180 | + throw new \RuntimeException( |
|
181 | + sprintf('CONFIG: "classes" mapping must be an array (%s given)', gettype($classes))); |
|
182 | + } |
|
183 | + |
|
184 | + $mandatory_keys = [ |
|
185 | + ResponseBuilder::KEY_KEY, |
|
186 | + ResponseBuilder::KEY_METHOD, |
|
187 | + ]; |
|
188 | + foreach ($classes as $class_name => $class_config) { |
|
189 | + foreach ($mandatory_keys as $key_name) { |
|
190 | + if (!array_key_exists($key_name, $class_config)) { |
|
191 | + throw new \RuntimeException("CONFIG: Missing '{$key_name}' for '{$class_name}' class mapping"); |
|
192 | + } |
|
193 | + } |
|
194 | + } |
|
195 | + } else { |
|
196 | + $classes = []; |
|
197 | + } |
|
198 | + |
|
199 | + return $classes; |
|
200 | + } |
|
201 | 201 | } |
@@ -34,20 +34,20 @@ |
||
34 | 34 | $array = $original; |
35 | 35 | foreach ($merging as $m_key => $m_val) { |
36 | 36 | if (array_key_exists($m_key, $original)) { |
37 | - $orig_type = gettype($original[ $m_key ]); |
|
37 | + $orig_type = gettype($original[$m_key]); |
|
38 | 38 | $m_type = gettype($m_val); |
39 | 39 | if ($orig_type !== $m_type) { |
40 | 40 | throw new \RuntimeException( |
41 | 41 | "Incompatible types. Cannot merge {$m_type} into {$orig_type} (key '{$m_key}')."); |
42 | 42 | } |
43 | 43 | |
44 | - if (is_array($merging[ $m_key ])) { |
|
45 | - $array[ $m_key ] = static::mergeConfig($original[ $m_key ], $m_val); |
|
44 | + if (is_array($merging[$m_key])) { |
|
45 | + $array[$m_key] = static::mergeConfig($original[$m_key], $m_val); |
|
46 | 46 | } else { |
47 | - $array[ $m_key ] = $m_val; |
|
47 | + $array[$m_key] = $m_val; |
|
48 | 48 | } |
49 | 49 | } else { |
50 | - $array[ $m_key ] = $m_val; |
|
50 | + $array[$m_key] = $m_val; |
|
51 | 51 | } |
52 | 52 | } |
53 | 53 |
@@ -15,59 +15,59 @@ |
||
15 | 15 | */ |
16 | 16 | final class Util |
17 | 17 | { |
18 | - /** |
|
19 | - * Merges the configs together and takes multi-dimensional arrays into account. |
|
20 | - * Support for multi-dimensional config array. Built-in config merge only supports flat arrays. |
|
21 | - * Throws \RuntimeException if arrays stucture causes type conflics (i.e. you want to merge |
|
22 | - * array with int). |
|
23 | - * |
|
24 | - * @param array $original Array to merge other array into. Usually default values to overwrite. |
|
25 | - * @param array $merging Array with items to be merged into $original, overriding (primitives) or merging |
|
26 | - * (arrays) entries in destination array. |
|
27 | - * |
|
28 | - * @return array |
|
29 | - * |
|
30 | - * @throws \RuntimeException |
|
31 | - */ |
|
32 | - public static function mergeConfig(array $original, array $merging): array |
|
33 | - { |
|
34 | - $array = $original; |
|
35 | - foreach ($merging as $m_key => $m_val) { |
|
36 | - if (array_key_exists($m_key, $original)) { |
|
37 | - $orig_type = gettype($original[ $m_key ]); |
|
38 | - $m_type = gettype($m_val); |
|
39 | - if ($orig_type !== $m_type) { |
|
40 | - throw new \RuntimeException( |
|
41 | - "Incompatible types. Cannot merge {$m_type} into {$orig_type} (key '{$m_key}')."); |
|
42 | - } |
|
18 | + /** |
|
19 | + * Merges the configs together and takes multi-dimensional arrays into account. |
|
20 | + * Support for multi-dimensional config array. Built-in config merge only supports flat arrays. |
|
21 | + * Throws \RuntimeException if arrays stucture causes type conflics (i.e. you want to merge |
|
22 | + * array with int). |
|
23 | + * |
|
24 | + * @param array $original Array to merge other array into. Usually default values to overwrite. |
|
25 | + * @param array $merging Array with items to be merged into $original, overriding (primitives) or merging |
|
26 | + * (arrays) entries in destination array. |
|
27 | + * |
|
28 | + * @return array |
|
29 | + * |
|
30 | + * @throws \RuntimeException |
|
31 | + */ |
|
32 | + public static function mergeConfig(array $original, array $merging): array |
|
33 | + { |
|
34 | + $array = $original; |
|
35 | + foreach ($merging as $m_key => $m_val) { |
|
36 | + if (array_key_exists($m_key, $original)) { |
|
37 | + $orig_type = gettype($original[ $m_key ]); |
|
38 | + $m_type = gettype($m_val); |
|
39 | + if ($orig_type !== $m_type) { |
|
40 | + throw new \RuntimeException( |
|
41 | + "Incompatible types. Cannot merge {$m_type} into {$orig_type} (key '{$m_key}')."); |
|
42 | + } |
|
43 | 43 | |
44 | - if (is_array($merging[ $m_key ])) { |
|
45 | - $array[ $m_key ] = static::mergeConfig($original[ $m_key ], $m_val); |
|
46 | - } else { |
|
47 | - $array[ $m_key ] = $m_val; |
|
48 | - } |
|
49 | - } else { |
|
50 | - $array[ $m_key ] = $m_val; |
|
51 | - } |
|
52 | - } |
|
44 | + if (is_array($merging[ $m_key ])) { |
|
45 | + $array[ $m_key ] = static::mergeConfig($original[ $m_key ], $m_val); |
|
46 | + } else { |
|
47 | + $array[ $m_key ] = $m_val; |
|
48 | + } |
|
49 | + } else { |
|
50 | + $array[ $m_key ] = $m_val; |
|
51 | + } |
|
52 | + } |
|
53 | 53 | |
54 | - return $array; |
|
55 | - } |
|
54 | + return $array; |
|
55 | + } |
|
56 | 56 | |
57 | - /** |
|
58 | - * Sorts array by value, assuming value is an array and contains `pri` key with integer (positive/negative) |
|
59 | - * value which is used for sorting higher -> lower priority. |
|
60 | - * |
|
61 | - * @param array &$array |
|
62 | - */ |
|
63 | - public static function sortArrayByPri(array &$array): void |
|
64 | - { |
|
65 | - // we now need to sort 'classes' node by priority |
|
66 | - uasort($array, function($array_a, $array_b) { |
|
67 | - $pri_a = $array_a['pri'] ?? 0; |
|
68 | - $pri_b = $array_b['pri'] ?? 0; |
|
57 | + /** |
|
58 | + * Sorts array by value, assuming value is an array and contains `pri` key with integer (positive/negative) |
|
59 | + * value which is used for sorting higher -> lower priority. |
|
60 | + * |
|
61 | + * @param array &$array |
|
62 | + */ |
|
63 | + public static function sortArrayByPri(array &$array): void |
|
64 | + { |
|
65 | + // we now need to sort 'classes' node by priority |
|
66 | + uasort($array, function($array_a, $array_b) { |
|
67 | + $pri_a = $array_a['pri'] ?? 0; |
|
68 | + $pri_b = $array_b['pri'] ?? 0; |
|
69 | 69 | |
70 | - return $pri_b <=> $pri_a; |
|
71 | - }); |
|
72 | - } |
|
70 | + return $pri_b <=> $pri_a; |
|
71 | + }); |
|
72 | + } |
|
73 | 73 | } |
@@ -10,19 +10,19 @@ |
||
10 | 10 | */ |
11 | 11 | return [ |
12 | 12 | |
13 | - 'ok' => 'موفق', |
|
14 | - 'no_error_message' => 'خطای شماره :api_code', |
|
13 | + 'ok' => 'موفق', |
|
14 | + 'no_error_message' => 'خطای شماره :api_code', |
|
15 | 15 | |
16 | - // can be used by Exception Handler (if enabled) |
|
17 | - 'uncaught_exception' => 'استثناء مدیریت نشده :message', |
|
18 | - // we talking API call here, hence we have http_not_found |
|
19 | - 'http_not_found' => 'مورد یافت نشد', |
|
20 | - 'http_exception' => 'استثناء HTTP :message', |
|
21 | - 'http_service_unavailable' => 'عملیات نگهداری در حال انجام است', |
|
16 | + // can be used by Exception Handler (if enabled) |
|
17 | + 'uncaught_exception' => 'استثناء مدیریت نشده :message', |
|
18 | + // we talking API call here, hence we have http_not_found |
|
19 | + 'http_not_found' => 'مورد یافت نشد', |
|
20 | + 'http_exception' => 'استثناء HTTP :message', |
|
21 | + 'http_service_unavailable' => 'عملیات نگهداری در حال انجام است', |
|
22 | 22 | |
23 | - // Exception Handler (added in 3.3.0) |
|
24 | - 'authentication_exception' => 'اجازه دسترسی ندارید', |
|
25 | - 'validation_exception' => 'داده معتبر نیست', |
|
23 | + // Exception Handler (added in 3.3.0) |
|
24 | + 'authentication_exception' => 'اجازه دسترسی ندارید', |
|
25 | + 'validation_exception' => 'داده معتبر نیست', |
|
26 | 26 | |
27 | 27 | ]; |
28 | 28 |
@@ -12,69 +12,69 @@ discard block |
||
12 | 12 | */ |
13 | 13 | |
14 | 14 | return [ |
15 | - /* |
|
15 | + /* |
|
16 | 16 | |----------------------------------------------------------------------------------------------------------- |
17 | 17 | | Code range settings |
18 | 18 | |----------------------------------------------------------------------------------------------------------- |
19 | 19 | */ |
20 | - 'min_code' => 100, |
|
21 | - 'max_code' => 1024, |
|
20 | + 'min_code' => 100, |
|
21 | + 'max_code' => 1024, |
|
22 | 22 | |
23 | - /* |
|
23 | + /* |
|
24 | 24 | |----------------------------------------------------------------------------------------------------------- |
25 | 25 | | Error code to message mapping |
26 | 26 | |----------------------------------------------------------------------------------------------------------- |
27 | 27 | | |
28 | 28 | */ |
29 | - 'map' => [ |
|
29 | + 'map' => [ |
|
30 | 30 | |
31 | - ], |
|
31 | + ], |
|
32 | 32 | |
33 | - /* |
|
33 | + /* |
|
34 | 34 | |----------------------------------------------------------------------------------------------------------- |
35 | 35 | | Response Builder classes |
36 | 36 | |----------------------------------------------------------------------------------------------------------- |
37 | 37 | | |
38 | 38 | */ |
39 | - 'classes' => [ |
|
40 | - \Illuminate\Database\Eloquent\Model::class => [ |
|
41 | - 'key' => 'item', |
|
42 | - 'method' => 'toArray', |
|
43 | - 'pri' => 0, |
|
44 | - ], |
|
45 | - \Illuminate\Support\Collection::class => [ |
|
46 | - 'key' => 'items', |
|
47 | - 'method' => 'toArray', |
|
48 | - 'pri' => -1, |
|
49 | - ], |
|
50 | - \Illuminate\Database\Eloquent\Collection::class => [ |
|
51 | - 'key' => 'items', |
|
52 | - 'method' => 'toArray', |
|
53 | - 'pri' => 0, |
|
54 | - ], |
|
55 | - \Illuminate\Http\Resources\Json\JsonResource::class => [ |
|
56 | - 'key' => 'item', |
|
57 | - 'method' => 'toArray', |
|
58 | - 'pri' => 0, |
|
59 | - ], |
|
60 | - ], |
|
39 | + 'classes' => [ |
|
40 | + \Illuminate\Database\Eloquent\Model::class => [ |
|
41 | + 'key' => 'item', |
|
42 | + 'method' => 'toArray', |
|
43 | + 'pri' => 0, |
|
44 | + ], |
|
45 | + \Illuminate\Support\Collection::class => [ |
|
46 | + 'key' => 'items', |
|
47 | + 'method' => 'toArray', |
|
48 | + 'pri' => -1, |
|
49 | + ], |
|
50 | + \Illuminate\Database\Eloquent\Collection::class => [ |
|
51 | + 'key' => 'items', |
|
52 | + 'method' => 'toArray', |
|
53 | + 'pri' => 0, |
|
54 | + ], |
|
55 | + \Illuminate\Http\Resources\Json\JsonResource::class => [ |
|
56 | + 'key' => 'item', |
|
57 | + 'method' => 'toArray', |
|
58 | + 'pri' => 0, |
|
59 | + ], |
|
60 | + ], |
|
61 | 61 | |
62 | - /* |
|
62 | + /* |
|
63 | 63 | |----------------------------------------------------------------------------------------------------------- |
64 | 64 | | data-to-json encoding options |
65 | 65 | |----------------------------------------------------------------------------------------------------------- |
66 | 66 | | |
67 | 67 | */ |
68 | - 'encoding_options' => JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE, |
|
68 | + 'encoding_options' => JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE, |
|
69 | 69 | |
70 | - /* |
|
70 | + /* |
|
71 | 71 | |----------------------------------------------------------------------------------------------------------- |
72 | 72 | | Exception handler error codes |
73 | 73 | |----------------------------------------------------------------------------------------------------------- |
74 | 74 | | |
75 | 75 | */ |
76 | - 'exception_handler' => [ |
|
77 | - 'exception' => [ |
|
76 | + 'exception_handler' => [ |
|
77 | + 'exception' => [ |
|
78 | 78 | // 'http_not_found' => [ |
79 | 79 | // 'code' => \App\ApiCodes::HTTP_NOT_FOUND(), |
80 | 80 | // 'http_code' => Symfony\Component\HttpFoundation\Response::HTTP_BAD_REQUEST, |
@@ -99,21 +99,21 @@ discard block |
||
99 | 99 | // 'code' => \App\ApiCodes::VALIDATION_EXCEPTION(), |
100 | 100 | // 'http_code' => Symfony\Component\HttpFoundation\Response::HTTP_UNPROCESSABLE_ENTITY, |
101 | 101 | // ], |
102 | - ], |
|
103 | - ], |
|
102 | + ], |
|
103 | + ], |
|
104 | 104 | |
105 | - /* |
|
105 | + /* |
|
106 | 106 | |----------------------------------------------------------------------------------------------------------- |
107 | 107 | | Debug config |
108 | 108 | |----------------------------------------------------------------------------------------------------------- |
109 | 109 | | |
110 | 110 | */ |
111 | - 'debug' => [ |
|
112 | - 'debug_key' => 'debug', |
|
113 | - 'exception_handler' => [ |
|
114 | - 'trace_key' => 'trace', |
|
115 | - 'trace_enabled' => env('APP_DEBUG', false), |
|
116 | - ], |
|
117 | - ], |
|
111 | + 'debug' => [ |
|
112 | + 'debug_key' => 'debug', |
|
113 | + 'exception_handler' => [ |
|
114 | + 'trace_key' => 'trace', |
|
115 | + 'trace_enabled' => env('APP_DEBUG', false), |
|
116 | + ], |
|
117 | + ], |
|
118 | 118 | |
119 | 119 | ]; |
@@ -25,50 +25,50 @@ |
||
25 | 25 | |
26 | 26 | class ResponseBuilderServiceProvider extends ServiceProvider |
27 | 27 | { |
28 | - /** |
|
29 | - * Register bindings in the container. |
|
30 | - * |
|
31 | - * @return void |
|
32 | - */ |
|
33 | - public function register() |
|
34 | - { |
|
35 | - $this->mergeConfigFrom( |
|
36 | - __DIR__ . '/../config/response_builder.php', 'response_builder' |
|
37 | - ); |
|
38 | - } |
|
28 | + /** |
|
29 | + * Register bindings in the container. |
|
30 | + * |
|
31 | + * @return void |
|
32 | + */ |
|
33 | + public function register() |
|
34 | + { |
|
35 | + $this->mergeConfigFrom( |
|
36 | + __DIR__ . '/../config/response_builder.php', 'response_builder' |
|
37 | + ); |
|
38 | + } |
|
39 | 39 | |
40 | - /** |
|
41 | - * Sets up package resources |
|
42 | - * |
|
43 | - * @return void |
|
44 | - */ |
|
45 | - public function boot() |
|
46 | - { |
|
47 | - $this->loadTranslationsFrom(__DIR__ . '/lang', 'response-builder'); |
|
40 | + /** |
|
41 | + * Sets up package resources |
|
42 | + * |
|
43 | + * @return void |
|
44 | + */ |
|
45 | + public function boot() |
|
46 | + { |
|
47 | + $this->loadTranslationsFrom(__DIR__ . '/lang', 'response-builder'); |
|
48 | 48 | |
49 | - $source = __DIR__ . '/../config/response_builder.php'; |
|
50 | - $this->publishes([ |
|
51 | - $source => config_path('response_builder.php'), |
|
52 | - ]); |
|
53 | - } |
|
49 | + $source = __DIR__ . '/../config/response_builder.php'; |
|
50 | + $this->publishes([ |
|
51 | + $source => config_path('response_builder.php'), |
|
52 | + ]); |
|
53 | + } |
|
54 | 54 | |
55 | - /** |
|
56 | - * Merge the given configuration with the existing configuration. |
|
57 | - * |
|
58 | - * @param string $path |
|
59 | - * @param string $key |
|
60 | - * |
|
61 | - * @return void |
|
62 | - */ |
|
63 | - protected function mergeConfigFrom($path, $key) |
|
64 | - { |
|
65 | - $defaults = require $path; |
|
66 | - $config = $this->app['config']->get($key, []); |
|
55 | + /** |
|
56 | + * Merge the given configuration with the existing configuration. |
|
57 | + * |
|
58 | + * @param string $path |
|
59 | + * @param string $key |
|
60 | + * |
|
61 | + * @return void |
|
62 | + */ |
|
63 | + protected function mergeConfigFrom($path, $key) |
|
64 | + { |
|
65 | + $defaults = require $path; |
|
66 | + $config = $this->app['config']->get($key, []); |
|
67 | 67 | |
68 | - $merged_config = Util::mergeConfig($defaults, $config); |
|
69 | - Util::sortArrayByPri($merged_config['classes']); |
|
68 | + $merged_config = Util::mergeConfig($defaults, $config); |
|
69 | + Util::sortArrayByPri($merged_config['classes']); |
|
70 | 70 | |
71 | - $this->app['config']->set($key, $merged_config); |
|
72 | - } |
|
71 | + $this->app['config']->set($key, $merged_config); |
|
72 | + } |
|
73 | 73 | |
74 | 74 | } |