|
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\Condorcet; |
|
13
|
|
|
use Condorcet\CondorcetVersion; |
|
14
|
|
|
|
|
15
|
|
|
// Custom Exeption |
|
16
|
|
|
class CondorcetException extends \Exception |
|
17
|
|
|
{ |
|
18
|
|
|
use CondorcetVersion; |
|
19
|
|
|
|
|
20
|
|
|
protected $_infos; |
|
21
|
|
|
|
|
22
|
2 |
|
public function __construct (int $code = 0, $infos = '') |
|
23
|
|
|
{ |
|
24
|
2 |
|
$this->_infos = $infos; |
|
25
|
|
|
|
|
26
|
2 |
|
parent::__construct($this->correspondence($code), $code); |
|
27
|
2 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function __toString () : string |
|
30
|
|
|
{ |
|
31
|
|
|
return __CLASS__ . ": [{$this->code}]: {$this->message} (line: {$this->file}:{$this->line})\n"; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
2 |
|
protected function correspondence (int $code) : string |
|
35
|
|
|
{ |
|
36
|
2 |
|
$error = []; |
|
37
|
|
|
|
|
38
|
|
|
// Common |
|
39
|
2 |
|
$error[1] = 'Bad candidate format'; |
|
40
|
2 |
|
$error[2] = 'The voting process has already started'; |
|
41
|
2 |
|
$error[3] = 'This candidate ID is already registered'; |
|
42
|
2 |
|
$error[4] = 'This candidate ID do not exist'; |
|
43
|
2 |
|
$error[5] = 'Bad vote format | '.$this->_infos; |
|
44
|
2 |
|
$error[6] = 'You need to specify votes before results'; |
|
45
|
2 |
|
$error[7] = 'Your Candidate ID is too long > ' . Election::MAX_LENGTH_CANDIDATE_ID; |
|
46
|
2 |
|
$error[8] = 'This method do not exist'; |
|
47
|
2 |
|
$error[9] = 'The algo class you want has not been defined'; |
|
48
|
2 |
|
$error[10] = 'The algo class you want is not correct'; |
|
49
|
2 |
|
$error[11] = 'You try to unserialize an object version older than your actual Class version. This is a problematic thing'; |
|
50
|
2 |
|
$error[12] = 'You have exceeded the number of votes allowed for this method.'; |
|
51
|
2 |
|
$error[13] = 'Formatting error: You must specify an integer'; |
|
52
|
2 |
|
$error[14] = 'parseVote() must take a string (raw or path) as argument'; |
|
53
|
2 |
|
$error[15] = 'Input must be valid Json format'; |
|
54
|
2 |
|
$error[16] = 'You have exceeded the maximum number of votes allowed per election ('.$this->_infos.').'; |
|
55
|
2 |
|
$error[17] = 'Bad tags input format'; |
|
56
|
2 |
|
$error[18] = 'New vote can\'t match Candidate of his elections'; |
|
57
|
2 |
|
$error[19] = 'This name is not allowed in because of a namesake in the election in which the object participates.'; |
|
58
|
2 |
|
$error[20] = 'You need to specify one or more candidates before voting'; |
|
59
|
2 |
|
$error[21] = 'Bad vote timestamp format'; |
|
60
|
2 |
|
$error[22] = 'This context is not valid'; |
|
61
|
2 |
|
$error[23] = 'No Data Handler in use'; |
|
62
|
2 |
|
$error[24] = 'A Data Handler is already in use'; |
|
63
|
2 |
|
$error[25] = 'Algo class try to use existing alias'; |
|
64
|
2 |
|
$error[26] = 'Weight can not be < 1'; |
|
65
|
|
|
|
|
66
|
2 |
|
$error[30] = 'Candidate not in Ranking'; |
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
// DataManager |
|
70
|
2 |
|
$error[30] = 'This entity does not exist.'; |
|
71
|
|
|
|
|
72
|
|
|
// Algorithms |
|
73
|
2 |
|
if ($code === 101) : |
|
74
|
|
|
$error[101] = $this->_infos; |
|
75
|
|
|
endif; |
|
76
|
2 |
|
$error[102] = 'Marquis of Condorcet algortihm can\'t provide a full ranking. But only Winner and Loser.'; |
|
77
|
|
|
|
|
78
|
|
|
|
|
79
|
2 |
|
if ( array_key_exists($code, $error) ) : |
|
80
|
2 |
|
return $error[$code]; |
|
81
|
|
|
else : |
|
82
|
|
|
return (!is_null($this->_infos)) ? $this->_infos : 'Mysterious Error'; |
|
83
|
|
|
endif; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|