Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Ddeboer\Imap; |
| 6 | |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 7 | use Ddeboer\Imap\Message\PartInterface; |
| 8 | |
| 9 | /** |
| 10 | * @extends \ArrayIterator<int, MessageInterface> |
| 11 | */ |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 12 | final class MessageIterator extends \ArrayIterator implements MessageIteratorInterface |
| 13 | { |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 14 | private ImapResourceInterface $resource; |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 15 | |
| 16 | /** |
| 17 | * Constructor. |
| 18 | * |
| 19 | * @param ImapResourceInterface $resource IMAP resource |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 20 | * @param int[] $messageNumbers Array of message numbers |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 21 | */ |
| 22 | public function __construct(ImapResourceInterface $resource, array $messageNumbers) |
| 23 | { |
| 24 | $this->resource = $resource; |
| 25 | |
| 26 | parent::__construct($messageNumbers); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * Get current message. |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame^] | 31 | * |
| 32 | * @return MessageInterface<PartInterface> |
Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame] | 33 | */ |
| 34 | public function current(): MessageInterface |
| 35 | { |
| 36 | $current = parent::current(); |
| 37 | if (!\is_int($current)) { |
| 38 | throw new Exception\OutOfBoundsException(\sprintf( |
| 39 | 'The current value "%s" isn\'t an integer and doesn\'t represent a message;' |
| 40 | . ' try to cycle this "%s" with a native php function like foreach or with the method getArrayCopy(),' |
| 41 | . ' or check it by calling the methods valid().', |
| 42 | \is_object($current) ? \get_class($current) : \gettype($current), |
| 43 | static::class |
| 44 | )); |
| 45 | } |
| 46 | |
| 47 | return new Message($this->resource, $current); |
| 48 | } |
| 49 | } |