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
|
3 |
|
public function __construct(array $ranking) |
36
|
|
|
{ |
37
|
3 |
|
$this->weight = 1.0; |
38
|
3 |
|
$this->ranking = $ranking; |
39
|
3 |
|
$this->levelUsed = -1; |
40
|
3 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Gets the Ranking of candidates ids. |
44
|
|
|
* |
45
|
|
|
* @return array |
46
|
|
|
*/ |
47
|
1 |
|
public function getRanking(): array |
48
|
|
|
{ |
49
|
1 |
|
return $this->ranking; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Sets the Ranking of candidates ids. |
54
|
|
|
* |
55
|
|
|
* @param array $ranking the ranking |
56
|
|
|
* |
57
|
|
|
* @return self |
58
|
|
|
*/ |
59
|
|
|
public function setRanking(array $ranking) |
60
|
|
|
{ |
61
|
|
|
$this->ranking = $ranking; |
62
|
|
|
|
63
|
|
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Gets the The current weighting or value of this person's vote. |
68
|
|
|
* |
69
|
|
|
* @return float |
70
|
|
|
*/ |
71
|
1 |
|
public function getWeight(): float |
72
|
|
|
{ |
73
|
1 |
|
return $this->weight; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Sets the The current weighting or value of this person's vote. |
78
|
|
|
* |
79
|
|
|
* @param float $weight The weight |
80
|
|
|
* @return float $weight The inputted weight |
81
|
|
|
*/ |
82
|
|
|
public function setWeight(float $weight): float |
83
|
|
|
{ |
84
|
|
|
$this->weight = $weight; |
85
|
|
|
|
86
|
|
|
return $weight; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Gets the the current preference in use from this ballot. |
91
|
|
|
* |
92
|
|
|
* @return int |
93
|
|
|
*/ |
94
|
1 |
|
public function getLevelUsed(): int |
95
|
|
|
{ |
96
|
1 |
|
return $this->levelUsed; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Sets the the current preference in use from this ballot. |
101
|
|
|
* |
102
|
|
|
* @return int |
103
|
|
|
*/ |
104
|
|
|
public function incrementLevelUsed(): int |
105
|
|
|
{ |
106
|
|
|
$this->levelUsed++; |
107
|
|
|
|
108
|
|
|
return $this->levelUSed; |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function getLastChoice() |
112
|
|
|
{ |
113
|
|
|
$level = $levelUsed; |
|
|
|
|
114
|
|
|
|
115
|
|
|
if (empty($this->ranking[$level])) |
116
|
|
|
{ |
117
|
|
|
return null; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $this->ranking[$level]; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function getNextChoice() |
124
|
|
|
{ |
125
|
|
|
$level = $levelUsed + 1; |
|
|
|
|
126
|
|
|
|
127
|
|
|
if (empty($this->ranking[$level])) |
128
|
|
|
{ |
129
|
|
|
return null; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $this->ranking[$level]; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.