1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Michaelc\Voting\STV; |
4
|
|
|
|
5
|
|
|
use Michaelc\Voting\STV\{Election, Ballot, Candidate}; |
|
|
|
|
6
|
|
|
|
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
|
|
|
* Constructor |
32
|
|
|
* |
33
|
|
|
* @param Election $election |
34
|
|
|
*/ |
35
|
1 |
|
public function __construct(Election $election) |
36
|
|
|
{ |
37
|
1 |
|
$this->election = $election; |
38
|
1 |
|
$this->ballots = $this->election->getBallots(); |
39
|
1 |
|
$this->quota = $this->getQuota(); |
40
|
1 |
|
} |
41
|
|
|
|
42
|
|
|
public function step($step) |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
foreach ($this->ballots as $i => $ballot) |
45
|
|
|
{ |
46
|
|
|
$this->allocateVotes($ballot); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$candidates = $election->getActiveCandidates(); |
|
|
|
|
50
|
|
|
|
51
|
|
|
foreach ($candidates as $i => $candidate) |
52
|
|
|
{ |
53
|
|
|
if ($candidate->getVotes() >= $this->quota) |
54
|
|
|
{ |
55
|
|
|
$this->electCandidate($candidate); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
protected function allocateVotes(&$ballot): Ballot |
61
|
|
|
{ |
62
|
|
|
$weight = $ballot->getWeight(); |
63
|
|
|
$candidate = $ballot->getNextPreference(); |
64
|
|
|
$this->election->getCandidate($candidate->getId())->addVotes($weight); |
65
|
|
|
$ballot->setLastUsedLevel(($step - 1)); |
|
|
|
|
66
|
|
|
|
67
|
|
|
return $ballot; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected function transferVotes(float $votes, Candidate $candidate) |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
return; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Elect a candidate after they've passed the threshold |
77
|
|
|
* |
78
|
|
|
* @param \Michaelc\Voting\STV\Candidate $candidate |
79
|
|
|
* @return null |
80
|
|
|
*/ |
81
|
|
|
protected function electCandidate(Candidate $candidate) |
82
|
|
|
{ |
83
|
|
|
if ($candidate->getVotes() < $this->quota) |
84
|
|
|
{ |
85
|
|
|
throw new Exception("We shouldn't be electing someone who hasn't met the quota"); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$candidate->setState(Candidate::ELECTED); |
89
|
|
|
$surplus = $candidate->getVotes() - $quota; |
|
|
|
|
90
|
|
|
|
91
|
|
|
$this->transferVotes($surplus, $candidate); |
|
|
|
|
92
|
|
|
|
93
|
|
|
return; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get the quota to win |
98
|
|
|
* |
99
|
|
|
* @return int |
100
|
|
|
*/ |
101
|
1 |
|
public function getQuota(): int |
102
|
|
|
{ |
103
|
1 |
|
return floor( |
104
|
1 |
|
($this->election->getNumBallots() / |
105
|
1 |
|
($this->election->getWinnersCount() + 1) |
106
|
|
|
) |
107
|
1 |
|
+ 1); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: