ChildrenIterator.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. * @subpackage Node
  18. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Ldap_Node
  24. */
  25. require_once 'Zend/Ldap/Node.php';
  26. /**
  27. * Zend_Ldap_Node_ChildrenIterator provides an iterator to a collection of children nodes.
  28. *
  29. * @category Zend
  30. * @package Zend_Ldap
  31. * @subpackage Node
  32. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. */
  35. class Zend_Ldap_Node_ChildrenIterator implements Iterator, Countable, RecursiveIterator, ArrayAccess
  36. {
  37. /**
  38. * An array of Zend_Ldap_Node objects
  39. *
  40. * @var array
  41. */
  42. private $_data;
  43. /**
  44. * Constructor.
  45. *
  46. * @param array $data
  47. * @return void
  48. */
  49. public function __construct(array $data)
  50. {
  51. $this->_data = $data;
  52. }
  53. /**
  54. * Returns the number of child nodes.
  55. * Implements Countable
  56. *
  57. * @return int
  58. */
  59. public function count()
  60. {
  61. return count($this->_data);
  62. }
  63. /**
  64. * Return the current child.
  65. * Implements Iterator
  66. *
  67. * @return Zend_Ldap_Node
  68. */
  69. public function current()
  70. {
  71. return current($this->_data);
  72. }
  73. /**
  74. * Return the child'd RDN.
  75. * Implements Iterator
  76. *
  77. * @return string
  78. */
  79. public function key()
  80. {
  81. return key($this->_data);
  82. }
  83. /**
  84. * Move forward to next child.
  85. * Implements Iterator
  86. */
  87. public function next()
  88. {
  89. next($this->_data);
  90. }
  91. /**
  92. * Rewind the Iterator to the first child.
  93. * Implements Iterator
  94. */
  95. public function rewind()
  96. {
  97. reset($this->_data);
  98. }
  99. /**
  100. * Check if there is a current child
  101. * after calls to rewind() or next().
  102. * Implements Iterator
  103. *
  104. * @return boolean
  105. */
  106. public function valid()
  107. {
  108. return (current($this->_data)!==false);
  109. }
  110. /**
  111. * Checks if current node has children.
  112. * Returns whether the current element has children.
  113. *
  114. * @return boolean
  115. */
  116. public function hasChildren()
  117. {
  118. if ($this->current() instanceof Zend_Ldap_Node) {
  119. return $this->current()->hasChildren();
  120. } else {
  121. return false;
  122. }
  123. }
  124. /**
  125. * Returns the children for the current node.
  126. *
  127. * @return Zend_Ldap_Node_ChildrenIterator
  128. */
  129. public function getChildren()
  130. {
  131. if ($this->current() instanceof Zend_Ldap_Node) {
  132. return $this->current()->getChildren();
  133. } else {
  134. return null;
  135. }
  136. }
  137. /**
  138. * Returns a child with a given RDN.
  139. * Implements ArrayAccess.
  140. *
  141. * @param string $rdn
  142. * @return Zend_Ldap_node
  143. */
  144. public function offsetGet($rdn)
  145. {
  146. if ($this->offsetExists($rdn)) {
  147. return $this->_data[$rdn];
  148. } else {
  149. return null;
  150. }
  151. }
  152. /**
  153. * Checks whether a given rdn exists.
  154. * Implements ArrayAccess.
  155. *
  156. * @param string $rdn
  157. * @return boolean
  158. */
  159. public function offsetExists($rdn)
  160. {
  161. return (array_key_exists($rdn, $this->_data));
  162. }
  163. /**
  164. * Does nothing.
  165. * Implements ArrayAccess.
  166. *
  167. * @param string $name
  168. * @return null
  169. */
  170. public function offsetUnset($name) { }
  171. /**
  172. * Does nothing.
  173. * Implements ArrayAccess.
  174. *
  175. * @param string $name
  176. * @param mixed $value
  177. * @return null
  178. */
  179. public function offsetSet($name, $value) { }
  180. /**
  181. * Get all children as an array
  182. *
  183. * @return array
  184. */
  185. public function toArray()
  186. {
  187. $data = array();
  188. foreach ($this as $rdn => $node) {
  189. $data[$rdn] = $node;
  190. }
  191. return $data;
  192. }
  193. }