@@ -6,99 +6,99 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Election |
| 8 | 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 | - public function __construct(int $winnersCount, array $candidates, array $ballots) |
|
| 45 | - { |
|
| 46 | - $this->winnersCount = $winnersCount; |
|
| 47 | - $this->candidates = $candidates; |
|
| 48 | - $this->ballots = $ballots; |
|
| 49 | - $this->candidateCount = count($candidates); |
|
| 50 | - } |
|
| 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 | - public function getCandidate(int $id): Candidate |
|
| 59 | - { |
|
| 60 | - return $this->candidates[$id]; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * Get a count of candidates competing |
|
| 65 | - * |
|
| 66 | - * @return int |
|
| 67 | - */ |
|
| 68 | - public function getCandidateCount(): int |
|
| 69 | - { |
|
| 70 | - 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 | - public function getActiveCandidates(): array |
|
| 79 | - { |
|
| 80 | - $activeCandidates = []; |
|
| 81 | - |
|
| 82 | - foreach ($this->candidates as $candidateId => $candidate) |
|
| 83 | - { |
|
| 84 | - if ($candidate->getState() == Candidate::RUNNING) |
|
| 85 | - { |
|
| 86 | - $activeCandidates[$candidateId] = $candidate; |
|
| 87 | - } |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - return $activeCandidates; |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - /** |
|
| 94 | - * Get a count of candidates still running (not elected or defeated) |
|
| 95 | - * |
|
| 96 | - * @return int |
|
| 97 | - */ |
|
| 98 | - public function getActiveCandidateCount(): int |
|
| 99 | - { |
|
| 100 | - return count($this->getActiveCandidates()); |
|
| 101 | - } |
|
| 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 | + public function __construct(int $winnersCount, array $candidates, array $ballots) |
|
| 45 | + { |
|
| 46 | + $this->winnersCount = $winnersCount; |
|
| 47 | + $this->candidates = $candidates; |
|
| 48 | + $this->ballots = $ballots; |
|
| 49 | + $this->candidateCount = count($candidates); |
|
| 50 | + } |
|
| 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 | + public function getCandidate(int $id): Candidate |
|
| 59 | + { |
|
| 60 | + return $this->candidates[$id]; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * Get a count of candidates competing |
|
| 65 | + * |
|
| 66 | + * @return int |
|
| 67 | + */ |
|
| 68 | + public function getCandidateCount(): int |
|
| 69 | + { |
|
| 70 | + 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 | + public function getActiveCandidates(): array |
|
| 79 | + { |
|
| 80 | + $activeCandidates = []; |
|
| 81 | + |
|
| 82 | + foreach ($this->candidates as $candidateId => $candidate) |
|
| 83 | + { |
|
| 84 | + if ($candidate->getState() == Candidate::RUNNING) |
|
| 85 | + { |
|
| 86 | + $activeCandidates[$candidateId] = $candidate; |
|
| 87 | + } |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + return $activeCandidates; |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + /** |
|
| 94 | + * Get a count of candidates still running (not elected or defeated) |
|
| 95 | + * |
|
| 96 | + * @return int |
|
| 97 | + */ |
|
| 98 | + public function getActiveCandidateCount(): int |
|
| 99 | + { |
|
| 100 | + return count($this->getActiveCandidates()); |
|
| 101 | + } |
|
| 102 | 102 | |
| 103 | 103 | /** |
| 104 | 104 | * Gets the value of winnersCount. |
@@ -4,48 +4,48 @@ |
||
| 4 | 4 | |
| 5 | 5 | class Candidate |
| 6 | 6 | { |
| 7 | - const ELECTED = 1; |
|
| 8 | - const RUNNING = 2; |
|
| 9 | - const DEFEATED = 3; |
|
| 10 | - |
|
| 11 | - /** |
|
| 12 | - * Identifier for the candidate |
|
| 13 | - * |
|
| 14 | - * @var integer |
|
| 15 | - */ |
|
| 16 | - protected $id; |
|
| 17 | - |
|
| 18 | - /** |
|
| 19 | - * Number of votes the candidate currently has |
|
| 20 | - * |
|
| 21 | - * @var float |
|
| 22 | - */ |
|
| 23 | - protected $votes; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * Number of surplus votes the candidate has to be re-allocated |
|
| 27 | - * |
|
| 28 | - * @var float |
|
| 29 | - */ |
|
| 30 | - protected $surplus; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * State of the candidate (use class constants) |
|
| 34 | - * |
|
| 35 | - * @var integer |
|
| 36 | - */ |
|
| 37 | - protected $state; |
|
| 38 | - |
|
| 39 | - /** |
|
| 40 | - * Constructor |
|
| 41 | - */ |
|
| 42 | - public function __construct(int $id) |
|
| 43 | - { |
|
| 44 | - $this->id = $id; |
|
| 45 | - $this->votes = 0.0; |
|
| 46 | - $this->surplus = 0.0; |
|
| 47 | - $this->state = self::RUNNING; |
|
| 48 | - } |
|
| 7 | + const ELECTED = 1; |
|
| 8 | + const RUNNING = 2; |
|
| 9 | + const DEFEATED = 3; |
|
| 10 | + |
|
| 11 | + /** |
|
| 12 | + * Identifier for the candidate |
|
| 13 | + * |
|
| 14 | + * @var integer |
|
| 15 | + */ |
|
| 16 | + protected $id; |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * Number of votes the candidate currently has |
|
| 20 | + * |
|
| 21 | + * @var float |
|
| 22 | + */ |
|
| 23 | + protected $votes; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * Number of surplus votes the candidate has to be re-allocated |
|
| 27 | + * |
|
| 28 | + * @var float |
|
| 29 | + */ |
|
| 30 | + protected $surplus; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * State of the candidate (use class constants) |
|
| 34 | + * |
|
| 35 | + * @var integer |
|
| 36 | + */ |
|
| 37 | + protected $state; |
|
| 38 | + |
|
| 39 | + /** |
|
| 40 | + * Constructor |
|
| 41 | + */ |
|
| 42 | + public function __construct(int $id) |
|
| 43 | + { |
|
| 44 | + $this->id = $id; |
|
| 45 | + $this->votes = 0.0; |
|
| 46 | + $this->surplus = 0.0; |
|
| 47 | + $this->state = self::RUNNING; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | 50 | /** |
| 51 | 51 | * Gets the Identifier for the candidate. |