Debian Codegraph @

lib/3rdParty/composer/webklex/php-imap/src/Query/Query.php

raw symbols SCIP
1 <?php
2 /*
3 * File: Query.php
4 * Category: -
5 * Author: M. Goldenbaum
6 * Created: 21.07.18 18:54
7 * Updated: -
8 *
9 * Description:
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 * Class Query
41 *
42 * @package Webklex\PHPIMAP\Query
43 */
44 class Query {
45
46 /** @var Collection $query */
47 protected Collection $query;
48
49 /** @var string $raw_query */
50 protected string $raw_query;
51
52 /** @var string[] $extensions */
53 protected array $extensions;
54
55 /** @var Client $client */
56 protected Client $client;
57
58 /** @var ?int $limit */
59 protected ?int $limit = null;
60
61 /** @var int $page */
62 protected int $page = 1;
63
64 /** @var ?int $fetch_options */
65 protected ?int $fetch_options = null;
66
67 /** @var boolean $fetch_body */
68 protected bool $fetch_body = true;
69
70 /** @var boolean $fetch_flags */
71 protected bool $fetch_flags = true;
72
73 /** @var int|string $sequence */
74 protected mixed $sequence = IMAP::NIL;
75
76 /** @var string $fetch_order */
77 protected string $fetch_order;
78
79 /** @var string $date_format */
80 protected string $date_format;
81
82 /** @var bool $soft_fail */
83 protected bool $soft_fail = false;
84
85 /** @var array $errors */
86 protected array $errors = [];
87
88 /**
89 * Query constructor.
90 * @param Client $client
91 * @param string[] $extensions
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 * Instance boot method for additional functionality
116 */
117 protected function boot(): void {
118 }
119
120 /**
121 * Parse a given value
122 * @param mixed $value
123 *
124 * @return string
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 * Check if a given date is a valid carbon object and if not try to convert it
136 * @param mixed $date
137 *
138 * @return Carbon
139 * @throws MessageSearchValidationException
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 * Get the raw IMAP search query
155 *
156 * @return string
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 * Perform an imap search request
188 *
189 * @return Collection
190 * @throws GetMessagesFailedException
191 * @throws AuthFailedException
192 * @throws ImapBadRequestException
193 * @throws ImapServerErrorException
194 * @throws ResponseException
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 * Count all available messages matching the current search criteria
209 *
210 * @return int
211 * @throws AuthFailedException
212 * @throws GetMessagesFailedException
213 * @throws ImapBadRequestException
214 * @throws ImapServerErrorException
215 * @throws ResponseException
216 */
217 public function count(): int {
218 return $this->search()->count();
219 }
220
221 /**
222 * Fetch a given id collection
223 * @param Collection $available_messages
224 *
225 * @return array
226 * @throws AuthFailedException
227 * @throws ConnectionFailedException
228 * @throws ImapBadRequestException
229 * @throws ImapServerErrorException
230 * @throws RuntimeException
231 * @throws ResponseException
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 // this polymorphic call is fine - the method exists at this point
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 * Make a new message from given raw components
263 * @param integer $uid
264 * @param integer $msglist
265 * @param string $header
266 * @param string $content
267 * @param array $flags
268 *
269 * @return Message|null
270 * @throws AuthFailedException
271 * @throws ConnectionFailedException
272 * @throws EventNotFoundException
273 * @throws GetMessagesFailedException
274 * @throws ImapBadRequestException
275 * @throws ImapServerErrorException
276 * @throws ReflectionException
277 * @throws ResponseException
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 * Get the message key for a given message
293 * @param string $message_key
294 * @param integer $msglist
295 * @param Message $message
296 *
297 * @return string
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 * Curates a given collection aof messages
311 * @param Collection $available_messages
312 *
313 * @return MessageCollection
314 * @throws GetMessagesFailedException
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 * Populate a given id collection and receive a fully fetched message collection
329 * @param Collection $available_messages
330 *
331 * @return MessageCollection
332 * @throws AuthFailedException
333 * @throws ConnectionFailedException
334 * @throws EventNotFoundException
335 * @throws GetMessagesFailedException
336 * @throws ImapBadRequestException
337 * @throws ImapServerErrorException
338 * @throws ReflectionException
339 * @throws RuntimeException
340 * @throws ResponseException
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 * Fetch the current query and return all found messages
374 *
375 * @return MessageCollection
376 * @throws AuthFailedException
377 * @throws GetMessagesFailedException
378 * @throws ImapBadRequestException
379 * @throws ImapServerErrorException
380 * @throws ResponseException
381 */
382 public function get(): MessageCollection {
383 return $this->curate_messages($this->search());
384 }
385
386 /**
387 * Fetch the current query as chunked requests
388 * @param callable $callback
389 * @param int $chunk_size
390 * @param int $start_chunk
391 *
392 * @throws AuthFailedException
393 * @throws ConnectionFailedException
394 * @throws EventNotFoundException
395 * @throws GetMessagesFailedException
396 * @throws ImapBadRequestException
397 * @throws ImapServerErrorException
398 * @throws ReflectionException
399 * @throws RuntimeException
400 * @throws ResponseException
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 * Paginate the current query
430 * @param int $per_page Results you which to receive per page
431 * @param null $page The current page you are on (e.g. 0, 1, 2, ...) use `null` to enable auto mode
432 * @param string $page_name The page name / uri parameter used for the generated links and the auto mode
433 *
434 * @return LengthAwarePaginator
435 * @throws AuthFailedException
436 * @throws GetMessagesFailedException
437 * @throws ImapBadRequestException
438 * @throws ImapServerErrorException
439 * @throws ResponseException
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 * Get a new Message instance
455 * @param int $uid
456 * @param null $msglist
457 * @param null $sequence
458 *
459 * @return Message
460 * @throws AuthFailedException
461 * @throws ConnectionFailedException
462 * @throws EventNotFoundException
463 * @throws ImapBadRequestException
464 * @throws ImapServerErrorException
465 * @throws InvalidMessageDateException
466 * @throws MessageContentFetchingException
467 * @throws MessageFlagException
468 * @throws MessageHeaderFetchingException
469 * @throws RuntimeException
470 * @throws ResponseException
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 * Get a message by its message number
478 * @param $msgn
479 * @param null $msglist
480 *
481 * @return Message
482 * @throws AuthFailedException
483 * @throws ConnectionFailedException
484 * @throws EventNotFoundException
485 * @throws ImapBadRequestException
486 * @throws ImapServerErrorException
487 * @throws InvalidMessageDateException
488 * @throws MessageContentFetchingException
489 * @throws MessageFlagException
490 * @throws MessageHeaderFetchingException
491 * @throws RuntimeException
492 * @throws ResponseException
493 */
494 public function getMessageByMsgn($msgn, $msglist = null): Message {
495 return $this->getMessage($msgn, $msglist, IMAP::ST_MSGN);
496 }
497
498 /**
499 * Get a message by its uid
500 * @param $uid
501 *
502 * @return Message
503 * @throws AuthFailedException
504 * @throws ConnectionFailedException
505 * @throws EventNotFoundException
506 * @throws ImapBadRequestException
507 * @throws ImapServerErrorException
508 * @throws InvalidMessageDateException
509 * @throws MessageContentFetchingException
510 * @throws MessageFlagException
511 * @throws MessageHeaderFetchingException
512 * @throws RuntimeException
513 * @throws ResponseException
514 */
515 public function getMessageByUid($uid): Message {
516 return $this->getMessage($uid, null, IMAP::ST_UID);
517 }
518
519 /**
520 * Filter all available uids by a given closure and get a curated list of messages
521 * @param callable $closure
522 *
523 * @return MessageCollection
524 * @throws AuthFailedException
525 * @throws ConnectionFailedException
526 * @throws GetMessagesFailedException
527 * @throws ImapBadRequestException
528 * @throws ImapServerErrorException
529 * @throws MessageNotFoundException
530 * @throws RuntimeException
531 * @throws ResponseException
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 * Get all messages with an uid greater or equal to a given UID
551 * @param int $uid
552 *
553 * @return MessageCollection
554 * @throws AuthFailedException
555 * @throws ConnectionFailedException
556 * @throws GetMessagesFailedException
557 * @throws ImapBadRequestException
558 * @throws ImapServerErrorException
559 * @throws MessageNotFoundException
560 * @throws RuntimeException
561 * @throws ResponseException
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 * Get all messages with an uid greater than a given UID
571 * @param int $uid
572 *
573 * @return MessageCollection
574 * @throws AuthFailedException
575 * @throws ConnectionFailedException
576 * @throws GetMessagesFailedException
577 * @throws ImapBadRequestException
578 * @throws ImapServerErrorException
579 * @throws MessageNotFoundException
580 * @throws RuntimeException
581 * @throws ResponseException
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 * Get all messages with an uid lower than a given UID
591 * @param int $uid
592 *
593 * @return MessageCollection
594 * @throws AuthFailedException
595 * @throws ConnectionFailedException
596 * @throws GetMessagesFailedException
597 * @throws ImapBadRequestException
598 * @throws ImapServerErrorException
599 * @throws MessageNotFoundException
600 * @throws RuntimeException
601 * @throws ResponseException
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 * Get all messages with an uid lower or equal to a given UID
611 * @param int $uid
612 *
613 * @return MessageCollection
614 * @throws AuthFailedException
615 * @throws ConnectionFailedException
616 * @throws GetMessagesFailedException
617 * @throws ImapBadRequestException
618 * @throws ImapServerErrorException
619 * @throws MessageNotFoundException
620 * @throws RuntimeException
621 * @throws ResponseException
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 * Get all messages with an uid greater than a given UID
631 * @param int $uid
632 *
633 * @return MessageCollection
634 * @throws AuthFailedException
635 * @throws ConnectionFailedException
636 * @throws GetMessagesFailedException
637 * @throws ImapBadRequestException
638 * @throws ImapServerErrorException
639 * @throws MessageNotFoundException
640 * @throws RuntimeException
641 * @throws ResponseException
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 * Don't mark messages as read when fetching
651 *
652 * @return $this
653 */
654 public function leaveUnread(): static {
655 $this->setFetchOptions(IMAP::FT_PEEK);
656
657 return $this;
658 }
659
660 /**
661 * Mark all messages as read when fetching
662 *
663 * @return $this
664 */
665 public function markAsRead(): static {
666 $this->setFetchOptions(IMAP::FT_UID);
667
668 return $this;
669 }
670
671 /**
672 * Set the sequence type
673 * @param int $sequence
674 *
675 * @return $this
676 */
677 public function setSequence(int $sequence): static {
678 $this->sequence = $sequence;
679
680 return $this;
681 }
682
683 /**
684 * Get the sequence type
685 *
686 * @return int|string
687 */
688 public function getSequence(): int|string {
689 return $this->sequence;
690 }
691
692 /**
693 * @return Client
694 * @throws AuthFailedException
695 * @throws ConnectionFailedException
696 * @throws ImapBadRequestException
697 * @throws ImapServerErrorException
698 * @throws RuntimeException
699 * @throws ResponseException
700 */
701 public function getClient(): Client {
702 $this->client->checkConnection();
703 return $this->client;
704 }
705
706 /**
707 * Set the limit and page for the current query
708 * @param int $limit
709 * @param int $page
710 *
711 * @return $this
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 * Get the current query collection
722 *
723 * @return Collection
724 */
725 public function getQuery(): Collection {
726 return $this->query;
727 }
728
729 /**
730 * Set all query parameters
731 * @param array $query
732 *
733 * @return $this
734 */
735 public function setQuery(array $query): static {
736 $this->query = new Collection($query);
737 return $this;
738 }
739
740 /**
741 * Get the raw query
742 *
743 * @return string
744 */
745 public function getRawQuery(): string {
746 return $this->raw_query;
747 }
748
749 /**
750 * Set the raw query
751 * @param string $raw_query
752 *
753 * @return $this
754 */
755 public function setRawQuery(string $raw_query): static {
756 $this->raw_query = $raw_query;
757 return $this;
758 }
759
760 /**
761 * Get all applied extensions
762 *
763 * @return string[]
764 */
765 public function getExtensions(): array {
766 return $this->extensions;
767 }
768
769 /**
770 * Set all extensions that should be used
771 * @param string[] $extensions
772 *
773 * @return $this
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 * Set the client instance
787 * @param Client $client
788 *
789 * @return $this
790 */
791 public function setClient(Client $client): static {
792 $this->client = $client;
793 return $this;
794 }
795
796 /**
797 * Get the set fetch limit
798 *
799 * @return ?int
800 */
801 public function getLimit(): ?int {
802 return $this->limit;
803 }
804
805 /**
806 * Set the fetch limit
807 * @param int $limit
808 *
809 * @return $this
810 */
811 public function setLimit(int $limit): static {
812 $this->limit = $limit <= 0 ? null : $limit;
813 return $this;
814 }
815
816 /**
817 * Get the set page
818 *
819 * @return int
820 */
821 public function getPage(): int {
822 return $this->page;
823 }
824
825 /**
826 * Set the page
827 * @param int $page
828 *
829 * @return $this
830 */
831 public function setPage(int $page): static {
832 $this->page = $page;
833 return $this;
834 }
835
836 /**
837 * Set the fetch option flag
838 * @param int $fetch_options
839 *
840 * @return $this
841 */
842 public function setFetchOptions(int $fetch_options): static {
843 $this->fetch_options = $fetch_options;
844 return $this;
845 }
846
847 /**
848 * Set the fetch option flag
849 * @param int $fetch_options
850 *
851 * @return $this
852 */
853 public function fetchOptions(int $fetch_options): static {
854 return $this->setFetchOptions($fetch_options);
855 }
856
857 /**
858 * Get the fetch option flag
859 *
860 * @return ?int
861 */
862 public function getFetchOptions(): ?int {
863 return $this->fetch_options;
864 }
865
866 /**
867 * Get the fetch body flag
868 *
869 * @return boolean
870 */
871 public function getFetchBody(): bool {
872 return $this->fetch_body;
873 }
874
875 /**
876 * Set the fetch body flag
877 * @param boolean $fetch_body
878 *
879 * @return $this
880 */
881 public function setFetchBody(bool $fetch_body): static {
882 $this->fetch_body = $fetch_body;
883 return $this;
884 }
885
886 /**
887 * Set the fetch body flag
888 * @param boolean $fetch_body
889 *
890 * @return $this
891 */
892 public function fetchBody(bool $fetch_body): static {
893 return $this->setFetchBody($fetch_body);
894 }
895
896 /**
897 * Get the fetch body flag
898 *
899 * @return bool
900 */
901 public function getFetchFlags(): bool {
902 return $this->fetch_flags;
903 }
904
905 /**
906 * Set the fetch flag
907 * @param bool $fetch_flags
908 *
909 * @return $this
910 */
911 public function setFetchFlags(bool $fetch_flags): static {
912 $this->fetch_flags = $fetch_flags;
913 return $this;
914 }
915
916 /**
917 * Set the fetch order
918 * @param string $fetch_order
919 *
920 * @return $this
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 * Set the fetch order
934 * @param string $fetch_order
935 *
936 * @return $this
937 */
938 public function fetchOrder(string $fetch_order): static {
939 return $this->setFetchOrder($fetch_order);
940 }
941
942 /**
943 * Get the fetch order
944 *
945 * @return string
946 */
947 public function getFetchOrder(): string {
948 return $this->fetch_order;
949 }
950
951 /**
952 * Set the fetch order to ascending
953 *
954 * @return $this
955 */
956 public function setFetchOrderAsc(): static {
957 return $this->setFetchOrder('asc');
958 }
959
960 /**
961 * Set the fetch order to ascending
962 *
963 * @return $this
964 */
965 public function fetchOrderAsc(): static {
966 return $this->setFetchOrderAsc();
967 }
968
969 /**
970 * Set the fetch order to descending
971 *
972 * @return $this
973 */
974 public function setFetchOrderDesc(): static {
975 return $this->setFetchOrder('desc');
976 }
977
978 /**
979 * Set the fetch order to descending
980 *
981 * @return $this
982 */
983 public function fetchOrderDesc(): static {
984 return $this->setFetchOrderDesc();
985 }
986
987 /**
988 * Set soft fail mode
989 * @var boolean $state
990 *
991 * @return $this
992 */
993 public function softFail(bool $state = true): static {
994 return $this->setSoftFail($state);
995 }
996
997 /**
998 * Set soft fail mode
999 *
1000 * @var boolean $state
1001 * @return $this
1002 */
1003 public function setSoftFail(bool $state = true): static {
1004 $this->soft_fail = $state;
1005
1006 return $this;
1007 }
1008
1009 /**
1010 * Get soft fail mode
1011 *
1012 * @return boolean
1013 */
1014 public function getSoftFail(): bool {
1015 return $this->soft_fail;
1016 }
1017
1018 /**
1019 * Handle the exception for a given uid
1020 * @param integer $uid
1021 *
1022 * @throws GetMessagesFailedException
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 * Add a new error to the error holder
1033 * @param integer $uid
1034 * @param Exception $error
1035 */
1036 protected function setError(int $uid, Exception $error): void {
1037 $this->errors[$uid] = $error;
1038 }
1039
1040 /**
1041 * Check if there are any errors / exceptions present
1042 * @var ?integer $uid
1043 *
1044 * @return boolean
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 * Check if there is an error / exception present
1055 * @var integer $uid
1056 *
1057 * @return boolean
1058 */
1059 public function hasError(int $uid): bool {
1060 return isset($this->errors[$uid]);
1061 }
1062
1063 /**
1064 * Get all available errors / exceptions
1065 *
1066 * @return array
1067 */
1068 public function errors(): array {
1069 return $this->getErrors();
1070 }
1071
1072 /**
1073 * Get all available errors / exceptions
1074 *
1075 * @return array
1076 */
1077 public function getErrors(): array {
1078 return $this->errors;
1079 }
1080
1081 /**
1082 * Get a specific error / exception
1083 * @var integer $uid
1084 *
1085 * @return Exception|null
1086 */
1087 public function error(int $uid): ?Exception {
1088 return $this->getError($uid);
1089 }
1090
1091 /**
1092 * Get a specific error / exception
1093 * @var integer $uid
1094 *
1095 * @return ?Exception
1096 */
1097 public function getError(int $uid): ?Exception {
1098 if ($this->hasError($uid)) {
1099 return $this->errors[$uid];
1100 }
1101 return null;
1102 }
1103 }