Skip to content

Commit 3b44f7e

Browse files
authored
Merge pull request #84 from DirectoryTree/enhancement-83
Move flags to flaggable interface
2 parents 81bf3d1 + edf49a4 commit 3b44f7e

File tree

8 files changed

+421
-174
lines changed

8 files changed

+421
-174
lines changed

src/FileMessage.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class FileMessage implements MessageInterface
88
{
9-
use HasParsedMessage;
9+
use HasFlags, HasParsedMessage;
1010

1111
/**
1212
* Constructor.
@@ -23,6 +23,14 @@ public function uid(): int
2323
throw new BadMethodCallException('FileMessage does not support a UID');
2424
}
2525

26+
/**
27+
* {@inheritDoc}
28+
*/
29+
public function flag(mixed $flag, string $operation, bool $expunge = false): void
30+
{
31+
throw new BadMethodCallException('FileMessage does not support flagging');
32+
}
33+
2634
/**
2735
* Get the string representation of the message.
2836
*/
@@ -40,6 +48,14 @@ public function is(MessageInterface $message): bool
4048
&& $this->contents === $message->contents;
4149
}
4250

51+
/**
52+
* Get the message flags.
53+
*/
54+
public function flags(): array
55+
{
56+
return [];
57+
}
58+
4359
/**
4460
* Determine if the message is empty.
4561
*/

src/FlaggableInterface.php

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
3+
namespace DirectoryTree\ImapEngine;
4+
5+
use BackedEnum;
6+
7+
interface FlaggableInterface
8+
{
9+
/**
10+
* Mark the message as read. Alias for markSeen.
11+
*/
12+
public function markRead(): void;
13+
14+
/**
15+
* Mark the message as unread. Alias for unmarkSeen.
16+
*/
17+
public function markUnread(): void;
18+
19+
/**
20+
* Mark the message as seen.
21+
*/
22+
public function markSeen(): void;
23+
24+
/**
25+
* Unmark the seen flag.
26+
*/
27+
public function unmarkSeen(): void;
28+
29+
/**
30+
* Mark the message as answered.
31+
*/
32+
public function markAnswered(): void;
33+
34+
/**
35+
* Unmark the answered flag.
36+
*/
37+
public function unmarkAnswered(): void;
38+
39+
/**
40+
* Mark the message as flagged.
41+
*/
42+
public function markFlagged(): void;
43+
44+
/**
45+
* Unmark the flagged flag.
46+
*/
47+
public function unmarkFlagged(): void;
48+
49+
/**
50+
* Mark the message as deleted.
51+
*/
52+
public function markDeleted(bool $expunge = false): void;
53+
54+
/**
55+
* Unmark the deleted flag.
56+
*/
57+
public function unmarkDeleted(): void;
58+
59+
/**
60+
* Mark the message as a draft.
61+
*/
62+
public function markDraft(): void;
63+
64+
/**
65+
* Unmark the draft flag.
66+
*/
67+
public function unmarkDraft(): void;
68+
69+
/**
70+
* Mark the message as recent.
71+
*/
72+
public function markRecent(): void;
73+
74+
/**
75+
* Unmark the recent flag.
76+
*/
77+
public function unmarkRecent(): void;
78+
79+
/**
80+
* Determine if the message is marked as seen.
81+
*/
82+
public function isSeen(): bool;
83+
84+
/**
85+
* Determine if the message is marked as answered.
86+
*/
87+
public function isAnswered(): bool;
88+
89+
/**
90+
* Determine if the message is flagged.
91+
*/
92+
public function isFlagged(): bool;
93+
94+
/**
95+
* Determine if the message is marked as deleted.
96+
*/
97+
public function isDeleted(): bool;
98+
99+
/**
100+
* Determine if the message is marked as a draft.
101+
*/
102+
public function isDraft(): bool;
103+
104+
/**
105+
* Determine if the message is marked as recent.
106+
*/
107+
public function isRecent(): bool;
108+
109+
/**
110+
* Get the message's flags.
111+
*
112+
* @return string[]
113+
*/
114+
public function flags(): array;
115+
116+
/**
117+
* Determine if the message has the given flag.
118+
*/
119+
public function hasFlag(BackedEnum|string $flag): bool;
120+
121+
/**
122+
* Add or remove a flag from the message.
123+
*/
124+
public function flag(mixed $flag, string $operation, bool $expunge = false): void;
125+
}

src/HasFlags.php

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
<?php
2+
3+
namespace DirectoryTree\ImapEngine;
4+
5+
use BackedEnum;
6+
use DirectoryTree\ImapEngine\Enums\ImapFlag;
7+
use DirectoryTree\ImapEngine\Support\Str;
8+
9+
trait HasFlags
10+
{
11+
/**
12+
* {@inheritDoc}
13+
*/
14+
public function markRead(): void
15+
{
16+
$this->markSeen();
17+
}
18+
19+
/**
20+
* {@inheritDoc}
21+
*/
22+
public function markUnread(): void
23+
{
24+
$this->unmarkSeen();
25+
}
26+
27+
/**
28+
* {@inheritDoc}
29+
*/
30+
public function markSeen(): void
31+
{
32+
$this->flag(ImapFlag::Seen, '+');
33+
}
34+
35+
/**
36+
* {@inheritDoc}
37+
*/
38+
public function unmarkSeen(): void
39+
{
40+
$this->flag(ImapFlag::Seen, '-');
41+
}
42+
43+
/**
44+
* {@inheritDoc}
45+
*/
46+
public function markAnswered(): void
47+
{
48+
$this->flag(ImapFlag::Answered, '+');
49+
}
50+
51+
/**
52+
* {@inheritDoc}
53+
*/
54+
public function unmarkAnswered(): void
55+
{
56+
$this->flag(ImapFlag::Answered, '-');
57+
}
58+
59+
/**
60+
* {@inheritDoc}
61+
*/
62+
public function markFlagged(): void
63+
{
64+
$this->flag(ImapFlag::Flagged, '+');
65+
}
66+
67+
/**
68+
* {@inheritDoc}
69+
*/
70+
public function unmarkFlagged(): void
71+
{
72+
$this->flag(ImapFlag::Flagged, '-');
73+
}
74+
75+
/**
76+
* {@inheritDoc}
77+
*/
78+
public function markDeleted(bool $expunge = false): void
79+
{
80+
$this->flag(ImapFlag::Deleted, '+', $expunge);
81+
}
82+
83+
/**
84+
* {@inheritDoc}
85+
*/
86+
public function unmarkDeleted(): void
87+
{
88+
$this->flag(ImapFlag::Deleted, '-');
89+
}
90+
91+
/**
92+
* {@inheritDoc}
93+
*/
94+
public function markDraft(): void
95+
{
96+
$this->flag(ImapFlag::Draft, '+');
97+
}
98+
99+
/**
100+
* {@inheritDoc}
101+
*/
102+
public function unmarkDraft(): void
103+
{
104+
$this->flag(ImapFlag::Draft, '-');
105+
}
106+
107+
/**
108+
* {@inheritDoc}
109+
*/
110+
public function markRecent(): void
111+
{
112+
$this->flag(ImapFlag::Recent, '+');
113+
}
114+
115+
/**
116+
* {@inheritDoc}
117+
*/
118+
public function unmarkRecent(): void
119+
{
120+
$this->flag(ImapFlag::Recent, '-');
121+
}
122+
123+
/**
124+
* {@inheritDoc}
125+
*/
126+
public function isSeen(): bool
127+
{
128+
return $this->hasFlag(ImapFlag::Seen);
129+
}
130+
131+
/**
132+
* {@inheritDoc}
133+
*/
134+
public function isAnswered(): bool
135+
{
136+
return $this->hasFlag(ImapFlag::Answered);
137+
}
138+
139+
/**
140+
* {@inheritDoc}
141+
*/
142+
public function isFlagged(): bool
143+
{
144+
return $this->hasFlag(ImapFlag::Flagged);
145+
}
146+
147+
/**
148+
* {@inheritDoc}
149+
*/
150+
public function isDeleted(): bool
151+
{
152+
return $this->hasFlag(ImapFlag::Deleted);
153+
}
154+
155+
/**
156+
* {@inheritDoc}
157+
*/
158+
public function isDraft(): bool
159+
{
160+
return $this->hasFlag(ImapFlag::Draft);
161+
}
162+
163+
/**
164+
* {@inheritDoc}
165+
*/
166+
public function isRecent(): bool
167+
{
168+
return $this->hasFlag(ImapFlag::Recent);
169+
}
170+
171+
/**
172+
* {@inheritDoc}
173+
*/
174+
public function hasFlag(BackedEnum|string $flag): bool
175+
{
176+
return in_array(Str::enum($flag), $this->flags());
177+
}
178+
179+
/**
180+
* {@inheritDoc}
181+
*/
182+
abstract public function flags(): array;
183+
184+
/**
185+
* {@inheritDoc}
186+
*/
187+
abstract public function flag(mixed $flag, string $operation, bool $expunge = false): void;
188+
}

0 commit comments

Comments
 (0)