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; |
12
|
|
|
|
13
|
|
|
use CondorcetPHP\Condorcet\Throwable\CondorcetException; |
14
|
|
|
|
15
|
|
|
class Candidate |
16
|
|
|
{ |
17
|
|
|
use Linkable, CondorcetVersion; |
18
|
|
|
|
19
|
|
|
private $_name = []; |
20
|
|
|
private $_provisional = false; |
21
|
|
|
|
22
|
|
|
/// |
23
|
|
|
|
24
|
161 |
|
public function __construct (string $name) |
25
|
|
|
{ |
26
|
161 |
|
$this->setName($name); |
27
|
161 |
|
} |
28
|
|
|
|
29
|
157 |
|
public function __toString () : string |
30
|
|
|
{ |
31
|
157 |
|
return $this->getName(); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/// |
35
|
|
|
|
36
|
|
|
// SETTERS |
37
|
|
|
|
38
|
161 |
|
public function setName (string $name) : bool |
39
|
|
|
{ |
40
|
161 |
|
$name = trim($name); |
41
|
|
|
|
42
|
161 |
|
if (mb_strlen($name) > Election::MAX_LENGTH_CANDIDATE_ID ) : |
43
|
1 |
|
throw new CondorcetException(1, $name); |
44
|
|
|
endif; |
45
|
|
|
|
46
|
161 |
|
if (!$this->checkName($name)) : |
47
|
1 |
|
throw new CondorcetException(19, $name); |
48
|
|
|
endif; |
49
|
|
|
|
50
|
161 |
|
$this->_name[] = [ 'name' => $name, 'timestamp' => microtime(true) ]; |
51
|
|
|
|
52
|
161 |
|
return true; |
53
|
|
|
} |
54
|
|
|
|
55
|
156 |
|
public function setProvisionalState (bool $provisional) : bool |
56
|
|
|
{ |
57
|
156 |
|
$this->_provisional = $provisional; |
58
|
156 |
|
return true; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// GETTERS |
62
|
|
|
|
63
|
158 |
|
public function getName () : string |
64
|
|
|
{ |
65
|
158 |
|
return end($this->_name)['name']; |
66
|
|
|
} |
67
|
|
|
|
68
|
3 |
|
public function getHistory () : array |
69
|
|
|
{ |
70
|
3 |
|
return $this->_name; |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
public function getCreateTimestamp () : float |
74
|
|
|
{ |
75
|
2 |
|
return $this->_name[0]['timestamp']; |
76
|
|
|
} |
77
|
|
|
|
78
|
2 |
|
public function getTimestamp () : float |
79
|
|
|
{ |
80
|
2 |
|
return end($this->_name)['timestamp']; |
81
|
|
|
} |
82
|
|
|
|
83
|
113 |
|
public function getProvisionalState () : bool |
84
|
|
|
{ |
85
|
113 |
|
return $this->_provisional; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/// |
89
|
|
|
|
90
|
|
|
// INTERNAL |
91
|
|
|
|
92
|
161 |
|
private function checkName (string $name) : bool |
93
|
|
|
{ |
94
|
161 |
|
foreach ($this->_link as &$link) : |
95
|
3 |
|
if (!$link->canAddCandidate($name)) : |
96
|
3 |
|
return false; |
97
|
|
|
endif; |
98
|
|
|
endforeach; |
99
|
|
|
|
100
|
161 |
|
return true; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|