1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace drupol\phpermutations\Iterators; |
4
|
|
|
|
5
|
|
|
use drupol\phpermutations\Combinatorics; |
6
|
|
|
use drupol\phpermutations\IteratorInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class Product. |
10
|
|
|
*/ |
11
|
|
|
class Product extends Combinatorics implements IteratorInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* The key. |
15
|
|
|
* |
16
|
|
|
* @var int |
17
|
|
|
*/ |
18
|
|
|
protected $key; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The iterators. |
22
|
|
|
* |
23
|
|
|
* @var \Iterator[] |
24
|
|
|
*/ |
25
|
|
|
protected $iterators = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Product constructor. |
29
|
|
|
* |
30
|
|
|
* @param array $datasetArray |
31
|
|
|
* The array of dataset |
32
|
|
|
*/ |
33
|
4 |
|
public function __construct(array $datasetArray) |
34
|
|
|
{ |
35
|
4 |
|
parent::__construct($datasetArray); |
36
|
|
|
|
37
|
4 |
|
foreach ($datasetArray as $dataset) { |
38
|
4 |
|
$this->iterators[] = new \ArrayIterator($dataset); |
39
|
|
|
} |
40
|
|
|
|
41
|
4 |
|
$this->key = 0; |
42
|
4 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
4 |
|
public function current() |
48
|
|
|
{ |
49
|
4 |
|
$tuple = []; |
50
|
|
|
|
51
|
4 |
|
foreach ($this->iterators as $iterator) { |
52
|
4 |
|
$tuple[] = $iterator->current(); |
53
|
|
|
} |
54
|
|
|
|
55
|
4 |
|
return $tuple; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
4 |
|
public function next() |
|
|
|
|
62
|
|
|
{ |
63
|
4 |
|
foreach (array_reverse($this->iterators) as $key => $iterator) { |
64
|
4 |
|
$iterator->next(); |
65
|
4 |
|
if ($iterator->valid()) { |
66
|
4 |
|
$count_iterators = count($this->iterators); |
67
|
4 |
|
foreach ($this->iterators as $key2 => $iterator2) { |
68
|
4 |
|
if ($key >= $count_iterators - $key2) { |
69
|
4 |
|
$iterator2->rewind(); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
4 |
|
break; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
4 |
|
++$this->key; |
78
|
4 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
4 |
|
public function valid() |
84
|
|
|
{ |
85
|
4 |
|
$isUnlessOneValid = false; |
86
|
|
|
|
87
|
4 |
|
foreach ($this->iterators as $iterator) { |
88
|
4 |
|
if ($iterator->valid()) { |
89
|
4 |
|
$isUnlessOneValid = true; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
4 |
|
return $isUnlessOneValid; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
4 |
|
public function rewind() |
100
|
|
|
{ |
101
|
4 |
|
foreach ($this->iterators as $iterator) { |
102
|
4 |
|
$iterator->rewind(); |
103
|
|
|
} |
104
|
|
|
|
105
|
4 |
|
$this->key = 0; |
106
|
4 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
4 |
|
public function count() |
112
|
|
|
{ |
113
|
4 |
|
$product = 1; |
114
|
|
|
|
115
|
4 |
|
foreach ($this->getDataset() as $dataset) { |
116
|
4 |
|
$product *= count($dataset); |
117
|
|
|
} |
118
|
|
|
|
119
|
4 |
|
return $product; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Convert the iterator into an array. |
124
|
|
|
* |
125
|
|
|
* @return array |
126
|
|
|
* The elements |
127
|
|
|
*/ |
128
|
4 |
View Code Duplication |
public function toArray() |
|
|
|
|
129
|
|
|
{ |
130
|
4 |
|
$data = []; |
131
|
|
|
|
132
|
4 |
|
for ($this->rewind(); $this->valid(); $this->next()) { |
133
|
4 |
|
$data[] = $this->current(); |
134
|
|
|
} |
135
|
|
|
|
136
|
4 |
|
return $data; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
This check marks variable names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString
.