SubIterator::map()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
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