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 | /** |
||
56 | * Constructor. |
||
57 | * |
||
58 | * @param Election $election |
||
59 | */ |
||
60 | 2 | public function __construct(Election $election, Logger $logger) |
|
69 | |||
70 | /** |
||
71 | * Run the election. |
||
72 | * |
||
73 | * @return \MichaelC\Voting\STV\Candidate[] Winning candidates |
||
74 | */ |
||
75 | public function run() |
||
99 | |||
100 | /** |
||
101 | * Perform the initial vote allocation. |
||
102 | * |
||
103 | * @return |
||
104 | */ |
||
105 | protected function firstStep() |
||
121 | |||
122 | /** |
||
123 | * Check if any candidates have reached the quota and can be elected. |
||
124 | * |
||
125 | * @param array $candidates Array of active candidates to check |
||
126 | * |
||
127 | * @return bool Whether any candidates were changed to elected |
||
128 | */ |
||
129 | protected function checkCandidates(array $candidates): bool |
||
160 | |||
161 | /** |
||
162 | * Allocate the next votes from a Ballot. |
||
163 | * |
||
164 | * @param Ballot $ballot The ballot to allocate the votes from |
||
165 | * @param float $multiplier Number to multiply the weight by (surplus) |
||
166 | * @param float $divisor The divisor of the weight (Total number of |
||
167 | * candidate votes) |
||
168 | * |
||
169 | * @return Ballot The same ballot passed in modified |
||
170 | */ |
||
171 | protected function allocateVotes(Ballot $ballot, float $multiplier = 1.0, float $divisor = 1.0): Ballot |
||
198 | |||
199 | /** |
||
200 | * Transfer the votes from one candidate to other candidates. |
||
201 | * |
||
202 | * @param float $surplus The number of surplus votes to transfer |
||
203 | * @param Candidate $candidate The candidate being elected to transfer |
||
204 | * the votes from |
||
205 | * |
||
206 | * @return |
||
207 | */ |
||
208 | protected function transferSurplusVotes(float $surplus, Candidate $candidate) |
||
223 | |||
224 | /** |
||
225 | * Transfer the votes from one eliminated candidate to other candidates. |
||
226 | * |
||
227 | * @param Candidate $candidate Candidate being eliminated to transfer |
||
228 | * the votes from |
||
229 | * |
||
230 | * @return |
||
231 | */ |
||
232 | protected function transferEliminatedVotes(Candidate $candidate) |
||
248 | |||
249 | /** |
||
250 | * Elect a candidate after they've passed the threshold. |
||
251 | * |
||
252 | * @param \Michaelc\Voting\STV\Candidate $candidate |
||
253 | */ |
||
254 | protected function electCandidate(Candidate $candidate) |
||
277 | |||
278 | /** |
||
279 | * Eliminate the candidate with the lowest number of votes |
||
280 | * and reallocated their votes. |
||
281 | * |
||
282 | * TODO: Eliminate all lowest candidates after step one, then |
||
283 | * randomly choose. |
||
284 | * |
||
285 | * @param \Michaelc\Voting\STV\Candidate[] $candidates |
||
286 | * Array of active candidates |
||
287 | * |
||
288 | * @return int Number of candidates eliminated |
||
289 | */ |
||
290 | protected function eliminateCandidates(array $candidates): int |
||
303 | |||
304 | /** |
||
305 | * Get candidates with the lowest number of votes. |
||
306 | * |
||
307 | * @param \Michaelc\Voting\STV\Candidate[] $candidates |
||
308 | * Array of active candidates |
||
309 | * |
||
310 | * @return \Michaelc\Voting\STV\Candidate[] |
||
311 | * Candidates with lowest score |
||
312 | */ |
||
313 | public function getLowestCandidates(array $candidates): array |
||
334 | |||
335 | /** |
||
336 | * Reject any invalid ballots. |
||
337 | * |
||
338 | * @return int Number of rejected ballots |
||
339 | */ |
||
340 | protected function rejectInvalidBallots(): int |
||
357 | |||
358 | /** |
||
359 | * Check if ballot is valid. |
||
360 | * |
||
361 | * @param Ballot $ballot Ballot to test |
||
362 | * |
||
363 | * @return bool True if valid, false if invalid |
||
364 | */ |
||
365 | 1 | public function checkBallotValidity(Ballot $ballot): bool |
|
389 | |||
390 | /** |
||
391 | * Get the quota to win. |
||
392 | * |
||
393 | * @return int |
||
394 | */ |
||
395 | public function getQuota(): int |
||
407 | } |
||
408 |