Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
MappedIterator
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 8
210
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
 next
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 rewind
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 accept
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 key
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 valid
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 current
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 init
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2/**
3 * Convenience class for generating iterators from iterators.
4 *
5 * @license GPL-2.0-or-later
6 * @file
7 */
8
9namespace Wikimedia\Iterators;
10
11use ArrayIterator;
12use FilterIterator;
13use Iterator;
14use UnexpectedValueException;
15
16/**
17 * Convenience class for generating iterators from iterators.
18 *
19 * @since 1.21
20 */
21class MappedIterator extends FilterIterator {
22    /** @var callable */
23    protected $vCallback;
24    /** @var callable|null */
25    protected $aCallback;
26    /** @var array */
27    protected $cache = [];
28
29    /** @var bool whether rewind() has been called */
30    protected $rewound = false;
31
32    /**
33     * Build a new iterator from a base iterator by having the former wrap the
34     * later, returning the result of "value" callback for each current() invocation.
35     * The callback takes the result of current() on the base iterator as an argument.
36     * The keys of the base iterator are reused verbatim.
37     *
38     * An "accept" callback can also be provided which will be called for each value in
39     * the base iterator (post-callback) and will return true if that value should be
40     * included in iteration of the MappedIterator (otherwise it will be filtered out).
41     *
42     * @param Iterator|array $iter
43     * @param callable $vCallback Value transformation callback
44     * @param array $options Options map (includes "accept") (since 1.22)
45     * @phan-param array{accept?:callable} $options
46     * @throws UnexpectedValueException
47     */
48    public function __construct( $iter, $vCallback, array $options = [] ) {
49        if ( is_array( $iter ) ) {
50            $baseIterator = new ArrayIterator( $iter );
51        } elseif ( $iter instanceof Iterator ) {
52            $baseIterator = $iter;
53        } else {
54            throw new UnexpectedValueException( "Invalid base iterator provided." );
55        }
56        parent::__construct( $baseIterator );
57        $this->vCallback = $vCallback;
58        $this->aCallback = $options['accept'] ?? null;
59    }
60
61    public function next(): void {
62        $this->cache = [];
63        parent::next();
64    }
65
66    public function rewind(): void {
67        $this->rewound = true;
68        $this->cache = [];
69        parent::rewind();
70    }
71
72    public function accept(): bool {
73        $inner = $this->getInnerIterator();
74        '@phan-var Iterator $inner';
75        $value = ( $this->vCallback )( $inner->current() );
76        $ok = ( $this->aCallback ) ? ( $this->aCallback )( $value ) : true;
77        if ( $ok ) {
78            $this->cache['current'] = $value;
79        }
80
81        return $ok;
82    }
83
84    /** @inheritDoc */
85    #[\ReturnTypeWillChange]
86    public function key() {
87        $this->init();
88
89        return parent::key();
90    }
91
92    public function valid(): bool {
93        $this->init();
94
95        return parent::valid();
96    }
97
98    /** @inheritDoc */
99    #[\ReturnTypeWillChange]
100    public function current() {
101        $this->init();
102        if ( parent::valid() ) {
103            return $this->cache['current'];
104        } else {
105            return null; // out of range
106        }
107    }
108
109    /**
110     * Obviate the usual need for rewind() before using a FilterIterator in a manual loop
111     */
112    protected function init() {
113        if ( !$this->rewound ) {
114            $this->rewind();
115        }
116    }
117}
118
119/** @deprecated class alias since 1.47 */
120class_alias( MappedIterator::class, 'MappedIterator' );