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.
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
23 * Zend_Ldap_Collection wraps a list of LDAP entries.
27 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
28 * @license http://framework.zend.com/license/new-bsd New BSD License
30 class Zend_Ldap_Collection
implements Iterator
, Countable
35 * @var Zend_Ldap_Collection_Iterator_Default
37 protected $_iterator = null;
44 protected $_current = -1;
47 * Container for item caching to speed up multiple iterations
51 protected $_cache = array();
56 * @param Zend_Ldap_Collection_Iterator_Default $iterator
58 public function __construct(Zend_Ldap_Collection_Iterator_Default
$iterator)
60 $this->_iterator
= $iterator;
63 public function __destruct()
69 * Closes the current result set
73 public function close()
75 return $this->_iterator
->close();
79 * Get all entries as an array
83 public function toArray()
86 foreach ($this as $item) {
97 public function getFirst()
99 if ($this->count() > 0) {
101 return $this->current();
108 * Returns the underlying iterator
110 * @return Zend_Ldap_Collection_Iterator_Default
112 public function getInnerIterator()
114 return $this->_iterator
;
118 * Returns the number of items in current result
119 * Implements Countable
123 public function count()
125 return $this->_iterator
->count();
129 * Return the current result item
130 * Implements Iterator
133 * @throws Zend_Ldap_Exception
135 public function current()
137 if ($this->count() > 0) {
138 if ($this->_current
< 0) {
141 if (!array_key_exists($this->_current
, $this->_cache
)) {
142 $current = $this->_iterator
->current();
143 if ($current === null) {
146 $this->_cache
[$this->_current
] = $this->_createEntry($current);
148 return $this->_cache
[$this->_current
];
155 * Creates the data structure for the given entry data
160 protected function _createEntry(array $data)
166 * Return the current result item DN
168 * @return string|null
172 if ($this->count() > 0) {
173 if ($this->_current
< 0) {
176 return $this->_iterator
->key();
183 * Return the current result item key
184 * Implements Iterator
188 public function key()
190 if ($this->count() > 0) {
191 if ($this->_current
< 0) {
194 return $this->_current
;
201 * Move forward to next result item
202 * Implements Iterator
204 * @throws Zend_Ldap_Exception
206 public function next()
208 $this->_iterator
->next();
213 * Rewind the Iterator to the first result item
214 * Implements Iterator
216 * @throws Zend_Ldap_Exception
218 public function rewind()
220 $this->_iterator
->rewind();
225 * Check if there is a current result item
226 * after calls to rewind() or next()
227 * Implements Iterator
231 public function valid()
233 if (isset($this->_cache
[$this->_current
])) {
236 return $this->_iterator
->valid();