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