summaryrefslogtreecommitdiff
path: root/ext/spl/examples/callbackfilteriterator.inc
blob: 51757012ec85e1c9ab46dec09b19e0f3cccd2b7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php

/** @file callbackfilteriterator.inc
 * @ingroup Examples
 * @brief class CallbackFilterIterator
 * @author  Marcus Boerger
 * @author  Kevin McArthur
 * @date    2006 - 2006
 *
 * SPL - Standard PHP Library
 */

/** @ingroup Examples
 * @brief   A non abstract FiletrIterator that uses a callback foreach element
 * @author  Marcus Boerger
 * @author  Kevin McArthur
 * @version 1.0
 */
class CallbackFilterIterator extends FilterIterator
{
	const USE_FALSE = 0;  /**< mode: accept no elements, no callback */
	const USE_TRUE  = 1;  /**< mode: accept all elements, no callback */
	const USE_VALUE = 2;  /**< mode: pass value to callback */
	const USE_KEY   = 3;  /**< mode: pass key to callback */
	const USE_BOTH  = 4;  /**< mode: pass value and key to callback */

	const REPLACE   = 0x00000001; /**< flag: pass key/value by reference */

	private $callback; /**< callback to use */
	private $mode;     /**< mode any of USE_VALUE, USE_KEY, USE_BOTH */
	private $flags;    /**< flags (REPLACE) */
	private $key;      /**< key value */
	private $current;  /**< current value */

	/** Construct a CallbackFilterIterator
	 *
	 * @param it        inner iterator (iterator to filter)
	 * @param callback  callback function
	 * @param mode      any of USE_VALUE, USE_KEY, USE_BOTH
	 * @param flags     any of 0, REPLACE
	 */
	public function __construct(Iterator $it, $callback, $mode = self::USE_VALUE, $flags = 0)
	{
		parent::__construct($it);
		$this->callback = $callback;
		$this->mode     = $mode;
		$this->flags    = $flags;
	}

	/** Call the filter callback
	 * @return result of filter callback
	 */
	public function accept()
	{
		$this->key     = parent::key();
		$this->current = parent::current();

		switch($this->mode) {
		default:
		case self::USE_FALSE;
			return false;
		case self::USE_TRUE:
			return true;
		case self::USE_VALUE:
			if($this->flags & self::REPLACE) {
				return (bool) call_user_func($this->callback, &$this->current);
			} else {
				return (bool) call_user_func($this->callback, $this->current);
			}
		case self::USE_KEY:
			if($this->flags & self::REPLACE) {
				return (bool) call_user_func($this->callback, &$this->key);
			} else {
				return (bool) call_user_func($this->callback, $this->key);
			}
		case SELF::USE_BOTH:
			if($this->flags & self::REPLACE) {
				return (bool) call_user_func($this->callback, &$this->key, &$this->current);
			} else {
				return (bool) call_user_func($this->callback, $this->key, $this->current);
			}
		}
	}

	/** @return current key value */
	function key()
	{
		return $this->key;
	}

	/** @return current value */
	function current()
	{
		return $this->current;
	}

	/** @return operation mode */
	function getMode()
	{
		return $this->mode;
	}

	/** @param $mode set new mode, @see mode */
	function setMode($mode)
	{
		$this->mode = $mode;
	}

	/** @return operation flags */
	function getFlags()
	{
		return $this->flags;
	}

	/** @param $flags set new flags, @see flags */
	function setFlags($flags)
	{
		$this->flags = $flags;
	}
}

?>