Passed
Push — master ( 59b03f...8d3068 )
by Alec
02:20
created

Circular::getElement()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * User: alec
4
 * Date: 01.11.18
5
 * Time: 16:10
6
 */
7
declare(strict_types=1);
8
9
namespace AlecRabbit;
10
11
12
/**
13
 * Class Circular
14
 */
15
class Circular implements \Iterator
16
{
17
    /** @var array */
18
    protected $data;
19
20
    /**
21
     * Circular constructor.
22
     * @param array $data
23
     */
24
    public function __construct(array $data)
25
    {
26
        $this->data = $data;
27
        reset($this->data);
28
    }
29
30
    public function __invoke()
31
    {
32
        return $this->getElement();
33
    }
34
35
    /**
36
     * @return mixed
37
     */
38
    public function getElement()
39
    {
40
        if (($result = current($this->data)) === false) {
41
            $result = reset($this->data);
42
        }
43
        next($this->data);
44
45
        return $result;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function current()
52
    {
53
        return current($this->data);
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function next(): void
60
    {
61
        next($this->data);
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function key()
68
    {
69
        return key($this->data);
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function valid(): bool
76
    {
77
        return
78
            false !== current($this->data);
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function rewind(): void
85
    {
86
        reset($this->data);
87
    }
88
}