-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSavingIterator.php
More file actions
61 lines (58 loc) · 1.96 KB
/
SavingIterator.php
File metadata and controls
61 lines (58 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
namespace MaxGoryunov\SavingIterator\Src;
use Iterator;
/**
* Iterator which stores iterated values.
*
* @todo #195:15min README has to mention that now it is not required to
* create ArrayAddingIterator and pass it into the constructor because now it
* is a default option.
*
* @template TKey
* @template TValue
* @extends IteratorEnvelope<TKey, TValue>
*/
final class SavingIterator extends IteratorEnvelope
{
/**
* Ctor.
*
* @phpstan-param Iterator<TKey, TValue> $origin
* @phpstan-param AddingIterator<TKey, TValue> $target
* @param Iterator $origin original iterator.
* @param AddingIterator $target iterator to which the values are saved.
*/
public function __construct(
Iterator $origin,
AddingIterator $target = new ArrayAddingIterator()
) {
parent::__construct(
/** @phpstan-ignore-next-line */
new ContextVeil(
$target,
/**
* @todo #196:60min There are phpstan issues with
* ClosureReaction here and in ContextVeilTest. Now they are
* fixed by ignore-line stubs but need to be fixed according
* to phpstan ruleset.
*/
/** @phpstan-ignore-next-line */
new ClosureReaction(
/**
* @phpstan-param AddingIterator<TKey, TValue> $stored
* Iterator for value storage.
*/
fn (AddingIterator $stored) => (new ValidTernary(
$origin,
function (Iterator $source) use ($stored) {
$temp = $stored->from($source);
$source->next();
return $temp;
},
fn () => $stored
))->value()
)
)
);
}
}