| 1 | <?php |
||
| 10 | class Combinations extends Combinatorics implements \Iterator { |
||
| 11 | |||
| 12 | /** |
||
| 13 | * @var array |
||
| 14 | */ |
||
| 15 | protected $c = array(); |
||
|
|
|||
| 16 | |||
| 17 | /** |
||
| 18 | * @var int |
||
| 19 | */ |
||
| 20 | protected $count = 0; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var int |
||
| 24 | */ |
||
| 25 | protected $pos = 0; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Combinations constructor. |
||
| 29 | * |
||
| 30 | * @param array $dataset |
||
| 31 | * @param null $length |
||
| 32 | */ |
||
| 33 | 6 | public function __construct(array $dataset = array(), $length = NULL) { |
|
| 37 | |||
| 38 | /** |
||
| 39 | * @return int |
||
| 40 | */ |
||
| 41 | public function key() { |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @return array |
||
| 47 | */ |
||
| 48 | 6 | public function current() { |
|
| 49 | 6 | $r = array(); |
|
| 50 | 6 | for($i = 0; $i < $this->length; $i++) { |
|
| 51 | 6 | $r[] = $this->dataset[$this->c[$i]]; |
|
| 52 | 6 | } |
|
| 53 | 6 | return $r; |
|
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * |
||
| 58 | */ |
||
| 59 | 6 | public function next() { |
|
| 60 | 6 | if ($this->_next()) { |
|
| 61 | 4 | $this->pos++; |
|
| 62 | 4 | } |
|
| 63 | else { |
||
| 64 | 6 | $this->pos = -1; |
|
| 65 | } |
||
| 66 | 6 | } |
|
| 67 | |||
| 68 | /** |
||
| 69 | * |
||
| 70 | */ |
||
| 71 | 6 | public function rewind() { |
|
| 75 | |||
| 76 | /** |
||
| 77 | * @return bool |
||
| 78 | */ |
||
| 79 | 6 | public function valid() { |
|
| 82 | |||
| 83 | /** |
||
| 84 | * @return bool |
||
| 85 | */ |
||
| 86 | 6 | protected function _next() { |
|
| 87 | 6 | $i = $this->length - 1; |
|
| 88 | 6 | while ($i >= 0 && $this->c[$i] == $this->datasetCount - $this->length + $i) { |
|
| 89 | 6 | $i--; |
|
| 90 | 6 | } |
|
| 91 | 6 | if($i < 0) { |
|
| 92 | 6 | return FALSE; |
|
|
1 ignored issue
–
show
|
|||
| 93 | } |
||
| 94 | 4 | $this->c[$i]++; |
|
| 95 | 4 | while($i++ < $this->length - 1) { |
|
| 96 | 4 | $this->c[$i] = $this->c[$i - 1] + 1; |
|
| 97 | 4 | } |
|
| 98 | |||
| 99 | 4 | return TRUE; |
|
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @return array |
||
| 104 | */ |
||
| 105 | 3 | public function toArray() { |
|
| 106 | 3 | $data = []; |
|
| 107 | 3 | for($this->rewind(); $this->valid(); $this->next()) { |
|
| 108 | 3 | $data[] = $this->current(); |
|
| 109 | 3 | } |
|
| 110 | 3 | return $data; |
|
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @return int |
||
| 115 | */ |
||
| 116 | 3 | public function count() { |
|
| 119 | |||
| 120 | } |
||
| 121 |
Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.