Collection.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  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-2008 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_Interface
  35. */
  36. protected $_iterator = null;
  37. /**
  38. * Current item number
  39. *
  40. * @var integer
  41. */
  42. protected $_currentNumber = -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_Interface $iterator
  53. */
  54. public function __construct(Zend_Ldap_Collection_Iterator_Interface $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. }
  95. else return null;
  96. }
  97. /**
  98. * Returns the number of items in current result
  99. * Implements Countable
  100. *
  101. * @return int
  102. */
  103. public function count()
  104. {
  105. return $this->_iterator->count();
  106. }
  107. /**
  108. * Return the current result item
  109. * Implements Iterator
  110. *
  111. * @return array
  112. * @throws Zend_Ldap_Exception
  113. */
  114. public function current()
  115. {
  116. if (!array_key_exists($this->_currentNumber, $this->_cache))
  117. {
  118. $this->_cache[$this->_currentNumber] =
  119. $this->_createEntry($this->_iterator->current());
  120. }
  121. return $this->_cache[$this->_currentNumber];
  122. }
  123. /**
  124. * Creates the data structure for the given entry data
  125. *
  126. * @param array $data
  127. * @return array
  128. */
  129. protected function _createEntry(array $data)
  130. {
  131. return $data;
  132. }
  133. /**
  134. * Return the result item key
  135. * Implements Iterator
  136. *
  137. * @return int
  138. */
  139. public function key()
  140. {
  141. return $this->_currentNumber;
  142. }
  143. /**
  144. * Move forward to next result item
  145. * Implements Iterator
  146. *
  147. * @throws Zend_Ldap_Exception
  148. */
  149. public function next()
  150. {
  151. $this->_iterator->next();
  152. $this->_currentNumber++;
  153. }
  154. /**
  155. * Rewind the Iterator to the first result item
  156. * Implements Iterator
  157. *
  158. * @throws Zend_Ldap_Exception
  159. */
  160. public function rewind()
  161. {
  162. $this->_iterator->rewind();
  163. $this->_currentNumber = 0;
  164. }
  165. /**
  166. * Check if there is a current result item
  167. * after calls to rewind() or next()
  168. * Implements Iterator
  169. *
  170. * @return boolean
  171. */
  172. public function valid()
  173. {
  174. if (isset($this->_cache[$this->_currentNumber])) {
  175. return true;
  176. } else {
  177. return $this->_iterator->valid();
  178. }
  179. }
  180. }