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