2011-05-02 06:33:05 +02:00
|
|
|
<?php
|
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
class CombinationsArrayIterator implements Iterator
|
|
|
|
{
|
|
|
|
protected $arrays;
|
|
|
|
protected $keys;
|
|
|
|
protected $numArrays;
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
protected $isValid = false;
|
|
|
|
protected $k = 0;
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
public function __construct($args)
|
|
|
|
{
|
|
|
|
$this->arrays = array();
|
|
|
|
$this->keys = array();
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
$keys = array_keys($args);
|
|
|
|
$values = array_values($args);
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
foreach ($values as $i => $arg) {
|
|
|
|
if (is_array($arg) && count($arg)) {
|
|
|
|
$this->arrays[] = $arg;
|
|
|
|
$this->keys[] = $keys[$i];
|
|
|
|
}
|
|
|
|
}
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
$this->numArrays = count($this->arrays);
|
|
|
|
$this->rewind();
|
|
|
|
}
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
public function rewind()
|
|
|
|
{
|
|
|
|
if (!$this->numArrays) {
|
|
|
|
$this->isValid = false;
|
|
|
|
} else {
|
|
|
|
$this->isValid = true;
|
|
|
|
$this->k = 0;
|
|
|
|
|
|
|
|
for ($i = 0; $i < $this->numArrays; $i++) {
|
|
|
|
reset($this->arrays[$i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
public function valid()
|
|
|
|
{
|
|
|
|
return $this->isValid;
|
|
|
|
}
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
public function next()
|
|
|
|
{
|
|
|
|
$this->k++;
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
for ($i = 0; $i < $this->numArrays; $i++) {
|
|
|
|
if (next($this->arrays[$i]) === false) {
|
|
|
|
if ($i == $this->numArrays-1) {
|
|
|
|
$this->isValid = false;
|
|
|
|
} else {
|
|
|
|
reset($this->arrays[$i]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
public function current()
|
|
|
|
{
|
|
|
|
$res = array();
|
|
|
|
for ($i = 0; $i < $this->numArrays; $i++) {
|
|
|
|
$res[$this->keys[$i]] = current($this->arrays[$i]);
|
|
|
|
}
|
|
|
|
return $res;
|
|
|
|
}
|
2011-05-02 06:33:05 +02:00
|
|
|
|
2015-11-21 07:19:20 +01:00
|
|
|
public function key()
|
|
|
|
{
|
|
|
|
return $this->k;
|
|
|
|
}
|
|
|
|
}
|