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: /**
19: * Callback recursive iterator filter.
20: *
21: * @author David Grudl
22: */
23: class RecursiveFilter extends \FilterIterator implements \RecursiveIterator
24: {
25: /** @var callable */
26: private $callback;
27:
28: /** @var callable */
29: private $childrenCallback;
30:
31:
32: public function __construct(\RecursiveIterator $iterator, $callback, $childrenCallback = NULL)
33: {
34: parent::__construct($iterator);
35: $this->callback = $callback === NULL ? NULL : new Nette\Callback($callback);
36: $this->childrenCallback = $childrenCallback === NULL ? NULL : new Nette\Callback($childrenCallback);
37: }
38:
39:
40:
41: public function accept()
42: {
43: return $this->callback === NULL || $this->callback->invoke($this);
44: }
45:
46:
47:
48: public function hasChildren()
49: {
50: return $this->getInnerIterator()->hasChildren()
51: && ($this->childrenCallback === NULL || $this->childrenCallback->invoke($this));
52: }
53:
54:
55:
56: public function getChildren()
57: {
58: return new static($this->getInnerIterator()->getChildren(), $this->callback, $this->childrenCallback);
59: }
60:
61: }
62: