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