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