Total Complexity | 14 |
Total Lines | 109 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
9 | class Combinations extends Iterators |
||
10 | { |
||
11 | /** |
||
12 | * The values. |
||
13 | * |
||
14 | * @var array<int, mixed> |
||
15 | */ |
||
16 | protected $c = []; |
||
17 | |||
18 | /** |
||
19 | * Combinations constructor. |
||
20 | * |
||
21 | * @param array<int, mixed> $dataset |
||
22 | * The dataset |
||
23 | * @param int|null $length |
||
24 | * The length |
||
25 | */ |
||
26 | 5 | public function __construct(array $dataset = [], $length = null) |
|
27 | { |
||
28 | 5 | parent::__construct(array_values($dataset), $length); |
|
29 | 5 | $this->rewind(); |
|
30 | 5 | } |
|
31 | |||
32 | /** |
||
33 | * {@inheritdoc} |
||
34 | */ |
||
35 | 5 | public function count(): int |
|
36 | { |
||
37 | 5 | $i = 0; |
|
38 | |||
39 | 5 | for ($this->rewind(); $this->valid(); $this->next()) { |
|
40 | 5 | ++$i; |
|
41 | } |
||
42 | |||
43 | 5 | return $i; |
|
44 | } |
||
45 | |||
46 | /** |
||
47 | * {@inheritdoc} |
||
48 | */ |
||
49 | 5 | public function current(): mixed |
|
50 | { |
||
51 | 5 | $r = []; |
|
52 | |||
53 | 5 | for ($i = 0; $i < $this->length; ++$i) { |
|
54 | 5 | $r[] = $this->dataset[$this->c[$i]]; |
|
55 | } |
||
56 | |||
57 | 5 | return $r; |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * {@inheritdoc} |
||
62 | * |
||
63 | * @return void |
||
64 | */ |
||
65 | 5 | public function next(): void |
|
66 | { |
||
67 | 5 | if ($this->nextHelper()) { |
|
68 | 4 | ++$this->key; |
|
69 | } else { |
||
70 | 5 | $this->key = -1; |
|
71 | } |
||
72 | 5 | } |
|
73 | |||
74 | /** |
||
75 | * {@inheritdoc} |
||
76 | */ |
||
77 | 5 | public function rewind(): void |
|
78 | { |
||
79 | 5 | $this->c = range(0, $this->length); |
|
80 | 5 | $this->key = 0; |
|
81 | 5 | } |
|
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | * |
||
86 | * @return bool |
||
87 | */ |
||
88 | 5 | public function valid(): bool |
|
89 | { |
||
90 | 5 | return 0 <= $this->key; |
|
91 | } |
||
92 | |||
93 | /** |
||
94 | * Custom next() callback. |
||
95 | * |
||
96 | * @return bool |
||
97 | * Return true or false |
||
98 | */ |
||
99 | 5 | protected function nextHelper(): bool |
|
118 | } |
||
119 | } |
||
120 |