1 | <?php |
||
9 | class VoteHandler |
||
10 | { |
||
11 | /** |
||
12 | * Election object. |
||
13 | * |
||
14 | * @var \Michaelc\Voting\STV\Election; |
||
15 | */ |
||
16 | protected $election; |
||
17 | |||
18 | /** |
||
19 | * Array of all ballots in election. |
||
20 | * |
||
21 | * @var \MichaelC\Voting\STV\Ballot[] |
||
22 | */ |
||
23 | protected $ballots; |
||
24 | |||
25 | /** |
||
26 | * Quota of votes needed for a candidate to be elected. |
||
27 | * |
||
28 | * @var int |
||
29 | */ |
||
30 | protected $quota; |
||
31 | |||
32 | /** |
||
33 | * Number of candidates elected so far. |
||
34 | * |
||
35 | * @var int |
||
36 | */ |
||
37 | protected $electedCandidates; |
||
38 | |||
39 | /** |
||
40 | * Invalid ballots. |
||
41 | * |
||
42 | * @var \MichaelC\Voting\STV\Ballot[] |
||
43 | */ |
||
44 | protected $rejectedBallots; |
||
45 | |||
46 | /** |
||
47 | * Logger. |
||
48 | * |
||
49 | * @var \Psr\Log\LoggerInterface |
||
50 | */ |
||
51 | protected $logger; |
||
52 | |||
53 | protected $validBallots; |
||
54 | |||
55 | protected $candidatesToElect; |
||
56 | |||
57 | /** |
||
58 | * Constructor. |
||
59 | * |
||
60 | * @param Election $election |
||
61 | */ |
||
62 | 2 | public function __construct(Election $election, Logger $logger) |
|
72 | |||
73 | /** |
||
74 | * Run the election. |
||
75 | * |
||
76 | * @return \MichaelC\Voting\STV\Candidate[] Winning candidates |
||
77 | */ |
||
78 | 1 | public function run() |
|
112 | |||
113 | /** |
||
114 | * Perform the initial vote allocation. |
||
115 | * |
||
116 | * @return |
||
117 | */ |
||
118 | 1 | protected function firstStep() |
|
134 | |||
135 | /** |
||
136 | * Check if any candidates have reached the quota and can be elected. |
||
137 | * |
||
138 | * @param array $candidates Array of active candidates to check |
||
139 | * |
||
140 | * @return bool Whether any candidates were changed to elected |
||
141 | */ |
||
142 | 1 | protected function checkCandidates(array $candidates): bool |
|
173 | |||
174 | /** |
||
175 | * Allocate the next votes from a Ballot. |
||
176 | * |
||
177 | * @param Ballot $ballot The ballot to allocate the votes from |
||
178 | * @param float $multiplier Number to multiply the weight by (surplus) |
||
179 | * @param float $divisor The divisor of the weight (Total number of |
||
180 | * candidate votes) |
||
181 | * |
||
182 | * @return Ballot The same ballot passed in modified |
||
183 | */ |
||
184 | 1 | protected function allocateVotes(Ballot $ballot, float $multiplier = 1.0, float $divisor = 1.0): Ballot |
|
211 | |||
212 | /** |
||
213 | * Transfer the votes from one candidate to other candidates. |
||
214 | * |
||
215 | * @param float $surplus The number of surplus votes to transfer |
||
216 | * @param Candidate $candidate The candidate being elected to transfer |
||
217 | * the votes from |
||
218 | * |
||
219 | * @return |
||
220 | */ |
||
221 | 1 | protected function transferSurplusVotes(float $surplus, Candidate $candidate) |
|
236 | |||
237 | /** |
||
238 | * Transfer the votes from one eliminated candidate to other candidates. |
||
239 | * |
||
240 | * @param Candidate $candidate Candidate being eliminated to transfer |
||
241 | * the votes from |
||
242 | * |
||
243 | * @return |
||
244 | */ |
||
245 | 1 | protected function transferEliminatedVotes(Candidate $candidate) |
|
261 | |||
262 | /** |
||
263 | * Elect a candidate after they've passed the threshold. |
||
264 | * |
||
265 | * @param \Michaelc\Voting\STV\Candidate $candidate |
||
266 | */ |
||
267 | 1 | protected function electCandidate(Candidate $candidate) |
|
287 | |||
288 | /** |
||
289 | * Eliminate the candidate with the lowest number of votes |
||
290 | * and reallocated their votes. |
||
291 | * |
||
292 | * @param \Michaelc\Voting\STV\Candidate[] $candidates Array of active candidates |
||
293 | * |
||
294 | * @return int Number of candidates eliminated |
||
295 | */ |
||
296 | 1 | protected function eliminateCandidates(array $candidates): int |
|
311 | |||
312 | /** |
||
313 | * Get candidates with the lowest number of votes. |
||
314 | * |
||
315 | * @param \Michaelc\Voting\STV\Candidate[] $candidates |
||
316 | * Array of active candidates |
||
317 | * |
||
318 | * @return \Michaelc\Voting\STV\Candidate[] |
||
319 | * Candidates with lowest score |
||
320 | */ |
||
321 | 1 | public function getLowestCandidates(array $candidates): array |
|
341 | |||
342 | /** |
||
343 | * Reject any invalid ballots. |
||
344 | * |
||
345 | * @return int Number of rejected ballots |
||
346 | */ |
||
347 | 1 | protected function rejectInvalidBallots(): int |
|
364 | |||
365 | /** |
||
366 | * Check if ballot is valid. |
||
367 | * |
||
368 | * @param Ballot $ballot Ballot to test |
||
369 | * |
||
370 | * @return bool True if valid, false if invalid |
||
371 | */ |
||
372 | 2 | public function checkBallotValidity(Ballot $ballot): bool |
|
396 | |||
397 | /** |
||
398 | * Get the quota to win. |
||
399 | * |
||
400 | * @return int |
||
401 | */ |
||
402 | 1 | public function getQuota(): int |
|
414 | } |
||
415 |