/framework/vendor/zend/Zend/Mail/Storage/Folder.php

http://zoop.googlecode.com/ · PHP · 236 lines · 80 code · 22 blank · 134 comment · 3 complexity · fb63f2c3ef09a2be2ad2a052b8d1b99e MD5 · raw file

  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. * @category Zend
  24. * @package Zend_Mail
  25. * @subpackage Storage
  26. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. class Zend_Mail_Storage_Folder implements RecursiveIterator
  30. {
  31. /**
  32. * subfolders of folder array(localName => Zend_Mail_Storage_Folder folder)
  33. * @var array
  34. */
  35. protected $_folders;
  36. /**
  37. * local name (name of folder in parent folder)
  38. * @var string
  39. */
  40. protected $_localName;
  41. /**
  42. * global name (absolute name of folder)
  43. * @var string
  44. */
  45. protected $_globalName;
  46. /**
  47. * folder is selectable if folder is able to hold messages, else it's just a parent folder
  48. * @var bool
  49. */
  50. protected $_selectable = true;
  51. /**
  52. * create a new mail folder instance
  53. *
  54. * @param string $localName name of folder in current subdirectory
  55. * @param string $globalName absolute name of folder
  56. * @param bool $selectable if true folder holds messages, if false it's just a parent for subfolders
  57. * @param array $folders init with given instances of Zend_Mail_Storage_Folder as subfolders
  58. */
  59. public function __construct($localName, $globalName = '', $selectable = true, array $folders = array())
  60. {
  61. $this->_localName = $localName;
  62. $this->_globalName = $globalName ? $globalName : $localName;
  63. $this->_selectable = $selectable;
  64. $this->_folders = $folders;
  65. }
  66. /**
  67. * implements RecursiveIterator::hasChildren()
  68. *
  69. * @return bool current element has children
  70. */
  71. public function hasChildren()
  72. {
  73. $current = $this->current();
  74. return $current && $current instanceof Zend_Mail_Storage_Folder && !$current->isLeaf();
  75. }
  76. /**
  77. * implements RecursiveIterator::getChildren()
  78. *
  79. * @return Zend_Mail_Storage_Folder same as self::current()
  80. */
  81. public function getChildren()
  82. {
  83. return $this->current();
  84. }
  85. /**
  86. * implements Iterator::valid()
  87. *
  88. * @return bool check if there's a current element
  89. */
  90. public function valid()
  91. {
  92. return key($this->_folders) !== null;
  93. }
  94. /**
  95. * implements Iterator::next()
  96. *
  97. * @return null
  98. */
  99. public function next()
  100. {
  101. next($this->_folders);
  102. }
  103. /**
  104. * implements Iterator::key()
  105. *
  106. * @return string key/local name of current element
  107. */
  108. public function key()
  109. {
  110. return key($this->_folders);
  111. }
  112. /**
  113. * implements Iterator::current()
  114. *
  115. * @return Zend_Mail_Storage_Folder current folder
  116. */
  117. public function current()
  118. {
  119. return current($this->_folders);
  120. }
  121. /**
  122. * implements Iterator::rewind()
  123. *
  124. * @return null
  125. */
  126. public function rewind()
  127. {
  128. reset($this->_folders);
  129. }
  130. /**
  131. * get subfolder named $name
  132. *
  133. * @param string $name wanted subfolder
  134. * @return Zend_Mail_Storage_Folder folder named $folder
  135. * @throws Zend_Mail_Storage_Exception
  136. */
  137. public function __get($name)
  138. {
  139. if (!isset($this->_folders[$name])) {
  140. /**
  141. * @see Zend_Mail_Storage_Exception
  142. */
  143. require_once 'Zend/Mail/Storage/Exception.php';
  144. throw new Zend_Mail_Storage_Exception("no subfolder named $name");
  145. }
  146. return $this->_folders[$name];
  147. }
  148. /**
  149. * add or replace subfolder named $name
  150. *
  151. * @param string $name local name of subfolder
  152. * @param Zend_Mail_Storage_Folder $folder instance for new subfolder
  153. * @return null
  154. */
  155. public function __set($name, Zend_Mail_Storage_Folder $folder)
  156. {
  157. $this->_folders[$name] = $folder;
  158. }
  159. /**
  160. * remove subfolder named $name
  161. *
  162. * @param string $name local name of subfolder
  163. * @return null
  164. */
  165. public function __unset($name)
  166. {
  167. unset($this->_folders[$name]);
  168. }
  169. /**
  170. * magic method for easy output of global name
  171. *
  172. * @return string global name of folder
  173. */
  174. public function __toString()
  175. {
  176. return (string)$this->getGlobalName();
  177. }
  178. /**
  179. * get local name
  180. *
  181. * @return string local name
  182. */
  183. public function getLocalName()
  184. {
  185. return $this->_localName;
  186. }
  187. /**
  188. * get global name
  189. *
  190. * @return string global name
  191. */
  192. public function getGlobalName()
  193. {
  194. return $this->_globalName;
  195. }
  196. /**
  197. * is this folder selectable?
  198. *
  199. * @return bool selectable
  200. */
  201. public function isSelectable()
  202. {
  203. return $this->_selectable;
  204. }
  205. /**
  206. * check if folder has no subfolder
  207. *
  208. * @return bool true if no subfolders
  209. */
  210. public function isLeaf()
  211. {
  212. return empty($this->_folders);
  213. }
  214. }