Mapper.php
28 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This file is part of the Nette Framework (https://nette.org) |
| 5 | * Copyright (c) 2004 David Grudl (https://davidgrudl.com) |
| 6 | */ |
| 7 | declare (strict_types=1); |
| 8 | namespace FapiMember\Library\Nette\Iterators; |
| 9 | |
| 10 | /** |
| 11 | * Applies the callback to the elements of the inner iterator. |
| 12 | */ |
| 13 | class Mapper extends \IteratorIterator |
| 14 | { |
| 15 | /** @var callable */ |
| 16 | private $callback; |
| 17 | public function __construct(\Traversable $iterator, callable $callback) |
| 18 | { |
| 19 | parent::__construct($iterator); |
| 20 | $this->callback = $callback; |
| 21 | } |
| 22 | #[\ReturnTypeWillChange] |
| 23 | public function current() |
| 24 | { |
| 25 | return ($this->callback)(parent::current(), parent::key()); |
| 26 | } |
| 27 | } |
| 28 |