88namespace Sazanof \PhpImapSockets \Models ;
99
1010use Sazanof \PhpImapSockets \Collections \AddressesCollection ;
11- use Sazanof \PhpImapSockets \Collections \Collection ;
1211use Sazanof \PhpImapSockets \Collections \MessageHeadersCollection ;
1312use Sazanof \PhpImapSockets \Query \FetchQuery ;
1413use Sazanof \PhpImapSockets \Response \AttachmentBodyResponse ;
1514use Sazanof \PhpImapSockets \Response \BodyResponse ;
1615
1716class Message
1817{
18+ const FLAG_IMPORTANT = '\flagged ' ;
19+ const FLAG_DELETED = '\deleted ' ;
1920 protected int $ uid ;
2021 protected int $ num ;
2122 protected string $ messageId ;
@@ -31,6 +32,7 @@ class Message
3132 protected string $ boundary ;
3233 protected string $ contentType ;
3334 protected bool $ hasAttachments = false ;
35+ protected bool $ isImportant = false ;
3436 protected array $ flags ;
3537 protected ?string $ body = null ;
3638
@@ -87,8 +89,9 @@ public function setMailbox(Mailbox $mailbox): void
8789 /**
8890 * @param string $section
8991 * @return BodyResponse|string|null
92+ * @throws \ReflectionException
9093 */
91- public function getBody (string $ section )
94+ public function getBody (string $ section ): BodyResponse | string | null
9295 {
9396 if (!is_null ($ this ->mailbox )) {
9497 $ q = new FetchQuery ();
@@ -112,6 +115,21 @@ public function isHasAttachments(): bool
112115 public function setFlags (array $ flags ): void
113116 {
114117 $ this ->flags = $ flags ;
118+ $ this ->updateIsImportant ();
119+ }
120+
121+ public function markAsDeleted ()
122+ {
123+ $ this ->addFlags (self ::FLAG_DELETED );
124+ return $ this ;
125+ }
126+
127+ /**
128+ * @return void
129+ */
130+ public function updateIsImportant (): void
131+ {
132+ $ this ->isImportant = in_array (self ::FLAG_IMPORTANT , $ this ->getFlags ());
115133 }
116134
117135 /**
@@ -163,6 +181,14 @@ public function getSubject(): string
163181 return $ this ->subject ;
164182 }
165183
184+ /**
185+ * @return bool
186+ */
187+ public function isImportant (): bool
188+ {
189+ return $ this ->isImportant ;
190+ }
191+
166192 /**
167193 * @param string|Address $from
168194 * @return void
@@ -223,6 +249,66 @@ public function getFlags(): array
223249 return $ this ->flags ;
224250 }
225251
252+ public function addFlags (string |array $ flag )
253+ {
254+ if (is_array ($ flag )) {
255+ $ this ->flags = array_merge ($ this ->flags , $ flag );
256+ } else {
257+ if (!in_array ($ flag , $ this ->getFlags ())) {
258+ $ this ->flags [] = $ flag ;
259+ }
260+ }
261+ return $ this ;
262+ }
263+
264+ public function deleteFlags (string |array $ flag )
265+ {
266+ if (is_string ($ flag )) {
267+ $ flag = [$ flag ];
268+ }
269+ $ this ->flags = array_diff ($ this ->flags , $ flag );
270+ return $ this ;
271+ }
272+
273+ public function clearFlags ()
274+ {
275+ $ this ->flags = [];
276+ return $ this ;
277+ }
278+
279+ public function replaceFlags (string |array $ flag )
280+ {
281+ if (is_string ($ flag )) {
282+ $ flag = [$ flag ];
283+ }
284+ $ this ->flags = $ flag ;
285+ return $ this ;
286+ }
287+
288+ public function saveFlags ()
289+ {
290+ return $ this ->getMailbox ()->store ([$ this ->getNum ()], $ this ->getFlags (), false , true );
291+ }
292+
293+ /**
294+ * @return $this
295+ */
296+ public function setImportant ()
297+ {
298+ $ this ->isImportant = true ;
299+ return $ this ->addFlags (self ::FLAG_IMPORTANT );
300+ }
301+
302+ /**
303+ * @return $this
304+ */
305+ public function unsetImportant ()
306+ {
307+ $ this ->isImportant = false ;
308+ return $ this ->deleteFlags (self ::FLAG_IMPORTANT );
309+ }
310+
311+
226312 /**
227313 * @return string
228314 */
0 commit comments