2222/**
2323 * @mixin \DirectoryTree\ImapEngine\Connection\ImapQueryBuilder
2424 */
25- class MessageQuery
25+ class MessageQuery implements MessageQueryInterface
2626{
27- use Conditionable, ForwardsCalls;
28-
29- /**
30- * The current page.
31- */
32- protected int $ page = 1 ;
33-
34- /**
35- * The fetch limit.
36- */
37- protected ?int $ limit = null ;
38-
39- /**
40- * Whether to fetch the message body.
41- */
42- protected bool $ fetchBody = false ;
43-
44- /**
45- * Whether to fetch the message flags.
46- */
47- protected bool $ fetchFlags = false ;
48-
49- /**
50- * Whether to fetch the message headers.
51- */
52- protected bool $ fetchHeaders = false ;
53-
54- /**
55- * The fetch order.
56- */
57- protected string $ fetchOrder = 'desc ' ;
58-
59- /**
60- * Whether to leave messages fetched as unread by default.
61- */
62- protected bool $ fetchAsUnread = true ;
63-
64- /**
65- * The methods that should be returned from query builder.
66- */
67- protected array $ passthru = ['toimap ' , 'isempty ' ];
27+ use Conditionable, ForwardsCalls, QueriesMessages;
6828
6929 /**
7030 * Constructor.
@@ -74,230 +34,6 @@ public function __construct(
7434 protected ImapQueryBuilder $ query ,
7535 ) {}
7636
77- /**
78- * Handle dynamic method calls into the query builder.
79- */
80- public function __call (string $ method , array $ parameters ): mixed
81- {
82- if (in_array (strtolower ($ method ), $ this ->passthru )) {
83- return $ this ->query ->{$ method }(...$ parameters );
84- }
85-
86- $ this ->forwardCallTo ($ this ->query , $ method , $ parameters );
87-
88- return $ this ;
89- }
90-
91- /**
92- * Don't mark messages as read when fetching.
93- */
94- public function leaveUnread (): static
95- {
96- $ this ->fetchAsUnread = true ;
97-
98- return $ this ;
99- }
100-
101- /**
102- * Mark all messages as read when fetching.
103- */
104- public function markAsRead (): static
105- {
106- $ this ->fetchAsUnread = false ;
107-
108- return $ this ;
109- }
110-
111- /**
112- * Set the limit and page for the current query.
113- */
114- public function limit (int $ limit , int $ page = 1 ): static
115- {
116- if ($ page >= 1 ) {
117- $ this ->page = $ page ;
118- }
119-
120- $ this ->limit = $ limit ;
121-
122- return $ this ;
123- }
124-
125- /**
126- * Get the set fetch limit.
127- */
128- public function getLimit (): ?int
129- {
130- return $ this ->limit ;
131- }
132-
133- /**
134- * Set the fetch limit.
135- */
136- public function setLimit (int $ limit ): static
137- {
138- $ this ->limit = max ($ limit , 1 );
139-
140- return $ this ;
141- }
142-
143- /**
144- * Get the set page.
145- */
146- public function getPage (): int
147- {
148- return $ this ->page ;
149- }
150-
151- /**
152- * Set the page.
153- */
154- public function setPage (int $ page ): static
155- {
156- $ this ->page = $ page ;
157-
158- return $ this ;
159- }
160-
161- /**
162- * Determine if the body of messages is being fetched.
163- */
164- public function isFetchingBody (): bool
165- {
166- return $ this ->fetchBody ;
167- }
168-
169- /**
170- * Determine if the flags of messages is being fetched.
171- */
172- public function isFetchingFlags (): bool
173- {
174- return $ this ->fetchFlags ;
175- }
176-
177- /**
178- * Determine if the headers of messages is being fetched.
179- */
180- public function isFetchingHeaders (): bool
181- {
182- return $ this ->fetchHeaders ;
183- }
184-
185- /**
186- * Fetch the body of messages.
187- */
188- public function withFlags (): static
189- {
190- return $ this ->setFetchFlags (true );
191- }
192-
193- /**
194- * Fetch the body of messages.
195- */
196- public function withBody (): static
197- {
198- return $ this ->setFetchBody (true );
199- }
200-
201- /**
202- * Fetch the body of messages.
203- */
204- public function withHeaders (): static
205- {
206- return $ this ->setFetchHeaders (true );
207- }
208-
209- /**
210- * Don't fetch the body of messages.
211- */
212- public function withoutBody (): static
213- {
214- return $ this ->setFetchBody (false );
215- }
216-
217- /**
218- * Don't fetch the body of messages.
219- */
220- public function withoutHeaders (): static
221- {
222- return $ this ->setFetchHeaders (false );
223- }
224-
225- /**
226- * Don't fetch the body of messages.
227- */
228- public function withoutFlags (): static
229- {
230- return $ this ->setFetchFlags (false );
231- }
232-
233- /**
234- * Set whether to fetch the flags.
235- */
236- protected function setFetchFlags (bool $ fetchFlags ): static
237- {
238- $ this ->fetchFlags = $ fetchFlags ;
239-
240- return $ this ;
241- }
242-
243- /**
244- * Set the fetch body flag.
245- */
246- protected function setFetchBody (bool $ fetchBody ): static
247- {
248- $ this ->fetchBody = $ fetchBody ;
249-
250- return $ this ;
251- }
252-
253- /**
254- * Set whether to fetch the headers.
255- */
256- protected function setFetchHeaders (bool $ fetchHeaders ): static
257- {
258- $ this ->fetchHeaders = $ fetchHeaders ;
259-
260- return $ this ;
261- }
262-
263- /**
264- * Set the fetch order.
265- */
266- public function setFetchOrder (string $ fetchOrder ): static
267- {
268- $ fetchOrder = strtolower ($ fetchOrder );
269-
270- if (in_array ($ fetchOrder , ['asc ' , 'desc ' ])) {
271- $ this ->fetchOrder = $ fetchOrder ;
272- }
273-
274- return $ this ;
275- }
276-
277- /**
278- * Get the fetch order.
279- */
280- public function getFetchOrder (): string
281- {
282- return $ this ->fetchOrder ;
283- }
284-
285- /**
286- * Set the fetch order to 'ascending'.
287- */
288- public function setFetchOrderAsc (): static
289- {
290- return $ this ->setFetchOrder ('asc ' );
291- }
292-
293- /**
294- * Set the fetch order to 'descending'.
295- */
296- public function setFetchOrderDesc (): static
297- {
298- return $ this ->setFetchOrder ('desc ' );
299- }
300-
30137 /**
30238 * Execute an IMAP search request.
30339 */
@@ -518,7 +254,7 @@ public function paginate(int $perPage = 5, $page = null, string $pageName = 'pag
518254 /**
519255 * Find a message by the given identifier type or throw an exception.
520256 */
521- public function findOrFail (int $ id , ImapFetchIdentifier $ identifier = ImapFetchIdentifier::Uid)
257+ public function findOrFail (int $ id , ImapFetchIdentifier $ identifier = ImapFetchIdentifier::Uid): MessageInterface
522258 {
523259 /** @var UntaggedResponse $response */
524260 $ response = $ this ->uid ($ id , $ identifier )->firstOrFail ();
0 commit comments