1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Michaelc\Voting\STV; |
4
|
|
|
|
5
|
|
|
class Ballot |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Ranking of candidates ids. |
9
|
|
|
* |
10
|
|
|
* @var int[] |
11
|
|
|
*/ |
12
|
|
|
protected $ranking; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The current weighting or value of this person's vote. |
16
|
|
|
* |
17
|
|
|
* @var float |
18
|
|
|
*/ |
19
|
|
|
protected $weight; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The current preference in use from this ballot. |
23
|
|
|
* |
24
|
|
|
* @var int |
25
|
|
|
*/ |
26
|
|
|
protected $levelUsed; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Constructor. |
30
|
|
|
* |
31
|
|
|
* @param int[] $ranking The ranking of candidates Key being ranking, |
32
|
|
|
* value being a candidate id. Zero-indexed (Key |
33
|
|
|
* 0 for first choice) |
34
|
|
|
*/ |
35
|
11 |
|
public function __construct(array $ranking) |
36
|
|
|
{ |
37
|
11 |
|
$this->weight = 1.0; |
38
|
11 |
|
$this->ranking = $ranking; |
39
|
11 |
|
$this->levelUsed = -1; |
40
|
11 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Gets the Ranking of candidates ids. |
44
|
|
|
* |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
4 |
|
public function getRanking(): array |
48
|
|
|
{ |
49
|
4 |
|
return $this->ranking; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Gets the The current weighting or value of this person's vote. |
54
|
|
|
* |
55
|
|
|
* @return float |
56
|
|
|
*/ |
57
|
3 |
|
public function getWeight(): float |
58
|
|
|
{ |
59
|
3 |
|
return $this->weight; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Sets the The current weighting or value of this person's vote. |
64
|
|
|
* |
65
|
|
|
* @param float $weight The weight |
66
|
|
|
* |
67
|
|
|
* @return float $weight The inputted weight |
68
|
|
|
*/ |
69
|
2 |
|
public function setWeight(float $weight): float |
70
|
|
|
{ |
71
|
2 |
|
$this->weight = round($weight, 5); |
72
|
|
|
|
73
|
2 |
|
return $weight; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Gets the the current preference in use from this ballot. |
78
|
|
|
* |
79
|
|
|
* @return int |
80
|
|
|
*/ |
81
|
2 |
|
public function getLevelUsed(): int |
82
|
|
|
{ |
83
|
2 |
|
return $this->levelUsed; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Sets the the current preference in use from this ballot. |
88
|
|
|
* |
89
|
|
|
* @return int |
90
|
|
|
*/ |
91
|
3 |
|
public function incrementLevelUsed(): int |
92
|
|
|
{ |
93
|
3 |
|
++$this->levelUsed; |
94
|
|
|
|
95
|
3 |
|
return $this->levelUsed; |
96
|
|
|
} |
97
|
|
|
|
98
|
2 |
|
public function getLastChoice() |
99
|
|
|
{ |
100
|
2 |
|
$level = $this->levelUsed; |
101
|
|
|
|
102
|
2 |
|
if (empty($this->ranking[$level])) { |
103
|
1 |
|
return; |
104
|
|
|
} |
105
|
|
|
|
106
|
2 |
|
return $this->ranking[$level]; |
107
|
|
|
} |
108
|
|
|
|
109
|
2 |
|
public function getNextChoice() |
110
|
|
|
{ |
111
|
2 |
|
$level = $this->levelUsed + 1; |
112
|
|
|
|
113
|
2 |
|
if (empty($this->ranking[$level])) { |
114
|
2 |
|
return; |
115
|
|
|
} |
116
|
|
|
|
117
|
2 |
|
return $this->ranking[$level]; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|