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 |
|
foreach ($tags as $oneTag) : |
41
|
15 |
|
if (empty($oneTag)) : |
42
|
15 |
|
throw new CondorcetException(17); |
43
|
|
|
endif; |
44
|
|
|
endforeach; |
45
|
|
|
|
46
|
15 |
|
return $tags; |
47
|
|
|
} |
48
|
|
|
|
49
|
143 |
|
public static function getRankingAsString (array $ranking) : string |
50
|
|
|
{ |
51
|
143 |
|
foreach ($ranking as &$rank) : |
52
|
140 |
|
if (is_array($rank)) : |
53
|
140 |
|
sort($rank); |
54
|
140 |
|
$rank = implode(' = ',$rank); |
55
|
|
|
endif; |
56
|
|
|
endforeach; |
57
|
|
|
|
58
|
143 |
|
return implode(' > ', $ranking); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// From a string like 'A>B=C=H>G=T>Q' |
62
|
118 |
|
public static function convertVoteInput (string $formula) : array |
63
|
|
|
{ |
64
|
118 |
|
$ranking = explode('>', $formula); |
65
|
|
|
|
66
|
118 |
|
foreach ($ranking as &$rank_vote) : |
67
|
118 |
|
$rank_vote = explode('=', $rank_vote); |
68
|
|
|
|
69
|
|
|
// Del space at start and end |
70
|
118 |
|
foreach ($rank_vote as &$value) : |
71
|
118 |
|
$value = trim($value); |
72
|
|
|
endforeach; |
73
|
|
|
endforeach; |
74
|
|
|
|
75
|
118 |
|
return $ranking; |
76
|
|
|
} |
77
|
|
|
|
78
|
89 |
|
public static function parseAnalysingOneLine ($searchCharacter, string &$line) : int |
79
|
|
|
{ |
80
|
89 |
|
if ($searchCharacter !== false) : |
81
|
76 |
|
$value = trim( substr($line, $searchCharacter + 1) ); |
82
|
|
|
|
83
|
|
|
// Errors |
84
|
76 |
|
if ( !is_numeric($value) ) : |
85
|
|
|
throw new CondorcetException(13, null); |
86
|
|
|
endif; |
87
|
|
|
|
88
|
|
|
// Reformat line |
89
|
76 |
|
$line = substr($line, 0, $searchCharacter); |
90
|
|
|
|
91
|
76 |
|
return intval($value); |
92
|
|
|
else : |
93
|
89 |
|
return 1; |
94
|
|
|
endif; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|