1: <?php
2:
3: /**
4: * This file is part of the Nette Framework.
5: *
6: * Copyright (c) 2004, 2010 David Grudl (http://davidgrudl.com)
7: *
8: * This source file is subject to the "Nette license", and/or
9: * GPL license. For more information please see http://nette.org
10: */
11:
12: namespace Nette;
13:
14: use Nette;
15:
16:
17:
18: /**
19: * Callback iterator filter.
20: *
21: * @author David Grudl
22: */
23: class CallbackFilterIterator extends \FilterIterator
24: {
25: /** @var callback */
26: private $callback;
27:
28:
29: /**
30: * Constructs a filter around another iterator.
31: * @param
32: * @param callback
33: */
34: function __construct(\Iterator $iterator, $callback)
35: {
36: parent::__construct($iterator);
37: $this->callback = $callback;
38: }
39:
40:
41:
42: function accept()
43: {
44: return call_user_func($this->callback, $this);
45: }
46:
47: }
48:
49:
50:
51: /**
52: * Callback recursive iterator filter.
53: *
54: * @author David Grudl
55: */
56: class RecursiveCallbackFilterIterator extends \FilterIterator implements \RecursiveIterator
57: {
58: /** @var callback */
59: private $callback;
60:
61: /** @var callback */
62: private $childrenCallback;
63:
64:
65: /**
66: * Constructs a filter around another iterator.
67: * @param
68: * @param callback
69: */
70: function __construct(\RecursiveIterator $iterator, $callback, $childrenCallback = NULL)
71: {
72: parent::__construct($iterator);
73: $this->callback = $callback;
74: $this->childrenCallback = $childrenCallback;
75: }
76:
77:
78:
79: function accept()
80: {
81: return $this->callback === NULL || call_user_func($this->callback, $this);
82: }
83:
84:
85:
86: function hasChildren()
87: {
88: return $this->getInnerIterator()->hasChildren()
89: && ($this->childrenCallback === NULL || call_user_func($this->childrenCallback, $this));
90: }
91:
92:
93:
94: function getChildren()
95: {
96: return new self($this->getInnerIterator()->getChildren(), $this->callback, $this->childrenCallback);
97: }
98:
99: }
100: