MultipleArrayIterator::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 3
nop 0
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
namespace SilverStripe\FullTextSearch\Utils;
3
4
use Iterator;
5
6
class MultipleArrayIterator implements Iterator
7
{
8
    protected $arrays;
9
    protected $active;
10
11
    public function __construct()
12
    {
13
        $args = func_get_args();
14
15
        $this->arrays = array();
16
        foreach ($args as $arg) {
17
            if (is_array($arg) && count($arg)) {
18
                $this->arrays[] = $arg;
19
            }
20
        }
21
22
        $this->rewind();
23
    }
24
25
    public function rewind()
26
    {
27
        $this->active = $this->arrays;
28
        if ($this->active) {
29
            reset($this->active[0]);
30
        }
31
    }
32
33
    public function current()
34
    {
35
        return $this->active ? current($this->active[0]) : false;
36
    }
37
38
    public function key()
39
    {
40
        return $this->active ? key($this->active[0]) : false;
41
    }
42
43
    public function next()
44
    {
45
        if (!$this->active) {
46
            return;
47
        }
48
49
        if (next($this->active[0]) === false) {
50
            array_shift($this->active);
51
            if ($this->active) {
52
                reset($this->active[0]);
53
            }
54
        }
55
    }
56
57
    public function valid()
58
    {
59
        return $this->active && (current($this->active[0]) !== false);
60
    }
61
}
62