Skip to content

Commit

Permalink
Implement bulk-move
Browse files Browse the repository at this point in the history
  • Loading branch information
particleflux committed Feb 17, 2018
1 parent f90d03f commit 4735d31
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/Mailbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,25 @@ public function getThread(): array

return false !== $tree ? $tree : [];
}

/**
* Bulk move messages.
*
* @param array|MessageIteratorInterface|string $numbers Message numbers
* @param MailboxInterface $mailbox Destination Mailbox to move the messages to
*
* @return bool true on success
*/
public function move($numbers, MailboxInterface $mailbox): bool
{
if ($numbers instanceof MessageIterator) {
$numbers = $numbers->getArrayCopy();
}

if (\is_array($numbers)) {
$numbers = \implode(',', $numbers);
}

return \imap_mail_move($this->resource->getStream(), (string) $numbers, $mailbox->getEncodedName(), \CP_UID);
}
}
10 changes: 10 additions & 0 deletions src/MailboxInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,14 @@ public function addMessage(string $message, string $options = null, DateTimeInte
* @return array
*/
public function getThread(): array;

/**
* Bulk move messages.
*
* @param array|MessageIteratorInterface|string $numbers Message numbers
* @param MailboxInterface $mailbox Destination Mailbox to move the messages to
*
* @return bool true on success
*/
public function move($numbers, self $mailbox): bool;
}
26 changes: 25 additions & 1 deletion tests/MailboxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use DateTimeImmutable;
use Ddeboer\Imap\Exception\MessageDoesNotExistException;
use Ddeboer\Imap\Exception\ReopenMailboxException;
use Ddeboer\Imap\Mailbox;
use Ddeboer\Imap\MailboxInterface;

/**
* @covers \Ddeboer\Imap\Exception\AbstractException
Expand All @@ -16,6 +16,7 @@
*/
final class MailboxTest extends AbstractTest
{
/** @var MailboxInterface */
protected $mailbox;

protected function setUp()
Expand Down Expand Up @@ -215,4 +216,27 @@ public function testAppendOptionalArguments()
$this->assertTrue($message->isSeen());
$this->assertSame(' 3-Jan-2012 09:30:03 +0000', $message->getHeaders()->get('maildate'));
}

public function testBulkMove()
{
$anotherMailbox = $this->createMailbox();

// Test move by id
$messages = [1, 2, 3];

$this->assertSame(0, $anotherMailbox->count());
$this->mailbox->move($messages, $anotherMailbox);
$this->getConnection()->expunge();

$this->assertSame(3, $anotherMailbox->count());
$this->assertSame(0, $this->mailbox->count());

// move back by iterator
$messages = $anotherMailbox->getMessages();
$anotherMailbox->move($messages, $this->mailbox);
$this->getConnection()->expunge();

$this->assertSame(0, $anotherMailbox->count());
$this->assertSame(3, $this->mailbox->count());
}
}

0 comments on commit 4735d31

Please sign in to comment.