Collection.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 $ldap
  53. * @param Zend_Ldap_Collection_Iterator_Interface $iterator
  54. * @return void
  55. */
  56. public function __construct(Zend_Ldap_Collection_Iterator_Interface $iterator)
  57. {
  58. $this->_iterator = $iterator;
  59. }
  60. public function __destruct()
  61. {
  62. $this->close();
  63. }
  64. /**
  65. * Closes the current result set
  66. *
  67. * @return boolean
  68. */
  69. public function close()
  70. {
  71. return $this->_iterator->close();
  72. }
  73. /**
  74. * Get all entries as an array
  75. *
  76. * @return array
  77. */
  78. public function toArray()
  79. {
  80. $data = array();
  81. foreach ($this as $item) {
  82. $data[] = $item;
  83. }
  84. return $data;
  85. }
  86. /**
  87. * Get first entry
  88. *
  89. * @return array
  90. */
  91. public function getFirst()
  92. {
  93. if ($this->count()>0) {
  94. $this->rewind();
  95. return $this->current();
  96. }
  97. else return null;
  98. }
  99. /**
  100. * Returns the number of items in current result
  101. * Implements Countable
  102. *
  103. * @return int
  104. */
  105. public function count()
  106. {
  107. return $this->_iterator->count();
  108. }
  109. /**
  110. * Return the current result item
  111. * Implements Iterator
  112. *
  113. * @return array
  114. * @throws Zend_Ldap_Exception
  115. */
  116. public function current()
  117. {
  118. if (!array_key_exists($this->_currentNumber, $this->_cache))
  119. {
  120. $this->_cache[$this->_currentNumber] =
  121. $this->_createEntry($this->_iterator->current());
  122. }
  123. return $this->_cache[$this->_currentNumber];
  124. }
  125. /**
  126. * Creates the data structure for the given entry data
  127. *
  128. * @param array $data
  129. * @return array
  130. */
  131. protected function _createEntry(array $data)
  132. {
  133. return $data;
  134. }
  135. /**
  136. * Return the result item key
  137. * Implements Iterator
  138. *
  139. * @return int
  140. */
  141. public function key()
  142. {
  143. return $this->_currentNumber;
  144. }
  145. /**
  146. * Move forward to next result item
  147. * Implements Iterator
  148. *
  149. * @throws Zend_Ldap_Exception
  150. */
  151. public function next()
  152. {
  153. $this->_iterator->next();
  154. $this->_currentNumber++;
  155. }
  156. /**
  157. * Rewind the Iterator to the first result item
  158. * Implements Iterator
  159. *
  160. * @throws Zend_Ldap_Exception
  161. */
  162. public function rewind()
  163. {
  164. $this->_iterator->rewind();
  165. $this->_currentNumber = 0;
  166. }
  167. /**
  168. * Check if there is a current result item
  169. * after calls to rewind() or next()
  170. * Implements Iterator
  171. *
  172. * @return boolean
  173. */
  174. public function valid()
  175. {
  176. if (isset($this->_cache[$this->_currentNumber])) {
  177. return true;
  178. } else {
  179. return $this->_iterator->valid();
  180. }
  181. }
  182. }