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