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