CombinationsArrayIterator   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 38
dl 0
loc 76
rs 10
c 0
b 0
f 0
wmc 15

6 Methods

Rating   Name   Duplication   Size   Complexity  
A valid() 0 3 1
A key() 0 3 1
A next() 0 13 4
A __construct() 0 17 4
A current() 0 7 2
A rewind() 0 10 3
1
<?php
2
3
namespace SilverStripe\FullTextSearch\Utils;
4
5
use Iterator;
6
7
class CombinationsArrayIterator implements Iterator
8
{
9
    protected $arrays;
10
    protected $keys;
11
    protected $numArrays;
12
13
    protected $isValid = false;
14
    protected $k = 0;
15
16
    public function __construct($args)
17
    {
18
        $this->arrays = array();
19
        $this->keys = array();
20
21
        $keys = array_keys($args);
22
        $values = array_values($args);
23
24
        foreach ($values as $i => $arg) {
25
            if (is_array($arg) && count($arg)) {
26
                $this->arrays[] = $arg;
27
                $this->keys[] = $keys[$i];
28
            }
29
        }
30
31
        $this->numArrays = count($this->arrays);
32
        $this->rewind();
33
    }
34
35
    public function rewind()
36
    {
37
        if (!$this->numArrays) {
38
            $this->isValid = false;
39
        } else {
40
            $this->isValid = true;
41
            $this->k = 0;
42
            
43
            for ($i = 0; $i < $this->numArrays; $i++) {
44
                reset($this->arrays[$i]);
45
            }
46
        }
47
    }
48
49
    public function valid()
50
    {
51
        return $this->isValid;
52
    }
53
54
    public function next()
55
    {
56
        $this->k++;
57
58
        for ($i = 0; $i < $this->numArrays; $i++) {
59
            if (next($this->arrays[$i]) === false) {
60
                if ($i == $this->numArrays-1) {
61
                    $this->isValid = false;
62
                } else {
63
                    reset($this->arrays[$i]);
64
                }
65
            } else {
66
                break;
67
            }
68
        }
69
    }
70
71
    public function current()
72
    {
73
        $res = array();
74
        for ($i = 0; $i < $this->numArrays; $i++) {
75
            $res[$this->keys[$i]] = current($this->arrays[$i]);
76
        }
77
        return $res;
78
    }
79
80
    public function key()
81
    {
82
        return $this->k;
83
    }
84
}
85