|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
Condorcet PHP - Election manager and results calculator. |
|
4
|
|
|
Designed for the Condorcet method. Integrating a large number of algorithms extending Condorcet. Expandable for all types of voting systems. |
|
5
|
|
|
|
|
6
|
|
|
By Julien Boudry and contributors - MIT LICENSE (Please read LICENSE.txt) |
|
7
|
|
|
https://github.com/julien-boudry/Condorcet |
|
8
|
|
|
*/ |
|
9
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace CondorcetPHP\Condorcet; |
|
12
|
|
|
|
|
13
|
|
|
use CondorcetPHP\Condorcet\ElectionProcess\VoteUtil; |
|
14
|
|
|
use CondorcetPHP\Condorcet\Throwable\CondorcetException; |
|
15
|
|
|
|
|
16
|
|
|
class Result implements \ArrayAccess, \Countable, \Iterator |
|
17
|
|
|
{ |
|
18
|
|
|
use CondorcetVersion; |
|
19
|
|
|
|
|
20
|
|
|
// Implement Iterator |
|
21
|
|
|
|
|
22
|
1 |
|
public function rewind() :void { |
|
23
|
1 |
|
reset($this->_UserResult); |
|
24
|
1 |
|
} |
|
25
|
|
|
|
|
26
|
1 |
|
public function current () { |
|
27
|
1 |
|
return current($this->_UserResult); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
1 |
|
public function key () : int { |
|
31
|
1 |
|
return key($this->_UserResult); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
1 |
|
public function next () : void { |
|
35
|
1 |
|
next($this->_UserResult); |
|
36
|
1 |
|
} |
|
37
|
|
|
|
|
38
|
1 |
|
public function valid () : bool { |
|
39
|
1 |
|
return key($this->_UserResult) !== null; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
// Implement ArrayAccess |
|
43
|
|
|
|
|
44
|
1 |
|
public function offsetSet ($offset, $value) : void { |
|
45
|
1 |
|
throw new CondorcetException (0,"Can't change a result"); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
public function offsetExists ($offset) : bool { |
|
49
|
1 |
|
return isset($this->_UserResult[$offset]); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
1 |
|
public function offsetUnset ($offset) : void { |
|
53
|
1 |
|
throw new CondorcetException (0,"Can't change a result"); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
51 |
|
public function offsetGet ($offset) { |
|
57
|
51 |
|
return $this->_UserResult[$offset] ?? null; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
// Implement Countable |
|
61
|
|
|
|
|
62
|
3 |
|
public function count () : int { |
|
63
|
3 |
|
return count($this->_UserResult); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
/////////// CONSTRUCTOR /////////// |
|
68
|
|
|
|
|
69
|
|
|
protected $_Result; |
|
70
|
|
|
protected $_UserResult; |
|
71
|
|
|
protected $_stringResult; |
|
72
|
|
|
protected $_CondorcetWinner; |
|
73
|
|
|
protected $_CondorcetLoser; |
|
74
|
|
|
|
|
75
|
|
|
protected $_Stats; |
|
76
|
|
|
|
|
77
|
|
|
protected $_warning = []; |
|
78
|
|
|
|
|
79
|
|
|
protected $_BuildTimeStamp; |
|
80
|
|
|
protected $_fromMethod; |
|
81
|
|
|
protected $_byClass; |
|
82
|
|
|
protected $_ElectionCondorcetVersion; |
|
83
|
|
|
|
|
84
|
|
|
|
|
85
|
87 |
|
public function __construct (string $fromMethod, string $byClass, Election $election, array $result, $stats) |
|
86
|
|
|
{ |
|
87
|
87 |
|
ksort($result, SORT_NUMERIC); |
|
88
|
|
|
|
|
89
|
87 |
|
$this->_Result = $result; |
|
90
|
87 |
|
$this->_UserResult = $this->makeUserResult($election); |
|
91
|
87 |
|
$this->_stringResult = $this->getResultAsArray(true); |
|
92
|
87 |
|
$this->_Stats = $stats; |
|
93
|
87 |
|
$this->_fromMethod = $fromMethod; |
|
94
|
87 |
|
$this->_byClass = $byClass; |
|
95
|
87 |
|
$this->_ElectionCondorcetVersion = $election->getObjectVersion(); |
|
96
|
87 |
|
$this->_CondorcetWinner = $election->getWinner(); |
|
97
|
87 |
|
$this->_CondorcetLoser = $election->getLoser(); |
|
98
|
87 |
|
$this->_BuildTimeStamp = microtime(true); |
|
99
|
87 |
|
} |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
|
|
/////////// Get Result /////////// |
|
103
|
|
|
|
|
104
|
87 |
|
public function getResultAsArray (bool $convertToString = false) : array |
|
105
|
|
|
{ |
|
106
|
87 |
|
$r = $this->_UserResult; |
|
107
|
|
|
|
|
108
|
87 |
|
foreach ($r as &$rank) : |
|
109
|
87 |
|
if (count($rank) === 1) : |
|
110
|
83 |
|
$rank = $convertToString ? (string) $rank[0] : $rank[0]; |
|
111
|
30 |
|
elseif ($convertToString) : |
|
112
|
30 |
|
foreach ($rank as &$subRank) : |
|
113
|
87 |
|
$subRank = (string) $subRank; |
|
114
|
|
|
endforeach; |
|
115
|
|
|
endif; |
|
116
|
|
|
endforeach; |
|
117
|
|
|
|
|
118
|
87 |
|
return $r; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
4 |
|
public function getResultAsString () : string |
|
122
|
|
|
{ |
|
123
|
4 |
|
return VoteUtil::getRankingAsString($this->getResultAsArray(true)); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
1 |
|
public function getOriginalResultArrayWithString () : array |
|
127
|
|
|
{ |
|
128
|
1 |
|
return $this->_stringResult; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
1 |
|
public function getResultAsInternalKey () : array |
|
132
|
|
|
{ |
|
133
|
1 |
|
return $this->_Result; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
19 |
|
public function getStats () { |
|
137
|
19 |
|
return $this->_Stats; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
50 |
|
public function getWinner () { |
|
141
|
50 |
|
return CondorcetUtil::format($this[1],false); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
3 |
|
public function getLoser () { |
|
145
|
3 |
|
return CondorcetUtil::format($this[count($this)],false); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
1 |
|
public function getCondorcetWinner () : ?Candidate { |
|
149
|
1 |
|
return $this->_CondorcetWinner; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
1 |
|
public function getCondorcetLoser () : ?Candidate { |
|
153
|
1 |
|
return $this->_CondorcetLoser; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
87 |
|
protected function makeUserResult (Election $election) : array |
|
157
|
|
|
{ |
|
158
|
87 |
|
$userResult = []; |
|
159
|
|
|
|
|
160
|
87 |
|
foreach ( $this->_Result as $key => $value ) : |
|
161
|
87 |
|
if (is_array($value)) : |
|
162
|
83 |
|
foreach ($value as $candidate_key) : |
|
163
|
83 |
|
$userResult[$key][] = $election->getCandidateObjectFromKey($candidate_key); |
|
164
|
|
|
endforeach; |
|
165
|
6 |
|
elseif (is_null($value)) : |
|
166
|
|
|
$userResult[$key] = null; |
|
167
|
|
|
else : |
|
168
|
87 |
|
$userResult[$key][] = $election->getCandidateObjectFromKey($value); |
|
169
|
|
|
endif; |
|
170
|
|
|
endforeach; |
|
171
|
|
|
|
|
172
|
87 |
|
foreach ( $userResult as $key => $value ) : |
|
173
|
87 |
|
if (is_null($value)) : |
|
174
|
87 |
|
$userResult[$key] = null; |
|
175
|
|
|
endif; |
|
176
|
|
|
endforeach; |
|
177
|
|
|
|
|
178
|
87 |
|
return $userResult; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
|
|
182
|
|
|
/////////// Get & Set MetaData /////////// |
|
183
|
|
|
|
|
184
|
1 |
|
public function addWarning (int $type, string $msg = null) : bool |
|
185
|
|
|
{ |
|
186
|
1 |
|
$this->_warning[] = ['type' => $type, 'msg' => $msg]; |
|
187
|
|
|
|
|
188
|
1 |
|
return true; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
2 |
|
public function getWarning (?int $type = null) : array |
|
192
|
|
|
{ |
|
193
|
2 |
|
if ($type === null) : |
|
194
|
1 |
|
return $this->_warning; |
|
195
|
|
|
else : |
|
196
|
2 |
|
$r = []; |
|
197
|
|
|
|
|
198
|
2 |
|
foreach ($this->_warning as $oneWarning) : |
|
199
|
1 |
|
if ($oneWarning['type'] === $type) : |
|
200
|
1 |
|
$r[] = $oneWarning; |
|
201
|
|
|
endif; |
|
202
|
|
|
endforeach; |
|
203
|
|
|
|
|
204
|
2 |
|
return $r; |
|
205
|
|
|
endif; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
1 |
|
public function getClassGenerator () : string { |
|
209
|
1 |
|
return $this->_byClass; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
1 |
|
public function getMethod () : string { |
|
213
|
1 |
|
return $this->_fromMethod; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
1 |
|
public function getBuildTimeStamp () : float { |
|
217
|
1 |
|
return (float) $this->_BuildTimeStamp; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
1 |
|
public function getCondorcetElectionGeneratorVersion () : string { |
|
221
|
1 |
|
return $this->_ElectionCondorcetVersion; |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
|