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 |
|
$activeCandidates = []; |
81
|
|
|
|
82
|
1 |
|
foreach ($this->candidates as $candidateId => $candidate) |
83
|
|
|
{ |
84
|
1 |
|
if ($candidate->getState() == Candidate::RUNNING) |
85
|
|
|
{ |
86
|
1 |
|
$activeCandidates[$candidateId] = $candidate; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
1 |
|
return $activeCandidates; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get a count of candidates still running (not elected or defeated) |
95
|
|
|
* |
96
|
|
|
* @return int |
97
|
|
|
*/ |
98
|
1 |
|
public function getActiveCandidateCount(): int |
99
|
|
|
{ |
100
|
1 |
|
return count($this->getActiveCandidates()); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Gets the value of winnersCount. |
105
|
|
|
* |
106
|
|
|
* @return int |
107
|
|
|
*/ |
108
|
2 |
|
public function getWinnersCount(): int |
109
|
|
|
{ |
110
|
2 |
|
return $this->winnersCount; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Gets the value of candidates. |
115
|
|
|
* |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
|
|
public function getCandidates(): array |
119
|
|
|
{ |
120
|
|
|
return $this->candidates; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Gets the value of ballots. |
125
|
|
|
* |
126
|
|
|
* @return array |
127
|
|
|
*/ |
128
|
1 |
|
public function getBallots(): array |
129
|
|
|
{ |
130
|
1 |
|
return $this->ballots; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get the number of ballots |
135
|
|
|
* |
136
|
|
|
* @return int |
137
|
|
|
*/ |
138
|
2 |
|
public function getNumBallots(): int |
139
|
|
|
{ |
140
|
2 |
|
return count($this->ballots); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|