| 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|null */ |
| 32 | protected $aCallback; |
| 33 | /** @var array */ |
| 34 | protected $cache = []; |
| 35 | |
| 36 | /** @var bool whether rewind() has been called */ |
| 37 | protected $rewound = false; |
| 38 | |
| 39 | /** |
| 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. |
| 44 | * |
| 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). |
| 48 | * |
| 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 |
| 54 | */ |
| 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; |
| 60 | } else { |
| 61 | throw new UnexpectedValueException( "Invalid base iterator provided." ); |
| 62 | } |
| 63 | parent::__construct( $baseIterator ); |
| 64 | $this->vCallback = $vCallback; |
| 65 | $this->aCallback = $options['accept'] ?? null; |
| 66 | } |
| 67 | |
| 68 | public function next(): void { |
| 69 | $this->cache = []; |
| 70 | parent::next(); |
| 71 | } |
| 72 | |
| 73 | public function rewind(): void { |
| 74 | $this->rewound = true; |
| 75 | $this->cache = []; |
| 76 | parent::rewind(); |
| 77 | } |
| 78 | |
| 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; |
| 84 | if ( $ok ) { |
| 85 | $this->cache['current'] = $value; |
| 86 | } |
| 87 | |
| 88 | return $ok; |
| 89 | } |
| 90 | |
| 91 | #[\ReturnTypeWillChange] |
| 92 | public function key() { |
| 93 | $this->init(); |
| 94 | |
| 95 | return parent::key(); |
| 96 | } |
| 97 | |
| 98 | public function valid(): bool { |
| 99 | $this->init(); |
| 100 | |
| 101 | return parent::valid(); |
| 102 | } |
| 103 | |
| 104 | #[\ReturnTypeWillChange] |
| 105 | public function current() { |
| 106 | $this->init(); |
| 107 | if ( parent::valid() ) { |
| 108 | return $this->cache['current']; |
| 109 | } else { |
| 110 | return null; // out of range |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Obviate the usual need for rewind() before using a FilterIterator in a manual loop |
| 116 | */ |
| 117 | protected function init() { |
| 118 | if ( !$this->rewound ) { |
| 119 | $this->rewind(); |
| 120 | } |
| 121 | } |
| 122 | } |