Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
10.29% |
7 / 68 |
|
14.29% |
2 / 14 |
CRAP | |
0.00% |
0 / 1 |
| ResponseAbstract | |
10.29% |
7 / 68 |
|
14.29% |
2 / 14 |
1254.47 | |
0.00% |
0 / 1 |
| get | |
29.41% |
5 / 17 |
|
0.00% |
0 / 1 |
53.56 | |||
| getDataString | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getDataArray | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| getDataInt | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getDataFloat | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getDataBool | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getDataDateTime | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| getDataJson | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| getDataList | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| getLike | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| hasData | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| set | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| jsonSerialize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| toArray | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| getLanguage | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getBody | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| 1 | <?php |
| 2 | /** |
| 3 | * Jingga |
| 4 | * |
| 5 | * PHP Version 8.1 |
| 6 | * |
| 7 | * @package phpOMS\Message |
| 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; |
| 16 | |
| 17 | use phpOMS\Localization\ISO639x1Enum; |
| 18 | |
| 19 | /** |
| 20 | * Response abstract class. |
| 21 | * |
| 22 | * @package phpOMS\Message |
| 23 | * @license OMS License 2.0 |
| 24 | * @link https://jingga.app |
| 25 | * @since 1.0.0 |
| 26 | */ |
| 27 | abstract class ResponseAbstract implements \JsonSerializable, MessageInterface |
| 28 | { |
| 29 | /** |
| 30 | * Responses. |
| 31 | * |
| 32 | * @var array |
| 33 | * @since 1.0.0 |
| 34 | */ |
| 35 | public array $data = []; |
| 36 | |
| 37 | /** |
| 38 | * Header. |
| 39 | * |
| 40 | * @var HeaderAbstract |
| 41 | * @since 1.0.0 |
| 42 | */ |
| 43 | public HeaderAbstract $header; |
| 44 | |
| 45 | /** |
| 46 | * Get response by ID. |
| 47 | * |
| 48 | * @param mixed $key Response ID |
| 49 | * |
| 50 | * @return mixed |
| 51 | * |
| 52 | * @since 1.0.0 |
| 53 | */ |
| 54 | public function get(mixed $key, string $type = null) : mixed |
| 55 | { |
| 56 | if ($key === null) { |
| 57 | return $this->data; |
| 58 | } |
| 59 | |
| 60 | $key = \is_string($key) ? \mb_strtolower($key) : $key; |
| 61 | if (!isset($this->data[$key])) { |
| 62 | return null; |
| 63 | } |
| 64 | |
| 65 | switch ($type) { |
| 66 | case null: |
| 67 | return $this->data[$key]; |
| 68 | case 'int': |
| 69 | return (int) $this->data[$key]; |
| 70 | case 'string': |
| 71 | return (string) $this->data[$key]; |
| 72 | case 'float': |
| 73 | return (float) $this->data[$key]; |
| 74 | case 'bool': |
| 75 | return (bool) $this->data[$key]; |
| 76 | case 'DateTime': |
| 77 | return new \DateTime((string) $this->data[$key]); |
| 78 | default: |
| 79 | return $this->data[$key]; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Get data. |
| 85 | * |
| 86 | * @param string $key Data key |
| 87 | * |
| 88 | * @return null|string |
| 89 | * |
| 90 | * @since 1.0.0 |
| 91 | */ |
| 92 | public function getDataString(string $key) : ?string |
| 93 | { |
| 94 | $key = \mb_strtolower($key); |
| 95 | |
| 96 | if (($this->data[$key] ?? '') === '') { |
| 97 | return null; |
| 98 | } |
| 99 | |
| 100 | return (string) $this->data[$key]; |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Get data. |
| 105 | * |
| 106 | * @param string $key Data key |
| 107 | * |
| 108 | * @return null|array |
| 109 | * |
| 110 | * @since 1.0.0 |
| 111 | */ |
| 112 | public function getDataArray(string $key) : ?array |
| 113 | { |
| 114 | $key = \mb_strtolower($key); |
| 115 | |
| 116 | if (($this->data[$key] ?? '') === '' || !\is_array($this->data[$key])) { |
| 117 | return null; |
| 118 | } |
| 119 | |
| 120 | return $this->data[$key]; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Get data. |
| 125 | * |
| 126 | * @param string $key Data key |
| 127 | * |
| 128 | * @return null|int |
| 129 | * |
| 130 | * @since 1.0.0 |
| 131 | */ |
| 132 | public function getDataInt(string $key) : ?int |
| 133 | { |
| 134 | $key = \mb_strtolower($key); |
| 135 | |
| 136 | if (($this->data[$key] ?? '') === '') { |
| 137 | return null; |
| 138 | } |
| 139 | |
| 140 | return (int) $this->data[$key]; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Get data. |
| 145 | * |
| 146 | * @param string $key Data key |
| 147 | * |
| 148 | * @return null|float |
| 149 | * |
| 150 | * @since 1.0.0 |
| 151 | */ |
| 152 | public function getDataFloat(string $key) : ?float |
| 153 | { |
| 154 | $key = \mb_strtolower($key); |
| 155 | |
| 156 | if (($this->data[$key] ?? '') === '') { |
| 157 | return null; |
| 158 | } |
| 159 | |
| 160 | return (float) $this->data[$key]; |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Get data. |
| 165 | * |
| 166 | * @param string $key Data key |
| 167 | * |
| 168 | * @return null|bool |
| 169 | * |
| 170 | * @since 1.0.0 |
| 171 | */ |
| 172 | public function getDataBool(string $key) : ?bool |
| 173 | { |
| 174 | $key = \mb_strtolower($key); |
| 175 | |
| 176 | if (($this->data[$key] ?? '') === '') { |
| 177 | return null; |
| 178 | } |
| 179 | |
| 180 | return (bool) $this->data[$key]; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Get data. |
| 185 | * |
| 186 | * @param string $key Data key |
| 187 | * |
| 188 | * @return null|\DateTime |
| 189 | * |
| 190 | * @since 1.0.0 |
| 191 | */ |
| 192 | public function getDataDateTime(string $key) : ?\DateTime |
| 193 | { |
| 194 | $key = \mb_strtolower($key); |
| 195 | |
| 196 | return empty($this->data[$key] ?? null) |
| 197 | ? null |
| 198 | : new \DateTime((string) $this->data[$key]); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Get data. |
| 203 | * |
| 204 | * @param string $key Data key |
| 205 | * |
| 206 | * @return array |
| 207 | * |
| 208 | * @since 1.0.0 |
| 209 | */ |
| 210 | public function getDataJson(string $key) : array |
| 211 | { |
| 212 | $key = \mb_strtolower($key); |
| 213 | |
| 214 | if (($this->data[$key] ?? '') === '') { |
| 215 | return []; |
| 216 | } |
| 217 | |
| 218 | $json = \json_decode($this->data[$key], true); /** @phpstan-ignore-line */ |
| 219 | |
| 220 | return \is_array($json) ? $json : []; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Get data. |
| 225 | * |
| 226 | * @param string $key Data key |
| 227 | * @param string $delim Data delimiter |
| 228 | * |
| 229 | * @return array |
| 230 | * |
| 231 | * @since 1.0.0 |
| 232 | */ |
| 233 | public function getDataList(string $key, string $delim = ',') : array |
| 234 | { |
| 235 | $key = \mb_strtolower($key); |
| 236 | |
| 237 | if (($this->data[$key] ?? '') === '') { |
| 238 | return []; |
| 239 | } |
| 240 | |
| 241 | /* @phpstan-ignore-next-line */ |
| 242 | $list = \explode($delim, $this->data[$key]); |
| 243 | |
| 244 | if ($list === false) { |
| 245 | return []; // @codeCoverageIgnore |
| 246 | } |
| 247 | |
| 248 | foreach ($list as $i => $e) { |
| 249 | $list[$i] = \trim($e); |
| 250 | } |
| 251 | |
| 252 | return $list; |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Get data based on wildcard. |
| 257 | * |
| 258 | * @param string $regex Regex data key |
| 259 | * |
| 260 | * @return array |
| 261 | * |
| 262 | * @since 1.0.0 |
| 263 | */ |
| 264 | public function getLike(string $regex) : array |
| 265 | { |
| 266 | $data = []; |
| 267 | foreach ($this->data as $key => $value) { |
| 268 | if (\preg_match('/' . $regex . '/', (string) $key) === 1) { |
| 269 | $data[$key] = $value; |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | return $data; |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Check if has data. |
| 278 | * |
| 279 | * The following empty values are considered as not set (null, '', 0) |
| 280 | * |
| 281 | * @param string $key Data key |
| 282 | * |
| 283 | * @return bool |
| 284 | * |
| 285 | * @since 1.0.0 |
| 286 | */ |
| 287 | public function hasData(string $key) : bool |
| 288 | { |
| 289 | $key = \mb_strtolower($key); |
| 290 | |
| 291 | return isset($this->data[$key]) |
| 292 | && $this->data[$key] !== '' |
| 293 | && $this->data[$key] !== null; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Add response. |
| 298 | * |
| 299 | * @param mixed $key Response id |
| 300 | * @param mixed $response Response to add |
| 301 | * @param bool $overwrite Overwrite |
| 302 | * |
| 303 | * @return void |
| 304 | * |
| 305 | * @since 1.0.0 |
| 306 | */ |
| 307 | public function set(mixed $key, mixed $response, bool $overwrite = false) : void |
| 308 | { |
| 309 | $this->data[$key] = $response; |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * {@inheritdoc} |
| 314 | */ |
| 315 | public function jsonSerialize() : mixed |
| 316 | { |
| 317 | return $this->toArray(); |
| 318 | } |
| 319 | |
| 320 | /** |
| 321 | * Generate response array from views. |
| 322 | * |
| 323 | * @return array |
| 324 | * |
| 325 | * @throws \Exception |
| 326 | * |
| 327 | * @since 1.0.0 |
| 328 | */ |
| 329 | abstract public function toArray() : array; |
| 330 | |
| 331 | /** |
| 332 | * Get response language. |
| 333 | * |
| 334 | * @return string |
| 335 | * |
| 336 | * @since 1.0.0 |
| 337 | */ |
| 338 | public function getLanguage() : string |
| 339 | { |
| 340 | if (!isset($this->header)) { |
| 341 | return ISO639x1Enum::_EN; |
| 342 | } |
| 343 | |
| 344 | return $this->header->l11n->language; |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Get response body. |
| 349 | * |
| 350 | * @param bool $optimize Optimize response / minify |
| 351 | * |
| 352 | * @return string |
| 353 | * |
| 354 | * @since 1.0.0 |
| 355 | */ |
| 356 | abstract public function getBody(bool $optimize = false) : string; |
| 357 | } |