| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
namespace Webklex\PHPIMAP\Connection\Protocols; |
| 15 |
|
| 16 |
use Webklex\PHPIMAP\Exceptions\ResponseException; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
class Response { |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
protected array $commands = []; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
protected array $response = []; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
protected array $errors = []; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
protected mixed $result = null; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
protected int $noun = 0; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
protected array $response_stack = []; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
protected bool $debug = false; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
protected bool $can_be_empty = false; |
| 72 |
|
| 73 |
|
| 74 |
|
| 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 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 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 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public static function empty(bool $debug = false): Response { |
| 101 |
return (new self(0, $debug)); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public function stack(Response $response): void { |
| 111 |
$this->response_stack[] = $response; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public function getStack(): array { |
| 120 |
return $this->response_stack; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public function getCommands(): array { |
| 129 |
return $this->commands; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public function addCommand(string $command): Response { |
| 139 |
$this->commands[] = $command; |
| 140 |
return $this; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public function setCommands(array $commands): Response { |
| 150 |
$this->commands = $commands; |
| 151 |
return $this; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 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 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public function setErrors(array $errors): Response { |
| 174 |
$this->errors = $errors; |
| 175 |
return $this; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
public function addError(string $error): Response { |
| 185 |
$this->errors[] = $error; |
| 186 |
return $this; |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
public function addResponse(mixed $response): Response { |
| 196 |
$this->response[] = $response; |
| 197 |
return $this; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
public function setResponse(array $response): Response { |
| 207 |
$this->response = $response; |
| 208 |
return $this; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
public function getResponse(): array { |
| 217 |
return $this->response; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
public function setResult(mixed $result): Response { |
| 227 |
$this->result = $result; |
| 228 |
return $this; |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
public function wrap(callable $callback): Response { |
| 238 |
$this->result = call_user_func($callback, $this); |
| 239 |
return $this; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
public function data(): mixed { |
| 248 |
if ($this->result !== null) { |
| 249 |
return $this->result; |
| 250 |
} |
| 251 |
return $this->getResponse(); |
| 252 |
} |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 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 |
|
| 269 |
|
| 270 |
|
| 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 |
|
| 282 |
|
| 283 |
|
| 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 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
public function boolean(): bool { |
| 299 |
return (bool)$this->data(); |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
public function validatedData(): mixed { |
| 308 |
return $this->validate()->data(); |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 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 |
|
| 325 |
|
| 326 |
|
| 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 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 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 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 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 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
public function failed(): bool { |
| 386 |
return !$this->successful(); |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
public function Noun(): int { |
| 395 |
return $this->noun; |
| 396 |
} |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 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 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
public function canBeEmpty(): bool { |
| 415 |
return $this->can_be_empty; |
| 416 |
} |
| 417 |
} |