blob: 48d87ee3aecc3e9e89bd17b6c338c114b845c5bd [file] [log] [blame]
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001<?php
2
3declare(strict_types=1);
4
5namespace Ddeboer\Imap;
6
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02007use Ddeboer\Imap\Message\PartInterface;
8
9/**
10 * @extends \ArrayIterator<int, MessageInterface>
11 */
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010012final class MessageIterator extends \ArrayIterator implements MessageIteratorInterface
13{
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020014 private ImapResourceInterface $resource;
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010015
16 /**
17 * Constructor.
18 *
19 * @param ImapResourceInterface $resource IMAP resource
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020020 * @param int[] $messageNumbers Array of message numbers
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010021 */
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 Benkard7b2a3a12021-08-16 10:57:25 +020031 *
32 * @return MessageInterface<PartInterface>
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +010033 */
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}