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