Completed
Push — master ( e86a88...9f06ff )
by Boudry
04:59 queued 02:04
created

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