| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
namespace Webklex\PHPIMAP\Query; |
| 14 |
|
| 15 |
use Carbon\Carbon; |
| 16 |
use Exception; |
| 17 |
use Illuminate\Pagination\LengthAwarePaginator; |
| 18 |
use Illuminate\Support\Collection; |
| 19 |
use ReflectionException; |
| 20 |
use Webklex\PHPIMAP\Client; |
| 21 |
use Webklex\PHPIMAP\Exceptions\AuthFailedException; |
| 22 |
use Webklex\PHPIMAP\Exceptions\ConnectionFailedException; |
| 23 |
use Webklex\PHPIMAP\Exceptions\EventNotFoundException; |
| 24 |
use Webklex\PHPIMAP\Exceptions\GetMessagesFailedException; |
| 25 |
use Webklex\PHPIMAP\Exceptions\ImapBadRequestException; |
| 26 |
use Webklex\PHPIMAP\Exceptions\ImapServerErrorException; |
| 27 |
use Webklex\PHPIMAP\Exceptions\InvalidMessageDateException; |
| 28 |
use Webklex\PHPIMAP\Exceptions\MessageContentFetchingException; |
| 29 |
use Webklex\PHPIMAP\Exceptions\MessageFlagException; |
| 30 |
use Webklex\PHPIMAP\Exceptions\MessageHeaderFetchingException; |
| 31 |
use Webklex\PHPIMAP\Exceptions\MessageNotFoundException; |
| 32 |
use Webklex\PHPIMAP\Exceptions\MessageSearchValidationException; |
| 33 |
use Webklex\PHPIMAP\Exceptions\ResponseException; |
| 34 |
use Webklex\PHPIMAP\Exceptions\RuntimeException; |
| 35 |
use Webklex\PHPIMAP\IMAP; |
| 36 |
use Webklex\PHPIMAP\Message; |
| 37 |
use Webklex\PHPIMAP\Support\MessageCollection; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
class Query { |
| 45 |
|
| 46 |
|
| 47 |
protected Collection $query; |
| 48 |
|
| 49 |
|
| 50 |
protected string $raw_query; |
| 51 |
|
| 52 |
|
| 53 |
protected array $extensions; |
| 54 |
|
| 55 |
|
| 56 |
protected Client $client; |
| 57 |
|
| 58 |
|
| 59 |
protected ?int $limit = null; |
| 60 |
|
| 61 |
|
| 62 |
protected int $page = 1; |
| 63 |
|
| 64 |
|
| 65 |
protected ?int $fetch_options = null; |
| 66 |
|
| 67 |
|
| 68 |
protected bool $fetch_body = true; |
| 69 |
|
| 70 |
|
| 71 |
protected bool $fetch_flags = true; |
| 72 |
|
| 73 |
|
| 74 |
protected mixed $sequence = IMAP::NIL; |
| 75 |
|
| 76 |
|
| 77 |
protected string $fetch_order; |
| 78 |
|
| 79 |
|
| 80 |
protected string $date_format; |
| 81 |
|
| 82 |
|
| 83 |
protected bool $soft_fail = false; |
| 84 |
|
| 85 |
|
| 86 |
protected array $errors = []; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
public function __construct(Client $client, array $extensions = []) { |
| 94 |
$this->setClient($client); |
| 95 |
$config = $this->client->getConfig(); |
| 96 |
|
| 97 |
$this->sequence = $config->get('options.sequence', IMAP::ST_MSGN); |
| 98 |
if ($config->get('options.fetch') === IMAP::FT_PEEK) $this->leaveUnread(); |
| 99 |
|
| 100 |
if ($config->get('options.fetch_order') === 'desc') { |
| 101 |
$this->fetch_order = 'desc'; |
| 102 |
} else { |
| 103 |
$this->fetch_order = 'asc'; |
| 104 |
} |
| 105 |
|
| 106 |
$this->date_format = $config->get('date_format', 'd M y'); |
| 107 |
$this->soft_fail = $config->get('options.soft_fail', false); |
| 108 |
|
| 109 |
$this->setExtensions($extensions); |
| 110 |
$this->query = new Collection(); |
| 111 |
$this->boot(); |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
protected function boot(): void { |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
protected function parse_value(mixed $value): string { |
| 127 |
if ($value instanceof Carbon) { |
| 128 |
$value = $value->format($this->date_format); |
| 129 |
} |
| 130 |
|
| 131 |
return (string)$value; |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
protected function parse_date(mixed $date): Carbon { |
| 142 |
if ($date instanceof Carbon) return $date; |
| 143 |
|
| 144 |
try { |
| 145 |
$date = Carbon::parse($date); |
| 146 |
} catch (Exception) { |
| 147 |
throw new MessageSearchValidationException(); |
| 148 |
} |
| 149 |
|
| 150 |
return $date; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public function generate_query(): string { |
| 159 |
$query = ''; |
| 160 |
$this->query->each(function($statement) use (&$query) { |
| 161 |
if (count($statement) == 1) { |
| 162 |
$query .= $statement[0]; |
| 163 |
} else { |
| 164 |
if ($statement[1] === null) { |
| 165 |
$query .= $statement[0]; |
| 166 |
} else { |
| 167 |
if (is_numeric($statement[1]) || ( |
| 168 |
($statement[0] === 'SINCE' || $statement[0] === 'BEFORE') && |
| 169 |
$this->client->getConfig()->get('options.unescaped_search_dates', false) |
| 170 |
)) { |
| 171 |
$query .= $statement[0] . ' ' . $statement[1]; |
| 172 |
} else { |
| 173 |
$query .= $statement[0] . ' "' . $statement[1] . '"'; |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
$query .= ' '; |
| 178 |
|
| 179 |
}); |
| 180 |
|
| 181 |
$this->raw_query = trim($query); |
| 182 |
|
| 183 |
return $this->raw_query; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
public function search(): Collection { |
| 197 |
$this->generate_query(); |
| 198 |
|
| 199 |
try { |
| 200 |
$available_messages = $this->client->getConnection()->search([$this->getRawQuery()], $this->sequence)->validatedData(); |
| 201 |
return new Collection($available_messages); |
| 202 |
} catch (RuntimeException|ConnectionFailedException $e) { |
| 203 |
throw new GetMessagesFailedException("failed to fetch messages", 0, $e); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
public function count(): int { |
| 218 |
return $this->search()->count(); |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
protected function fetch(Collection $available_messages): array { |
| 234 |
if ($this->fetch_order === 'desc') { |
| 235 |
$available_messages = $available_messages->reverse(); |
| 236 |
} |
| 237 |
|
| 238 |
$uids = $available_messages->forPage($this->page, $this->limit)->toArray(); |
| 239 |
$extensions = $this->getExtensions(); |
| 240 |
if (empty($extensions) === false && method_exists($this->client->getConnection(), "fetch")) { |
| 241 |
|
| 242 |
$extensions = $this->client->getConnection()->fetch($extensions, $uids, null, $this->sequence)->validatedData(); |
| 243 |
} |
| 244 |
$flags = $this->client->getConnection()->flags($uids, $this->sequence)->validatedData(); |
| 245 |
$headers = $this->client->getConnection()->headers($uids, "RFC822", $this->sequence)->validatedData(); |
| 246 |
|
| 247 |
$contents = []; |
| 248 |
if ($this->getFetchBody()) { |
| 249 |
$contents = $this->client->getConnection()->content($uids, $this->client->rfc, $this->sequence)->validatedData(); |
| 250 |
} |
| 251 |
|
| 252 |
return [ |
| 253 |
"uids" => $uids, |
| 254 |
"flags" => $flags, |
| 255 |
"headers" => $headers, |
| 256 |
"contents" => $contents, |
| 257 |
"extensions" => $extensions, |
| 258 |
]; |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
protected function make(int $uid, int $msglist, string $header, string $content, array $flags): ?Message { |
| 280 |
try { |
| 281 |
return Message::make($uid, $msglist, $this->getClient(), $header, $content, $flags, $this->getFetchOptions(), $this->sequence); |
| 282 |
} catch (RuntimeException|MessageFlagException|InvalidMessageDateException|MessageContentFetchingException $e) { |
| 283 |
$this->setError($uid, $e); |
| 284 |
} |
| 285 |
|
| 286 |
$this->handleException($uid); |
| 287 |
|
| 288 |
return null; |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
protected function getMessageKey(string $message_key, int $msglist, Message $message): string { |
| 300 |
$key = match ($message_key) { |
| 301 |
'number' => $message->getMessageNo(), |
| 302 |
'list' => $msglist, |
| 303 |
'uid' => $message->getUid(), |
| 304 |
default => $message->getMessageId(), |
| 305 |
}; |
| 306 |
return (string)$key; |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
public function curate_messages(Collection $available_messages): MessageCollection { |
| 317 |
try { |
| 318 |
if ($available_messages->count() > 0) { |
| 319 |
return $this->populate($available_messages); |
| 320 |
} |
| 321 |
return MessageCollection::make(); |
| 322 |
} catch (Exception $e) { |
| 323 |
throw new GetMessagesFailedException($e->getMessage(), 0, $e); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
protected function populate(Collection $available_messages): MessageCollection { |
| 343 |
$messages = MessageCollection::make(); |
| 344 |
$config = $this->client->getConfig(); |
| 345 |
|
| 346 |
$messages->total($available_messages->count()); |
| 347 |
|
| 348 |
$message_key = $config->get('options.message_key'); |
| 349 |
|
| 350 |
$raw_messages = $this->fetch($available_messages); |
| 351 |
|
| 352 |
$msglist = 0; |
| 353 |
foreach ($raw_messages["headers"] as $uid => $header) { |
| 354 |
$content = $raw_messages["contents"][$uid] ?? ""; |
| 355 |
$flag = $raw_messages["flags"][$uid] ?? []; |
| 356 |
$extensions = $raw_messages["extensions"][$uid] ?? []; |
| 357 |
|
| 358 |
$message = $this->make($uid, $msglist, $header, $content, $flag); |
| 359 |
foreach ($extensions as $key => $extension) { |
| 360 |
$message->getHeader()->set($key, $extension); |
| 361 |
} |
| 362 |
if ($message !== null) { |
| 363 |
$key = $this->getMessageKey($message_key, $msglist, $message); |
| 364 |
$messages->put("$key", $message); |
| 365 |
} |
| 366 |
$msglist++; |
| 367 |
} |
| 368 |
|
| 369 |
return $messages; |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
public function get(): MessageCollection { |
| 383 |
return $this->curate_messages($this->search()); |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
public function chunked(callable $callback, int $chunk_size = 10, int $start_chunk = 1): void { |
| 403 |
$start_chunk = max($start_chunk,1); |
| 404 |
$chunk_size = max($chunk_size,1); |
| 405 |
$skipped_messages_count = $chunk_size * ($start_chunk-1); |
| 406 |
|
| 407 |
$available_messages = $this->search(); |
| 408 |
$available_messages_count = max($available_messages->count() - $skipped_messages_count,0); |
| 409 |
|
| 410 |
if ($available_messages_count > 0) { |
| 411 |
$old_limit = $this->limit; |
| 412 |
$old_page = $this->page; |
| 413 |
|
| 414 |
$this->limit = $chunk_size; |
| 415 |
$this->page = $start_chunk; |
| 416 |
$handled_messages_count = 0; |
| 417 |
do { |
| 418 |
$messages = $this->populate($available_messages); |
| 419 |
$handled_messages_count += $messages->count(); |
| 420 |
$callback($messages, $this->page); |
| 421 |
$this->page++; |
| 422 |
} while ($handled_messages_count < $available_messages_count); |
| 423 |
$this->limit = $old_limit; |
| 424 |
$this->page = $old_page; |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
public function paginate(int $per_page = 5, $page = null, string $page_name = 'imap_page'): LengthAwarePaginator { |
| 442 |
if ($page === null && isset($_GET[$page_name]) && $_GET[$page_name] > 0) { |
| 443 |
$this->page = intval($_GET[$page_name]); |
| 444 |
} elseif ($page > 0) { |
| 445 |
$this->page = (int)$page; |
| 446 |
} |
| 447 |
|
| 448 |
$this->limit = $per_page; |
| 449 |
|
| 450 |
return $this->get()->paginate($per_page, $this->page, $page_name, true); |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
public function getMessage(int $uid, $msglist = null, $sequence = null): Message { |
| 473 |
return new Message($uid, $msglist, $this->getClient(), $this->getFetchOptions(), $this->getFetchBody(), $this->getFetchFlags(), $sequence ?: $this->sequence); |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
public function getMessageByMsgn($msgn, $msglist = null): Message { |
| 495 |
return $this->getMessage($msgn, $msglist, IMAP::ST_MSGN); |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
public function getMessageByUid($uid): Message { |
| 516 |
return $this->getMessage($uid, null, IMAP::ST_UID); |
| 517 |
} |
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
public function filter(callable $closure): MessageCollection { |
| 534 |
$connection = $this->getClient()->getConnection(); |
| 535 |
|
| 536 |
$uids = $connection->getUid()->validatedData(); |
| 537 |
$available_messages = new Collection(); |
| 538 |
if (is_array($uids)) { |
| 539 |
foreach ($uids as $id) { |
| 540 |
if ($closure($id)) { |
| 541 |
$available_messages->push($id); |
| 542 |
} |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
return $this->curate_messages($available_messages); |
| 547 |
} |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
public function getByUidGreaterOrEqual(int $uid): MessageCollection { |
| 564 |
return $this->filter(function($id) use ($uid) { |
| 565 |
return $id >= $uid; |
| 566 |
}); |
| 567 |
} |
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
public function getByUidGreater(int $uid): MessageCollection { |
| 584 |
return $this->filter(function($id) use ($uid) { |
| 585 |
return $id > $uid; |
| 586 |
}); |
| 587 |
} |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
public function getByUidLower(int $uid): MessageCollection { |
| 604 |
return $this->filter(function($id) use ($uid) { |
| 605 |
return $id < $uid; |
| 606 |
}); |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
public function getByUidLowerOrEqual(int $uid): MessageCollection { |
| 624 |
return $this->filter(function($id) use ($uid) { |
| 625 |
return $id <= $uid; |
| 626 |
}); |
| 627 |
} |
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
public function getByUidLowerThan(int $uid): MessageCollection { |
| 644 |
return $this->filter(function($id) use ($uid) { |
| 645 |
return $id < $uid; |
| 646 |
}); |
| 647 |
} |
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
public function leaveUnread(): static { |
| 655 |
$this->setFetchOptions(IMAP::FT_PEEK); |
| 656 |
|
| 657 |
return $this; |
| 658 |
} |
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
public function markAsRead(): static { |
| 666 |
$this->setFetchOptions(IMAP::FT_UID); |
| 667 |
|
| 668 |
return $this; |
| 669 |
} |
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
public function setSequence(int $sequence): static { |
| 678 |
$this->sequence = $sequence; |
| 679 |
|
| 680 |
return $this; |
| 681 |
} |
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
public function getSequence(): int|string { |
| 689 |
return $this->sequence; |
| 690 |
} |
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
public function getClient(): Client { |
| 702 |
$this->client->checkConnection(); |
| 703 |
return $this->client; |
| 704 |
} |
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
public function limit(int $limit, int $page = 1): static { |
| 714 |
if ($page >= 1) $this->page = $page; |
| 715 |
$this->limit = $limit; |
| 716 |
|
| 717 |
return $this; |
| 718 |
} |
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
public function getQuery(): Collection { |
| 726 |
return $this->query; |
| 727 |
} |
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
public function setQuery(array $query): static { |
| 736 |
$this->query = new Collection($query); |
| 737 |
return $this; |
| 738 |
} |
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
public function getRawQuery(): string { |
| 746 |
return $this->raw_query; |
| 747 |
} |
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
public function setRawQuery(string $raw_query): static { |
| 756 |
$this->raw_query = $raw_query; |
| 757 |
return $this; |
| 758 |
} |
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
public function getExtensions(): array { |
| 766 |
return $this->extensions; |
| 767 |
} |
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
public function setExtensions(array $extensions): static { |
| 776 |
$this->extensions = $extensions; |
| 777 |
if (count($this->extensions) > 0) { |
| 778 |
if (in_array("UID", $this->extensions) === false) { |
| 779 |
$this->extensions[] = "UID"; |
| 780 |
} |
| 781 |
} |
| 782 |
return $this; |
| 783 |
} |
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
public function setClient(Client $client): static { |
| 792 |
$this->client = $client; |
| 793 |
return $this; |
| 794 |
} |
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
public function getLimit(): ?int { |
| 802 |
return $this->limit; |
| 803 |
} |
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
public function setLimit(int $limit): static { |
| 812 |
$this->limit = $limit <= 0 ? null : $limit; |
| 813 |
return $this; |
| 814 |
} |
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
public function getPage(): int { |
| 822 |
return $this->page; |
| 823 |
} |
| 824 |
|
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
public function setPage(int $page): static { |
| 832 |
$this->page = $page; |
| 833 |
return $this; |
| 834 |
} |
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
public function setFetchOptions(int $fetch_options): static { |
| 843 |
$this->fetch_options = $fetch_options; |
| 844 |
return $this; |
| 845 |
} |
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
public function fetchOptions(int $fetch_options): static { |
| 854 |
return $this->setFetchOptions($fetch_options); |
| 855 |
} |
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
public function getFetchOptions(): ?int { |
| 863 |
return $this->fetch_options; |
| 864 |
} |
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
public function getFetchBody(): bool { |
| 872 |
return $this->fetch_body; |
| 873 |
} |
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
public function setFetchBody(bool $fetch_body): static { |
| 882 |
$this->fetch_body = $fetch_body; |
| 883 |
return $this; |
| 884 |
} |
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
public function fetchBody(bool $fetch_body): static { |
| 893 |
return $this->setFetchBody($fetch_body); |
| 894 |
} |
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
public function getFetchFlags(): bool { |
| 902 |
return $this->fetch_flags; |
| 903 |
} |
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
public function setFetchFlags(bool $fetch_flags): static { |
| 912 |
$this->fetch_flags = $fetch_flags; |
| 913 |
return $this; |
| 914 |
} |
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
public function setFetchOrder(string $fetch_order): static { |
| 923 |
$fetch_order = strtolower($fetch_order); |
| 924 |
|
| 925 |
if (in_array($fetch_order, ['asc', 'desc'])) { |
| 926 |
$this->fetch_order = $fetch_order; |
| 927 |
} |
| 928 |
|
| 929 |
return $this; |
| 930 |
} |
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
public function fetchOrder(string $fetch_order): static { |
| 939 |
return $this->setFetchOrder($fetch_order); |
| 940 |
} |
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
public function getFetchOrder(): string { |
| 948 |
return $this->fetch_order; |
| 949 |
} |
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
public function setFetchOrderAsc(): static { |
| 957 |
return $this->setFetchOrder('asc'); |
| 958 |
} |
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
public function fetchOrderAsc(): static { |
| 966 |
return $this->setFetchOrderAsc(); |
| 967 |
} |
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
public function setFetchOrderDesc(): static { |
| 975 |
return $this->setFetchOrder('desc'); |
| 976 |
} |
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
public function fetchOrderDesc(): static { |
| 984 |
return $this->setFetchOrderDesc(); |
| 985 |
} |
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
public function softFail(bool $state = true): static { |
| 994 |
return $this->setSoftFail($state); |
| 995 |
} |
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
public function setSoftFail(bool $state = true): static { |
| 1004 |
$this->soft_fail = $state; |
| 1005 |
|
| 1006 |
return $this; |
| 1007 |
} |
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
public function getSoftFail(): bool { |
| 1015 |
return $this->soft_fail; |
| 1016 |
} |
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
protected function handleException(int $uid): void { |
| 1025 |
if ($this->soft_fail === false && $this->hasError($uid)) { |
| 1026 |
$error = $this->getError($uid); |
| 1027 |
throw new GetMessagesFailedException($error->getMessage(), 0, $error); |
| 1028 |
} |
| 1029 |
} |
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
protected function setError(int $uid, Exception $error): void { |
| 1037 |
$this->errors[$uid] = $error; |
| 1038 |
} |
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
public function hasErrors(?int $uid = null): bool { |
| 1047 |
if ($uid !== null) { |
| 1048 |
return $this->hasError($uid); |
| 1049 |
} |
| 1050 |
return count($this->errors) > 0; |
| 1051 |
} |
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
public function hasError(int $uid): bool { |
| 1060 |
return isset($this->errors[$uid]); |
| 1061 |
} |
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
public function errors(): array { |
| 1069 |
return $this->getErrors(); |
| 1070 |
} |
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
public function getErrors(): array { |
| 1078 |
return $this->errors; |
| 1079 |
} |
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
public function error(int $uid): ?Exception { |
| 1088 |
return $this->getError($uid); |
| 1089 |
} |
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
public function getError(int $uid): ?Exception { |
| 1098 |
if ($this->hasError($uid)) { |
| 1099 |
return $this->errors[$uid]; |
| 1100 |
} |
| 1101 |
return null; |
| 1102 |
} |
| 1103 |
} |