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