MultipleArrayIterator   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 54
rs 10
c 0
b 0
f 0
wmc 16

6 Methods

Rating   Name   Duplication   Size   Complexity  
A key() 0 3 2
A valid() 0 3 2
A current() 0 3 2
A rewind() 0 5 2
A __construct() 0 12 4
A next() 0 10 4
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