1 | <?php |
||
7 | class VoteHandler |
||
8 | { |
||
9 | /** |
||
10 | * Election object. |
||
11 | * |
||
12 | * @var \Michaelc\Voting\STV\Election; |
||
13 | */ |
||
14 | protected $election; |
||
15 | |||
16 | /** |
||
17 | * Array of all ballots in election. |
||
18 | * |
||
19 | * @var \MichaelC\Voting\STV\Ballot[] |
||
20 | */ |
||
21 | protected $ballots; |
||
22 | |||
23 | /** |
||
24 | * Quota of votes needed for a candidate to be elected. |
||
25 | * |
||
26 | * @var int |
||
27 | */ |
||
28 | protected $quota; |
||
29 | |||
30 | /** |
||
31 | * Number of candidates elected so far. |
||
32 | * |
||
33 | * @var int |
||
34 | */ |
||
35 | protected $electedCandidates; |
||
36 | |||
37 | /** |
||
38 | * Invalid ballots. |
||
39 | * |
||
40 | * @var \MichaelC\Voting\STV\Ballot[] |
||
41 | */ |
||
42 | protected $rejectedBallots; |
||
43 | |||
44 | /** |
||
45 | * Logger. |
||
46 | * |
||
47 | * @var \Psr\Log\LoggerInterface |
||
48 | */ |
||
49 | protected $logger; |
||
50 | |||
51 | /** |
||
52 | * Constructor. |
||
53 | * |
||
54 | * @param Election $election |
||
55 | */ |
||
56 | 2 | public function __construct(Election $election, Logger $logger) |
|
63 | |||
64 | /** |
||
65 | * Run the election. |
||
66 | * |
||
67 | * @return \MichaelC\Voting\STV\Candidate[] Winning candidates |
||
68 | */ |
||
69 | public function run() |
||
92 | |||
93 | /** |
||
94 | * Perform the initial vote allocation. |
||
95 | * |
||
96 | * @return |
||
97 | */ |
||
98 | protected function firstStep() |
||
116 | |||
117 | /** |
||
118 | * Check if any candidates have reached the quota and can be elected. |
||
119 | * |
||
120 | * @param array $candidates Array of active candidates to check |
||
121 | * |
||
122 | * @return bool Whether any candidates were changed to elected |
||
123 | */ |
||
124 | protected function checkCandidates(array $candidates): bool |
||
148 | |||
149 | /** |
||
150 | * Allocate the next votes from a Ballot. |
||
151 | * |
||
152 | * @param Ballot $ballot The ballot to allocate the votes from |
||
153 | * @param float $multiplier Number to multiply the weight by (surplus) |
||
154 | * @param float $divisor The divisor of the weight (Total number of |
||
155 | * candidate votes) |
||
156 | * |
||
157 | * @return Ballot The same ballot passed in modified |
||
158 | */ |
||
159 | protected function allocateVotes(Ballot $ballot, float $multiplier = 1.0, float $divisor = 1.0): Ballot |
||
178 | |||
179 | /** |
||
180 | * Transfer the votes from one candidate to other candidates. |
||
181 | * |
||
182 | * @param float $surplus The number of surplus votes to transfer |
||
183 | * @param Candidate $candidate The candidate being elected to transfer |
||
184 | * the votes from |
||
185 | * |
||
186 | * @return |
||
187 | */ |
||
188 | protected function transferSurplusVotes(float $surplus, Candidate $candidate) |
||
207 | |||
208 | /** |
||
209 | * Transfer the votes from one eliminated candidate to other candidates. |
||
210 | * |
||
211 | * @param Candidate $candidate Candidate being eliminated to transfer |
||
212 | * the votes from |
||
213 | * |
||
214 | * @return |
||
215 | */ |
||
216 | protected function transferEliminatedVotes(Candidate $candidate) |
||
233 | |||
234 | /** |
||
235 | * Elect a candidate after they've passed the threshold. |
||
236 | * |
||
237 | * @param \Michaelc\Voting\STV\Candidate $candidate |
||
238 | */ |
||
239 | protected function electCandidate(Candidate $candidate) |
||
259 | |||
260 | /** |
||
261 | * Eliminate the candidate with the lowest number of votes |
||
262 | * and reallocated their votes. |
||
263 | * |
||
264 | * TODO: Eliminate all lowest candidates after step one, then |
||
265 | * randomly choose. |
||
266 | * |
||
267 | * @param \Michaelc\Voting\STV\Candidate[] $candidates |
||
268 | * Array of active candidates |
||
269 | * |
||
270 | * @return int Number of candidates eliminated |
||
271 | */ |
||
272 | protected function eliminateCandidates(array $candidates): int |
||
287 | |||
288 | /** |
||
289 | * Get candidates with the lowest number of votes. |
||
290 | * |
||
291 | * @param \Michaelc\Voting\STV\Candidate[] $candidates |
||
292 | * Array of active candidates |
||
293 | * |
||
294 | * @return \Michaelc\Voting\STV\Candidate[] |
||
295 | * Candidates with lowest score |
||
296 | */ |
||
297 | public function getLowestCandidates(array $candidates): array |
||
318 | |||
319 | /** |
||
320 | * Reject any invalid ballots. |
||
321 | * |
||
322 | * @return int Number of rejected ballots |
||
323 | */ |
||
324 | protected function rejectInvalidBallots(): int |
||
341 | |||
342 | /** |
||
343 | * Check if ballot is valid. |
||
344 | * |
||
345 | * @param Ballot $ballot Ballot to test |
||
346 | * |
||
347 | * @return bool True if valid, false if invalid |
||
348 | */ |
||
349 | 1 | public function checkBallotValidity(Ballot $ballot): bool |
|
375 | |||
376 | /** |
||
377 | * Get the quota to win. |
||
378 | * |
||
379 | * @return int |
||
380 | */ |
||
381 | public function getQuota(): int |
||
393 | } |
||
394 |