samuelgfeller /
slim-example-project
| 1 | <?php |
||
| 2 | |||
| 3 | namespace App\Application\Responder; |
||
| 4 | |||
| 5 | use Psr\Http\Message\ResponseInterface; |
||
| 6 | |||
| 7 | final readonly class JsonResponder |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 8 | { |
||
| 9 | /** |
||
| 10 | * Write JSON to the response body. |
||
| 11 | * |
||
| 12 | * @param ResponseInterface $response The response |
||
| 13 | * @param mixed $data The data |
||
| 14 | * @param int $status |
||
| 15 | * |
||
| 16 | * @return ResponseInterface The response |
||
| 17 | */ |
||
| 18 | 143 | public function encodeAndAddToResponse( |
|
| 19 | ResponseInterface $response, |
||
| 20 | mixed $data = null, |
||
| 21 | int $status = 200, |
||
| 22 | ): ResponseInterface { |
||
| 23 | 143 | $response->getBody()->write((string)json_encode($data, JSON_UNESCAPED_SLASHES | JSON_PARTIAL_OUTPUT_ON_ERROR)); |
|
| 24 | 143 | $response = $response->withStatus($status); |
|
| 25 | |||
| 26 | 143 | return $response->withHeader('Content-Type', 'application/json'); |
|
| 27 | } |
||
| 28 | } |
||
| 29 |