1
|
|
|
<?php |
2
|
|
|
/****************************************************************************** |
3
|
|
|
* An iterator interface over the Leagues flysystem. |
4
|
|
|
* Copyright (c) 2021, 2015 Richard Klees <[email protected]> |
5
|
|
|
* |
6
|
|
|
* This software is licensed under GPLv3. You should have received |
7
|
|
|
* a copy of the along with the code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Lechimp\Flightcontrol; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* An iterator on each of the contents of another iterator. |
14
|
|
|
*/ |
15
|
|
|
class SubIterator extends Iterator |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var Iterator |
19
|
|
|
*/ |
20
|
|
|
protected $top; |
21
|
|
|
|
22
|
6 |
|
public function __construct(Iterator $top) |
23
|
|
|
{ |
24
|
6 |
|
$this->top = $top; |
25
|
6 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @inheritdoc |
29
|
|
|
*/ |
30
|
2 |
|
public function filter(\Closure $predicate) : Iterator |
31
|
|
|
{ |
32
|
|
|
// As we are working somewhere down the iteration, we have to apply |
33
|
|
|
// the filter on all non files in the iterator above and return |
34
|
|
|
// something similar to this. |
35
|
2 |
|
return $this->wrappedMapTopOnNonFiles(function ($obj) use ($predicate) { |
36
|
2 |
|
return $obj->filter($predicate); |
37
|
2 |
|
}); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @inheritdoc |
42
|
|
|
*/ |
43
|
4 |
|
public function map(\Closure $trans) : Iterator |
44
|
|
|
{ |
45
|
|
|
// As we are working somewhere down the iteration, we have to map |
46
|
|
|
// the transformation on all non files in the iterator above and return |
47
|
|
|
// something similar to this. |
48
|
4 |
|
return $this->wrappedMapTopOnNonFiles(function ($obj) use ($trans) { |
49
|
4 |
|
return $obj->map($trans); |
50
|
4 |
|
}); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritdoc |
55
|
|
|
*/ |
56
|
6 |
|
public function fold($start_value, $iteration) |
57
|
|
|
{ |
58
|
|
|
// As this unwraps one layer of the iteration, we need to apply the fold |
59
|
|
|
// function to the things in the iterator above and return something |
60
|
|
|
// similar to the iterator above. |
61
|
6 |
|
return $this->mapTopOnNonFiles(function ($v) use ($start_value, $iteration) { |
62
|
6 |
|
return $v->fold($start_value, $iteration); |
63
|
6 |
|
}); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// Helpers |
67
|
|
|
|
68
|
6 |
|
protected function mapTop(\Closure $trans) |
69
|
|
|
{ |
70
|
6 |
|
return $this->top->map($trans); |
71
|
|
|
} |
72
|
|
|
|
73
|
6 |
|
protected function mapTopOnNonFiles(\Closure $trans) |
74
|
|
|
{ |
75
|
6 |
|
return $this->mapTop(function ($obj) use ($trans) { |
76
|
6 |
|
if ($obj->isFile()) { |
77
|
4 |
|
return $obj; |
78
|
|
|
} |
79
|
6 |
|
return $trans($obj); |
80
|
6 |
|
}); |
81
|
|
|
} |
82
|
|
|
|
83
|
4 |
|
protected function wrap(Iterator $iter) |
84
|
|
|
{ |
85
|
4 |
|
return new SubIterator($iter); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
protected function wrappedMapTop(\Closure $trans) |
89
|
|
|
{ |
90
|
|
|
return $this->wrap($this->mapTop($trans)); |
91
|
|
|
} |
92
|
|
|
|
93
|
4 |
|
protected function wrappedMapTopOnNonFiles(\Closure $trans) |
94
|
|
|
{ |
95
|
4 |
|
return $this->wrap($this->mapTopOnNonFiles($trans)); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|