1: | <?php declare(strict_types=1); |
2: | |
3: | namespace Salient\Iterator; |
4: | |
5: | use IteratorIterator; |
6: | use ReturnTypeWillChange; |
7: | use Traversable; |
8: | |
9: | /** |
10: | * Iterates over an iterator, applying a callback to each value returned |
11: | * |
12: | * @api |
13: | * |
14: | * @template TKey |
15: | * @template TValue |
16: | * @template TIterator of Traversable<TKey,TValue> |
17: | * @template TReturn of TValue |
18: | * |
19: | * @extends IteratorIterator<TKey,TValue,TIterator> |
20: | */ |
21: | class MapIterator extends IteratorIterator |
22: | { |
23: | /** @var TIterator */ |
24: | private Traversable $Iterator; |
25: | /** @var callable(TValue, TKey, TIterator): TReturn */ |
26: | private $Callback; |
27: | |
28: | /** |
29: | * @api |
30: | * |
31: | * @param TIterator $iterator |
32: | * @param callable(TValue, TKey, TIterator): TReturn $callback |
33: | */ |
34: | public function __construct(Traversable $iterator, callable $callback) |
35: | { |
36: | $this->Iterator = $iterator; |
37: | $this->Callback = $callback; |
38: | parent::__construct($iterator); |
39: | } |
40: | |
41: | /** |
42: | * @return TReturn |
43: | * @disregard P1038 |
44: | */ |
45: | #[ReturnTypeWillChange] |
46: | public function current() |
47: | { |
48: | return ($this->Callback)( |
49: | parent::current(), |
50: | $this->key(), |
51: | $this->Iterator, |
52: | ); |
53: | } |
54: | } |
55: |