66use DirectoryTree \ImapEngine \FolderInterface ;
77use DirectoryTree \ImapEngine \FolderRepositoryInterface ;
88use DirectoryTree \ImapEngine \MailboxInterface ;
9- use Illuminate \ Support \ItemNotFoundException ;
9+ use DirectoryTree \ ImapEngine \ Support \Str ;
1010
1111class 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}
0 commit comments