Completed
Push — master ( 747a6a...c6c655 )
by Ítalo
02:55
created

LazyConcatIterator   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 81
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 2
A __clone() 0 7 2
A rewind() 0 12 2
A valid() 0 4 1
A next() 0 9 3
A key() 0 4 1
A current() 0 4 1
1
<?php
2
3
namespace Collections\Iterator;
4
5
class LazyConcatIterator implements \Iterator
6
{
7
    /**
8
     * @var \Iterator
9
     */
10
    private $it1;
11
12
    /**
13
     * @var \Iterator
14
     */
15
    private $it2;
16
17
    /**
18
     * @var \Iterator
19
     */
20
    private $currentIt;
21
22
    /**
23
     * @var int
24
     */
25
    private $state;
26
27
    public function __construct($it1, $it2)
28
    {
29
        $this->it1 = $it1;
30
        $this->it2 = $it2;
31
        $this->currentIt = $it1;
32
        $this->state = 1;
33
34
        if (!$this->currentIt->valid()) {
35
            $this->currentIt = $this->it2;
36
            $this->state = 2;
37
        }
38
    }
39
40
    public function __clone()
41
    {
42
        $this->it1 = clone $this->it1;
43
        $this->it2 = clone $this->it2;
44
45
        $this->currentIt = ($this->state === 1) ? $this->it1 : $this->it2;
46
    }
47
48
    public function rewind()
49
    {
50
        $this->it1->rewind();
51
        $this->it2->rewind();
52
        $this->currentIt = $this->it1;
53
        $this->state = 1;
54
55
        if (!$this->currentIt->valid()) {
56
            $this->currentIt = $this->it2;
57
            $this->state = 2;
58
        }
59
    }
60
61
    public function valid()
62
    {
63
        return $this->currentIt->valid();
64
    }
65
66
    public function next()
67
    {
68
        $this->currentIt->next();
69
70
        if ($this->state === 1 && !$this->currentIt->valid()) {
71
            $this->currentIt = $this->it2;
72
            $this->state = 2;
73
        }
74
    }
75
76
    public function key()
77
    {
78
        return $this->currentIt->key();
79
    }
80
81
    public function current()
82
    {
83
        return $this->currentIt->current();
84
    }
85
}
86