Debian Codegraph @

lib/3rdParty/composer/webklex/php-imap/src/Connection/Protocols/Response.php

raw symbols SCIP
1 <?php
2 /*
3 * File: Response.php
4 * Category: -
5 * Author: M.Goldenbaum
6 * Created: 30.12.22 19:46
7 * Updated: -
8 *
9 * Description:
10 * -
11 */
12
13
14 namespace Webklex\PHPIMAP\Connection\Protocols;
15
16 use Webklex\PHPIMAP\Exceptions\ResponseException;
17
18 /**
19 * Class Response
20 *
21 * @package Webklex\PHPIMAP\Connection\Protocols
22 */
23 class Response {
24
25 /**
26 * The commands used to fetch or manipulate data
27 * @var array $command
28 */
29 protected array $commands = [];
30
31 /**
32 * The original response received
33 * @var array $response
34 */
35 protected array $response = [];
36
37 /**
38 * Errors that have occurred while fetching or parsing the response
39 * @var array $errors
40 */
41 protected array $errors = [];
42
43 /**
44 * Result to be returned
45 * @var mixed|null $result
46 */
47 protected mixed $result = null;
48
49 /**
50 * Noun to identify the request / response
51 * @var int $noun
52 */
53 protected int $noun = 0;
54
55 /**
56 * Other related responses
57 * @var array $response_stack
58 */
59 protected array $response_stack = [];
60
61 /**
62 * Debug flag
63 * @var bool $debug
64 */
65 protected bool $debug = false;
66
67 /**
68 * Can the response be empty?
69 * @var bool $can_be_empty
70 */
71 protected bool $can_be_empty = false;
72
73 /**
74 * Create a new Response instance
75 */
76 public function __construct(int $noun, bool $debug = false) {
77 $this->debug = $debug;
78 $this->noun = $noun > 0 ? $noun : (int)str_replace(".", "", (string)microtime(true));
79 }
80
81 /**
82 * Make a new response instance
83 * @param int $noun
84 * @param array $commands
85 * @param array $responses
86 * @param bool $debug
87 *
88 * @return Response
89 */
90 public static function make(int $noun, array $commands = [], array $responses = [], bool $debug = false): Response {
91 return (new self($noun, $debug))->setCommands($commands)->setResponse($responses);
92 }
93
94 /**
95 * Create a new empty response
96 * @param bool $debug
97 *
98 * @return Response
99 */
100 public static function empty(bool $debug = false): Response {
101 return (new self(0, $debug));
102 }
103
104 /**
105 * Stack another response
106 * @param Response $response
107 *
108 * @return void
109 */
110 public function stack(Response $response): void {
111 $this->response_stack[] = $response;
112 }
113
114 /**
115 * Get the associated response stack
116 *
117 * @return array
118 */
119 public function getStack(): array {
120 return $this->response_stack;
121 }
122
123 /**
124 * Get all assigned commands
125 *
126 * @return array
127 */
128 public function getCommands(): array {
129 return $this->commands;
130 }
131
132 /**
133 * Add a new command
134 * @param string $command
135 *
136 * @return Response
137 */
138 public function addCommand(string $command): Response {
139 $this->commands[] = $command;
140 return $this;
141 }
142
143 /**
144 * Set and overwrite all commands
145 * @param array $commands
146 *
147 * @return Response
148 */
149 public function setCommands(array $commands): Response {
150 $this->commands = $commands;
151 return $this;
152 }
153
154 /**
155 * Get all set errors
156 *
157 * @return array
158 */
159 public function getErrors(): array {
160 $errors = $this->errors;
161 foreach($this->getStack() as $response) {
162 $errors = array_merge($errors, $response->getErrors());
163 }
164 return $errors;
165 }
166
167 /**
168 * Set and overwrite all existing errors
169 * @param array $errors
170 *
171 * @return Response
172 */
173 public function setErrors(array $errors): Response {
174 $this->errors = $errors;
175 return $this;
176 }
177
178 /**
179 * Set the response
180 * @param string $error
181 *
182 * @return Response
183 */
184 public function addError(string $error): Response {
185 $this->errors[] = $error;
186 return $this;
187 }
188
189 /**
190 * Set the response
191 * @param array $response
192 *
193 * @return Response
194 */
195 public function addResponse(mixed $response): Response {
196 $this->response[] = $response;
197 return $this;
198 }
199
200 /**
201 * Set the response
202 * @param array $response
203 *
204 * @return Response
205 */
206 public function setResponse(array $response): Response {
207 $this->response = $response;
208 return $this;
209 }
210
211 /**
212 * Get the assigned response
213 *
214 * @return array
215 */
216 public function getResponse(): array {
217 return $this->response;
218 }
219
220 /**
221 * Set the result data
222 * @param mixed $result
223 *
224 * @return Response
225 */
226 public function setResult(mixed $result): Response {
227 $this->result = $result;
228 return $this;
229 }
230
231 /**
232 * Wrap a result bearing action
233 * @param callable $callback
234 *
235 * @return Response
236 */
237 public function wrap(callable $callback): Response {
238 $this->result = call_user_func($callback, $this);
239 return $this;
240 }
241
242 /**
243 * Get the response data
244 *
245 * @return mixed
246 */
247 public function data(): mixed {
248 if ($this->result !== null) {
249 return $this->result;
250 }
251 return $this->getResponse();
252 }
253
254 /**
255 * Get the response data as array
256 *
257 * @return array
258 */
259 public function array(): array {
260 $data = $this->data();
261 if(is_array($data)){
262 return $data;
263 }
264 return [$data];
265 }
266
267 /**
268 * Get the response data as string
269 *
270 * @return string
271 */
272 public function string(): string {
273 $data = $this->data();
274 if(is_array($data)){
275 return implode(" ", $data);
276 }
277 return (string)$data;
278 }
279
280 /**
281 * Get the response data as integer
282 *
283 * @return int
284 */
285 public function integer(): int {
286 $data = $this->data();
287 if(is_array($data) && isset($data[0])){
288 return (int)$data[0];
289 }
290 return (int)$data;
291 }
292
293 /**
294 * Get the response data as boolean
295 *
296 * @return bool
297 */
298 public function boolean(): bool {
299 return (bool)$this->data();
300 }
301
302 /**
303 * Validate and retrieve the response data
304 *
305 * @throws ResponseException
306 */
307 public function validatedData(): mixed {
308 return $this->validate()->data();
309 }
310
311 /**
312 * Validate the response date
313 *
314 * @throws ResponseException
315 */
316 public function validate(): Response {
317 if ($this->failed()) {
318 throw ResponseException::make($this, $this->debug);
319 }
320 return $this;
321 }
322
323 /**
324 * Check if the Response can be considered successful
325 *
326 * @return bool
327 */
328 public function successful(): bool {
329 foreach(array_merge($this->getResponse(), $this->array()) as $data) {
330 if (!$this->verify_data($data)) {
331 return false;
332 }
333 }
334 foreach($this->getStack() as $response) {
335 if (!$response->successful()) {
336 return false;
337 }
338 }
339 return ($this->boolean() || $this->canBeEmpty()) && !$this->getErrors();
340 }
341
342
343 /**
344 * Check if the Response can be considered failed
345 * @param mixed $data
346 *
347 * @return bool
348 */
349 public function verify_data(mixed $data): bool {
350 if (is_array($data)) {
351 foreach ($data as $line) {
352 if (is_array($line)) {
353 if(!$this->verify_data($line)){
354 return false;
355 }
356 }else{
357 if (!$this->verify_line((string)$line)) {
358 return false;
359 }
360 }
361 }
362 }else{
363 if (!$this->verify_line((string)$data)) {
364 return false;
365 }
366 }
367 return true;
368 }
369
370 /**
371 * Verify a single line
372 * @param string $line
373 *
374 * @return bool
375 */
376 public function verify_line(string $line): bool {
377 return !str_starts_with($line, "TAG".$this->noun." BAD ") && !str_starts_with($line, "TAG".$this->noun." NO ");
378 }
379
380 /**
381 * Check if the Response can be considered failed
382 *
383 * @return bool
384 */
385 public function failed(): bool {
386 return !$this->successful();
387 }
388
389 /**
390 * Get the Response noun
391 *
392 * @return int
393 */
394 public function Noun(): int {
395 return $this->noun;
396 }
397
398 /**
399 * Set the Response to be allowed to be empty
400 * @param bool $can_be_empty
401 *
402 * @return $this
403 */
404 public function setCanBeEmpty(bool $can_be_empty): Response {
405 $this->can_be_empty = $can_be_empty;
406 return $this;
407 }
408
409 /**
410 * Check if the Response can be empty
411 *
412 * @return bool
413 */
414 public function canBeEmpty(): bool {
415 return $this->can_be_empty;
416 }
417 }