| Total Complexity | 3 |
| Total Lines | 31 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 12 | final class JsonDataResponseFormatter implements DataResponseFormatterInterface |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * @var string the Content-Type header for the response |
||
| 16 | */ |
||
| 17 | private string $contentType = 'application/json'; |
||
| 18 | |||
| 19 | private int $options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE; |
||
| 20 | |||
| 21 | public function format(DataResponse $dataResponse): ResponseInterface |
||
| 22 | { |
||
| 23 | $jsonSerializer = new JsonSerializer($this->options); |
||
| 24 | $content = $jsonSerializer->serialize($dataResponse->getData()); |
||
| 25 | $response = $dataResponse->getResponse(); |
||
| 26 | $response->getBody()->write($content); |
||
| 27 | |||
| 28 | return $response->withHeader('Content-Type', $this->contentType); |
||
| 29 | } |
||
| 30 | |||
| 31 | public function withOptions(int $options): self |
||
| 32 | { |
||
| 33 | $formatter = clone $this; |
||
| 34 | $formatter->options = $options; |
||
| 35 | return $formatter; |
||
| 36 | } |
||
| 37 | |||
| 38 | public function withContentType(string $contentType): self |
||
| 43 | } |
||
| 44 | } |
||
| 45 |