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: * @package Nette
11: */
12:
13:
14:
15: /**
16: * Callback iterator filter.
17: *
18: * @author David Grudl
19: */
20: class CallbackFilterIterator extends FilterIterator
21: {
22: /** @var callback */
23: private $callback;
24:
25:
26: /**
27: * Constructs a filter around another iterator.
28: * @param
29: * @param callback
30: */
31: function __construct(Iterator $iterator, $callback)
32: {
33: parent::__construct($iterator);
34: $this->callback = $callback;
35: }
36:
37:
38:
39: function accept()
40: {
41: return call_user_func($this->callback, $this);
42: }
43:
44: }
45:
46:
47:
48: /**
49: * Callback recursive iterator filter.
50: *
51: * @author David Grudl
52: */
53: class RecursiveCallbackFilterIterator extends FilterIterator implements RecursiveIterator
54: {
55: /** @var callback */
56: private $callback;
57:
58: /** @var callback */
59: private $childrenCallback;
60:
61:
62: /**
63: * Constructs a filter around another iterator.
64: * @param
65: * @param callback
66: */
67: function __construct(RecursiveIterator $iterator, $callback, $childrenCallback = NULL)
68: {
69: parent::__construct($iterator);
70: $this->callback = $callback;
71: $this->childrenCallback = $childrenCallback;
72: }
73:
74:
75:
76: function accept()
77: {
78: return $this->callback === NULL || call_user_func($this->callback, $this);
79: }
80:
81:
82:
83: function hasChildren()
84: {
85: return $this->getInnerIterator()->hasChildren()
86: && ($this->childrenCallback === NULL || call_user_func($this->childrenCallback, $this));
87: }
88:
89:
90:
91: function getChildren()
92: {
93: return new self($this->getInnerIterator()->getChildren(), $this->callback, $this->childrenCallback);
94: }
95:
96: }
97: