| Total Complexity | 15 |
| Total Lines | 76 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 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() |
||
| 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() |
||
| 83 | } |
||
| 84 | } |
||
| 85 |