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