[ZF-10089] Zend_Log
[zend/radio.git] / library / Zend / Ldap / Collection.php
blob9bf83ec5b7673beb2a8ef07570a28ae2b6d6f3f7
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
15 * @category Zend
16 * @package Zend_Ldap
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
19 * @version $Id$
22 /**
23 * Zend_Ldap_Collection wraps a list of LDAP entries.
25 * @category Zend
26 * @package Zend_Ldap
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
32 /**
33 * Iterator
35 * @var Zend_Ldap_Collection_Iterator_Default
37 protected $_iterator = null;
39 /**
40 * Current item number
42 * @var integer
44 protected $_current = -1;
46 /**
47 * Container for item caching to speed up multiple iterations
49 * @var array
51 protected $_cache = array();
53 /**
54 * Constructor.
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()
65 $this->close();
68 /**
69 * Closes the current result set
71 * @return boolean
73 public function close()
75 return $this->_iterator->close();
78 /**
79 * Get all entries as an array
81 * @return array
83 public function toArray()
85 $data = array();
86 foreach ($this as $item) {
87 $data[] = $item;
89 return $data;
92 /**
93 * Get first entry
95 * @return array
97 public function getFirst()
99 if ($this->count() > 0) {
100 $this->rewind();
101 return $this->current();
102 } else {
103 return null;
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
121 * @return int
123 public function count()
125 return $this->_iterator->count();
129 * Return the current result item
130 * Implements Iterator
132 * @return array|null
133 * @throws Zend_Ldap_Exception
135 public function current()
137 if ($this->count() > 0) {
138 if ($this->_current < 0) {
139 $this->rewind();
141 if (!array_key_exists($this->_current, $this->_cache)) {
142 $current = $this->_iterator->current();
143 if ($current === null) {
144 return null;
146 $this->_cache[$this->_current] = $this->_createEntry($current);
148 return $this->_cache[$this->_current];
149 } else {
150 return null;
155 * Creates the data structure for the given entry data
157 * @param array $data
158 * @return array
160 protected function _createEntry(array $data)
162 return $data;
166 * Return the current result item DN
168 * @return string|null
170 public function dn()
172 if ($this->count() > 0) {
173 if ($this->_current < 0) {
174 $this->rewind();
176 return $this->_iterator->key();
177 } else {
178 return null;
183 * Return the current result item key
184 * Implements Iterator
186 * @return int|null
188 public function key()
190 if ($this->count() > 0) {
191 if ($this->_current < 0) {
192 $this->rewind();
194 return $this->_current;
195 } else {
196 return null;
201 * Move forward to next result item
202 * Implements Iterator
204 * @throws Zend_Ldap_Exception
206 public function next()
208 $this->_iterator->next();
209 $this->_current++;
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();
221 $this->_current = 0;
225 * Check if there is a current result item
226 * after calls to rewind() or next()
227 * Implements Iterator
229 * @return boolean
231 public function valid()
233 if (isset($this->_cache[$this->_current])) {
234 return true;
235 } else {
236 return $this->_iterator->valid();