Completed
Push — master ( 2a86f1...70e886 )
by Lars
02:23 queued 10s
created

ArrayyRewindableGenerator::next()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arrayy;
6
7
/**
8
 * @internal
9
 */
10
final class ArrayyRewindableGenerator implements \Iterator
11
{
12
    /**
13
     * @var callable
14
     */
15
    private $generatorFunction;
16
17
    /**
18
     * @var \Generator
19
     */
20
    private $generator;
21
22
    /**
23
     * @var callable|null
24
     */
25
    private $onRewind;
26
27
    /**
28
     * @param callable $generatorConstructionFunction A callable that should return a Generator
29
     * @param callable $onRewind                      callable that gets invoked with 0 arguments after the iterator
30
     *                                                was rewinded
31
     *
32
     * @throws \InvalidArgumentException
33
     */
34 5
    public function __construct(callable $generatorConstructionFunction, callable $onRewind = null)
35
    {
36 5
        $this->generatorFunction = $generatorConstructionFunction;
37 5
        $this->onRewind = $onRewind;
38 5
        $this->generateGenerator();
39 5
    }
40
41 5
    private function generateGenerator()
42
    {
43 5
        $this->generator = call_user_func($this->generatorFunction);
44
45 5
        if (!($this->generator instanceof \Generator)) {
46
            throw new \InvalidArgumentException('The callable needs to return a Generator');
47
        }
48 5
    }
49
50
    /**
51
     * Return the current element.
52
     *
53
     * @return mixed
54
     * @link http://php.net/manual/en/iterator.current.php
55
     * @see  Iterator::current
56
     */
57 4
    public function current()
58
    {
59 4
        return $this->generator->current();
60
    }
61
62
    /**
63
     * Move forward to next element.
64
     *
65
     * @see  Iterator::next
66
     * @link http://php.net/manual/en/iterator.next.php
67
     */
68 4
    public function next()
69
    {
70 4
        $this->generator->next();
71 4
    }
72
73
    /**
74
     * Return the key of the current element.
75
     *
76
     * @return mixed scalar on success, or null on failure.
77
     * @link http://php.net/manual/en/iterator.key.php
78
     * @see  Iterator::key
79
     */
80 4
    public function key()
81
    {
82 4
        return $this->generator->key();
83
    }
84
85
    /**
86
     * Checks if current position is valid.
87
     *
88
     * @return boolean
89
     * @link http://php.net/manual/en/iterator.valid.php
90
     * @see  Iterator::rewind
91
     */
92 5
    public function valid()
93
    {
94 5
        return $this->generator->valid();
95
    }
96
97
    /**
98
     * Rewind the Iterator to the first element.
99
     *
100
     * @see  Iterator::rewind
101
     * @link http://php.net/manual/en/iterator.rewind.php
102
     */
103 5
    public function rewind()
104
    {
105 5
        $this->generateGenerator();
106
107 5
        if (is_callable($this->onRewind)) {
108
            call_user_func($this->onRewind);
109
        }
110 5
    }
111
}
112