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_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: Collection.php 20096 2010-01-06 02:05:09Z bkarwin $ |
---|
20 | */ |
---|
21 | |
---|
22 | /** |
---|
23 | * Zend_Ldap_Collection wraps a list of LDAP entries. |
---|
24 | * |
---|
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 |
---|
29 | */ |
---|
30 | class Zend_Ldap_Collection implements Iterator, Countable |
---|
31 | { |
---|
32 | /** |
---|
33 | * Iterator |
---|
34 | * |
---|
35 | * @var Zend_Ldap_Collection_Iterator_Default |
---|
36 | */ |
---|
37 | protected $_iterator = null; |
---|
38 | |
---|
39 | /** |
---|
40 | * Current item number |
---|
41 | * |
---|
42 | * @var integer |
---|
43 | */ |
---|
44 | protected $_current = -1; |
---|
45 | |
---|
46 | /** |
---|
47 | * Container for item caching to speed up multiple iterations |
---|
48 | * |
---|
49 | * @var array |
---|
50 | */ |
---|
51 | protected $_cache = array(); |
---|
52 | |
---|
53 | /** |
---|
54 | * Constructor. |
---|
55 | * |
---|
56 | * @param Zend_Ldap_Collection_Iterator_Default $iterator |
---|
57 | */ |
---|
58 | public function __construct(Zend_Ldap_Collection_Iterator_Default $iterator) |
---|
59 | { |
---|
60 | $this->_iterator = $iterator; |
---|
61 | } |
---|
62 | |
---|
63 | public function __destruct() |
---|
64 | { |
---|
65 | $this->close(); |
---|
66 | } |
---|
67 | |
---|
68 | /** |
---|
69 | * Closes the current result set |
---|
70 | * |
---|
71 | * @return boolean |
---|
72 | */ |
---|
73 | public function close() |
---|
74 | { |
---|
75 | return $this->_iterator->close(); |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | * Get all entries as an array |
---|
80 | * |
---|
81 | * @return array |
---|
82 | */ |
---|
83 | public function toArray() |
---|
84 | { |
---|
85 | $data = array(); |
---|
86 | foreach ($this as $item) { |
---|
87 | $data[] = $item; |
---|
88 | } |
---|
89 | return $data; |
---|
90 | } |
---|
91 | |
---|
92 | /** |
---|
93 | * Get first entry |
---|
94 | * |
---|
95 | * @return array |
---|
96 | */ |
---|
97 | public function getFirst() |
---|
98 | { |
---|
99 | if ($this->count() > 0) { |
---|
100 | $this->rewind(); |
---|
101 | return $this->current(); |
---|
102 | } else { |
---|
103 | return null; |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | /** |
---|
108 | * Returns the underlying iterator |
---|
109 | * |
---|
110 | * @return Zend_Ldap_Collection_Iterator_Default |
---|
111 | */ |
---|
112 | public function getInnerIterator() |
---|
113 | { |
---|
114 | return $this->_iterator; |
---|
115 | } |
---|
116 | |
---|
117 | /** |
---|
118 | * Returns the number of items in current result |
---|
119 | * Implements Countable |
---|
120 | * |
---|
121 | * @return int |
---|
122 | */ |
---|
123 | public function count() |
---|
124 | { |
---|
125 | return $this->_iterator->count(); |
---|
126 | } |
---|
127 | |
---|
128 | /** |
---|
129 | * Return the current result item |
---|
130 | * Implements Iterator |
---|
131 | * |
---|
132 | * @return array|null |
---|
133 | * @throws Zend_Ldap_Exception |
---|
134 | */ |
---|
135 | public function current() |
---|
136 | { |
---|
137 | if ($this->count() > 0) { |
---|
138 | if ($this->_current < 0) { |
---|
139 | $this->rewind(); |
---|
140 | } |
---|
141 | if (!array_key_exists($this->_current, $this->_cache)) { |
---|
142 | $current = $this->_iterator->current(); |
---|
143 | if ($current === null) { |
---|
144 | return null; |
---|
145 | } |
---|
146 | $this->_cache[$this->_current] = $this->_createEntry($current); |
---|
147 | } |
---|
148 | return $this->_cache[$this->_current]; |
---|
149 | } else { |
---|
150 | return null; |
---|
151 | } |
---|
152 | } |
---|
153 | |
---|
154 | /** |
---|
155 | * Creates the data structure for the given entry data |
---|
156 | * |
---|
157 | * @param array $data |
---|
158 | * @return array |
---|
159 | */ |
---|
160 | protected function _createEntry(array $data) |
---|
161 | { |
---|
162 | return $data; |
---|
163 | } |
---|
164 | |
---|
165 | /** |
---|
166 | * Return the current result item DN |
---|
167 | * |
---|
168 | * @return string|null |
---|
169 | */ |
---|
170 | public function dn() |
---|
171 | { |
---|
172 | if ($this->count() > 0) { |
---|
173 | if ($this->_current < 0) { |
---|
174 | $this->rewind(); |
---|
175 | } |
---|
176 | return $this->_iterator->key(); |
---|
177 | } else { |
---|
178 | return null; |
---|
179 | } |
---|
180 | } |
---|
181 | |
---|
182 | /** |
---|
183 | * Return the current result item key |
---|
184 | * Implements Iterator |
---|
185 | * |
---|
186 | * @return int|null |
---|
187 | */ |
---|
188 | public function key() |
---|
189 | { |
---|
190 | if ($this->count() > 0) { |
---|
191 | if ($this->_current < 0) { |
---|
192 | $this->rewind(); |
---|
193 | } |
---|
194 | return $this->_current; |
---|
195 | } else { |
---|
196 | return null; |
---|
197 | } |
---|
198 | } |
---|
199 | |
---|
200 | /** |
---|
201 | * Move forward to next result item |
---|
202 | * Implements Iterator |
---|
203 | * |
---|
204 | * @throws Zend_Ldap_Exception |
---|
205 | */ |
---|
206 | public function next() |
---|
207 | { |
---|
208 | $this->_iterator->next(); |
---|
209 | $this->_current++; |
---|
210 | } |
---|
211 | |
---|
212 | /** |
---|
213 | * Rewind the Iterator to the first result item |
---|
214 | * Implements Iterator |
---|
215 | * |
---|
216 | * @throws Zend_Ldap_Exception |
---|
217 | */ |
---|
218 | public function rewind() |
---|
219 | { |
---|
220 | $this->_iterator->rewind(); |
---|
221 | $this->_current = 0; |
---|
222 | } |
---|
223 | |
---|
224 | /** |
---|
225 | * Check if there is a current result item |
---|
226 | * after calls to rewind() or next() |
---|
227 | * Implements Iterator |
---|
228 | * |
---|
229 | * @return boolean |
---|
230 | */ |
---|
231 | public function valid() |
---|
232 | { |
---|
233 | if (isset($this->_cache[$this->_current])) { |
---|
234 | return true; |
---|
235 | } else { |
---|
236 | return $this->_iterator->valid(); |
---|
237 | } |
---|
238 | } |
---|
239 | } |
---|