1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Reasoning.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 ArrayAccess; |
21
|
|
|
use JsonSerializable; |
22
|
|
|
use Pvra\AnalysisResult; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class Reasoning |
26
|
|
|
* |
27
|
|
|
* @package Pvra\Result |
28
|
|
|
*/ |
29
|
|
|
class Reasoning implements ArrayAccess, JsonSerializable |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* The reason this reasoning maps to |
33
|
|
|
* |
34
|
|
|
* This may be an arbitrary scalar but is most likely to map to a known constant in `RequirementReason`. |
35
|
|
|
* |
36
|
|
|
* @var int|string |
37
|
|
|
* @see RequirementReason Possible mapping target |
38
|
|
|
*/ |
39
|
|
|
private $reasonId; |
40
|
|
|
/** |
41
|
|
|
* @var int |
42
|
|
|
*/ |
43
|
|
|
private $line; |
44
|
|
|
/** |
45
|
|
|
* @var null|string |
46
|
|
|
*/ |
47
|
|
|
private $msg; |
48
|
|
|
/** |
49
|
|
|
* @var \Pvra\AnalysisResult |
50
|
|
|
*/ |
51
|
|
|
private $result; |
52
|
|
|
/** |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
private $data; |
56
|
|
|
/** |
57
|
|
|
* @var string|false |
58
|
|
|
*/ |
59
|
|
|
private $version; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* RequirementReasoning constructor |
63
|
|
|
* |
64
|
|
|
* Used to construct this reasoning. Only the `reasonId`, `line` and `result` parameters |
65
|
|
|
* are required. The remaining parameters can be determined based on the reasonId and result instance. * |
66
|
|
|
* |
67
|
|
|
* @param int|string $reasonId The mapped reasonId |
68
|
|
|
* @param int $line The mapped line |
69
|
|
|
* @param \Pvra\AnalysisResult $result The result this reasoning applies to |
70
|
|
|
* @param string|null $version The required version. |
71
|
|
|
* @param null|string $msg The message related to this reasoning. If this parameter is set to `null` the message is |
72
|
|
|
* fetched from the `MessageLocator` attached to the result instance related to this instance. |
73
|
|
|
* @param array $data An array of additional data passed to the `MessageFormatter` |
74
|
|
|
*/ |
75
|
174 |
|
public function __construct( |
76
|
|
|
$reasonId, |
77
|
|
|
$line, |
78
|
|
|
AnalysisResult $result, |
79
|
|
|
$version = null, |
80
|
|
|
$msg = null, |
81
|
|
|
$data = [] |
82
|
|
|
) { |
83
|
174 |
|
$this->reasonId = $reasonId; |
84
|
174 |
|
$this->line = $line; |
85
|
174 |
|
$this->msg = $msg; |
86
|
174 |
|
$this->result = $result; |
87
|
174 |
|
$this->data = $data; |
88
|
174 |
|
if ($version === null) { |
89
|
2 |
|
$this->version = Reason::getVersionFromReason($reasonId); |
90
|
1 |
|
} else { |
91
|
172 |
|
$this->version = $version; |
92
|
|
|
} |
93
|
174 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Array representation of this object |
97
|
|
|
* |
98
|
|
|
* This method creates an array representation of this object including all keys that would be |
99
|
|
|
* available through offsetGet. |
100
|
|
|
* |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
10 |
|
public function toArray() |
104
|
|
|
{ |
105
|
|
|
return [ |
106
|
10 |
|
'data' => $this->get('data'), |
107
|
10 |
|
'reason' => $this->get('reason'), |
108
|
10 |
|
'reasonName' => $this->get('reasonName'), |
109
|
10 |
|
'line' => $this->get('line'), |
110
|
10 |
|
'msg' => $this->get('msg'), |
111
|
10 |
|
'raw_msg' => $this->get('raw_msg'), |
112
|
10 |
|
'version' => $this->get('version'), |
113
|
10 |
|
'targetId' => $this->get('targetId'), |
114
|
5 |
|
]; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get the data this instance represents in a format understood by json_encode. |
119
|
|
|
* |
120
|
|
|
* @return array Data to be encoded as json |
121
|
|
|
*/ |
122
|
2 |
|
public function jsonSerialize() |
123
|
|
|
{ |
124
|
2 |
|
return $this->toArray(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Get the related result |
130
|
|
|
* |
131
|
|
|
* @return \Pvra\AnalysisResult |
132
|
|
|
*/ |
133
|
50 |
|
protected function getResult() |
134
|
|
|
{ |
135
|
50 |
|
return $this->result; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param string $name |
140
|
|
|
* @return array|int|null|string |
141
|
|
|
*/ |
142
|
20 |
|
public function get($name) |
143
|
|
|
{ |
144
|
20 |
|
if ($this->offsetExists($name)) { |
145
|
18 |
|
return $this->offsetGet($name); |
146
|
|
|
} else { |
147
|
2 |
|
throw new \RuntimeException(sprintf('%s::$%s accessed through "get" does not exist or is not accessible.', |
148
|
1 |
|
get_class($this), |
149
|
1 |
|
$name)); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param int|string $offset |
155
|
|
|
* @return bool |
156
|
|
|
*/ |
157
|
24 |
|
public function offsetExists($offset) |
158
|
|
|
{ |
159
|
24 |
|
return in_array($offset, ['data', 'reason', 'reasonName', 'line', 'msg', 'raw_msg', 'version', 'targetId']); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param mixed $offset |
164
|
|
|
* @return array|int|null|string |
165
|
|
|
*/ |
166
|
136 |
|
public function offsetGet($offset) |
167
|
|
|
{ |
168
|
|
|
switch ($offset) { |
169
|
136 |
|
case 'data': |
170
|
18 |
|
return $this->data; |
171
|
134 |
|
case 'line': |
172
|
124 |
|
return $this->line; |
173
|
134 |
|
case 'version': |
174
|
58 |
|
return $this->version; |
175
|
126 |
|
case 'reason': |
176
|
88 |
|
return $this->reasonId; |
177
|
54 |
|
case 'reasonName': |
178
|
50 |
|
return Reason::getReasonNameFromValue($this->reasonId); |
179
|
54 |
|
case 'raw_msg': |
180
|
14 |
|
if ($this->msg !== null) { |
181
|
6 |
|
return $this->msg; |
182
|
|
|
} |
183
|
8 |
|
return $this->getResult()->getMsgFormatter()->getLocator()->getMessage($this->reasonId); |
184
|
52 |
|
case 'targetId': |
185
|
12 |
|
return $this->getResult()->getAnalysisTargetId(); |
186
|
52 |
|
case 'msg': |
187
|
52 |
|
if ($this->msg !== null) { |
188
|
6 |
|
return $this->msg; |
189
|
|
|
} |
190
|
46 |
|
return $this->getResult()->getMsgFormatter()->getFormattedMessageFromId($this->reasonId, |
191
|
46 |
|
array_merge($this->data, [ |
192
|
46 |
|
MessageFormatter::FORMAT_KEY_LINE => $this->line, |
193
|
46 |
|
MessageFormatter::FORMAT_KEY_REASON_ID => $this->reasonId, |
194
|
46 |
|
MessageFormatter::FORMAT_KEY_TARGET_ID => $this->getResult()->getAnalysisTargetId(), |
195
|
46 |
|
MessageFormatter::FORMAT_KEY_VERSION => $this->version, |
196
|
46 |
|
MessageFormatter::FORMAT_KEY_REASON_NAME => $this->offsetGet('reasonName'), |
197
|
23 |
|
])); |
198
|
|
|
} |
199
|
|
|
|
200
|
2 |
|
return null; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param mixed $offset |
205
|
|
|
* @param mixed $value |
206
|
|
|
* @throws \Exception |
207
|
|
|
*/ |
208
|
2 |
|
public function offsetSet($offset, $value) |
209
|
|
|
{ |
210
|
2 |
|
throw new \Exception('Unsupported operation.'); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @param mixed $offset |
215
|
|
|
* @throws \Exception |
216
|
|
|
*/ |
217
|
2 |
|
public function offsetUnset($offset) |
218
|
|
|
{ |
219
|
2 |
|
throw new \Exception('Unsupported operation.'); |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|