1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Michaelc\Voting\STV; |
4
|
|
|
|
5
|
|
|
use Michaelc\Voting\STV\Candidate; |
6
|
|
|
|
7
|
|
|
class Election |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Count of candidates in election |
11
|
|
|
* |
12
|
|
|
* @var int |
13
|
|
|
*/ |
14
|
|
|
protected $candidateCount; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Count of number of seats/winners |
18
|
|
|
* |
19
|
|
|
* @var int |
20
|
|
|
*/ |
21
|
|
|
protected $winnersCount; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Array of candidates competing in election |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $candidates; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Array of ballots cast in election |
32
|
|
|
* |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $ballots; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Constructor |
39
|
|
|
* |
40
|
|
|
* @param int $winnersCount Number of winners to allocate |
41
|
|
|
* @param array $candidates Array of candidates competing |
42
|
|
|
* @param array $ballots Array of all ballots cast in election |
43
|
|
|
*/ |
44
|
2 |
|
public function __construct(int $winnersCount, array $candidates, array $ballots) |
45
|
|
|
{ |
46
|
2 |
|
$this->winnersCount = $winnersCount; |
47
|
2 |
|
$this->candidates = $candidates; |
48
|
2 |
|
$this->ballots = $ballots; |
49
|
2 |
|
$this->candidateCount = count($candidates); |
50
|
2 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get a specific candidate object by their ID |
54
|
|
|
* |
55
|
|
|
* @param int $id ID of the candidate to get |
56
|
|
|
* @return \Michaelc\Voting\STV\Candidate |
57
|
|
|
*/ |
58
|
1 |
|
public function getCandidate(int $id): Candidate |
59
|
|
|
{ |
60
|
1 |
|
return $this->candidates[$id]; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get a count of candidates competing |
65
|
|
|
* |
66
|
|
|
* @return int |
67
|
|
|
*/ |
68
|
1 |
|
public function getCandidateCount(): int |
69
|
|
|
{ |
70
|
1 |
|
return $this->candidateCount; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get an array of candidates still running (not elected or defeated) |
75
|
|
|
* |
76
|
|
|
* @return \Michaelc\Voting\STV\Candidate[] |
77
|
|
|
*/ |
78
|
1 |
|
public function getActiveCandidates(): array |
79
|
|
|
{ |
80
|
1 |
|
return $this->getStateCandidates(Candidate::RUNNING); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get an array of candidates elected |
85
|
|
|
* |
86
|
|
|
* @return \Michaelc\Voting\STV\Candidate[] |
87
|
|
|
*/ |
88
|
|
|
public function getElectedCandidates(): array |
89
|
|
|
{ |
90
|
|
|
return $this->getStateCandidates(Candidate::ELECTED); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get an array of candidates defeated |
95
|
|
|
* |
96
|
|
|
* @return \Michaelc\Voting\STV\Candidate[] |
97
|
|
|
*/ |
98
|
|
|
public function getDefeatedCandidates(): array |
99
|
|
|
{ |
100
|
|
|
return $this->getStateCandidates(Candidate::DEFEATED); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get all candidates of a specific state |
105
|
|
|
* |
106
|
|
|
* @param int $state A candidate state (See Candidate constants) |
107
|
|
|
* @return \Michaelc\Voting\STV\Candidate[] |
108
|
|
|
*/ |
109
|
1 |
|
public function getStateCandidates(int $state): array |
110
|
|
|
{ |
111
|
1 |
|
$candidates = []; |
112
|
|
|
|
113
|
1 |
|
foreach ($this->candidates as $candidateId => $candidate) |
114
|
|
|
{ |
115
|
1 |
|
if ($candidate->getState() == $state) |
116
|
|
|
{ |
117
|
1 |
|
$candidates[$candidateId] = $candidate; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
1 |
|
return $candidates; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get a count of candidates still running (not elected or defeated) |
126
|
|
|
* |
127
|
|
|
* @return int |
128
|
|
|
*/ |
129
|
1 |
|
public function getActiveCandidateCount(): int |
130
|
|
|
{ |
131
|
1 |
|
return count($this->getActiveCandidates()); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get an array of candidate ids |
136
|
|
|
* |
137
|
|
|
* @return int[] |
138
|
|
|
*/ |
139
|
|
|
public function getCandidateIds(): array |
140
|
|
|
{ |
141
|
|
|
$candidateIds = []; |
142
|
|
|
|
143
|
|
|
foreach ($this->candidates as $i => $candidate) |
144
|
|
|
{ |
145
|
|
|
$candidateIds[] = $candidate->getId(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
return $candidateIds; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Gets the value of winnersCount. |
153
|
|
|
* |
154
|
|
|
* @return int |
155
|
|
|
*/ |
156
|
1 |
|
public function getWinnersCount(): int |
157
|
|
|
{ |
158
|
1 |
|
return $this->winnersCount; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Gets the value of candidates. |
163
|
|
|
* |
164
|
|
|
* @return array |
165
|
|
|
*/ |
166
|
|
|
public function getCandidates(): array |
167
|
|
|
{ |
168
|
|
|
return $this->candidates; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Gets the value of ballots. |
173
|
|
|
* |
174
|
|
|
* @return array |
175
|
|
|
*/ |
176
|
1 |
|
public function getBallots(): array |
177
|
|
|
{ |
178
|
1 |
|
return $this->ballots; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Get the number of ballots |
183
|
|
|
* |
184
|
|
|
* @return int |
185
|
|
|
*/ |
186
|
1 |
|
public function getNumBallots(): int |
187
|
|
|
{ |
188
|
1 |
|
return count($this->ballots); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Get status of all candidates |
193
|
|
|
* |
194
|
|
|
* @return array |
195
|
|
|
*/ |
196
|
|
|
public function getCandidatesStatus(): array |
197
|
|
|
{ |
198
|
|
|
$candidates = []; |
199
|
|
|
|
200
|
|
|
foreach ($this->getElectedCandidates() as $i => $candidate) |
201
|
|
|
{ |
202
|
|
|
$candidates['elected'][] = $candidate->getId(); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
foreach ($this->getActiveCandidates() as $i => $candidate) |
206
|
|
|
{ |
207
|
|
|
$candidates['active'][] = [$candidate->getId(), $candidate->getVotes()]; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
foreach ($this->getDefeatedCandidates() as $i => $candidate) |
211
|
|
|
{ |
212
|
|
|
$candidates['defeated'][] = $candidate->getId(); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
return $candidates; |
216
|
|
|
} |
217
|
|
|
} |
218
|
|
|
|