13
13
14
14
namespace CodeIgniter \API ;
15
15
16
+ use CodeIgniter \Format \Format ;
16
17
use CodeIgniter \Format \FormatterInterface ;
17
18
use CodeIgniter \HTTP \IncomingRequest ;
19
+ use CodeIgniter \HTTP \RequestInterface ;
18
20
use CodeIgniter \HTTP \ResponseInterface ;
19
21
20
22
/**
21
23
* Provides common, more readable, methods to provide
22
24
* consistent HTTP responses under a variety of common
23
25
* situations when working as an API.
24
26
*
25
- * @property bool $stringAsHtml Whether to treat string data as HTML in JSON response.
26
- * Setting `true` is only for backward compatibility.
27
+ * @property RequestInterface $request
28
+ * @property ResponseInterface $response
29
+ * @property bool $stringAsHtml Whether to treat string data as HTML in JSON response.
30
+ * Setting `true` is only for backward compatibility.
27
31
*/
28
32
trait ResponseTrait
29
33
{
@@ -84,7 +88,7 @@ trait ResponseTrait
84
88
* Provides a single, simple method to return an API response, formatted
85
89
* to match the requested format, with proper content-type and status code.
86
90
*
87
- * @param array|string|null $data
91
+ * @param array<string, mixed> |string|null $data
88
92
*
89
93
* @return ResponseInterface
90
94
*/
@@ -118,9 +122,9 @@ protected function respond($data = null, ?int $status = null, string $message =
118
122
/**
119
123
* Used for generic failures that no custom methods exist for.
120
124
*
121
- * @param array |string $messages
122
- * @param int $status HTTP status code
123
- * @param string|null $code Custom, API-specific, error code
125
+ * @param list<string> |string $messages
126
+ * @param int $status HTTP status code
127
+ * @param string|null $code Custom, API-specific, error code
124
128
*
125
129
* @return ResponseInterface
126
130
*/
@@ -146,7 +150,7 @@ protected function fail($messages, int $status = 400, ?string $code = null, stri
146
150
/**
147
151
* Used after successfully creating a new resource.
148
152
*
149
- * @param array|string|null $data
153
+ * @param array<string, mixed> |string|null $data
150
154
*
151
155
* @return ResponseInterface
152
156
*/
@@ -158,7 +162,7 @@ protected function respondCreated($data = null, string $message = '')
158
162
/**
159
163
* Used after a resource has been successfully deleted.
160
164
*
161
- * @param array|string|null $data
165
+ * @param array<string, mixed> |string|null $data
162
166
*
163
167
* @return ResponseInterface
164
168
*/
@@ -170,7 +174,7 @@ protected function respondDeleted($data = null, string $message = '')
170
174
/**
171
175
* Used after a resource has been successfully updated.
172
176
*
173
- * @param array|string|null $data
177
+ * @param array<string, mixed> |string|null $data
174
178
*
175
179
* @return ResponseInterface
176
180
*/
@@ -287,15 +291,17 @@ protected function failServerError(string $description = 'Internal Server Error'
287
291
* Handles formatting a response. Currently, makes some heavy assumptions
288
292
* and needs updating! :)
289
293
*
290
- * @param array|string|null $data
294
+ * @param array<string, mixed> |string|null $data
291
295
*
292
296
* @return string|null
293
297
*/
294
298
protected function format ($ data = null )
295
299
{
300
+ /** @var Format $format */
296
301
$ format = service ('format ' );
297
302
298
- $ mime = ($ this ->format === null ) ? $ format ->getConfig ()->supportedResponseFormats [0 ]
303
+ $ mime = $ this ->format === null
304
+ ? $ format ->getConfig ()->supportedResponseFormats [0 ]
299
305
: "application/ {$ this ->format }" ;
300
306
301
307
// Determine correct response type through content negotiation if not explicitly declared
@@ -313,14 +319,10 @@ protected function format($data = null)
313
319
$ this ->response ->setContentType ($ mime );
314
320
315
321
// if we don't have a formatter, make one
316
- if (! isset ($ this ->formatter )) {
317
- // if no formatter, use the default
318
- $ this ->formatter = $ format ->getFormatter ($ mime );
319
- }
322
+ $ this ->formatter ??= $ format ->getFormatter ($ mime );
320
323
321
324
$ asHtml = $ this ->stringAsHtml ?? false ;
322
325
323
- // Returns as HTML.
324
326
if (
325
327
($ mime === 'application/json ' && $ asHtml && is_string ($ data ))
326
328
|| ($ mime !== 'application/json ' && is_string ($ data ))
@@ -338,6 +340,7 @@ protected function format($data = null)
338
340
if ($ mime !== 'application/json ' ) {
339
341
// Recursively convert objects into associative arrays
340
342
// Conversion not required for JSONFormatter
343
+ /** @var array<string, mixed>|string|null $data */
341
344
$ data = json_decode (json_encode ($ data ), true );
342
345
}
343
346
@@ -353,7 +356,7 @@ protected function format($data = null)
353
356
*/
354
357
protected function setResponseFormat (?string $ format = null )
355
358
{
356
- $ this ->format = ( $ format === null ) ? null : strtolower ($ format );
359
+ $ this ->format = $ format === null ? null : strtolower ($ format );
357
360
358
361
return $ this ;
359
362
}
0 commit comments