3 * @see https://github.com/zendframework/zend-mail for the canonical source repository
4 * @copyright Copyright (c) 2005-2018 Zend Technologies USA Inc. (https://www.zend.com)
5 * @license https://github.com/zendframework/zend-mail/blob/master/LICENSE.md New BSD License
8 namespace Zend\Mail\Storage
;
14 abstract class AbstractStorage
implements
20 * class capabilities with default values
33 * current iteration position
36 protected $iterationPos = 0;
39 * maximum iteration position (= message count)
42 protected $iterationMax = null;
45 * used message class, change it in an extended class to extend the returned message class
48 protected $messageClass = 'Zend\Mail\Storage\Message';
51 * Getter for has-properties. The standard has properties
52 * are: hasFolder, hasUniqueid, hasDelete, hasCreate, hasTop
54 * The valid values for the has-properties are:
55 * - true if a feature is supported
56 * - false if a feature is not supported
57 * - null is it's not yet known or it can't be know if a feature is supported
59 * @param string $var property name
60 * @throws Exception\InvalidArgumentException
61 * @return bool supported or not
63 public function __get($var)
65 if (strpos($var, 'has') === 0) {
66 $var = strtolower(substr($var, 3));
67 return isset($this->has
[$var]) ?
$this->has
[$var] : null;
70 throw new Exception\
InvalidArgumentException($var . ' not found');
74 * Get a full list of features supported by the specific mail lib and the server
76 * @return array list of features as array(feature_name => true|false[|null])
78 public function getCapabilities()
84 * Count messages messages in current box/folder
86 * @return int number of messages
87 * @throws Exception\ExceptionInterface
89 abstract public function countMessages();
92 * Get a list of messages with number and size
94 * @param int $id number of message
95 * @return int|array size of given message of list with all messages as array(num => size)
97 abstract public function getSize($id = 0);
100 * Get a message with headers and body
102 * @param $id int number of message
105 abstract public function getMessage($id);
108 * Get raw header of message or part
110 * @param int $id number of message
111 * @param null|array|string $part path to part or null for message header
112 * @param int $topLines include this many lines with header (after an empty line)
113 * @return string raw header
115 abstract public function getRawHeader($id, $part = null, $topLines = 0);
118 * Get raw content of message or part
120 * @param int $id number of message
121 * @param null|array|string $part path to part or null for message content
122 * @return string raw content
124 abstract public function getRawContent($id, $part = null);
127 * Create instance with parameters
129 * @param array $params mail reader specific parameters
130 * @throws Exception\ExceptionInterface
132 abstract public function __construct($params);
135 * Destructor calls close() and therefore closes the resource.
137 public function __destruct()
143 * Close resource for mail lib. If you need to control, when the resource
144 * is closed. Otherwise the destructor would call this.
146 abstract public function close();
149 * Keep the resource alive.
151 abstract public function noop();
154 * delete a message from current box/folder
158 abstract public function removeMessage($id);
161 * get unique id for one or all messages
163 * if storage does not support unique ids it's the same as the message number
165 * @param int|null $id message number
166 * @return array|string message number for given message or all messages as array
167 * @throws Exception\ExceptionInterface
169 abstract public function getUniqueId($id = null);
172 * get a message number from a unique id
174 * I.e. if you have a webmailer that supports deleting messages you should use unique ids
175 * as parameter and use this method to translate it to message number right before calling removeMessage()
177 * @param string $id unique id
178 * @return int message number
179 * @throws Exception\ExceptionInterface
181 abstract public function getNumberByUniqueId($id);
183 // interface implementations follows
190 public function count()
192 return $this->countMessages();
196 * ArrayAccess::offsetExists()
201 public function offsetExists($id)
204 if ($this->getMessage($id)) {
207 } catch (Exception\ExceptionInterface
$e) {
214 * ArrayAccess::offsetGet()
217 * @return \Zend\Mail\Storage\Message message object
219 public function offsetGet($id)
221 return $this->getMessage($id);
225 * ArrayAccess::offsetSet()
228 * @param mixed $value
229 * @throws Exception\RuntimeException
231 public function offsetSet($id, $value)
233 throw new Exception\
RuntimeException('cannot write mail messages via array access');
237 * ArrayAccess::offsetUnset()
240 * @return bool success
242 public function offsetUnset($id)
244 return $this->removeMessage($id);
250 * Rewind always gets the new count from the storage. Thus if you use
251 * the interfaces and your scripts take long you should use reset()
254 public function rewind()
256 $this->iterationMax
= $this->countMessages();
257 $this->iterationPos
= 1;
261 * Iterator::current()
263 * @return Message current message
265 public function current()
267 return $this->getMessage($this->iterationPos
);
273 * @return int id of current position
275 public function key()
277 return $this->iterationPos
;
283 public function next()
285 ++
$this->iterationPos
;
293 public function valid()
295 if ($this->iterationMax
=== null) {
296 $this->iterationMax
= $this->countMessages();
298 return $this->iterationPos
&& $this->iterationPos
<= $this->iterationMax
;
302 * SeekableIterator::seek()
305 * @throws Exception\OutOfBoundsException
307 public function seek($pos)
309 if ($this->iterationMax
=== null) {
310 $this->iterationMax
= $this->countMessages();
313 if ($pos > $this->iterationMax
) {
314 throw new Exception\
OutOfBoundsException('this position does not exist');
316 $this->iterationPos
= $pos;