|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Collection.php |
|
4
|
|
|
* |
|
5
|
|
|
* MIT LICENSE |
|
6
|
|
|
* |
|
7
|
|
|
* LICENSE: This source file is subject to the MIT license. |
|
8
|
|
|
* A copy of the licenses text was distributed alongside this |
|
9
|
|
|
* file (usually the repository or package root). The text can also |
|
10
|
|
|
* be obtained through one of the following sources: |
|
11
|
|
|
* * http://opensource.org/licenses/MIT |
|
12
|
|
|
* * https://github.com/suralc/pvra/blob/master/LICENSE |
|
13
|
|
|
* |
|
14
|
|
|
* @author suralc <[email protected]> |
|
15
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
|
16
|
|
|
*/ |
|
17
|
|
|
namespace Pvra\Result; |
|
18
|
|
|
|
|
19
|
|
|
|
|
20
|
|
|
use Pvra\AnalysisResult; |
|
21
|
|
|
use Traversable; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Class Collection |
|
25
|
|
|
* |
|
26
|
|
|
* @package Pvra\Result |
|
27
|
|
|
*/ |
|
28
|
|
|
class Collection implements \Countable, \IteratorAggregate, \JsonSerializable |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* Result instances that are part of this collection |
|
32
|
|
|
* |
|
33
|
|
|
* @var \Pvra\AnalysisResult[] |
|
34
|
|
|
*/ |
|
35
|
|
|
private $results = []; |
|
36
|
|
|
/** |
|
37
|
|
|
* The targetId of the currently highest demanding result |
|
38
|
|
|
* |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
private $highestDemand; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Add a result to this collection |
|
45
|
|
|
* |
|
46
|
|
|
* @param \Pvra\AnalysisResult $result The result to be added |
|
47
|
|
|
* @param bool $ignoreIfExists Do not add+override an already existing result with the same targetId. |
|
48
|
|
|
* @return $this |
|
49
|
|
|
*/ |
|
50
|
34 |
|
public function add(AnalysisResult $result, $ignoreIfExists = true) |
|
51
|
|
|
{ |
|
52
|
34 |
|
if ($ignoreIfExists === true && $this->has($result)) { |
|
53
|
2 |
|
return $this; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
34 |
|
if ($this->highestDemand === null |
|
57
|
26 |
|
|| (isset($this->results[ $this->highestDemand ]) |
|
58
|
18 |
|
&& $this->results[ $this->highestDemand ]->getRequiredVersionId() |
|
59
|
26 |
|
< $result->getRequiredVersionId()) |
|
60
|
17 |
|
) { |
|
61
|
34 |
|
$this->highestDemand = $result->getAnalysisTargetId(); |
|
62
|
17 |
|
} |
|
63
|
|
|
|
|
64
|
34 |
|
$this->results[ $result->getAnalysisTargetId() ] = $result; |
|
65
|
|
|
|
|
66
|
34 |
|
return $this; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Remove a result from the collection |
|
71
|
|
|
* |
|
72
|
|
|
* Removes a result, which is identified by its analysis target id from |
|
73
|
|
|
* the current collection. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string|AnalysisResult $result |
|
76
|
|
|
* @return true |
|
77
|
|
|
*/ |
|
78
|
4 |
|
public function remove($result) |
|
79
|
|
|
{ |
|
80
|
4 |
|
if ($result instanceof AnalysisResult) { |
|
81
|
2 |
|
$id = $result->getAnalysisTargetId(); |
|
82
|
4 |
|
} elseif (is_string($result)) { |
|
83
|
2 |
|
$id = $result; |
|
84
|
2 |
|
if (isset($this->results[ $id ])) { |
|
85
|
2 |
|
$result = $this->results[ $id ]; |
|
86
|
1 |
|
} else { |
|
87
|
2 |
|
return true; |
|
88
|
|
|
} |
|
89
|
1 |
|
} else { |
|
90
|
2 |
|
throw new \InvalidArgumentException('The result argument has to be an instance of AnalysisResult or a string.'); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
2 |
|
$needRecalc = false; |
|
94
|
2 |
|
if ($this->highestDemand !== null |
|
95
|
2 |
|
&& $result->getAnalysisTargetId() === $this->getHighestDemandingResult()->getAnalysisTargetId() |
|
96
|
1 |
|
) { |
|
97
|
2 |
|
$needRecalc = true; |
|
98
|
1 |
|
} |
|
99
|
|
|
|
|
100
|
2 |
|
unset($this->results[ $id ]); |
|
101
|
|
|
|
|
102
|
2 |
|
if ($needRecalc) { |
|
103
|
2 |
|
$this->recalculateHighestDemandingResult(); |
|
104
|
1 |
|
} |
|
105
|
|
|
|
|
106
|
2 |
|
return true; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Recalculates the highest demanding result of this collection |
|
111
|
|
|
* |
|
112
|
|
|
* This can be expensive. Only do this if really necessary. |
|
113
|
|
|
*/ |
|
114
|
2 |
|
private function recalculateHighestDemandingResult() |
|
115
|
|
|
{ |
|
116
|
2 |
|
$highestVersionId = 0; |
|
117
|
2 |
|
$highestResult = null; |
|
118
|
2 |
|
foreach ($this->results as $result) { |
|
119
|
2 |
|
if ($result->getRequiredVersionId() > $highestVersionId) { |
|
120
|
2 |
|
$highestVersionId = $result->getRequiredVersionId(); |
|
121
|
2 |
|
$highestResult = $result; |
|
122
|
1 |
|
} |
|
123
|
1 |
|
} |
|
124
|
|
|
|
|
125
|
2 |
|
if ($highestResult !== null) { |
|
126
|
2 |
|
$this->highestDemand = $highestResult->getAnalysisTargetId(); |
|
127
|
1 |
|
} else { |
|
128
|
2 |
|
$this->highestDemand = null; |
|
129
|
|
|
} |
|
130
|
2 |
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Checks whether a given result is part of this collection |
|
134
|
|
|
* |
|
135
|
|
|
* @param string|\Pvra\AnalysisResult $result Analysis target Id or instance of Analysis Result |
|
136
|
|
|
* @return bool |
|
137
|
|
|
*/ |
|
138
|
36 |
|
public function has($result) |
|
139
|
|
|
{ |
|
140
|
36 |
|
if (is_string($result)) { |
|
141
|
2 |
|
return isset($this->results[ $result ]); |
|
142
|
18 |
|
} elseif ($result instanceof AnalysisResult) { |
|
143
|
34 |
|
return isset($this->results[ $result->getAnalysisTargetId() ]); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
2 |
|
return false; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Get the currently highest demanding result |
|
151
|
|
|
* |
|
152
|
|
|
* @return null|\Pvra\AnalysisResult |
|
153
|
|
|
*/ |
|
154
|
20 |
|
public function getHighestDemandingResult() |
|
155
|
|
|
{ |
|
156
|
20 |
|
if (isset($this->results[ $this->highestDemand ])) { |
|
157
|
20 |
|
return $this->results[ $this->highestDemand ]; |
|
158
|
|
|
} else { |
|
159
|
4 |
|
return null; |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Retrieve an external iterator |
|
165
|
|
|
* |
|
166
|
|
|
* @link http://php.net/manual/en/iteratoraggregate.getiterator.php |
|
167
|
|
|
* @return Traversable|AnalysisResult[] |
|
168
|
|
|
*/ |
|
169
|
12 |
|
public function getIterator() |
|
170
|
|
|
{ |
|
171
|
12 |
|
return new \ArrayIterator($this->results); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Count attached results |
|
176
|
|
|
* |
|
177
|
|
|
* @link http://php.net/manual/en/countable.count.php |
|
178
|
|
|
* @return int The custom count as an integer. |
|
179
|
|
|
* The return value is cast to an integer. |
|
180
|
|
|
*/ |
|
181
|
20 |
|
public function count() |
|
182
|
|
|
{ |
|
183
|
20 |
|
return count($this->results); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Specify data which should be serialized to JSON |
|
188
|
|
|
* |
|
189
|
|
|
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php |
|
190
|
|
|
* @return mixed data which can be serialized by json_encode, |
|
191
|
|
|
* which is a value of any type other than a resource. |
|
192
|
|
|
*/ |
|
193
|
6 |
|
public function jsonSerialize() |
|
194
|
|
|
{ |
|
195
|
6 |
|
$results = []; |
|
196
|
6 |
|
foreach ($this->getIterator() as $result) { |
|
197
|
6 |
|
$reasonings = []; |
|
198
|
6 |
|
foreach ($result->getIterator() as $reason) { |
|
199
|
6 |
|
$reasonings[] = $reason->toArray(); |
|
200
|
3 |
|
} |
|
201
|
6 |
|
$results[ $result->getAnalysisTargetId() ] = $reasonings; |
|
202
|
3 |
|
} |
|
203
|
|
|
|
|
204
|
6 |
|
return $results; |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|