Passed
Push — master ( 9e9b54...8c04f5 )
by
unknown
38s
created

AccumulateIterator::current()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Boudewijn Schoon <[email protected]>
4
 * @copyright Zicht Online <http://zicht.nl>
5
 */
6
7
namespace Zicht\Itertools\lib;
8
9
use Zicht\Itertools\lib\Interfaces\FiniteIterableInterface;
10
use Zicht\Itertools\lib\Traits\FiniteIterableTrait;
11
12
/**
13
 * Class AccumulateIterator
14
 *
15
 * @package Zicht\Itertools\lib
16
 */
17
class AccumulateIterator implements FiniteIterableInterface
18
{
19
    use FiniteIterableTrait;
20
21
    /** @var \Iterator */
22
    protected $iterable;
23
24
    /** @var \Closure */
25
    protected $func;
26
27
    /** @var mixed */
28
    protected $value;
29
30
    /**
31
     * AccumulateIterator constructor.
32
     *
33
     * @param \Iterator $iterable
34
     * @param \Closure $func
35
     */
36 12
    public function __construct(\Iterator $iterable, \Closure $func)
37
    {
38 12
        $this->iterable = $iterable;
39 12
        $this->func = $func;
40 12
        $this->value = null;
41 12
    }
42
43
    /**
44
     * @{inheritDoc}
45
     */
46 11
    public function rewind()
47
    {
48 11
        $this->iterable->rewind();
49 11
        $this->value = $this->iterable->valid() ? $this->iterable->current() : null;
50 11
    }
51
52
    /**
53
     * @{inheritDoc}
54
     */
55 9
    public function current()
56
    {
57 9
        return $this->value;
58
    }
59
60
    /**
61
     * @{inheritDoc}
62
     */
63 9
    public function key()
64
    {
65 9
        return $this->iterable->key();
66
    }
67
68
    /**
69
     * @{inheritDoc}
70
     */
71 9
    public function next()
72
    {
73 9
        $this->iterable->next();
74 9
        if ($this->iterable->valid()) {
75
            // must assign $this->func to $func before calling the closure
76
            // because otherwise it will try fo find a method called func,
77
            // which doesn't exist
78 9
            $func = $this->func;
79 9
            $this->value = $func($this->value, $this->iterable->current());
80
        }
81 9
    }
82
83
    /**
84
     * @{inheritDoc}
85
     */
86 11
    public function valid()
87
    {
88 11
        return $this->iterable->valid();
89
    }
90
}
91