ChainIterator   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getInnerIterator() 0 4 1
A setNextValidSubIterator() 0 12 3
A rewind() 0 6 1
A current() 0 4 1
A key() 0 8 2
A next() 0 9 2
A valid() 0 4 1
1
<?php
2
3
namespace itertools;
4
5
use EmptyIterator;
6
use OuterIterator;
7
8
9
class ChainIterator implements OuterIterator
10
{
11
	const DONT_USE_KEYS = 'DONT_USE_KEYS';
12
	const USE_KEYS = 'USE_KEYS';
13
14
	protected $useOriginalKeys;
15
	protected $iterator;
16
	protected $currentSubIterator;
17
	protected $count;
18
19
	public function __construct($iterator, $useOriginalKeys = self::USE_KEYS)
20
	{
21
		$this->useOriginalKeys = $useOriginalKeys;
22
		$this->iterator = IterUtil::asIterator($iterator);
23
		$this->currentSubIterator = new EmptyIterator();
24
	}
25
26
	public function getInnerIterator()
27
	{
28
		return $this->iterator;
29
	}
30
31
	public function setNextValidSubIterator()
32
	{
33
		while($this->iterator->valid()) {
34
			$this->currentSubIterator = IterUtil::asIterator($this->iterator->current());
35
			$this->currentSubIterator->rewind();
36
			if($this->currentSubIterator->valid()) {
37
				return;
38
			}
39
			$this->iterator->next();
40
		}
41
		$this->currentSubIterator = new EmptyIterator();
42
	}
43
44
    public function rewind()
45
	{
46
		$this->iterator->rewind();
47
		$this->setNextValidSubIterator();
48
		$this->count = 0;
49
    }
50
51
    public function current()
52
	{
53
        return $this->currentSubIterator->current();
54
    }
55
56
    public function key()
57
	{
58
		if($this->useOriginalKeys == self::DONT_USE_KEYS) {
59
			return $this->count;
60
		} else {
61
			return $this->currentSubIterator->key();
62
		}
63
    }
64
65
    public function next()
66
	{
67
		$this->currentSubIterator->next();
68
		if(! $this->currentSubIterator->valid()) {
69
			$this->iterator->next();
70
			$this->setNextValidSubIterator();
71
		}
72
		$this->count += 1;
73
    }
74
75
    public function valid()
76
	{
77
        return $this->currentSubIterator->valid();
78
    }
79
}
80
81