Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
7 / 7 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| Response | |
100.00% |
7 / 7 |
|
100.00% |
4 / 4 |
7 | |
100.00% |
1 / 1 |
| clearHeaders | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setHeader | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| header | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| send | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Dynart\Micro; |
| 4 | |
| 5 | class Response implements ResponseInterface { |
| 6 | |
| 7 | /** Stores the headers for the response */ |
| 8 | protected array $headers = []; |
| 9 | |
| 10 | public function clearHeaders(): void { |
| 11 | $this->headers = []; |
| 12 | } |
| 13 | |
| 14 | public function setHeader(string $name, string $value): void { |
| 15 | $this->headers[$name] = $value; |
| 16 | } |
| 17 | |
| 18 | public function header(string $name, mixed $default = null): mixed { |
| 19 | return array_key_exists($name, $this->headers) ? $this->headers[$name] : $default; |
| 20 | } |
| 21 | |
| 22 | public function send(string $content = ''): void { |
| 23 | $sendHeaderFunction = function_exists('header') ? function ($n, $v) { header($n.': '.$v); } : function($n, $v) {}; // because of CLI |
| 24 | foreach ($this->headers as $name => $value) { |
| 25 | $sendHeaderFunction($name, $value); |
| 26 | } |
| 27 | echo $content; |
| 28 | } |
| 29 | } |