Merge "Mime emails should be sent with UTF-8 charset"
[lhc/web/wiklou.git] / includes / MappedIterator.php
1 <?php
2 /**
3 * Convenience class for generating iterators from iterators.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @author Aaron Schulz
22 */
23
24 /**
25 * Convenience class for generating iterators from iterators.
26 *
27 * @since 1.21
28 */
29 class MappedIterator extends FilterIterator {
30 /** @var callable */
31 protected $vCallback;
32 /** @var callable */
33 protected $aCallback;
34 /** @var array */
35 protected $cache = array();
36
37 protected $rewound = false; // boolean; whether rewind() has been called
38
39 /**
40 * Build an new iterator from a base iterator by having the former wrap the
41 * later, returning the result of "value" callback for each current() invocation.
42 * The callback takes the result of current() on the base iterator as an argument.
43 * The keys of the base iterator are reused verbatim.
44 *
45 * An "accept" callback can also be provided which will be called for each value in
46 * the base iterator (post-callback) and will return true if that value should be
47 * included in iteration of the MappedIterator (otherwise it will be filtered out).
48 *
49 * @param Iterator|Array $iter
50 * @param callable $vCallback Value transformation callback
51 * @param array $options Options map (includes "accept") (since 1.22)
52 * @throws MWException
53 */
54 public function __construct( $iter, $vCallback, array $options = array() ) {
55 if ( is_array( $iter ) ) {
56 $baseIterator = new ArrayIterator( $iter );
57 } elseif ( $iter instanceof Iterator ) {
58 $baseIterator = $iter;
59 } else {
60 throw new MWException( "Invalid base iterator provided." );
61 }
62 parent::__construct( $baseIterator );
63 $this->vCallback = $vCallback;
64 $this->aCallback = isset( $options['accept'] ) ? $options['accept'] : null;
65 }
66
67 public function next() {
68 $this->cache = array();
69 parent::next();
70 }
71
72 public function rewind() {
73 $this->rewound = true;
74 $this->cache = array();
75 parent::rewind();
76 }
77
78 public function accept() {
79 $value = call_user_func( $this->vCallback, $this->getInnerIterator()->current() );
80 $ok = ( $this->aCallback ) ? call_user_func( $this->aCallback, $value ) : true;
81 if ( $ok ) {
82 $this->cache['current'] = $value;
83 }
84 return $ok;
85 }
86
87 public function key() {
88 $this->init();
89 return parent::key();
90 }
91
92 public function valid() {
93 $this->init();
94 return parent::valid();
95 }
96
97 public function current() {
98 $this->init();
99 if ( parent::valid() ) {
100 return $this->cache['current'];
101 } else {
102 return null; // out of range
103 }
104 }
105
106 /**
107 * Obviate the usual need for rewind() before using a FilterIterator in a manual loop
108 */
109 protected function init() {
110 if ( !$this->rewound ) {
111 $this->rewind();
112 }
113 }
114 }