silverstripe-fulltextsearch/code/utils/CombinationsArrayIterator.php
Elliot Sawyer 1728a62af5 WIP: Silverstripe 4 compatibility
Thanks to Marco Hermo and Brett Tasker for helping with this
* Bump framework/cms to ^4.0@dev
* WIP Silverstripe 4 compatibility fixes
* more replacements and patches to migrate this module to 4.0
* Update composer.json
* remove php <5.5 from travis.yml
* WIP more SS4 compatibility fixes
* WIP fix solr path to use DIR, avoid hardcoded module name
* WIP respect current include path
* WIP Namespacing and use on SearchIndex class
* Namespacing for tests
* WIP add namespaces to all classes
* Second push of Test changes + namespacing
* WIP split Solr files with multiple classes into single file / single class. Adjust namespaces
* Fix PHP errors in test
* break out search components with multiple classes into individual files and change namespaces
* Update namespacing for Search indexes and variants in tests
* Batch fixes for tests #2
* Update _config.php to use namespace
* Use root namespace in referencing Apache_Solr_Document
* Migrate task names so that the name is not fully qualified
2017-04-25 20:46:35 +12:00

82 lines
1.8 KiB
PHP

<?php
namespace SilverStripe\FullTextSearch\Utils;
use Iterator;
class CombinationsArrayIterator implements Iterator
{
protected $arrays;
protected $keys;
protected $numArrays;
protected $isValid = false;
protected $k = 0;
public function __construct($args)
{
$this->arrays = array();
$this->keys = array();
$keys = array_keys($args);
$values = array_values($args);
foreach ($values as $i => $arg) {
if (is_array($arg) && count($arg)) {
$this->arrays[] = $arg;
$this->keys[] = $keys[$i];
}
}
$this->numArrays = count($this->arrays);
$this->rewind();
}
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]);
}
}
}
public function valid()
{
return $this->isValid;
}
public function next()
{
$this->k++;
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;
}
}
}
public function current()
{
$res = array();
for ($i = 0; $i < $this->numArrays; $i++) {
$res[$this->keys[$i]] = current($this->arrays[$i]);
}
return $res;
}
public function key()
{
return $this->k;
}
}