Merge "Special:BlockList: Update remove/change block links"
[mediawiki.git] / includes / libs / MappedIterator.php
blobbe69f99f7a0ff2a5f26534a522801b3e165a67c7
1 <?php
2 /**
3 * Convenience class for generating iterators from iterators.
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.
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.
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
20 * @file
23 /**
24 * Convenience class for generating iterators from iterators.
26 * @since 1.21
28 class MappedIterator extends FilterIterator {
29 /** @var callable */
30 protected $vCallback;
31 /** @var callable|null */
32 protected $aCallback;
33 /** @var array */
34 protected $cache = [];
36 /** @var bool whether rewind() has been called */
37 protected $rewound = false;
39 /**
40 * Build a 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.
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).
49 * @param Iterator|array $iter
50 * @param callable $vCallback Value transformation callback
51 * @param array $options Options map (includes "accept") (since 1.22)
52 * @phan-param array{accept?:callable} $options
53 * @throws UnexpectedValueException
55 public function __construct( $iter, $vCallback, array $options = [] ) {
56 if ( is_array( $iter ) ) {
57 $baseIterator = new ArrayIterator( $iter );
58 } elseif ( $iter instanceof Iterator ) {
59 $baseIterator = $iter;
60 } else {
61 throw new UnexpectedValueException( "Invalid base iterator provided." );
63 parent::__construct( $baseIterator );
64 $this->vCallback = $vCallback;
65 $this->aCallback = $options['accept'] ?? null;
68 public function next(): void {
69 $this->cache = [];
70 parent::next();
73 public function rewind(): void {
74 $this->rewound = true;
75 $this->cache = [];
76 parent::rewind();
79 public function accept(): bool {
80 $inner = $this->getInnerIterator();
81 '@phan-var Iterator $inner';
82 $value = call_user_func( $this->vCallback, $inner->current() );
83 $ok = ( $this->aCallback ) ? call_user_func( $this->aCallback, $value ) : true;
84 if ( $ok ) {
85 $this->cache['current'] = $value;
88 return $ok;
91 #[\ReturnTypeWillChange]
92 public function key() {
93 $this->init();
95 return parent::key();
98 public function valid(): bool {
99 $this->init();
101 return parent::valid();
104 #[\ReturnTypeWillChange]
105 public function current() {
106 $this->init();
107 if ( parent::valid() ) {
108 return $this->cache['current'];
109 } else {
110 return null; // out of range
115 * Obviate the usual need for rewind() before using a FilterIterator in a manual loop
117 protected function init() {
118 if ( !$this->rewound ) {
119 $this->rewind();