recursivearrayiterator.inc 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. /** @file recursivearrayiterator.inc
  3. * @ingroup Examples
  4. * @brief class RecursiveArrayIterator
  5. * @author Marcus Boerger
  6. * @date 2003 - 2009
  7. *
  8. * SPL - Standard PHP Library
  9. */
  10. /** @ingroup SPL
  11. * @brief A recursive array iterator
  12. * @author Marcus Boerger
  13. * @version 1.0
  14. * @since PHP 5.1
  15. *
  16. * Passes the RecursiveIterator interface to the inner Iterator and provides
  17. * the same functionality as FilterIterator. This allows you to skip parents
  18. * and all their childs before loading them all. You need to care about
  19. * function getChildren() because it may not always suit your needs. The
  20. * builtin behavior uses reflection to return a new instance of the exact same
  21. * class it is called from. That is you extend RecursiveFilterIterator and
  22. * getChildren() will create instance of that class. The problem is that doing
  23. * this does not transport any state or control information of your accept()
  24. * implementation to the new instance. To overcome this problem you might
  25. * need to overwrite getChildren(), call this implementation and pass the
  26. * control vaules manually.
  27. */
  28. class RecursiveArrayIterator extends ArrayIterator implements RecursiveIterator
  29. {
  30. /** @return whether the current element has children
  31. */
  32. function hasChildren()
  33. {
  34. return is_array($this->current());
  35. }
  36. /** @return an iterator for the current elements children
  37. *
  38. * @note the returned iterator will be of the same class as $this
  39. */
  40. function getChildren()
  41. {
  42. if ($this->current() instanceof self)
  43. {
  44. return $this->current();
  45. }
  46. if (empty($this->ref))
  47. {
  48. $this->ref = new ReflectionClass($this);
  49. }
  50. return $this->ref->newInstance($this->current());
  51. }
  52. private $ref;
  53. }
  54. ?>