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\ElectionProcess; |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
use CondorcetPHP\Condorcet\CondorcetException; |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
// Base Condorcet class |
18
|
|
|
abstract class VoteUtil |
19
|
|
|
{ |
20
|
143 |
|
public static function tagsConvert ($tags) : ?array |
21
|
|
|
{ |
22
|
143 |
|
if (empty($tags)) : |
23
|
143 |
|
return null; |
24
|
|
|
endif; |
25
|
|
|
|
26
|
17 |
|
if (is_string($tags)) : |
27
|
15 |
|
$tags = explode(',', $tags); |
28
|
11 |
|
elseif (is_array($tags)) : |
29
|
9 |
|
foreach ($tags as $key => &$oneTag) : |
30
|
9 |
|
if (!is_string($oneTag)) : |
31
|
9 |
|
throw new CondorcetException(17); |
32
|
|
|
endif; |
33
|
|
|
endforeach; |
34
|
|
|
else : |
35
|
2 |
|
throw new CondorcetException(17); |
36
|
|
|
endif; |
37
|
|
|
|
38
|
15 |
|
$tags = array_map('trim', $tags); |
39
|
|
|
|
40
|
15 |
|
array_walk( |
41
|
15 |
|
$tags, |
42
|
|
|
function (string $oneTag) : void { |
43
|
15 |
|
if (empty($oneTag)) : |
44
|
1 |
|
throw new CondorcetException(17); |
45
|
|
|
endif; |
46
|
15 |
|
} |
47
|
|
|
); |
48
|
|
|
|
49
|
15 |
|
return $tags; |
50
|
|
|
} |
51
|
|
|
|
52
|
143 |
|
public static function getRankingAsString (array $ranking) : string |
53
|
|
|
{ |
54
|
143 |
|
foreach ($ranking as &$rank) : |
55
|
140 |
|
if (is_array($rank)) : |
56
|
140 |
|
sort($rank); |
57
|
140 |
|
$rank = implode(' = ',$rank); |
58
|
|
|
endif; |
59
|
|
|
endforeach; |
60
|
|
|
|
61
|
143 |
|
return implode(' > ', $ranking); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// From a string like 'A>B=C=H>G=T>Q' |
65
|
118 |
|
public static function convertVoteInput (string $formula) : array |
66
|
|
|
{ |
67
|
118 |
|
$ranking = explode('>', $formula); |
68
|
|
|
|
69
|
118 |
|
foreach ($ranking as &$rank_vote) : |
70
|
118 |
|
$rank_vote = explode('=', $rank_vote); |
71
|
|
|
|
72
|
|
|
// Del space at start and end |
73
|
118 |
|
foreach ($rank_vote as &$value) : |
74
|
118 |
|
$value = trim($value); |
75
|
|
|
endforeach; |
76
|
|
|
endforeach; |
77
|
|
|
|
78
|
118 |
|
return $ranking; |
79
|
|
|
} |
80
|
|
|
|
81
|
89 |
|
public static function parseAnalysingOneLine ($searchCharacter, string &$line) : int |
82
|
|
|
{ |
83
|
89 |
|
if ($searchCharacter !== false) : |
84
|
76 |
|
$value = trim( substr($line, $searchCharacter + 1) ); |
85
|
|
|
|
86
|
|
|
// Errors |
87
|
76 |
|
if ( !is_numeric($value) ) : |
88
|
|
|
throw new CondorcetException(13, null); |
89
|
|
|
endif; |
90
|
|
|
|
91
|
|
|
// Reformat line |
92
|
76 |
|
$line = substr($line, 0, $searchCharacter); |
93
|
|
|
|
94
|
76 |
|
return intval($value); |
95
|
|
|
else : |
96
|
89 |
|
return 1; |
97
|
|
|
endif; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|