PageRenderTime 30ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Ldap/Collection.php

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