Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
36 / 36 |
|
100.00% |
7 / 7 |
CRAP | |
100.00% |
1 / 1 |
CliResponse | |
100.00% |
36 / 36 |
|
100.00% |
7 / 7 |
18 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
setResponse | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
remove | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
getBody | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
render | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
getRaw | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
3 | |||
toArray | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
7 |
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\Contract\RenderableInterface; |
18 | use phpOMS\Localization\Localization; |
19 | use phpOMS\Log\FileLogger; |
20 | use phpOMS\Message\Http\RequestStatusCode; |
21 | use phpOMS\Message\ResponseAbstract; |
22 | use phpOMS\System\MimeType; |
23 | use phpOMS\Utils\StringUtils; |
24 | use phpOMS\Views\View; |
25 | |
26 | /** |
27 | * Response class. |
28 | * |
29 | * @package phpOMS\Message\Cli |
30 | * @license OMS License 2.0 |
31 | * @link https://jingga.app |
32 | * @since 1.0.0 |
33 | */ |
34 | final class CliResponse extends ResponseAbstract implements RenderableInterface |
35 | { |
36 | /** |
37 | * Response status. |
38 | * |
39 | * @var int |
40 | * @since 1.0.0 |
41 | */ |
42 | protected int $status = RequestStatusCode::R_200; |
43 | |
44 | /** |
45 | * Constructor. |
46 | * |
47 | * @param Localization $l11n Localization |
48 | * |
49 | * @since 1.0.0 |
50 | */ |
51 | public function __construct(Localization $l11n = null) |
52 | { |
53 | $this->header = new CliHeader(); |
54 | $this->header->l11n = $l11n ?? new Localization(); |
55 | } |
56 | |
57 | /** |
58 | * Set response. |
59 | * |
60 | * @param array $response Response to set |
61 | * |
62 | * @return void |
63 | * |
64 | * @since 1.0.0 |
65 | */ |
66 | public function setResponse(array $response) : void |
67 | { |
68 | $this->data = $response; |
69 | } |
70 | |
71 | /** |
72 | * Remove response by ID. |
73 | * |
74 | * @param string $id Response ID |
75 | * |
76 | * @return bool |
77 | * |
78 | * @since 1.0.0 |
79 | */ |
80 | public function remove(string $id) : bool |
81 | { |
82 | if (isset($this->data[$id])) { |
83 | unset($this->data[$id]); |
84 | |
85 | return true; |
86 | } |
87 | |
88 | return false; |
89 | } |
90 | |
91 | /** |
92 | * {@inheritdoc} |
93 | */ |
94 | public function getBody(bool $optimize = false) : string |
95 | { |
96 | return $this->render($optimize); |
97 | } |
98 | |
99 | /** |
100 | * Generate response based on header. |
101 | * |
102 | * @param mixed ...$data Data passt to render function. (0 => bool: $optimize) |
103 | * |
104 | * @return string |
105 | * |
106 | * @since 1.0.0 |
107 | */ |
108 | public function render(mixed ...$data) : string |
109 | { |
110 | $types = $this->header->get('Content-Type'); |
111 | |
112 | foreach ($types as $type) { |
113 | if (\stripos($type, MimeType::M_JSON) !== false) { |
114 | return (string) \json_encode($this->jsonSerialize()); |
115 | } |
116 | } |
117 | |
118 | /** @var array{0:bool} $data */ |
119 | return $this->getRaw($data[0] ?? false); |
120 | } |
121 | |
122 | /** |
123 | * Generate raw response. |
124 | * |
125 | * @param bool $optimize Optimize response / minify |
126 | * |
127 | * @return string |
128 | * |
129 | * @throws \Exception this exception is thrown if the response cannot be rendered |
130 | * |
131 | * @since 1.0.0 |
132 | */ |
133 | private function getRaw(bool $optimize = false) : string |
134 | { |
135 | $render = ''; |
136 | |
137 | foreach ($this->data as $response) { |
138 | $render .= StringUtils::stringify($response); |
139 | } |
140 | |
141 | return $optimize ? \trim($render) : $render; |
142 | } |
143 | |
144 | /** |
145 | * {@inheritdoc} |
146 | */ |
147 | public function toArray() : array |
148 | { |
149 | $result = []; |
150 | |
151 | foreach ($this->data as $response) { |
152 | if ($response instanceof View) { |
153 | $result[] = $response->toArray(); |
154 | } elseif (\is_array($response) || \is_scalar($response)) { |
155 | $result[] = $response; |
156 | } elseif ($response instanceof \JsonSerializable) { |
157 | $result[] = $response->jsonSerialize(); |
158 | } elseif ($response === null) { |
159 | continue; |
160 | } else { |
161 | FileLogger::getInstance() |
162 | ->error( |
163 | FileLogger::MSG_FULL, [ |
164 | 'message' => 'Unknown type.', |
165 | 'line' => __LINE__, |
166 | 'file' => self::class, |
167 | ] |
168 | ); |
169 | } |
170 | } |
171 | |
172 | return $result; |
173 | } |
174 | } |