File indexing completed on 2024-12-22 05:36:51

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Mail
0017  * @subpackage Storage
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 
0024 /**
0025  * @category   Zend
0026  * @package    Zend_Mail
0027  * @subpackage Storage
0028  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0029  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0030  */
0031 class Zend_Mail_Storage_Folder implements RecursiveIterator
0032 {
0033     /**
0034      * subfolders of folder array(localName => Zend_Mail_Storage_Folder folder)
0035      * @var array
0036      */
0037     protected $_folders;
0038 
0039     /**
0040      * local name (name of folder in parent folder)
0041      * @var string
0042      */
0043     protected $_localName;
0044 
0045     /**
0046      * global name (absolute name of folder)
0047      * @var string
0048      */
0049     protected $_globalName;
0050 
0051     /**
0052      * folder is selectable if folder is able to hold messages, else it's just a parent folder
0053      * @var bool
0054      */
0055     protected $_selectable = true;
0056 
0057     /**
0058      * create a new mail folder instance
0059      *
0060      * @param string $localName  name of folder in current subdirectory
0061      * @param string $globalName absolute name of folder
0062      * @param bool   $selectable if true folder holds messages, if false it's just a parent for subfolders
0063      * @param array  $folders    init with given instances of Zend_Mail_Storage_Folder as subfolders
0064      */
0065     public function __construct($localName, $globalName = '', $selectable = true, array $folders = array())
0066     {
0067         $this->_localName  = $localName;
0068         $this->_globalName = $globalName ? $globalName : $localName;
0069         $this->_selectable = $selectable;
0070         $this->_folders    = $folders;
0071     }
0072 
0073     /**
0074      * implements RecursiveIterator::hasChildren()
0075      *
0076      * @return bool current element has children
0077      */
0078     public function hasChildren()
0079     {
0080         $current = $this->current();
0081         return $current && $current instanceof Zend_Mail_Storage_Folder && !$current->isLeaf();
0082     }
0083 
0084     /**
0085      * implements RecursiveIterator::getChildren()
0086      *
0087      * @return Zend_Mail_Storage_Folder same as self::current()
0088      */
0089     public function getChildren()
0090     {
0091         return $this->current();
0092     }
0093 
0094     /**
0095      * implements Iterator::valid()
0096      *
0097      * @return bool check if there's a current element
0098      */
0099     public function valid()
0100     {
0101         return key($this->_folders) !== null;
0102     }
0103 
0104     /**
0105      * implements Iterator::next()
0106      *
0107      * @return null
0108      */
0109     public function next()
0110     {
0111         next($this->_folders);
0112     }
0113 
0114     /**
0115      * implements Iterator::key()
0116      *
0117      * @return string key/local name of current element
0118      */
0119     public function key()
0120     {
0121         return key($this->_folders);
0122     }
0123 
0124     /**
0125      * implements Iterator::current()
0126      *
0127      * @return Zend_Mail_Storage_Folder current folder
0128      */
0129     public function current()
0130     {
0131         return current($this->_folders);
0132     }
0133 
0134     /**
0135      * implements Iterator::rewind()
0136      *
0137      * @return null
0138      */
0139     public function rewind()
0140     {
0141         reset($this->_folders);
0142     }
0143 
0144     /**
0145      * get subfolder named $name
0146      *
0147      * @param  string $name wanted subfolder
0148      * @return Zend_Mail_Storage_Folder folder named $folder
0149      * @throws Zend_Mail_Storage_Exception
0150      */
0151     public function __get($name)
0152     {
0153         if (!isset($this->_folders[$name])) {
0154             /**
0155              * @see Zend_Mail_Storage_Exception
0156              */
0157             // require_once 'Zend/Mail/Storage/Exception.php';
0158             throw new Zend_Mail_Storage_Exception("no subfolder named $name");
0159         }
0160 
0161         return $this->_folders[$name];
0162     }
0163 
0164     /**
0165      * add or replace subfolder named $name
0166      *
0167      * @param string $name local name of subfolder
0168      * @param Zend_Mail_Storage_Folder $folder instance for new subfolder
0169      * @return null
0170      */
0171     public function __set($name, Zend_Mail_Storage_Folder $folder)
0172     {
0173         $this->_folders[$name] = $folder;
0174     }
0175 
0176     /**
0177      * remove subfolder named $name
0178      *
0179      * @param string $name local name of subfolder
0180      * @return null
0181      */
0182     public function __unset($name)
0183     {
0184         unset($this->_folders[$name]);
0185     }
0186 
0187     /**
0188      * magic method for easy output of global name
0189      *
0190      * @return string global name of folder
0191      */
0192     public function __toString()
0193     {
0194         return (string)$this->getGlobalName();
0195     }
0196 
0197     /**
0198      * get local name
0199      *
0200      * @return string local name
0201      */
0202     public function getLocalName()
0203     {
0204         return $this->_localName;
0205     }
0206 
0207     /**
0208      * get global name
0209      *
0210      * @return string global name
0211      */
0212     public function getGlobalName()
0213     {
0214         return $this->_globalName;
0215     }
0216 
0217     /**
0218      * is this folder selectable?
0219      *
0220      * @return bool selectable
0221      */
0222     public function isSelectable()
0223     {
0224         return $this->_selectable;
0225     }
0226 
0227     /**
0228      * check if folder has no subfolder
0229      *
0230      * @return bool true if no subfolders
0231      */
0232     public function isLeaf()
0233     {
0234         return empty($this->_folders);
0235     }
0236 }