1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (http://nette.org)
5: *
6: * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
7: *
8: * For the full copyright and license information, please view
9: * the file license.txt that was distributed with this source code.
10: */
11:
12: namespace Nette\Iterators;
13:
14: use Nette;
15:
16:
17: /**
18: * Callback recursive iterator filter.
19: *
20: * @author David Grudl
21: */
22: class RecursiveFilter extends \FilterIterator implements \RecursiveIterator
23: {
24: /** @var callable */
25: private $callback;
26:
27: /** @var callable */
28: private $childrenCallback;
29:
30:
31: public function __construct(\RecursiveIterator $iterator, $callback, $childrenCallback = NULL)
32: {
33: parent::__construct($iterator);
34: $this->callback = $callback === NULL ? NULL : new Nette\Callback($callback);
35: $this->childrenCallback = $childrenCallback === NULL ? NULL : new Nette\Callback($childrenCallback);
36: }
37:
38:
39: public function accept()
40: {
41: return $this->callback === NULL || $this->callback->invoke($this);
42: }
43:
44:
45: public function hasChildren()
46: {
47: return $this->getInnerIterator()->hasChildren()
48: && ($this->childrenCallback === NULL || $this->childrenCallback->invoke($this));
49: }
50:
51:
52: public function getChildren()
53: {
54: return new static($this->getInnerIterator()->getChildren(), $this->callback, $this->childrenCallback);
55: }
56:
57: }
58: