1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Michaelc\Voting\STV; |
4
|
|
|
|
5
|
|
|
class Ballot |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Ranking of candidates ids |
9
|
|
|
* |
10
|
|
|
* @var integer[] |
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 integer |
25
|
|
|
*/ |
26
|
|
|
protected $levelUsed; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Constructor |
30
|
|
|
* |
31
|
|
|
* @param integer[] $ranking The ranking of candidates Key being ranking, |
32
|
|
|
* value being a candidate id |
33
|
|
|
*/ |
34
|
3 |
|
public function __construct(array $ranking) |
35
|
|
|
{ |
36
|
3 |
|
$this->weight = 1.0; |
37
|
3 |
|
$this->ranking = $ranking; |
38
|
3 |
|
$this->levelUsed = -1; |
39
|
3 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Gets the Ranking of candidates ids. |
43
|
|
|
* |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
1 |
|
public function getRanking(): array |
47
|
|
|
{ |
48
|
1 |
|
return $this->ranking; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Sets the Ranking of candidates ids. |
53
|
|
|
* |
54
|
|
|
* @param array $ranking the ranking |
55
|
|
|
* |
56
|
|
|
* @return self |
57
|
|
|
*/ |
58
|
|
|
public function setRanking(array $ranking) |
59
|
|
|
{ |
60
|
|
|
$this->ranking = $ranking; |
61
|
|
|
|
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Gets the The current weighting or value of this person's vote. |
67
|
|
|
* |
68
|
|
|
* @return float |
69
|
|
|
*/ |
70
|
1 |
|
public function getWeight(): float |
71
|
|
|
{ |
72
|
1 |
|
return $this->weight; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Sets the The current weighting or value of this person's vote. |
77
|
|
|
* |
78
|
|
|
* @param float $weight The weight |
79
|
|
|
* @return float $weight The inputted weight |
80
|
|
|
*/ |
81
|
|
|
public function setWeight(float $weight): float |
82
|
|
|
{ |
83
|
|
|
$this->weight = $weight; |
84
|
|
|
|
85
|
|
|
return $weight; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Gets the the current preference in use from this ballot. |
90
|
|
|
* |
91
|
|
|
* @return integer |
92
|
|
|
*/ |
93
|
1 |
|
public function getLevelUsed(): int |
94
|
|
|
{ |
95
|
1 |
|
return $this->levelUsed; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Sets the the current preference in use from this ballot. |
100
|
|
|
* |
101
|
|
|
* @param integer $levelUsed |
102
|
|
|
* |
103
|
|
|
* @return self |
104
|
|
|
*/ |
105
|
|
|
public function setLevelUsed(int $levelUsed) |
106
|
|
|
{ |
107
|
|
|
$this->levelUsed = $levelUsed; |
108
|
|
|
|
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function getLastChoice() |
113
|
|
|
{ |
114
|
|
|
$level = $levelUsed; |
|
|
|
|
115
|
|
|
|
116
|
|
|
if (empty($this->ranking[$level])) |
117
|
|
|
{ |
118
|
|
|
return null; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $this->ranking[$level]; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function getNextChoice() |
125
|
|
|
{ |
126
|
|
|
$level = $levelUsed + 1; |
|
|
|
|
127
|
|
|
|
128
|
|
|
if (empty($this->ranking[$level])) |
129
|
|
|
{ |
130
|
|
|
return null; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $this->ranking[$level]; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.