3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Mail\Storage
;
16 abstract class AbstractStorage
implements
22 * class capabilities with default values
25 protected $has = array('uniqueid' => true,
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');
75 * Get a full list of features supported by the specific mail lib and the server
77 * @return array list of features as array(feature_name => true|false[|null])
79 public function getCapabilities()
86 * Count messages messages in current box/folder
88 * @return int number of messages
89 * @throws Exception\ExceptionInterface
91 abstract public function countMessages();
95 * Get a list of messages with number and size
97 * @param int $id number of message
98 * @return int|array size of given message of list with all messages as array(num => size)
100 abstract public function getSize($id = 0);
104 * Get a message with headers and body
106 * @param $id int number of message
109 abstract public function getMessage($id);
113 * Get raw header of message or part
115 * @param int $id number of message
116 * @param null|array|string $part path to part or null for message header
117 * @param int $topLines include this many lines with header (after an empty line)
118 * @return string raw header
120 abstract public function getRawHeader($id, $part = null, $topLines = 0);
123 * Get raw content of message or part
125 * @param int $id number of message
126 * @param null|array|string $part path to part or null for message content
127 * @return string raw content
129 abstract public function getRawContent($id, $part = null);
132 * Create instance with parameters
134 * @param array $params mail reader specific parameters
135 * @throws Exception\ExceptionInterface
137 abstract public function __construct($params);
141 * Destructor calls close() and therefore closes the resource.
143 public function __destruct()
150 * Close resource for mail lib. If you need to control, when the resource
151 * is closed. Otherwise the destructor would call this.
153 abstract public function close();
157 * Keep the resource alive.
159 abstract public function noop();
162 * delete a message from current box/folder
166 abstract public function removeMessage($id);
169 * get unique id for one or all messages
171 * if storage does not support unique ids it's the same as the message number
173 * @param int|null $id message number
174 * @return array|string message number for given message or all messages as array
175 * @throws Exception\ExceptionInterface
177 abstract public function getUniqueId($id = null);
180 * get a message number from a unique id
182 * I.e. if you have a webmailer that supports deleting messages you should use unique ids
183 * as parameter and use this method to translate it to message number right before calling removeMessage()
185 * @param string $id unique id
186 * @return int message number
187 * @throws Exception\ExceptionInterface
189 abstract public function getNumberByUniqueId($id);
191 // interface implementations follows
198 public function count()
200 return $this->countMessages();
205 * ArrayAccess::offsetExists()
210 public function offsetExists($id)
213 if ($this->getMessage($id)) {
216 } catch (Exception\ExceptionInterface
$e) {}
223 * ArrayAccess::offsetGet()
226 * @return \Zend\Mail\Storage\Message message object
228 public function offsetGet($id)
230 return $this->getMessage($id);
235 * ArrayAccess::offsetSet()
238 * @param mixed $value
239 * @throws Exception\RuntimeException
241 public function offsetSet($id, $value)
243 throw new Exception\
RuntimeException('cannot write mail messages via array access');
248 * ArrayAccess::offsetUnset()
251 * @return bool success
253 public function offsetUnset($id)
255 return $this->removeMessage($id);
262 * Rewind always gets the new count from the storage. Thus if you use
263 * the interfaces and your scripts take long you should use reset()
266 public function rewind()
268 $this->iterationMax
= $this->countMessages();
269 $this->iterationPos
= 1;
274 * Iterator::current()
276 * @return Message current message
278 public function current()
280 return $this->getMessage($this->iterationPos
);
287 * @return int id of current position
289 public function key()
291 return $this->iterationPos
;
298 public function next()
300 ++
$this->iterationPos
;
309 public function valid()
311 if ($this->iterationMax
=== null) {
312 $this->iterationMax
= $this->countMessages();
314 return $this->iterationPos
&& $this->iterationPos
<= $this->iterationMax
;
319 * SeekableIterator::seek()
322 * @throws Exception\OutOfBoundsException
324 public function seek($pos)
326 if ($this->iterationMax
=== null) {
327 $this->iterationMax
= $this->countMessages();
330 if ($pos > $this->iterationMax
) {
331 throw new Exception\
OutOfBoundsException('this position does not exist');
333 $this->iterationPos
= $pos;