File indexing completed on 2024-12-22 05:36:48
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_Ldap 0017 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0018 * @license http://framework.zend.com/license/new-bsd New BSD License 0019 * @version $Id$ 0020 */ 0021 0022 /** 0023 * Zend_Ldap_Collection wraps a list of LDAP entries. 0024 * 0025 * @category Zend 0026 * @package Zend_Ldap 0027 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0028 * @license http://framework.zend.com/license/new-bsd New BSD License 0029 */ 0030 class Zend_Ldap_Collection implements Iterator, Countable 0031 { 0032 /** 0033 * Iterator 0034 * 0035 * @var Zend_Ldap_Collection_Iterator_Default 0036 */ 0037 protected $_iterator = null; 0038 0039 /** 0040 * Current item number 0041 * 0042 * @var integer 0043 */ 0044 protected $_current = -1; 0045 0046 /** 0047 * Container for item caching to speed up multiple iterations 0048 * 0049 * @var array 0050 */ 0051 protected $_cache = array(); 0052 0053 /** 0054 * Constructor. 0055 * 0056 * @param Zend_Ldap_Collection_Iterator_Default $iterator 0057 */ 0058 public function __construct(Zend_Ldap_Collection_Iterator_Default $iterator) 0059 { 0060 $this->_iterator = $iterator; 0061 } 0062 0063 public function __destruct() 0064 { 0065 $this->close(); 0066 } 0067 0068 /** 0069 * Closes the current result set 0070 * 0071 * @return boolean 0072 */ 0073 public function close() 0074 { 0075 return $this->_iterator->close(); 0076 } 0077 0078 /** 0079 * Get all entries as an array 0080 * 0081 * @return array 0082 */ 0083 public function toArray() 0084 { 0085 $data = array(); 0086 foreach ($this as $item) { 0087 $data[] = $item; 0088 } 0089 return $data; 0090 } 0091 0092 /** 0093 * Get first entry 0094 * 0095 * @return array 0096 */ 0097 public function getFirst() 0098 { 0099 if ($this->count() > 0) { 0100 $this->rewind(); 0101 return $this->current(); 0102 } else { 0103 return null; 0104 } 0105 } 0106 0107 /** 0108 * Returns the underlying iterator 0109 * 0110 * @return Zend_Ldap_Collection_Iterator_Default 0111 */ 0112 public function getInnerIterator() 0113 { 0114 return $this->_iterator; 0115 } 0116 0117 /** 0118 * Returns the number of items in current result 0119 * Implements Countable 0120 * 0121 * @return int 0122 */ 0123 public function count() 0124 { 0125 return $this->_iterator->count(); 0126 } 0127 0128 /** 0129 * Return the current result item 0130 * Implements Iterator 0131 * 0132 * @return array|null 0133 * @throws Zend_Ldap_Exception 0134 */ 0135 public function current() 0136 { 0137 if ($this->count() > 0) { 0138 if ($this->_current < 0) { 0139 $this->rewind(); 0140 } 0141 if (!array_key_exists($this->_current, $this->_cache)) { 0142 $current = $this->_iterator->current(); 0143 if ($current === null) { 0144 return null; 0145 } 0146 $this->_cache[$this->_current] = $this->_createEntry($current); 0147 } 0148 return $this->_cache[$this->_current]; 0149 } else { 0150 return null; 0151 } 0152 } 0153 0154 /** 0155 * Creates the data structure for the given entry data 0156 * 0157 * @param array $data 0158 * @return array 0159 */ 0160 protected function _createEntry(array $data) 0161 { 0162 return $data; 0163 } 0164 0165 /** 0166 * Return the current result item DN 0167 * 0168 * @return string|null 0169 */ 0170 public function dn() 0171 { 0172 if ($this->count() > 0) { 0173 if ($this->_current < 0) { 0174 $this->rewind(); 0175 } 0176 return $this->_iterator->key(); 0177 } else { 0178 return null; 0179 } 0180 } 0181 0182 /** 0183 * Return the current result item key 0184 * Implements Iterator 0185 * 0186 * @return int|null 0187 */ 0188 public function key() 0189 { 0190 if ($this->count() > 0) { 0191 if ($this->_current < 0) { 0192 $this->rewind(); 0193 } 0194 return $this->_current; 0195 } else { 0196 return null; 0197 } 0198 } 0199 0200 /** 0201 * Move forward to next result item 0202 * Implements Iterator 0203 * 0204 * @throws Zend_Ldap_Exception 0205 */ 0206 public function next() 0207 { 0208 $this->_iterator->next(); 0209 $this->_current++; 0210 } 0211 0212 /** 0213 * Rewind the Iterator to the first result item 0214 * Implements Iterator 0215 * 0216 * @throws Zend_Ldap_Exception 0217 */ 0218 public function rewind() 0219 { 0220 $this->_iterator->rewind(); 0221 $this->_current = 0; 0222 } 0223 0224 /** 0225 * Check if there is a current result item 0226 * after calls to rewind() or next() 0227 * Implements Iterator 0228 * 0229 * @return boolean 0230 */ 0231 public function valid() 0232 { 0233 if (isset($this->_cache[$this->_current])) { 0234 return true; 0235 } else { 0236 return $this->_iterator->valid(); 0237 } 0238 } 0239 }