Completed
Push — dev-1.6.x ( f43552...3412fc )
by Boudry
16:50
created

Result::getStats()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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
use Condorcet\CondorcetVersion;
14
use Condorcet\Election;
15
use Condorcet\CondorcetUtil;
16
use Condorcet\Vote;
17
use Condorcet\ElectionProcess\VoteUtil;
18
19
20
class Result implements \ArrayAccess, \Countable, \Iterator 
21
{
22
    use CondorcetVersion;
23
24
    // Implement Iterator
25
26 1
    public function rewind() :void {
27 1
        reset($this->_UserResult);
28 1
    }
29
30 1
    public function current () {
31 1
        return current($this->_UserResult);
32
    }
33
34 1
    public function key () : int {
35 1
        return key($this->_UserResult);
36
    }
37
38 1
    public function next () : void {
39 1
        next($this->_UserResult);
40 1
    }
41
42 1
    public function valid () : bool {
43 1
        return (key($this->_UserResult) === null) ? false : true;
44
    }
45
46
    // Implement ArrayAccess
47
48 1
    public function offsetSet ($offset, $value) : void {
49 1
        throw new CondorcetException (0,"Can't change a result");
50
    }
51
52 1
    public function offsetExists ($offset) : bool {
53 1
        return isset($this->_UserResult[$offset]);
54
    }
55
56 1
    public function offsetUnset ($offset) : void {
57 1
        throw new CondorcetException (0,"Can't change a result");
58
    }
59
60 50
    public function offsetGet ($offset) {
61 50
        return $this->_UserResult[$offset] ?? null;
62
    }
63
64
    // Implement Countable
65
66 3
    public function count () : int {
67 3
        return count($this->_UserResult);
68
    }
69
70
71
/////////// CONSTRUCTOR ///////////
72
73
    protected $_Result;
74
    protected $_UserResult;
75
    protected $_stringResult;
76
    protected $_CondorcetWinner;
77
    protected $_CondorcetLoser;
78
79
    protected $_Stats;
80
81
    protected $_warning = [];
82
83
    protected $_BuildTimeStamp;
84
    protected $_fromMethod;
85
    protected $_byClass;
86
    protected $_ElectionCondorcetVersion;
87
88
89 72
    public function __construct (string $fromMethod, string $byClass, Election $election, array $result, $stats)
90
    {
91 72
        $this->_Result = $result;
92 72
        $this->_UserResult = $this->makeUserResult($election);
93 72
        $this->_stringResult = $this->getResultAsArray(true);
94 72
        $this->_Stats = $stats;
95 72
        $this->_fromMethod = $fromMethod;
96 72
        $this->_byClass = $byClass;
97 72
        $this->_ElectionCondorcetVersion = $election->getObjectVersion();
98 72
        $this->_CondorcetWinner = $election->getWinner();
99 72
        $this->_CondorcetLoser = $election->getLoser();
100 72
        $this->_BuildTimeStamp = microtime(true);
101 72
    }
102
103
104
/////////// Get Result ///////////
105
106 72
    public function getResultAsArray (bool $convertToString = false) : array
107
    {
108 72
        $r = $this->_UserResult;
109
110 72
        foreach ($r as &$rank) :
111 72
            if (count($rank) === 1) :
112 69
                $rank = ($convertToString) ? (string) $rank[0] : $rank[0];
113 24
            elseif ($convertToString) :
114 24
                foreach ($rank as &$subRank) :
115 72
                    $subRank = (string) $subRank;
116
                endforeach;
117
            endif;
118
        endforeach;
119
120 72
        return $r;
121
    }
122
123 3
    public function getResultAsString ()
124
    {
125 3
        return VoteUtil::getRankingAsString($this->getResultAsArray(true));
126
    }
127
128 1
    public function getOriginalArrayWithString () : array
129
    {
130 1
        return $this->_stringResult;
131
    }
132
133
    public function getResultAsInternalKey () : array
134
    {
135
        return $this->_Result;
136
    }
137
138 11
    public function getStats () {
139 11
        return $this->_Stats;
140
    }
141
142 49
    public function getWinner () {
143 49
        return CondorcetUtil::format($this[1],false);
144
    }
145
146 3
    public function getLoser () {
147 3
        return CondorcetUtil::format($this[count($this)],false);
148
    }
149
150 1
    public function getCondorcetWinner () {
151 1
        return $this->_CondorcetWinner;
152
    }
153
154 1
    public function getCondorcetLoser () {
155 1
        return $this->_CondorcetLoser;
156
    }
157
158 72
    protected function makeUserResult (Election $election) : array
159
    {
160 72
        $userResult = [];
161
162 72
        foreach ( $this->_Result as $key => $value ) :
163 72
            if (is_array($value)) :
164 69
                foreach ($value as $candidate_key) :
165 69
                    $userResult[$key][] = $election->getCandidateId($candidate_key);
166
                endforeach;
167 5
            elseif (is_null($value)) :
168
                $userResult[$key] = null;
169
            else :
170 72
                $userResult[$key][] = $election->getCandidateId($value);
171
            endif;
172
        endforeach;
173
174 72
        foreach ( $userResult as $key => $value ) :
175 72
            if (is_null($value)) :
176 72
                $userResult[$key] = null;
177
            endif;
178
        endforeach;
179
180 72
        return $userResult;
181
    }
182
183
184
/////////// Get & Set MetaData ///////////
185
186 1
    public function addWarning (int $type, string $msg = null) : bool
187
    {
188 1
        $this->_warning[] = ['type' => $type, 'msg' => $msg];
189
190 1
        return true;
191
    }
192
193 2
    public function getWarning ($type = null) : array
194
    {
195 2
        if ($type === null) :
196
            return $this->_warning;
197
        else :
198 2
            $r = [];
199
200 2
            foreach ($this->_warning as $oneWarning) :
201 1
                if ($oneWarning['type'] === (int) $type) :
202 1
                    $r[] = $oneWarning;
203
                endif;
204
            endforeach;
205
206 2
            return $r;
207
        endif;
208
    }
209
210 1
    public function getClassGenerator () : string {
211 1
        return $this->_byClass;
212
    }
213
214 1
    public function getMethod () : string {
215 1
        return $this->_fromMethod;
216
    }
217
218 1
    public function getBuildTimeStamp () : float {
219 1
        return (float) $this->_BuildTimeStamp;
220
    }
221
222 1
    public function getCondorcetElectionGeneratorVersion () : string {
223 1
        return $this->_ElectionCondorcetVersion;
224
    }
225
226
}
227