1 | <?php |
---|
2 | /** |
---|
3 | * Zend Framework |
---|
4 | * |
---|
5 | * LICENSE |
---|
6 | * |
---|
7 | * This source file is subject to the new BSD license that is bundled |
---|
8 | * with this package in the file LICENSE.txt. |
---|
9 | * It is also available through the world-wide-web at this URL: |
---|
10 | * http://framework.zend.com/license/new-bsd |
---|
11 | * If you did not receive a copy of the license and are unable to |
---|
12 | * obtain it through the world-wide-web, please send an email |
---|
13 | * to license@zend.com so we can send you a copy immediately. |
---|
14 | * |
---|
15 | * @category Zend |
---|
16 | * @package Zend_Mail |
---|
17 | * @subpackage Storage |
---|
18 | * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
---|
19 | * @license http://framework.zend.com/license/new-bsd New BSD License |
---|
20 | * @version $Id: Folder.php 20096 2010-01-06 02:05:09Z bkarwin $ |
---|
21 | */ |
---|
22 | |
---|
23 | |
---|
24 | /** |
---|
25 | * @category Zend |
---|
26 | * @package Zend_Mail |
---|
27 | * @subpackage Storage |
---|
28 | * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
---|
29 | * @license http://framework.zend.com/license/new-bsd New BSD License |
---|
30 | */ |
---|
31 | class Zend_Mail_Storage_Folder implements RecursiveIterator |
---|
32 | { |
---|
33 | /** |
---|
34 | * subfolders of folder array(localName => Zend_Mail_Storage_Folder folder) |
---|
35 | * @var array |
---|
36 | */ |
---|
37 | protected $_folders; |
---|
38 | |
---|
39 | /** |
---|
40 | * local name (name of folder in parent folder) |
---|
41 | * @var string |
---|
42 | */ |
---|
43 | protected $_localName; |
---|
44 | |
---|
45 | /** |
---|
46 | * global name (absolute name of folder) |
---|
47 | * @var string |
---|
48 | */ |
---|
49 | protected $_globalName; |
---|
50 | |
---|
51 | /** |
---|
52 | * folder is selectable if folder is able to hold messages, else it's just a parent folder |
---|
53 | * @var bool |
---|
54 | */ |
---|
55 | protected $_selectable = true; |
---|
56 | |
---|
57 | /** |
---|
58 | * create a new mail folder instance |
---|
59 | * |
---|
60 | * @param string $localName name of folder in current subdirectory |
---|
61 | * @param string $globalName absolute name of folder |
---|
62 | * @param bool $selectable if true folder holds messages, if false it's just a parent for subfolders |
---|
63 | * @param array $folders init with given instances of Zend_Mail_Storage_Folder as subfolders |
---|
64 | */ |
---|
65 | public function __construct($localName, $globalName = '', $selectable = true, array $folders = array()) |
---|
66 | { |
---|
67 | $this->_localName = $localName; |
---|
68 | $this->_globalName = $globalName ? $globalName : $localName; |
---|
69 | $this->_selectable = $selectable; |
---|
70 | $this->_folders = $folders; |
---|
71 | } |
---|
72 | |
---|
73 | /** |
---|
74 | * implements RecursiveIterator::hasChildren() |
---|
75 | * |
---|
76 | * @return bool current element has children |
---|
77 | */ |
---|
78 | public function hasChildren() |
---|
79 | { |
---|
80 | $current = $this->current(); |
---|
81 | return $current && $current instanceof Zend_Mail_Storage_Folder && !$current->isLeaf(); |
---|
82 | } |
---|
83 | |
---|
84 | /** |
---|
85 | * implements RecursiveIterator::getChildren() |
---|
86 | * |
---|
87 | * @return Zend_Mail_Storage_Folder same as self::current() |
---|
88 | */ |
---|
89 | public function getChildren() |
---|
90 | { |
---|
91 | return $this->current(); |
---|
92 | } |
---|
93 | |
---|
94 | /** |
---|
95 | * implements Iterator::valid() |
---|
96 | * |
---|
97 | * @return bool check if there's a current element |
---|
98 | */ |
---|
99 | public function valid() |
---|
100 | { |
---|
101 | return key($this->_folders) !== null; |
---|
102 | } |
---|
103 | |
---|
104 | /** |
---|
105 | * implements Iterator::next() |
---|
106 | * |
---|
107 | * @return null |
---|
108 | */ |
---|
109 | public function next() |
---|
110 | { |
---|
111 | next($this->_folders); |
---|
112 | } |
---|
113 | |
---|
114 | /** |
---|
115 | * implements Iterator::key() |
---|
116 | * |
---|
117 | * @return string key/local name of current element |
---|
118 | */ |
---|
119 | public function key() |
---|
120 | { |
---|
121 | return key($this->_folders); |
---|
122 | } |
---|
123 | |
---|
124 | /** |
---|
125 | * implements Iterator::current() |
---|
126 | * |
---|
127 | * @return Zend_Mail_Storage_Folder current folder |
---|
128 | */ |
---|
129 | public function current() |
---|
130 | { |
---|
131 | return current($this->_folders); |
---|
132 | } |
---|
133 | |
---|
134 | /** |
---|
135 | * implements Iterator::rewind() |
---|
136 | * |
---|
137 | * @return null |
---|
138 | */ |
---|
139 | public function rewind() |
---|
140 | { |
---|
141 | reset($this->_folders); |
---|
142 | } |
---|
143 | |
---|
144 | /** |
---|
145 | * get subfolder named $name |
---|
146 | * |
---|
147 | * @param string $name wanted subfolder |
---|
148 | * @return Zend_Mail_Storage_Folder folder named $folder |
---|
149 | * @throws Zend_Mail_Storage_Exception |
---|
150 | */ |
---|
151 | public function __get($name) |
---|
152 | { |
---|
153 | if (!isset($this->_folders[$name])) { |
---|
154 | /** |
---|
155 | * @see Zend_Mail_Storage_Exception |
---|
156 | */ |
---|
157 | require_once 'Zend/Mail/Storage/Exception.php'; |
---|
158 | throw new Zend_Mail_Storage_Exception("no subfolder named $name"); |
---|
159 | } |
---|
160 | |
---|
161 | return $this->_folders[$name]; |
---|
162 | } |
---|
163 | |
---|
164 | /** |
---|
165 | * add or replace subfolder named $name |
---|
166 | * |
---|
167 | * @param string $name local name of subfolder |
---|
168 | * @param Zend_Mail_Storage_Folder $folder instance for new subfolder |
---|
169 | * @return null |
---|
170 | */ |
---|
171 | public function __set($name, Zend_Mail_Storage_Folder $folder) |
---|
172 | { |
---|
173 | $this->_folders[$name] = $folder; |
---|
174 | } |
---|
175 | |
---|
176 | /** |
---|
177 | * remove subfolder named $name |
---|
178 | * |
---|
179 | * @param string $name local name of subfolder |
---|
180 | * @return null |
---|
181 | */ |
---|
182 | public function __unset($name) |
---|
183 | { |
---|
184 | unset($this->_folders[$name]); |
---|
185 | } |
---|
186 | |
---|
187 | /** |
---|
188 | * magic method for easy output of global name |
---|
189 | * |
---|
190 | * @return string global name of folder |
---|
191 | */ |
---|
192 | public function __toString() |
---|
193 | { |
---|
194 | return (string)$this->getGlobalName(); |
---|
195 | } |
---|
196 | |
---|
197 | /** |
---|
198 | * get local name |
---|
199 | * |
---|
200 | * @return string local name |
---|
201 | */ |
---|
202 | public function getLocalName() |
---|
203 | { |
---|
204 | return $this->_localName; |
---|
205 | } |
---|
206 | |
---|
207 | /** |
---|
208 | * get global name |
---|
209 | * |
---|
210 | * @return string global name |
---|
211 | */ |
---|
212 | public function getGlobalName() |
---|
213 | { |
---|
214 | return $this->_globalName; |
---|
215 | } |
---|
216 | |
---|
217 | /** |
---|
218 | * is this folder selectable? |
---|
219 | * |
---|
220 | * @return bool selectable |
---|
221 | */ |
---|
222 | public function isSelectable() |
---|
223 | { |
---|
224 | return $this->_selectable; |
---|
225 | } |
---|
226 | |
---|
227 | /** |
---|
228 | * check if folder has no subfolder |
---|
229 | * |
---|
230 | * @return bool true if no subfolders |
---|
231 | */ |
---|
232 | public function isLeaf() |
---|
233 | { |
---|
234 | return empty($this->_folders); |
---|
235 | } |
---|
236 | } |
---|