Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
91.30% |
21 / 23 |
|
75.00% |
6 / 8 |
CRAP | |
0.00% |
0 / 1 |
| CliHeader | |
91.30% |
21 / 23 |
|
75.00% |
6 / 8 |
16.17 | |
0.00% |
0 / 1 |
| set | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
5 | |||
| getProtocolVersion | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| remove | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| getReasonPhrase | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| get | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| has | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| generate | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getRequestTime | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS\Message\Cli |
| 8 | * @copyright Dennis Eichhorn |
| 9 | * @license OMS License 2.0 |
| 10 | * @version 1.0.0 |
| 11 | * @link https://jingga.app |
| 12 | */ |
| 13 | declare(strict_types=1); |
| 14 | |
| 15 | namespace phpOMS\Message\Cli; |
| 16 | |
| 17 | use phpOMS\Message\HeaderAbstract; |
| 18 | |
| 19 | /** |
| 20 | * Response class. |
| 21 | * |
| 22 | * @package phpOMS\Message\Cli |
| 23 | * @license OMS License 2.0 |
| 24 | * @link https://jingga.app |
| 25 | * @since 1.0.0 |
| 26 | * |
| 27 | * @SuppressWarnings(PHPMD.Superglobals) |
| 28 | */ |
| 29 | final class CliHeader extends HeaderAbstract |
| 30 | { |
| 31 | /** |
| 32 | * Protocol version. |
| 33 | * |
| 34 | * @var string |
| 35 | * @since 1.0.0 |
| 36 | */ |
| 37 | private const VERSION = '1.0'; |
| 38 | |
| 39 | /** |
| 40 | * Header. |
| 41 | * |
| 42 | * @var string[][] |
| 43 | * @since 1.0.0 |
| 44 | */ |
| 45 | private array $header = []; |
| 46 | |
| 47 | /** |
| 48 | * Set header. |
| 49 | * |
| 50 | * @param string $key Header key (case insensitive) |
| 51 | * @param string $header Header value |
| 52 | * @param bool $overwrite Overwrite if already existing |
| 53 | * |
| 54 | * @return bool |
| 55 | * |
| 56 | * @since 1.0.0 |
| 57 | */ |
| 58 | public function set(string $key, string $header, bool $overwrite = false) : bool |
| 59 | { |
| 60 | if ($this->isLocked) { |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | $key = \strtolower($key); |
| 65 | |
| 66 | if (!$overwrite && isset($this->header[$key])) { |
| 67 | return false; |
| 68 | } |
| 69 | |
| 70 | unset($this->header[$key]); |
| 71 | |
| 72 | if (!isset($this->header[$key])) { |
| 73 | $this->header[$key] = []; |
| 74 | } |
| 75 | |
| 76 | $this->header[$key][] = $header; |
| 77 | |
| 78 | return true; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * {@inheritdoc} |
| 83 | */ |
| 84 | public function getProtocolVersion() : string |
| 85 | { |
| 86 | return self::VERSION; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Remove header by ID. |
| 91 | * |
| 92 | * @param string $key Header key |
| 93 | * |
| 94 | * @return bool |
| 95 | * |
| 96 | * @since 1.0.0 |
| 97 | */ |
| 98 | public function remove(string $key) : bool |
| 99 | { |
| 100 | if ($this->isLocked) { |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | if (isset($this->header[$key])) { |
| 105 | unset($this->header[$key]); |
| 106 | |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | return false; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * {@inheritdoc} |
| 115 | */ |
| 116 | public function getReasonPhrase() : string |
| 117 | { |
| 118 | $phrases = $this->get('Status'); |
| 119 | |
| 120 | return empty($phrases) ? '' : $phrases[0]; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Get header by name. |
| 125 | * |
| 126 | * @param null|string $key Header key |
| 127 | * |
| 128 | * @return array |
| 129 | * |
| 130 | * @since 1.0.0 |
| 131 | */ |
| 132 | public function get(string $key = null) : array |
| 133 | { |
| 134 | return $key === null ? $this->header : ($this->header[\strtolower($key)] ?? []); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Check if header is defined. |
| 139 | * |
| 140 | * @param string $key Header key |
| 141 | * |
| 142 | * @return bool |
| 143 | * |
| 144 | * @since 1.0.0 |
| 145 | */ |
| 146 | public function has(string $key) : bool |
| 147 | { |
| 148 | return isset($this->header[$key]); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * {@inheritdoc} |
| 153 | */ |
| 154 | public function generate(int $code) : void |
| 155 | { |
| 156 | exit($code); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * {@inheritdoc} |
| 161 | */ |
| 162 | public function getRequestTime() : int |
| 163 | { |
| 164 | return $this->timestamp; |
| 165 | } |
| 166 | } |