composer package updates
[openemr.git] / vendor / zendframework / zend-mail / src / Storage / AbstractStorage.php
blob55d95cf163a678b00556fe6813f4edbdc02dea1a
1 <?php
2 /**
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
6 */
8 namespace Zend\Mail\Storage;
10 use ArrayAccess;
11 use Countable;
12 use SeekableIterator;
14 abstract class AbstractStorage implements
15 ArrayAccess,
16 Countable,
17 SeekableIterator
19 /**
20 * class capabilities with default values
21 * @var array
23 protected $has = [
24 'uniqueid' => true,
25 'delete' => false,
26 'create' => false,
27 'top' => false,
28 'fetchPart' => true,
29 'flags' => false,
32 /**
33 * current iteration position
34 * @var int
36 protected $iterationPos = 0;
38 /**
39 * maximum iteration position (= message count)
40 * @var null|int
42 protected $iterationMax = null;
44 /**
45 * used message class, change it in an extended class to extend the returned message class
46 * @var string
48 protected $messageClass = 'Zend\Mail\Storage\Message';
50 /**
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');
73 /**
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()
80 return $this->has;
83 /**
84 * Count messages messages in current box/folder
86 * @return int number of messages
87 * @throws Exception\ExceptionInterface
89 abstract public function countMessages();
91 /**
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);
99 /**
100 * Get a message with headers and body
102 * @param $id int number of message
103 * @return 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()
139 $this->close();
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
156 * @param $id
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
186 * Countable::count()
188 * @return int
190 public function count()
192 return $this->countMessages();
196 * ArrayAccess::offsetExists()
198 * @param int $id
199 * @return bool
201 public function offsetExists($id)
203 try {
204 if ($this->getMessage($id)) {
205 return true;
207 } catch (Exception\ExceptionInterface $e) {
210 return false;
214 * ArrayAccess::offsetGet()
216 * @param int $id
217 * @return \Zend\Mail\Storage\Message message object
219 public function offsetGet($id)
221 return $this->getMessage($id);
225 * ArrayAccess::offsetSet()
227 * @param mixed $id
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()
239 * @param int $id
240 * @return bool success
242 public function offsetUnset($id)
244 return $this->removeMessage($id);
248 * Iterator::rewind()
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()
252 * from time to time.
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);
271 * Iterator::key()
273 * @return int id of current position
275 public function key()
277 return $this->iterationPos;
281 * Iterator::next()
283 public function next()
285 ++$this->iterationPos;
289 * Iterator::valid()
291 * @return bool
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()
304 * @param int $pos
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;