Skip to content

Commit 3093354

Browse files
committed
WIP
1 parent 54307f0 commit 3093354

File tree

8 files changed

+79
-49
lines changed

8 files changed

+79
-49
lines changed

src/FolderInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function is(FolderInterface $folder): bool;
3939
/**
4040
* Begin querying for messages.
4141
*/
42-
public function messages(): MessageQuery;
42+
public function messages(): MessageQueryInterface;
4343

4444
/**
4545
* Begin idling on the current folder.

src/FolderRepository.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,35 @@ public function __construct(
1717
/**
1818
* {@inheritDoc}
1919
*/
20-
public function find(string $folder): ?FolderInterface
20+
public function find(string $path): ?FolderInterface
2121
{
22-
return $this->get($folder)->first();
22+
return $this->get($path)->first();
2323
}
2424

2525
/**
2626
* {@inheritDoc}
2727
*/
28-
public function findOrFail(string $folder): FolderInterface
28+
public function findOrFail(string $path): FolderInterface
2929
{
30-
return $this->get($folder)->firstOrFail();
30+
return $this->get($path)->firstOrFail();
3131
}
3232

3333
/**
3434
* {@inheritDoc}
3535
*/
36-
public function create(string $folder): FolderInterface
36+
public function create(string $path): FolderInterface
3737
{
38-
$this->mailbox->connection()->create($folder);
38+
$this->mailbox->connection()->create($path);
3939

40-
return $this->find($folder);
40+
return $this->find($path);
4141
}
4242

4343
/**
4444
* {@inheritDoc}
4545
*/
46-
public function firstOrCreate(string $folder): FolderInterface
46+
public function firstOrCreate(string $path): FolderInterface
4747
{
48-
return $this->find($folder) ?? $this->create($folder);
48+
return $this->find($path) ?? $this->create($path);
4949
}
5050

5151
/**

src/FolderRepositoryInterface.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ interface FolderRepositoryInterface
99
/**
1010
* Find a folder.
1111
*/
12-
public function find(string $folder): ?FolderInterface;
12+
public function find(string $path): ?FolderInterface;
1313

1414
/**
1515
* Find a folder or throw an exception.
1616
*/
17-
public function findOrFail(string $folder): FolderInterface;
17+
public function findOrFail(string $path): FolderInterface;
1818

1919
/**
2020
* Create a new folder.
2121
*/
22-
public function create(string $folder): FolderInterface;
22+
public function create(string $path): FolderInterface;
2323

2424
/**
2525
* Find or create a folder.
2626
*/
27-
public function firstOrCreate(string $folder): FolderInterface;
27+
public function firstOrCreate(string $path): FolderInterface;
2828

2929
/**
3030
* Get the mailboxes folders.

src/Support/Str.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,4 +199,41 @@ public static function decodeUtf7Imap(string $string): string
199199
return $result;
200200
}, $string);
201201
}
202+
203+
public static function is($pattern, $value, $ignoreCase = false)
204+
{
205+
$value = (string) $value;
206+
207+
if (! is_iterable($pattern)) {
208+
$pattern = [$pattern];
209+
}
210+
211+
foreach ($pattern as $pattern) {
212+
$pattern = (string) $pattern;
213+
214+
// If the given value is an exact match we can of course return true right
215+
// from the beginning. Otherwise, we will translate asterisks and do an
216+
// actual pattern match against the two strings to see if they match.
217+
if ($pattern === '*' || $pattern === $value) {
218+
return true;
219+
}
220+
221+
if ($ignoreCase && mb_strtolower($pattern) === mb_strtolower($value)) {
222+
return true;
223+
}
224+
225+
$pattern = preg_quote($pattern, '#');
226+
227+
// Asterisks are translated into zero-or-more regular expression wildcards
228+
// to make it convenient to check if the strings starts with the given
229+
// pattern such as "library/*", making any string check convenient.
230+
$pattern = str_replace('\*', '.*', $pattern);
231+
232+
if (preg_match('#^'.$pattern.'\z#'.($ignoreCase ? 'isu' : 'su'), $value) === 1) {
233+
return true;
234+
}
235+
}
236+
237+
return false;
238+
}
202239
}

src/Testing/FakeFolder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use DirectoryTree\ImapEngine\Exceptions\Exception;
66
use DirectoryTree\ImapEngine\FolderInterface;
77
use DirectoryTree\ImapEngine\MailboxInterface;
8-
use DirectoryTree\ImapEngine\MessageQuery;
8+
use DirectoryTree\ImapEngine\MessageQueryInterface;
99
use DirectoryTree\ImapEngine\Support\Str;
1010

1111
class FakeFolder implements FolderInterface
@@ -77,7 +77,7 @@ public function is(FolderInterface $folder): bool
7777
/**
7878
* {@inheritDoc}
7979
*/
80-
public function messages(): MessageQuery
80+
public function messages(): MessageQueryInterface
8181
{
8282
// Ensure the folder is selected.
8383
$this->select(true);

src/Testing/FakeFolderRepository.php

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use DirectoryTree\ImapEngine\FolderInterface;
77
use DirectoryTree\ImapEngine\FolderRepositoryInterface;
88
use DirectoryTree\ImapEngine\MailboxInterface;
9-
use Illuminate\Support\ItemNotFoundException;
9+
use DirectoryTree\ImapEngine\Support\Str;
1010

1111
class FakeFolderRepository implements FolderRepositoryInterface
1212
{
@@ -22,48 +22,46 @@ public function __construct(
2222
/**
2323
* {@inheritDoc}
2424
*/
25-
public function find(string $folder): ?FolderInterface
25+
public function find(string $path): ?FolderInterface
2626
{
27-
return $this->folders[$folder] ?? null;
27+
return $this->get()->first(
28+
fn (FolderInterface $folder) => $folder->path() === $path
29+
);
2830
}
2931

3032
/**
3133
* {@inheritDoc}
3234
*/
33-
public function findOrFail(string $folder): FolderInterface
35+
public function findOrFail(string $path): FolderInterface
3436
{
35-
return $this->folders[$folder] ?? throw new ItemNotFoundException("Folder [{$folder}] not found.");
37+
return $this->get()->firstOrFail(
38+
fn (FolderInterface $folder) => $folder->path() === $path
39+
);
3640
}
3741

3842
/**
3943
* {@inheritDoc}
4044
*/
41-
public function create(string $folder): FolderInterface
45+
public function create(string $path): FolderInterface
4246
{
43-
return $this->folders[$folder] = new FakeFolder($folder, mailbox: $this->mailbox);
47+
return $this->folders[] = new FakeFolder($path, mailbox: $this->mailbox);
4448
}
4549

4650
/**
4751
* {@inheritDoc}
4852
*/
49-
public function firstOrCreate(string $folder): FolderInterface
53+
public function firstOrCreate(string $path): FolderInterface
5054
{
51-
return $this->find($folder) ?? $this->create($folder);
55+
return $this->find($path) ?? $this->create($path);
5256
}
5357

5458
/**
5559
* {@inheritDoc}
5660
*/
5761
public function get(?string $match = '*', ?string $reference = ''): FolderCollection
5862
{
59-
$pattern = str_replace(
60-
['*', '%'],
61-
['.*', '[^/]*'],
62-
preg_quote($match, '/'),
63-
);
64-
6563
return FolderCollection::make($this->folders)->filter(
66-
fn (FolderInterface $folder) => (bool) preg_match('/^'.$pattern.'$/', $folder->path())
64+
fn (FolderInterface $folder) => Str::is($match, $folder->path())
6765
);
6866
}
6967
}

src/Testing/FakeMailbox.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ class FakeMailbox implements MailboxInterface
1515
*/
1616
protected ?FolderInterface $selected = null;
1717

18-
/**
19-
* The next available message UID.
20-
*/
21-
protected static int $nextMessageUid = 1;
22-
2318
/**
2419
* Constructor.
2520
*/
@@ -125,12 +120,4 @@ public function selected(FolderInterface $folder): bool
125120
{
126121
return $this->selected?->is($folder) ?? false;
127122
}
128-
129-
/**
130-
* Get the next available UID and increment the counter.
131-
*/
132-
public function getNextUid(): int
133-
{
134-
return static::$nextMessageUid++;
135-
}
136123
}

src/Testing/FakeMessageQuery.php

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ public function append(string $message, mixed $flags = null): int
6262
{
6363
$uid = 1;
6464

65-
if ($message = $this->get()->last()) {
66-
$uid = $message->uid() + 1;
65+
if ($lastMessage = $this->get()->last()) {
66+
$uid = $lastMessage->uid() + 1;
6767
}
6868

69-
$this->messages[] = new FakeMessage($uid, $flags, $message);
69+
$this->messages[] = new FakeMessage($uid, $flags === null ? [] : $flags, $message);
7070

7171
return $uid;
7272
}
@@ -116,6 +116,14 @@ public function find(int $id, ImapFetchIdentifier $identifier = ImapFetchIdentif
116116
*/
117117
public function destroy(array|int $uids, bool $expunge = false): void
118118
{
119-
// Do nothing.
119+
$messages = $this->get()->keyBy(
120+
fn (MessageInterface $message) => $message->uid()
121+
);
122+
123+
foreach ((array) $uids as $uid) {
124+
$messages->pull($uid);
125+
}
126+
127+
$this->messages = $messages->values()->all();
120128
}
121129
}

0 commit comments

Comments
 (0)