| Total Complexity | 6 |
| Total Lines | 63 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 12 | trait Output |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * Get response, set for controller |
||
| 16 | */ |
||
| 17 | abstract protected function getResponse(): ResponseInterface; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Set response. |
||
| 21 | */ |
||
| 22 | abstract protected function setResponse(ResponseInterface $response): void; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Get MIME type for extension |
||
| 26 | */ |
||
| 27 | protected function getMime(string $format): string |
||
| 28 | { |
||
| 29 | // Check if it's already MIME |
||
| 30 | if (str_contains($format, '/')) { |
||
| 31 | return $format; |
||
| 32 | } |
||
| 33 | |||
| 34 | $mime = (new MimeTypes())->getMimeType($format); |
||
| 35 | |||
| 36 | if (!isset($mime)) { |
||
| 37 | throw new \UnexpectedValueException("Format '$format' doesn't correspond with a MIME type"); |
||
| 38 | } |
||
| 39 | |||
| 40 | return $mime; |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Output data as json. |
||
| 45 | * |
||
| 46 | * @param mixed $data |
||
| 47 | * @param int $flags Flags for json_encode |
||
| 48 | * @return $this |
||
| 49 | */ |
||
| 50 | protected function json(mixed $data, int $flags = 0): static |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Output result |
||
| 57 | * |
||
| 58 | * @param string $content |
||
| 59 | * @param string|null $format Output format as MIME or extension |
||
| 60 | * @return $this |
||
| 61 | */ |
||
| 62 | protected function output(string $content, ?string $format = null): static |
||
| 75 | } |
||
| 76 | } |
||
| 77 |