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
24 * Convenience class for generating iterators from iterators.
28 class MappedIterator
extends FilterIterator
{
31 /** @var callable|null */
34 protected $cache = [];
36 /** @var bool whether rewind() has been called */
37 protected $rewound = false;
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;
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
{
73 public function rewind(): void
{
74 $this->rewound
= true;
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;
85 $this->cache
['current'] = $value;
91 #[\ReturnTypeWillChange]
92 public function key() {
98 public function valid(): bool {
101 return parent
::valid();
104 #[\ReturnTypeWillChange]
105 public function current() {
107 if ( parent
::valid() ) {
108 return $this->cache
['current'];
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
) {