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