1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (https://nette.org)
5: * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
6: */
7:
8: namespace Nette\Iterators;
9:
10: use Nette;
11:
12:
13: /**
14: * Generic recursive iterator.
15: *
16: * @author David Grudl
17: */
18: class Recursor extends \IteratorIterator implements \RecursiveIterator, \Countable
19: {
20:
21: /**
22: * Has the current element has children?
23: * @return bool
24: */
25: public function hasChildren()
26: {
27: $obj = $this->current();
28: return ($obj instanceof \IteratorAggregate && $obj->getIterator() instanceof \RecursiveIterator)
29: || $obj instanceof \RecursiveIterator;
30: }
31:
32:
33: /**
34: * The sub-iterator for the current element.
35: * @return \RecursiveIterator
36: */
37: public function getChildren()
38: {
39: $obj = $this->current();
40: return $obj instanceof \IteratorAggregate ? $obj->getIterator() : $obj;
41: }
42:
43:
44: /**
45: * Returns the count of elements.
46: * @return int
47: */
48: public function count()
49: {
50: return iterator_count($this);
51: }
52:
53: }
54: