Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Ldap / Collection / DefaultIterator.php
blob82b0ca7c37b042e94d1db9a3532970fdd8e1aba6
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Ldap\Collection;
12 use Countable;
13 use Iterator;
14 use Zend\Ldap;
15 use Zend\Ldap\Exception;
16 use Zend\Stdlib\ErrorHandler;
18 /**
19 * Zend\Ldap\Collection\DefaultIterator is the default collection iterator implementation
20 * using ext/ldap
22 class DefaultIterator implements Iterator, Countable
24 const ATTRIBUTE_TO_LOWER = 1;
25 const ATTRIBUTE_TO_UPPER = 2;
26 const ATTRIBUTE_NATIVE = 3;
28 /**
29 * LDAP Connection
31 * @var \Zend\Ldap\Ldap
33 protected $ldap = null;
35 /**
36 * Result identifier resource
38 * @var resource
40 protected $resultId = null;
42 /**
43 * Current result entry identifier
45 * @var resource
47 protected $current = null;
49 /**
50 * Number of items in query result
52 * @var int
54 protected $itemCount = -1;
56 /**
57 * The method that will be applied to the attribute's names.
59 * @var integer|callable
61 protected $attributeNameTreatment = self::ATTRIBUTE_TO_LOWER;
63 /**
64 * Constructor.
66 * @param \Zend\Ldap\Ldap $ldap
67 * @param resource $resultId
68 * @throws \Zend\Ldap\Exception\LdapException if no entries was found.
69 * @return DefaultIterator
71 public function __construct(Ldap\Ldap $ldap, $resultId)
73 $this->ldap = $ldap;
74 $this->resultId = $resultId;
76 $resource = $ldap->getResource();
77 ErrorHandler::start();
78 $this->itemCount = ldap_count_entries($resource, $resultId);
79 ErrorHandler::stop();
80 if ($this->itemCount === false) {
81 throw new Exception\LdapException($this->ldap, 'counting entries');
85 public function __destruct()
87 $this->close();
90 /**
91 * Closes the current result set
93 * @return bool
95 public function close()
97 $isClosed = false;
98 if (is_resource($this->resultId)) {
99 ErrorHandler::start();
100 $isClosed = ldap_free_result($this->resultId);
101 ErrorHandler::stop();
103 $this->resultId = null;
104 $this->current = null;
106 return $isClosed;
110 * Gets the current LDAP connection.
112 * @return \Zend\Ldap\Ldap
114 public function getLDAP()
116 return $this->ldap;
120 * Sets the attribute name treatment.
122 * Can either be one of the following constants
123 * - Zend\Ldap\Collection\DefaultIterator::ATTRIBUTE_TO_LOWER
124 * - Zend\Ldap\Collection\DefaultIterator::ATTRIBUTE_TO_UPPER
125 * - Zend\Ldap\Collection\DefaultIterator::ATTRIBUTE_NATIVE
126 * or a valid callback accepting the attribute's name as it's only
127 * argument and returning the new attribute's name.
129 * @param int|callable $attributeNameTreatment
130 * @return DefaultIterator Provides a fluent interface
132 public function setAttributeNameTreatment($attributeNameTreatment)
134 if (is_callable($attributeNameTreatment)) {
135 if (is_string($attributeNameTreatment) && !function_exists($attributeNameTreatment)) {
136 $this->attributeNameTreatment = self::ATTRIBUTE_TO_LOWER;
137 } elseif (is_array($attributeNameTreatment)
138 && !method_exists($attributeNameTreatment[0], $attributeNameTreatment[1])
140 $this->attributeNameTreatment = self::ATTRIBUTE_TO_LOWER;
141 } else {
142 $this->attributeNameTreatment = $attributeNameTreatment;
144 } else {
145 $attributeNameTreatment = (int) $attributeNameTreatment;
146 switch ($attributeNameTreatment) {
147 case self::ATTRIBUTE_TO_LOWER:
148 case self::ATTRIBUTE_TO_UPPER:
149 case self::ATTRIBUTE_NATIVE:
150 $this->attributeNameTreatment = $attributeNameTreatment;
151 break;
152 default:
153 $this->attributeNameTreatment = self::ATTRIBUTE_TO_LOWER;
154 break;
158 return $this;
162 * Returns the currently set attribute name treatment
164 * @return int|callable
166 public function getAttributeNameTreatment()
168 return $this->attributeNameTreatment;
172 * Returns the number of items in current result
173 * Implements Countable
175 * @return int
177 public function count()
179 return $this->itemCount;
183 * Return the current result item
184 * Implements Iterator
186 * @return array|null
187 * @throws \Zend\Ldap\Exception\LdapException
189 public function current()
191 if (!is_resource($this->current)) {
192 $this->rewind();
194 if (!is_resource($this->current)) {
195 return null;
198 $entry = array('dn' => $this->key());
199 $berIdentifier = null;
201 $resource = $this->ldap->getResource();
202 ErrorHandler::start();
203 $name = ldap_first_attribute(
204 $resource, $this->current,
205 $berIdentifier
207 ErrorHandler::stop();
209 while ($name) {
210 ErrorHandler::start();
211 $data = ldap_get_values_len($resource, $this->current, $name);
212 ErrorHandler::stop();
214 if (!$data) {
215 $data = array();
218 if (isset($data['count'])) {
219 unset($data['count']);
222 switch ($this->attributeNameTreatment) {
223 case self::ATTRIBUTE_TO_LOWER:
224 $attrName = strtolower($name);
225 break;
226 case self::ATTRIBUTE_TO_UPPER:
227 $attrName = strtoupper($name);
228 break;
229 case self::ATTRIBUTE_NATIVE:
230 $attrName = $name;
231 break;
232 default:
233 $attrName = call_user_func($this->attributeNameTreatment, $name);
234 break;
236 $entry[$attrName] = $data;
238 ErrorHandler::start();
239 $name = ldap_next_attribute(
240 $resource, $this->current,
241 $berIdentifier
243 ErrorHandler::stop();
245 ksort($entry, SORT_LOCALE_STRING);
246 return $entry;
250 * Return the result item key
251 * Implements Iterator
253 * @throws \Zend\Ldap\Exception\LdapException
254 * @return string|null
256 public function key()
258 if (!is_resource($this->current)) {
259 $this->rewind();
261 if (is_resource($this->current)) {
262 $resource = $this->ldap->getResource();
263 ErrorHandler::start();
264 $currentDn = ldap_get_dn($resource, $this->current);
265 ErrorHandler::stop();
267 if ($currentDn === false) {
268 throw new Exception\LdapException($this->ldap, 'getting dn');
271 return $currentDn;
272 } else {
273 return null;
278 * Move forward to next result item
279 * Implements Iterator
281 * @throws \Zend\Ldap\Exception\LdapException
283 public function next()
285 $code = 0;
287 if (is_resource($this->current) && $this->itemCount > 0) {
288 $resource = $this->ldap->getResource();
289 ErrorHandler::start();
290 $this->current = ldap_next_entry($resource, $this->current);
291 ErrorHandler::stop();
292 if ($this->current === false) {
293 $msg = $this->ldap->getLastError($code);
294 if ($code === Exception\LdapException::LDAP_SIZELIMIT_EXCEEDED) {
295 // we have reached the size limit enforced by the server
296 return;
297 } elseif ($code > Exception\LdapException::LDAP_SUCCESS) {
298 throw new Exception\LdapException($this->ldap, 'getting next entry (' . $msg . ')');
301 } else {
302 $this->current = false;
307 * Rewind the Iterator to the first result item
308 * Implements Iterator
311 * @throws \Zend\Ldap\Exception\LdapException
313 public function rewind()
315 if (is_resource($this->resultId)) {
316 $resource = $this->ldap->getResource();
317 ErrorHandler::start();
318 $this->current = ldap_first_entry($resource, $this->resultId);
319 ErrorHandler::stop();
320 if ($this->current === false
321 && $this->ldap->getLastErrorCode() > Exception\LdapException::LDAP_SUCCESS
323 throw new Exception\LdapException($this->ldap, 'getting first entry');
329 * Check if there is a current result item
330 * after calls to rewind() or next()
331 * Implements Iterator
333 * @return bool
335 public function valid()
337 return (is_resource($this->current));