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