Skip to content

Commit

Permalink
Get messages by UID sequence (#373)
Browse files Browse the repository at this point in the history
* Added getMessageSequence method to Mailbox

* Added tests and fixed code style

* Added test for empty sequence
  • Loading branch information
LeadTechVisas authored and Slamdunk committed Dec 4, 2018
1 parent 0078d24 commit 6eee3d3
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/Mailbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,31 @@ public function getMessages(ConditionInterface $search = null, int $sortCriteria
return new MessageIterator($this->resource, $messageNumbers);
}

/**
* Get message iterator for a sequence.
*
* @param string $sequence Message numbers
*
* @return MessageIteratorInterface
*/
public function getMessageSequence(string $sequence): MessageIteratorInterface
{
\imap_errors();

$overview = \imap_fetch_overview($this->resource->getStream(), $sequence, FT_UID);
if (empty($overview)) {
if (false !== \imap_last_error()) {
throw new InvalidSearchCriteriaException(\sprintf('Invalid sequence [%s]', $sequence));
}

$messageNumbers = [];
} else {
$messageNumbers = \array_column($overview, 'uid');
}

return new MessageIterator($this->resource, $messageNumbers);
}

/**
* Get a message by message number.
*
Expand Down
9 changes: 9 additions & 0 deletions src/MailboxInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ public function clearFlag(string $flag, $numbers): bool;
*/
public function getMessages(ConditionInterface $search = null, int $sortCriteria = null, bool $descending = false): MessageIteratorInterface;

/**
* Get message iterator for a sequence.
*
* @param string $sequence Message numbers
*
* @return MessageIteratorInterface
*/
public function getMessageSequence(string $sequence): MessageIteratorInterface;

/**
* Get a message by message number.
*
Expand Down
28 changes: 28 additions & 0 deletions tests/MailboxTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Ddeboer\Imap\Tests;

use DateTimeImmutable;
use Ddeboer\Imap\Exception\InvalidSearchCriteriaException;
use Ddeboer\Imap\Exception\MessageCopyException;
use Ddeboer\Imap\Exception\MessageDoesNotExistException;
use Ddeboer\Imap\Exception\MessageMoveException;
Expand Down Expand Up @@ -71,6 +72,33 @@ public function testGetMessages()
$this->assertSame(3, $aggregateIteratorMethodInc);
}

public function testGetMessageSequence()
{
$inc = 0;
foreach ($this->mailbox->getMessageSequence('1:*') as $message) {
++$inc;
}
$this->assertSame(3, $inc);

$inc = 0;
foreach ($this->mailbox->getMessageSequence('1:2') as $message) {
++$inc;
}

$this->assertSame(2, $inc);
$inc = 0;
foreach ($this->mailbox->getMessageSequence('99998:99999') as $message) {
++$inc;
}
$this->assertSame(0, $inc);
}

public function testGetMessageSequenceThrowsException()
{
$this->expectException(InvalidSearchCriteriaException::class);
$this->mailbox->getMessageSequence('-1:x');
}

public function testGetMessageThrowsException()
{
$message = $this->mailbox->getMessage(999);
Expand Down

0 comments on commit 6eee3d3

Please sign in to comment.