Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| JsonRenderer | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
| json | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace App\Renderer; |
| 6 | |
| 7 | use Psr\Http\Message\ResponseInterface; |
| 8 | |
| 9 | /** |
| 10 | * This class provides functionality for rendering JSON output |
| 11 | * in an HTTP response with the appropriate headers and status code. |
| 12 | */ |
| 13 | final class JsonRenderer |
| 14 | { |
| 15 | public function json( |
| 16 | ResponseInterface $response, |
| 17 | mixed $data = null, |
| 18 | int $statusCode = 200, |
| 19 | ): ResponseInterface { |
| 20 | $response = $response->withHeader('Content-Type', 'application/json') |
| 21 | ->withStatus($statusCode); |
| 22 | |
| 23 | $response->getBody()->write( |
| 24 | (string)json_encode( |
| 25 | $data, |
| 26 | JSON_UNESCAPED_SLASHES | JSON_PARTIAL_OUTPUT_ON_ERROR, |
| 27 | ), |
| 28 | ); |
| 29 | |
| 30 | return $response; |
| 31 | } |
| 32 | } |