1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/* |
3
|
|
|
Condorcet PHP Class, with Schulze Methods and others ! |
4
|
|
|
|
5
|
|
|
By Julien Boudry - MIT LICENSE (Please read LICENSE.txt) |
6
|
|
|
https://github.com/julien-boudry/Condorcet |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Condorcet; |
11
|
|
|
|
12
|
|
|
use Condorcet\CondorcetException; |
13
|
|
|
|
14
|
|
|
// Registering native Condorcet Methods implementation |
15
|
10 |
|
Condorcet::addMethod(__NAMESPACE__.'\\Algo\\Methods\\Copeland'); |
16
|
10 |
|
Condorcet::addMethod(__NAMESPACE__.'\\Algo\\Methods\\DodgsonQuick'); |
17
|
10 |
|
Condorcet::addMethod(__NAMESPACE__.'\\Algo\\Methods\\DodgsonTidemanApproximation'); |
18
|
10 |
|
Condorcet::addMethod(__NAMESPACE__.'\\Algo\\Methods\\KemenyYoung'); |
19
|
10 |
|
Condorcet::addMethod(__NAMESPACE__.'\\Algo\\Methods\\MinimaxWinning'); |
20
|
10 |
|
Condorcet::addMethod(__NAMESPACE__.'\\Algo\\Methods\\MinimaxMargin'); |
21
|
10 |
|
Condorcet::addMethod(__NAMESPACE__.'\\Algo\\Methods\\MinimaxOpposition'); |
22
|
10 |
|
Condorcet::addMethod(__NAMESPACE__.'\\Algo\\Methods\\RankedPairsMargin'); |
23
|
10 |
|
Condorcet::addMethod(__NAMESPACE__.'\\Algo\\Methods\\RankedPairsWinning'); |
24
|
10 |
|
Condorcet::addMethod(__NAMESPACE__.'\\Algo\\Methods\\SchulzeWinning'); |
25
|
10 |
|
Condorcet::addMethod(__NAMESPACE__.'\\Algo\\Methods\\SchulzeMargin'); |
26
|
10 |
|
Condorcet::addMethod(__NAMESPACE__.'\\Algo\\Methods\\SchulzeRatio'); |
27
|
|
|
|
28
|
|
|
// Set the default Condorcet Class algorithm |
29
|
10 |
|
Condorcet::setDefaultMethod('Schulze'); |
30
|
|
|
|
31
|
|
|
abstract class Condorcet |
32
|
|
|
{ |
33
|
|
|
|
34
|
|
|
/////////// CONSTANTS /////////// |
35
|
|
|
public const VERSION = '1.5.0'; |
36
|
|
|
|
37
|
|
|
public const CONDORCET_BASIC_CLASS = __NAMESPACE__.'\\Algo\\Methods\\CondorcetBasic'; |
38
|
|
|
|
39
|
|
|
protected static $_defaultMethod = null; |
40
|
|
|
protected static $_authMethods = [ self::CONDORCET_BASIC_CLASS => (__NAMESPACE__.'\\Algo\\Methods\\CondorcetBasic')::METHOD_NAME ]; |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/////////// STATICS METHODS /////////// |
44
|
|
|
|
45
|
|
|
// Return library version numer |
46
|
6 |
|
public static function getVersion (string $option = 'FULL') : string |
47
|
|
|
{ |
48
|
6 |
|
if ($option === 'MAJOR') : |
49
|
3 |
|
$version = explode('.', self::VERSION); |
50
|
3 |
|
return $version[0].'.'.$version[1]; |
51
|
|
|
else : |
52
|
5 |
|
return self::VERSION; |
53
|
|
|
endif; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// Return an array with auth methods |
57
|
1 |
|
public static function getAuthMethods (bool $basic = false) : array |
58
|
|
|
{ |
59
|
1 |
|
$auth = self::$_authMethods; |
60
|
|
|
|
61
|
|
|
// Don't show Natural Condorcet |
62
|
1 |
|
if (!$basic) : |
63
|
1 |
|
unset($auth[self::CONDORCET_BASIC_CLASS]); |
64
|
|
|
endif; |
65
|
|
|
|
66
|
1 |
|
return array_column($auth,0); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
// Return the Class default method |
71
|
15 |
|
public static function getDefaultMethod () { |
72
|
15 |
|
return self::$_defaultMethod; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
|
76
|
|
|
// Check if the method is supported |
77
|
81 |
|
public static function isAuthMethod (string $method) |
78
|
|
|
{ |
79
|
81 |
|
$auth = self::$_authMethods; |
80
|
|
|
|
81
|
81 |
|
if (empty($method)) : |
82
|
1 |
|
throw new CondorcetException (8); |
83
|
|
|
endif; |
84
|
|
|
|
85
|
80 |
|
if ( isset($auth[$method]) ) : |
86
|
12 |
|
return $method; |
87
|
|
|
else : // Alias |
88
|
78 |
|
foreach ($auth as $class => &$alias) : |
89
|
78 |
|
foreach ($alias as &$entry) : |
90
|
78 |
|
if ( strcasecmp($method,$entry) === 0 ) : |
91
|
78 |
|
return $class; |
92
|
|
|
endif; |
93
|
|
|
endforeach; |
94
|
|
|
endforeach; |
95
|
|
|
endif; |
96
|
|
|
|
97
|
13 |
|
return false; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
|
101
|
|
|
// Add algos |
102
|
12 |
|
public static function addMethod (string $algos) : bool |
103
|
|
|
{ |
104
|
|
|
// Check algos |
105
|
12 |
|
if ( self::isAuthMethod($algos) || !self::testMethod($algos) ) : |
106
|
1 |
|
return false; |
107
|
|
|
endif; |
108
|
|
|
|
109
|
|
|
// Adding algo |
110
|
10 |
|
self::$_authMethods[$algos] = $algos::METHOD_NAME; |
111
|
|
|
|
112
|
10 |
|
if (self::getDefaultMethod() === null) : |
113
|
10 |
|
self::setDefaultMethod($algos); |
114
|
|
|
endif; |
115
|
|
|
|
116
|
10 |
|
return true; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
|
120
|
|
|
// Check if the class Algo. exist and ready to be used |
121
|
11 |
|
protected static function testMethod (string $method) : bool |
122
|
|
|
{ |
123
|
11 |
|
if ( !class_exists($method) ) : |
124
|
1 |
|
throw new CondorcetException(9); |
125
|
|
|
endif; |
126
|
|
|
|
127
|
10 |
|
if ( !is_subclass_of($method, __NAMESPACE__.'\\Algo\\MethodInterface') || !is_subclass_of($method,__NAMESPACE__.'\\Algo\\Method') ) : |
128
|
1 |
|
throw new CondorcetException(10); |
129
|
|
|
endif; |
130
|
|
|
|
131
|
10 |
|
foreach ($method::METHOD_NAME as $alias) : |
132
|
10 |
|
if (self::isAuthMethod($alias)) : |
133
|
10 |
|
throw new CondorcetException(25); |
134
|
|
|
endif; |
135
|
|
|
endforeach; |
136
|
|
|
|
137
|
10 |
|
return true; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
// Change default method for this class. |
142
|
11 |
|
public static function setDefaultMethod (string $method) : bool |
143
|
|
|
{ |
144
|
11 |
|
if ( ($method = self::isAuthMethod($method)) && $method !== self::CONDORCET_BASIC_CLASS ) : |
145
|
10 |
|
self::$_defaultMethod = $method; |
146
|
10 |
|
return true; |
147
|
|
|
else : |
148
|
1 |
|
return false; |
149
|
|
|
endif; |
150
|
|
|
} |
151
|
|
|
|
152
|
79 |
|
public static function condorcetBasicSubstitution (?string $substitution) : string |
153
|
|
|
{ |
154
|
79 |
|
if ( $substitution !== null ) : |
155
|
50 |
|
if ( Condorcet::isAuthMethod($substitution) ) : |
156
|
50 |
|
$algo = $substitution; |
157
|
|
|
else : |
158
|
50 |
|
throw new CondorcetException(9,$substitution); |
159
|
|
|
endif; |
160
|
|
|
else : |
161
|
77 |
|
$algo = Condorcet::CONDORCET_BASIC_CLASS; |
162
|
|
|
endif; |
163
|
|
|
|
164
|
79 |
|
return $algo; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.