Annotation of embedaddon/php/ext/spl/examples/callbackfilteriterator.inc, revision 1.1.1.1
1.1 misho 1: <?php
2:
3: /** @file callbackfilteriterator.inc
4: * @ingroup Examples
5: * @brief class CallbackFilterIterator
6: * @author Marcus Boerger
7: * @author Kevin McArthur
8: * @date 2006 - 2006
9: *
10: * SPL - Standard PHP Library
11: */
12:
13: /** @ingroup Examples
14: * @brief A non abstract FiletrIterator that uses a callback foreach element
15: * @author Marcus Boerger
16: * @author Kevin McArthur
17: * @version 1.0
18: */
19: class CallbackFilterIterator extends FilterIterator
20: {
21: const USE_FALSE = 0; /**< mode: accept no elements, no callback */
22: const USE_TRUE = 1; /**< mode: accept all elements, no callback */
23: const USE_VALUE = 2; /**< mode: pass value to callback */
24: const USE_KEY = 3; /**< mode: pass key to callback */
25: const USE_BOTH = 4; /**< mode: pass value and key to callback */
26:
27: const REPLACE = 0x00000001; /**< flag: pass key/value by reference */
28:
29: private $callback; /**< callback to use */
30: private $mode; /**< mode any of USE_VALUE, USE_KEY, USE_BOTH */
31: private $flags; /**< flags (REPLACE) */
32: private $key; /**< key value */
33: private $current; /**< current value */
34:
35: /** Construct a CallbackFilterIterator
36: *
37: * @param it inner iterator (iterator to filter)
38: * @param callback callback function
39: * @param mode any of USE_VALUE, USE_KEY, USE_BOTH
40: * @param flags any of 0, REPLACE
41: */
42: public function __construct(Iterator $it, $callback, $mode = self::USE_VALUE, $flags = 0)
43: {
44: parent::__construct($it);
45: $this->callback = $callback;
46: $this->mode = $mode;
47: $this->flags = $flags;
48: }
49:
50: /** Call the filter callback
51: * @return result of filter callback
52: */
53: public function accept()
54: {
55: $this->key = parent::key();
56: $this->current = parent::current();
57:
58: switch($this->mode) {
59: default:
60: case self::USE_FALSE;
61: return false;
62: case self::USE_TRUE:
63: return true;
64: case self::USE_VALUE:
65: if($this->flags & self::REPLACE) {
66: return (bool) call_user_func($this->callback, &$this->current);
67: } else {
68: return (bool) call_user_func($this->callback, $this->current);
69: }
70: case self::USE_KEY:
71: if($this->flags & self::REPLACE) {
72: return (bool) call_user_func($this->callback, &$this->key);
73: } else {
74: return (bool) call_user_func($this->callback, $this->key);
75: }
76: case SELF::USE_BOTH:
77: if($this->flags & self::REPLACE) {
78: return (bool) call_user_func($this->callback, &$this->key, &$this->current);
79: } else {
80: return (bool) call_user_func($this->callback, $this->key, $this->current);
81: }
82: }
83: }
84:
85: /** @return current key value */
86: function key()
87: {
88: return $this->key;
89: }
90:
91: /** @return current value */
92: function current()
93: {
94: return $this->current;
95: }
96:
97: /** @return operation mode */
98: function getMode()
99: {
100: return $this->mode;
101: }
102:
103: /** @param $mode set new mode, @see mode */
104: function setMode($mode)
105: {
106: $this->mode = $mode;
107: }
108:
109: /** @return operation flags */
110: function getFlags()
111: {
112: return $this->flags;
113: }
114:
115: /** @param $flags set new flags, @see flags */
116: function setFlags($flags)
117: {
118: $this->flags = $flags;
119: }
120: }
121:
122: ?>
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>