Merge "MimeAnalyzer: Add testcases for mp3 detection"
[lhc/web/wiklou.git] / includes / libs / MappedIterator.php
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 */
28 class MappedIterator extends FilterIterator {
29 /** @var callable */
30 protected $vCallback;
31 /** @var callable */
32 protected $aCallback;
33 /** @var array */
34 protected $cache = [];
35
36 protected $rewound = false; // boolean; whether rewind() has been called
37
38 /**
39 * Build an 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 * @throws UnexpectedValueException
52 */
53 public function __construct( $iter, $vCallback, array $options = [] ) {
54 if ( is_array( $iter ) ) {
55 $baseIterator = new ArrayIterator( $iter );
56 } elseif ( $iter instanceof Iterator ) {
57 $baseIterator = $iter;
58 } else {
59 throw new UnexpectedValueException( "Invalid base iterator provided." );
60 }
61 parent::__construct( $baseIterator );
62 $this->vCallback = $vCallback;
63 $this->aCallback = isset( $options['accept'] ) ? $options['accept'] : null;
64 }
65
66 public function next() {
67 $this->cache = [];
68 parent::next();
69 }
70
71 public function rewind() {
72 $this->rewound = true;
73 $this->cache = [];
74 parent::rewind();
75 }
76
77 public function accept() {
78 $value = call_user_func( $this->vCallback, $this->getInnerIterator()->current() );
79 $ok = ( $this->aCallback ) ? call_user_func( $this->aCallback, $value ) : true;
80 if ( $ok ) {
81 $this->cache['current'] = $value;
82 }
83
84 return $ok;
85 }
86
87 public function key() {
88 $this->init();
89
90 return parent::key();
91 }
92
93 public function valid() {
94 $this->init();
95
96 return parent::valid();
97 }
98
99 public function current() {
100 $this->init();
101 if ( parent::valid() ) {
102 return $this->cache['current'];
103 } else {
104 return null; // out of range
105 }
106 }
107
108 /**
109 * Obviate the usual need for rewind() before using a FilterIterator in a manual loop
110 */
111 protected function init() {
112 if ( !$this->rewound ) {
113 $this->rewind();
114 }
115 }
116 }