1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
Condorcet PHP - Election manager and results calculator. |
4
|
|
|
Designed for the Condorcet method. Integrating a large number of algorithms extending Condorcet. Expandable for all types of voting systems. |
5
|
|
|
|
6
|
|
|
By Julien Boudry and contributors - MIT LICENSE (Please read LICENSE.txt) |
7
|
|
|
https://github.com/julien-boudry/Condorcet |
8
|
|
|
*/ |
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
namespace Condorcet\DataManager; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
use Condorcet\CondorcetException; |
16
|
|
|
use Condorcet\Election; |
17
|
|
|
use Condorcet\Vote; |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
class VotesManager extends ArrayManager |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/////////// Magic /////////// |
24
|
|
|
|
25
|
134 |
|
public function __construct (Election $election) |
26
|
|
|
{ |
27
|
134 |
|
$this->setElection($election); |
28
|
|
|
|
29
|
134 |
|
parent::__construct(); |
30
|
134 |
|
} |
31
|
|
|
|
32
|
134 |
|
public function setElection (Election $election) : void |
33
|
|
|
{ |
34
|
134 |
|
$this->_link[0] = $election; |
35
|
134 |
|
} |
36
|
|
|
|
37
|
11 |
|
public function getElection () : Election |
38
|
|
|
{ |
39
|
11 |
|
return $this->_link[0]; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/////////// Data CallBack for external drivers /////////// |
43
|
|
|
|
44
|
7 |
|
public function getDataContextObject () : DataContextInterface |
45
|
|
|
{ |
46
|
|
|
$context = new Class implements DataContextInterface { |
47
|
|
|
public $election; |
48
|
|
|
|
49
|
5 |
|
public function dataCallBack ($data) : Vote |
50
|
|
|
{ |
51
|
5 |
|
$vote = new Vote ($data); |
52
|
5 |
|
$this->election->checkVoteCandidate($vote); |
53
|
5 |
|
$vote->registerLink($this->election); |
54
|
|
|
|
55
|
5 |
|
return $vote; |
56
|
|
|
} |
57
|
|
|
|
58
|
6 |
|
public function dataPrepareStoringAndFormat ($data) : string |
59
|
|
|
{ |
60
|
6 |
|
$data->destroyLink($this->election); |
61
|
|
|
|
62
|
6 |
|
return (string) $data; |
63
|
|
|
} |
64
|
|
|
}; |
65
|
|
|
|
66
|
7 |
|
$context->election = $this->_link[0] ?? null; |
67
|
|
|
|
68
|
7 |
|
return $context; |
69
|
|
|
} |
70
|
|
|
|
71
|
11 |
|
protected function preDeletedTask ($object) : void |
72
|
|
|
{ |
73
|
11 |
|
$object->destroyLink($this->_link[0]); |
74
|
11 |
|
} |
75
|
|
|
|
76
|
|
|
/////////// Array Access - Specials improvements /////////// |
77
|
|
|
|
78
|
117 |
|
public function offsetSet($offset, $value) : void |
79
|
|
|
{ |
80
|
117 |
|
if ($value instanceof Vote) : |
81
|
117 |
|
parent::offsetSet($offset,$value); |
82
|
117 |
|
$this->setStateToVote(); |
83
|
|
|
else : |
84
|
1 |
|
throw new CondorcetException (0,'Value must be an instanceof Condorcet\\Vote'); |
85
|
|
|
endif; |
86
|
117 |
|
} |
87
|
|
|
|
88
|
7 |
|
public function offsetUnset($offset) : void |
89
|
|
|
{ |
90
|
7 |
|
parent::offsetUnset($offset); |
91
|
7 |
|
$this->setStateToVote(); |
92
|
7 |
|
} |
93
|
|
|
|
94
|
|
|
/////////// Internal Election related methods /////////// |
95
|
|
|
|
96
|
117 |
|
protected function setStateToVote () : void |
97
|
|
|
{ |
98
|
117 |
|
foreach ($this->_link as $element) : |
99
|
117 |
|
$element->setStateToVote(); |
100
|
|
|
endforeach; |
101
|
117 |
|
} |
102
|
|
|
|
103
|
|
|
/////////// Get Votes Methods /////////// |
104
|
|
|
|
105
|
6 |
|
public function getVoteKey (Vote $vote) { |
106
|
6 |
|
($r = array_search($vote, $this->_Container, true)) !== false || ($r = array_search($vote, $this->_Cache, true)); |
107
|
|
|
|
108
|
6 |
|
return $r; |
109
|
|
|
} |
110
|
|
|
|
111
|
2 |
|
protected function getFulllVotesListGenerator () : \Generator |
112
|
|
|
{ |
113
|
2 |
|
foreach ($this as $voteKey => $vote) : |
114
|
2 |
|
yield $voteKey => $vote; |
115
|
|
|
endforeach; |
116
|
2 |
|
} |
117
|
|
|
|
118
|
7 |
|
protected function getPartialVotesListGenerator (array $tag, bool $with) : \Generator |
119
|
|
|
{ |
120
|
7 |
|
foreach ($this as $voteKey => $vote) : |
121
|
7 |
|
$noOne = true; |
122
|
7 |
|
foreach ($tag as $oneTag) : |
123
|
7 |
|
if ( ( $oneTag === $voteKey ) || in_array($oneTag, $vote->getTags(),true) ) : |
124
|
7 |
|
if ($with) : |
125
|
7 |
|
yield $voteKey => $vote; |
126
|
7 |
|
break; |
127
|
|
|
else : |
128
|
7 |
|
$noOne = false; |
129
|
|
|
endif; |
130
|
|
|
endif; |
131
|
|
|
endforeach; |
132
|
|
|
|
133
|
7 |
|
if (!$with && $noOne) : |
134
|
7 |
|
yield $voteKey => $vote; |
135
|
|
|
endif; |
136
|
|
|
endforeach; |
137
|
7 |
|
} |
138
|
|
|
|
139
|
|
|
// Get the votes list |
140
|
14 |
|
public function getVotesList ($tag = null, bool $with = true) : array |
141
|
|
|
{ |
142
|
14 |
|
if ($tag === null) : |
143
|
12 |
|
return $this->getFullDataSet(); |
144
|
|
|
else : |
145
|
6 |
|
$search = []; |
146
|
|
|
|
147
|
6 |
|
foreach ($this->getPartialVotesListGenerator($tag,$with) as $voteKey => $vote) : |
148
|
6 |
|
$search[$voteKey] = $vote; |
149
|
|
|
endforeach; |
150
|
|
|
|
151
|
6 |
|
return $search; |
152
|
|
|
endif; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
// Get the votes list as a generator object |
156
|
2 |
|
public function getVotesListGenerator ($tag = null, bool $with = true) : \Generator |
157
|
|
|
{ |
158
|
2 |
|
if ($tag === null) : |
159
|
2 |
|
return $this->getFulllVotesListGenerator(); |
160
|
|
|
else : |
161
|
2 |
|
return $this->getPartialVotesListGenerator($tag,$with); |
162
|
|
|
endif; |
163
|
|
|
} |
164
|
|
|
|
165
|
5 |
|
public function getVotesListAsString () : string |
166
|
|
|
{ |
167
|
5 |
|
$simpleList = ''; |
168
|
|
|
|
169
|
5 |
|
$weight = []; |
170
|
5 |
|
$nb = []; |
171
|
|
|
|
172
|
5 |
|
foreach ($this as $oneVote) : |
173
|
5 |
|
$oneVoteString = $oneVote->getSimpleRanking($this->_link[0]); |
174
|
|
|
|
175
|
5 |
|
if(!array_key_exists($oneVoteString, $weight)) : |
176
|
5 |
|
$weight[$oneVoteString] = 0; |
177
|
|
|
endif; |
178
|
5 |
|
if(!array_key_exists($oneVoteString, $nb)) : |
179
|
5 |
|
$nb[$oneVoteString] = 0; |
180
|
|
|
endif; |
181
|
|
|
|
182
|
5 |
|
if ($this->getElection()->isVoteWeightIsAllowed()) : |
183
|
1 |
|
$weight[$oneVoteString] += $oneVote->getWeight(); |
184
|
|
|
else : |
185
|
4 |
|
$weight[$oneVoteString]++; |
186
|
|
|
endif; |
187
|
|
|
|
188
|
5 |
|
$nb[$oneVoteString]++; |
189
|
|
|
endforeach; |
190
|
|
|
|
191
|
5 |
|
ksort($weight); |
192
|
5 |
|
arsort($weight); |
193
|
|
|
|
194
|
5 |
|
$isFirst = true; |
195
|
5 |
|
foreach ($weight as $key => $value) : |
196
|
5 |
|
if (!$isFirst) : $simpleList .= "\n"; endif; |
197
|
5 |
|
$simpleList .= $key.' * '.$nb[$key]; |
198
|
5 |
|
$isFirst = false; |
199
|
|
|
endforeach; |
200
|
|
|
|
201
|
5 |
|
return $simpleList; |
202
|
|
|
} |
203
|
|
|
|
204
|
12 |
|
public function countVotes (?array $tag, bool $with) : int |
205
|
|
|
{ |
206
|
12 |
|
if ($tag === null) : |
207
|
9 |
|
return count($this); |
208
|
|
|
else : |
209
|
5 |
|
$count = 0; |
210
|
|
|
|
211
|
5 |
|
foreach ($this as $key => $value) : |
212
|
5 |
|
$noOne = true; |
213
|
5 |
|
foreach ($tag as $oneTag) : |
214
|
5 |
|
if ( ( $oneTag === $key ) || in_array($oneTag, $value->getTags(),true) ) : |
215
|
5 |
|
if ($with) : |
216
|
5 |
|
$count++; |
217
|
5 |
|
break; |
218
|
|
|
else : |
219
|
5 |
|
$noOne = false; |
220
|
|
|
endif; |
221
|
|
|
endif; |
222
|
|
|
endforeach; |
223
|
|
|
|
224
|
5 |
|
if (!$with && $noOne) : |
225
|
5 |
|
$count++; |
226
|
|
|
endif; |
227
|
|
|
endforeach; |
228
|
|
|
|
229
|
5 |
|
return $count; |
230
|
|
|
endif; |
231
|
|
|
} |
232
|
|
|
|
233
|
1 |
|
public function countInvalidVoteWithConstraints () : int |
234
|
|
|
{ |
235
|
1 |
|
$count = 0; |
236
|
|
|
|
237
|
1 |
|
foreach ($this as $oneVote) : |
238
|
1 |
|
if(!$this->getElection()->testIfVoteIsValidUnderElectionConstraints($oneVote)) : |
239
|
1 |
|
$count++; |
240
|
|
|
endif; |
241
|
|
|
endforeach; |
242
|
|
|
|
243
|
1 |
|
return $count; |
244
|
|
|
} |
245
|
|
|
|
246
|
7 |
|
public function sumVotesWeight (bool $constraint = false) : int |
247
|
|
|
{ |
248
|
7 |
|
$sum = 0; |
249
|
|
|
|
250
|
7 |
|
foreach ($this as $oneVote) : |
251
|
7 |
|
if ( !$constraint || $this->getElection()->testIfVoteIsValidUnderElectionConstraints($oneVote) ) : |
252
|
7 |
|
$sum += ($this->getElection()->isVoteWeightIsAllowed()) ? $oneVote->getWeight() : 1; |
253
|
|
|
endif; |
254
|
|
|
endforeach; |
255
|
|
|
|
256
|
7 |
|
return $sum; |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|