recursivedualiterator.inc 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. /** @file recursivedualiterator.inc
  3. * @ingroup Examples
  4. * @brief class RecursiveDualIterator
  5. * @author Marcus Boerger
  6. * @date 2003 - 2006
  7. *
  8. * SPL - Standard PHP Library
  9. */
  10. /** @ingroup Examples
  11. * @brief Synchronous iteration over two recursive iterators
  12. * @author Marcus Boerger
  13. * @version 1.0
  14. */
  15. class RecursiveDualIterator extends DualIterator implements RecursiveIterator
  16. {
  17. private $ref;
  18. /** construct iterator from two RecursiveIterator instances
  19. *
  20. * @param lhs Left Hand Side Iterator
  21. * @param rhs Right Hand Side Iterator
  22. * @param flags iteration flags
  23. */
  24. function __construct(RecursiveIterator $lhs, RecursiveIterator $rhs,
  25. $flags = 0x33 /*DualIterator::DEFAULT_FLAGS*/)
  26. {
  27. parent::__construct($lhs, $rhs, $flags);
  28. }
  29. /** @return bool whether both LHS and RHS have children
  30. */
  31. function hasChildren(): bool
  32. {
  33. return $this->getLHS()->hasChildren() && $this->getRHS()->hasChildren();
  34. }
  35. /** @return RecursiveDualIterator (late binding) for the two inner
  36. * iterators current children.
  37. */
  38. function getChildren(): RecursiveDualIterator
  39. {
  40. if (empty($this->ref))
  41. {
  42. $this->ref = new ReflectionClass($this);
  43. }
  44. return $this->ref->newInstance(
  45. $this->getLHS()->getChildren(), $this->getRHS()->getChildren(), $this->getFlags());
  46. }
  47. /** @return bool whether both inner iterators are valid, have same hasChildren()
  48. * state and identical current and key values or both are non valid.
  49. */
  50. function areIdentical(): bool
  51. {
  52. return $this->getLHS()->hasChildren() === $this->getRHS()->hasChildren()
  53. && parent::areIdentical();
  54. }
  55. /** @return bool whether both inner iterators are valid, have same hasChildren()
  56. * state and equal current and key values or both are invalid.
  57. */
  58. function areEqual(): bool
  59. {
  60. return $this->getLHS()->hasChildren() === $this->getRHS()->hasChildren()
  61. && parent::areEqual();
  62. }
  63. }
  64. ?>