Make list=logevents display log entries by anonymous users
[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 implements Iterator {
30 /** @var Iterator */
31 protected $baseIterator;
32 /** @var callable */
33 protected $vCallback;
34
35 /**
36 * Build an new iterator from a base iterator by having the former wrap the
37 * later, returning the result of "value" callback for each current() invocation.
38 * The callback takes the result of current() on the base iterator as an argument.
39 * The keys of the base iterator are reused verbatim.
40 *
41 * @param Iterator|Array $iter
42 * @param callable $vCallback
43 * @throws MWException
44 */
45 public function __construct( $iter, $vCallback ) {
46 if ( is_array( $iter ) ) {
47 $this->baseIterator = new ArrayIterator( $iter );
48 } elseif ( $iter instanceof Iterator ) {
49 $this->baseIterator = $iter;
50 } else {
51 throw new MWException( "Invalid base iterator provided." );
52 }
53 $this->vCallback = $vCallback;
54 }
55
56 /**
57 * @return void
58 */
59 public function rewind() {
60 $this->baseIterator->rewind();
61 }
62
63 /**
64 * @return Mixed|null Returns null if out of range
65 */
66 public function current() {
67 if ( !$this->baseIterator->valid() ) {
68 return null; // out of range
69 }
70 return call_user_func_array( $this->vCallback, array( $this->baseIterator->current() ) );
71 }
72
73 /**
74 * @return Mixed|null Returns null if out of range
75 */
76 public function key() {
77 if ( !$this->baseIterator->valid() ) {
78 return null; // out of range
79 }
80 return $this->baseIterator->key();
81 }
82
83 /**
84 * @return void
85 */
86 public function next() {
87 $this->baseIterator->next();
88 }
89
90 /**
91 * @return bool
92 */
93 public function valid() {
94 return $this->baseIterator->valid();
95 }
96 }