-
Notifications
You must be signed in to change notification settings - Fork 79
/
ResponseBuilder.php
465 lines (414 loc) · 18.4 KB
/
ResponseBuilder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
<?php
declare(strict_types=1);
namespace MarcinOrlowski\ResponseBuilder;
/**
* Laravel API Response Builder
*
* @author Marcin Orlowski <mail (#) marcinOrlowski (.) com>
* @copyright 2016-2024 Marcin Orlowski
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link https://github.com/MarcinOrlowski/laravel-api-response-builder
*/
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Response;
use Symfony\Component\HttpFoundation\Response as HttpResponse;
use MarcinOrlowski\ResponseBuilder\ResponseBuilder as RB;
use MarcinOrlowski\ResponseBuilder\Exceptions as Ex;
/**
* Builds standardized HttpResponse response object
*/
class ResponseBuilder extends ResponseBuilderBase
{
protected bool $success = false;
protected int $api_code;
protected ?int $http_code = null;
protected ?string $message = null;
protected ?array $placeholders = null;
protected ?int $json_opts = null;
protected ?array $debug_data = null;
protected array $http_headers = [];
/** @var mixed|null $data */
protected $data = null;
// ---------------------------------------------------------------------------------------------
/**
* Private constructor. Use asSuccess() and asError() static methods to obtain instance of Builder.
*
* @param bool $success
* @param int $api_code
*/
protected function __construct(bool $success, int $api_code)
{
$this->success = $success;
$this->api_code = $api_code;
}
// ---------------------------------------------------------------------------------------------
/**
* Returns success
*
* @param mixed|null $data Array of primitives and supported objects to be returned in
* 'data' node of the JSON response, single supported object
* or @null if there's no to be returned.
* @param integer|null $api_code API code to be returned or @null to use value of
* BaseApiCodes::OK().
* @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders
* substitution or @null if none.
* @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null
* for default DEFAULT_HTTP_CODE_OK.
* @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php for
* supported options or pass @null to use value from your
* config (or defaults).
*
* @throws Ex\MissingConfigurationKeyException
* @throws Ex\ConfigurationNotFoundException
* @throws Ex\IncompatibleTypeException
* @throws Ex\ArrayWithMixedKeysException
* @throws Ex\InvalidTypeException
* @throws Ex\NotIntegerException
*/
public static function success($data = null, int $api_code = null, array $placeholders = null,
int $http_code = null, int $json_opts = null): HttpResponse
{
return static::asSuccess($api_code)
->withData($data)
->withPlaceholders($placeholders)
->withHttpCode($http_code)
->withJsonOptions($json_opts)
->build();
}
/**
* Builds error Response object. Supports optional arguments passed to Lang::get() if associated error
* message uses placeholders as well as return data payload
*
* @param integer $api_code Your API code to be returned with the response object.
* @param array|null $placeholders Placeholders passed to Lang::get() for message
* placeholders substitution or @null if none.
* @param object|array|null $data Array of primitives and supported objects to be
* returned in 'data' node of the JSON response, single
* supported object or @null if there's no to be returned.
* @param integer|null $http_code HTTP code to be used for HttpResponse sent or @null
* for default DEFAULT_HTTP_CODE_ERROR.
* @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php
* for supported options or pass @null to use value from
* your config (or defaults).
*
* @throws Ex\ArrayWithMixedKeysException
* @throws Ex\MissingConfigurationKeyException
* @throws Ex\ConfigurationNotFoundException
* @throws Ex\IncompatibleTypeException
* @throws Ex\InvalidTypeException
* @throws Ex\NotIntegerException
*/
public static function error(int $api_code, array $placeholders = null, $data = null,
int $http_code = null,
int $json_opts = null): HttpResponse
{
return static::asError($api_code)
->withPlaceholders($placeholders)
->withData($data)
->withHttpCode($http_code)
->withJsonOptions($json_opts)
->build();
}
// ---------------------------------------------------------------------------------------------
/**
* @param int|null $api_code
*
* @throws Ex\InvalidTypeException
* @throws Ex\MissingConfigurationKeyException
* @throws Ex\NotIntegerException
*/
public static function asSuccess(int $api_code = null): self
{
/** @noinspection PhpUnhandledExceptionInspection */
return new static(true, $api_code ?? BaseApiCodes::OK());
}
/**
* @param int $api_code
*
* @throws Ex\MissingConfigurationKeyException
* @throws Ex\NotIntegerException
* @throws Ex\InvalidTypeException
*/
public static function asError(int $api_code): self
{
/** @noinspection PhpUnhandledExceptionInspection */
$code_ok = BaseApiCodes::OK();
if ($api_code !== $code_ok) {
/** @noinspection PhpUnhandledExceptionInspection */
Validator::assertIsIntRange('api_code', $api_code,
BaseApiCodes::getMinCode(), BaseApiCodes::getMaxCode());
}
if ($api_code === $code_ok) {
throw new \OutOfBoundsException(
"Error response cannot use api_code of value {$code_ok} which is reserved for OK.");
}
return new static(false, $api_code);
}
/**
* @param int|null $http_code
*
* @throws Ex\InvalidTypeException
*/
public function withHttpCode(int $http_code = null): self
{
Validator::assertIsType('http_code', $http_code, [
Type::INTEGER,
Type::NULL,
]);
$this->http_code = $http_code;
return $this;
}
/**
* @param mixed|null $data
*
* @throws Ex\InvalidTypeException
*/
public function withData($data = null): self
{
Validator::assertIsType('data', $data, [
Type::ARRAY,
Type::BOOLEAN,
Type::INTEGER,
Type::NULL,
Type::OBJECT,
Type::STRING,
Type::DOUBLE,
]);
$this->data = $data;
return $this;
}
/**
* @param int|null $json_opts
*
* @throws Ex\InvalidTypeException
*/
public function withJsonOptions(int $json_opts = null): self
{
Validator::assertIsType('json_opts', $json_opts, [Type::INTEGER,
Type::NULL,
]);
$this->json_opts = $json_opts;
return $this;
}
/**
* @param array|null $debug_data
*
* @throws Ex\InvalidTypeException
*/
public function withDebugData(array $debug_data = null): self
{
Validator::assertIsType('$debug_data', $debug_data, [Type::ARRAY,
Type::NULL,
]);
$this->debug_data = $debug_data;
return $this;
}
/**
* @param string|null $msg
*
* @throws Ex\InvalidTypeException
*/
public function withMessage(string $msg = null): self
{
Validator::assertIsType('message', $msg, [Type::STRING,
Type::NULL,
]);
$this->message = $msg;
return $this;
}
/**
* @param array|null $placeholders
*/
public function withPlaceholders(array $placeholders = null): self
{
$this->placeholders = $placeholders;
return $this;
}
/**
* @param array|null $http_headers
*/
public function withHttpHeaders(array $http_headers = null): self
{
$this->http_headers = $http_headers ?? [];
return $this;
}
/**
* Builds and returns final HttpResponse. It's safe to call build() as many times as needed, as no
* internal state is changed. It's also safe to alter any parameter set previously and call build()
* again to get new response object that includes new changes.
*
* @throws Ex\ArrayWithMixedKeysException
* @throws Ex\ConfigurationNotFoundException
* @throws Ex\IncompatibleTypeException
* @throws Ex\InvalidTypeException
* @throws Ex\MissingConfigurationKeyException
* @throws Ex\NotIntegerException
*/
public function build(): HttpResponse
{
$api_code = $this->api_code;
Validator::assertIsInt('api_code', $api_code);
$msg_or_api_code = $this->message ?? $api_code;
$http_headers = $this->http_headers ?? [];
if ($this->success) {
$http_code = $this->http_code ?? RB::DEFAULT_HTTP_CODE_OK;
Validator::assertOkHttpCode($http_code);
$result = $this->make($this->success, $api_code,
$msg_or_api_code, $this->data, $http_code,
$this->placeholders, $http_headers, $this->json_opts);
} else {
$http_code = $this->http_code ?? RB::DEFAULT_HTTP_CODE_ERROR;
Validator::assertErrorHttpCode($http_code);
$result = $this->make(false, $api_code, $msg_or_api_code,
$this->data, $http_code, $this->placeholders,
$this->http_headers, $this->json_opts, $this->debug_data);
}
return $result;
}
/**
* @param boolean $success TRUE if response reports successful operation,
* FALSE otherwise.
* @param integer $api_code API code to be returned with the response object.
* @param string|integer $msg_or_api_code Message string or valid API code to get message for
* @param mixed|null $data optional additional data to be included in response.
* @param integer|null $http_code HTTP code for the HttpResponse or @null for either
* DEFAULT_HTTP_CODE_OK or DEFAULT_HTTP_CODE_ERROR
* depending on the $success.
* @param array|null $placeholders Placeholders passed to Lang::get() for message
* placeholders substitution or @null if none.
* @param array|null $http_headers Optional HTTP headers to be returned in the response.
* @param integer|null $json_opts See http://php.net/manual/en/function.json-encode.php
* for supported options or pass @null to use value from
* your config (or defaults).
* @param array|null $debug_data Optional debug data array to be added to returned JSON.
*
* @throws Ex\MissingConfigurationKeyException
* @throws Ex\ConfigurationNotFoundException
* @throws Ex\ArrayWithMixedKeysException
* @throws Ex\IncompatibleTypeException
* @throws Ex\InvalidTypeException
* @throws Ex\NotIntegerException
* @throws Ex\NotStringException
*
* @noinspection PhpTooManyParametersInspection
*/
protected function make(bool $success, int $api_code, $msg_or_api_code, $data = null,
int $http_code = null, array $placeholders = null,
array $http_headers = null,
int $json_opts = null, array $debug_data = null): HttpResponse
{
$http_headers = $http_headers ?? [];
$http_code = $http_code ?? ($success ? RB::DEFAULT_HTTP_CODE_OK : RB::DEFAULT_HTTP_CODE_ERROR);
/** @var int $json_opts */
$json_opts = $json_opts ?? Config::get(RB::CONF_KEY_ENCODING_OPTIONS, RB::DEFAULT_ENCODING_OPTIONS);
Validator::assertIsInt('encoding_options', $json_opts);
Validator::assertIsInt('api_code', $api_code);
if (!BaseApiCodes::isCodeValid($api_code)) {
/** @noinspection PhpUnhandledExceptionInspection */
Validator::assertIsIntRange('api_code', $api_code, BaseApiCodes::getMinCode(), BaseApiCodes::getMaxCode());
}
return Response::json(
$this->buildResponse($success, $api_code, $msg_or_api_code, $placeholders, $data, $debug_data),
$http_code, $http_headers, $json_opts);
}
/**
* Creates standardised API response array. This is final method called in the whole pipeline
* before we return final JSON back to client. If you want to manipulate your response, this
* is the place to do that. If you set APP_DEBUG to true, 'code_hex' field will be additionally
* added to reported JSON for easier manual debugging. Returns response ready to be encoded as
* JSON and sent back to client.
*
* @param boolean $success TRUE if response reports successful operation, FALSE otherwise.
* @param integer $api_code Your API code to be returned with the response object.
* @param string|integer $msg_or_api_code Message string or valid API code to get message for.
* @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders
* substitution or @null if none.
* @param mixed|null $data API response data if any
* @param array|null $debug_data optional debug data array to be added to returned JSON.
*
* @throws Ex\ArrayWithMixedKeysException
* @throws Ex\ConfigurationNotFoundException
* @throws Ex\IncompatibleTypeException
* @throws Ex\MissingConfigurationKeyException
* @throws Ex\InvalidTypeException
*
* @noinspection PhpTooManyParametersInspection
*/
protected function buildResponse(bool $success,
int $api_code,
string|int $msg_or_api_code,
?array $placeholders = null,
mixed $data = null,
?array $debug_data = null): array
{
// ensure $data is either @null, array or object of class with configured mapping.
$data = (new Converter())->convert($data);
if ($data !== null) {
// ensure we get object in final JSON structure in data node
$data = (object)$data;
}
if ($data === null && Config::get(RB::CONF_KEY_DATA_ALWAYS_OBJECT, false)) {
$data = (object)[];
}
// get human readable message for API code or use message string (if given instead of API code)
if (\is_int($msg_or_api_code)) {
$message = $this->getMessageForApiCode($success, $msg_or_api_code, $placeholders);
} else {
Validator::assertIsType('message', $msg_or_api_code, [Type::STRING, Type::INTEGER]);
$message = $msg_or_api_code;
}
/** @noinspection PhpUndefinedClassInspection */
$response = [
RB::KEY_SUCCESS => $success,
RB::KEY_CODE => $api_code,
RB::KEY_LOCALE => \App::getLocale(),
RB::KEY_MESSAGE => $message,
RB::KEY_DATA => $data,
];
if ($debug_data !== null) {
$debug_key = Config::get(RB::CONF_KEY_DEBUG_DEBUG_KEY, RB::KEY_DEBUG);
$response[ $debug_key ] = $debug_data;
}
return $response;
}
/**
* If $msg_or_api_code is integer value, returns human readable message associated with that code
* (with fallback to built-in default string if no api code mapping is set. If $msg_or_api_code
* is a string, returns it unaltered.
*
* @param boolean $success TRUE if response reports successful operation, FALSE otherwise.
* @param integer $api_code Your API code to be returned with the response object.
* @param array|null $placeholders Placeholders passed to Lang::get() for message placeholders
* substitution or NULL if none.
*
* @throws Ex\IncompatibleTypeException
* @throws Ex\MissingConfigurationKeyException
* @throws Ex\InvalidTypeException
* @throws Ex\NotIntegerException
*/
protected function getMessageForApiCode(bool $success, int $api_code,
array $placeholders = null): string
{
// We got integer value here not a message string, so we need to check if we have the mapping for
// this string already configured.
$key = BaseApiCodes::getCodeMessageKey($api_code);
if ($key === null) {
// nope, let's get the default one instead, based of
$fallback_code = $success ? BaseApiCodes::OK() : BaseApiCodes::NO_ERROR_MESSAGE();
// default messages are expected to be always available
/** @var string $key */
$key = BaseApiCodes::getCodeMessageKey($fallback_code);
}
$placeholders = $placeholders ?? [];
if (!\array_key_exists('api_code', $placeholders)) {
$placeholders['api_code'] = $api_code;
}
// As Lang::get() is documented to also returning whole language arrays,
// so static analysers will alarm if that case is not taken care of.
$msg = \Lang::get($key, $placeholders);
if (\is_array($msg)) {
$msg = \implode('', $msg);
}
return $msg;
}
} // end of class