1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File containing the {@link OperationResult_Collection} class. |
4
|
|
|
* |
5
|
|
|
* @package Application Utils |
6
|
|
|
* @subpackage OperationResult |
7
|
|
|
* @see OperationResult_Collection |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace AppUtils; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Storage for several operation result instances, that acts |
16
|
|
|
* as a regular operation result. |
17
|
|
|
* |
18
|
|
|
* Can be used as replacement result object, which will catch |
19
|
|
|
* all makeError() and makeSuccess() calls as separate error |
20
|
|
|
* or success instances. Adding a collection to a collection |
21
|
|
|
* will make it inherit all results the target collection contains. |
22
|
|
|
* |
23
|
|
|
* @package Application Utils |
24
|
|
|
* @subpackage OperationResult |
25
|
|
|
* @author Sebastian Mordziol <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class OperationResult_Collection extends OperationResult |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var OperationResult[] |
31
|
|
|
*/ |
32
|
|
|
protected $results = array(); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param string $message |
36
|
|
|
* @param int $code |
37
|
|
|
* @return $this |
38
|
|
|
*/ |
39
|
|
|
public function makeError(string $message, int $code=0) : OperationResult |
40
|
|
|
{ |
41
|
|
|
return $this->add('makeError', $message, $code); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param string $message |
46
|
|
|
* @param int $code |
47
|
|
|
* @return $this |
48
|
|
|
*/ |
49
|
|
|
public function makeSuccess(string $message, int $code=0) : OperationResult |
50
|
|
|
{ |
51
|
|
|
return $this->add('makeSuccess', $message, $code); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $message |
56
|
|
|
* @param int $code |
57
|
|
|
* @return $this |
58
|
|
|
*/ |
59
|
|
|
public function makeWarning(string $message, int $code=0) : OperationResult |
60
|
|
|
{ |
61
|
|
|
return $this->add('makeWarning', $message, $code); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $message |
66
|
|
|
* @param int $code |
67
|
|
|
* @return $this |
68
|
|
|
*/ |
69
|
|
|
public function makeNotice(string $message, int $code=0) : OperationResult |
70
|
|
|
{ |
71
|
|
|
return $this->add('makeNotice', $message, $code); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param string $method |
76
|
|
|
* @param string $message |
77
|
|
|
* @param int $code |
78
|
|
|
* @return $this |
79
|
|
|
*/ |
80
|
|
|
protected function add(string $method, string $message, int $code=0) : OperationResult |
81
|
|
|
{ |
82
|
|
|
$result = new OperationResult($this->subject); |
83
|
|
|
$result->$method($message, $code); |
84
|
|
|
|
85
|
|
|
$this->results[] = $result; |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Adds a result to the collection. |
92
|
|
|
* |
93
|
|
|
* @param OperationResult $result |
94
|
|
|
* @return $this |
95
|
|
|
*/ |
96
|
|
|
public function addResult(OperationResult $result) : OperationResult_Collection |
97
|
|
|
{ |
98
|
|
|
if($result instanceof OperationResult_Collection) |
99
|
|
|
{ |
100
|
|
|
return $this->importCollection($result); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$this->results[] = $result; |
104
|
|
|
|
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Merges the target collection's results with this collection. |
110
|
|
|
* |
111
|
|
|
* @param OperationResult_Collection $collection |
112
|
|
|
* @return $this |
113
|
|
|
*/ |
114
|
|
|
private function importCollection(OperationResult_Collection $collection) : OperationResult_Collection |
115
|
|
|
{ |
116
|
|
|
$results = $collection->getResults(); |
117
|
|
|
|
118
|
|
|
foreach($results as $result) |
119
|
|
|
{ |
120
|
|
|
$this->addResult($result); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return OperationResult[] |
128
|
|
|
*/ |
129
|
|
|
public function getResults() : array |
130
|
|
|
{ |
131
|
|
|
return $this->results; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function isValid() : bool |
135
|
|
|
{ |
136
|
|
|
foreach($this->results as $result) |
137
|
|
|
{ |
138
|
|
|
if(!$result->isValid()) |
139
|
|
|
{ |
140
|
|
|
return false; |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return true; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function hasCode() : bool |
148
|
|
|
{ |
149
|
|
|
foreach($this->results as $result) |
150
|
|
|
{ |
151
|
|
|
if($result->hasCode()) |
152
|
|
|
{ |
153
|
|
|
return true; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return false; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function getCode() : int |
161
|
|
|
{ |
162
|
|
|
foreach($this->results as $result) |
163
|
|
|
{ |
164
|
|
|
if($result->hasCode()) |
165
|
|
|
{ |
166
|
|
|
return $result->getCode(); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
return 0; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function getMessage(string $type='') : string |
174
|
|
|
{ |
175
|
|
|
foreach($this->results as $result) |
176
|
|
|
{ |
177
|
|
|
$msg = $result->getMessage($type); |
178
|
|
|
|
179
|
|
|
if(!empty($msg)) |
180
|
|
|
{ |
181
|
|
|
return $msg; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return ''; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function containsCode(int $code) : bool |
189
|
|
|
{ |
190
|
|
|
foreach($this->results as $result) |
191
|
|
|
{ |
192
|
|
|
if($result->getCode() === $code) |
193
|
|
|
{ |
194
|
|
|
return true; |
195
|
|
|
} |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
return false; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function countErrors() : int |
202
|
|
|
{ |
203
|
|
|
return $this->countByType(self::TYPE_ERROR); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public function countWarnings() : int |
207
|
|
|
{ |
208
|
|
|
return $this->countByType(self::TYPE_WARNING); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public function countSuccesses() : int |
212
|
|
|
{ |
213
|
|
|
return $this->countByType(self::TYPE_SUCCESS); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function countNotices() : int |
217
|
|
|
{ |
218
|
|
|
return $this->countByType(self::TYPE_NOTICE); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function countByType(string $type) : int |
222
|
|
|
{ |
223
|
|
|
$amount = 0; |
224
|
|
|
|
225
|
|
|
foreach($this->results as $result) |
226
|
|
|
{ |
227
|
|
|
if($result->isType($type)) |
228
|
|
|
{ |
229
|
|
|
$amount++; |
230
|
|
|
} |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
return $amount; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
public function countResults() : int |
237
|
|
|
{ |
238
|
|
|
return count($this->results); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** |
242
|
|
|
* @return OperationResult[] |
243
|
|
|
*/ |
244
|
|
|
public function getErrors() : array |
245
|
|
|
{ |
246
|
|
|
return $this->getByType(self::TYPE_ERROR); |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* @return OperationResult[] |
251
|
|
|
*/ |
252
|
|
|
public function getSuccesses() : array |
253
|
|
|
{ |
254
|
|
|
return $this->getByType(self::TYPE_SUCCESS); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* @return OperationResult[] |
259
|
|
|
*/ |
260
|
|
|
public function getWarnings() : array |
261
|
|
|
{ |
262
|
|
|
return $this->getByType(self::TYPE_WARNING); |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* @return OperationResult[] |
267
|
|
|
*/ |
268
|
|
|
public function getNotices() : array |
269
|
|
|
{ |
270
|
|
|
return $this->getByType(self::TYPE_NOTICE); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* @param string $type |
275
|
|
|
* @return OperationResult[] |
276
|
|
|
*/ |
277
|
|
|
public function getByType(string $type) : array |
278
|
|
|
{ |
279
|
|
|
$results = array(); |
280
|
|
|
|
281
|
|
|
foreach($this->results as $result) |
282
|
|
|
{ |
283
|
|
|
if($result->isType($type)) |
284
|
|
|
{ |
285
|
|
|
$results[] = $result; |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
return $results; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
public function isType(string $type) : bool |
293
|
|
|
{ |
294
|
|
|
foreach($this->results as $result) |
295
|
|
|
{ |
296
|
|
|
if($result->isType($type)) |
297
|
|
|
{ |
298
|
|
|
return true; |
299
|
|
|
} |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
return false; |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
public function getSummary() : string |
306
|
|
|
{ |
307
|
|
|
$lines = array(); |
308
|
|
|
|
309
|
|
|
$lines[] = 'Collection #'.$this->getID(); |
310
|
|
|
$lines[] = 'Subject: '.get_class($this->subject); |
311
|
|
|
|
312
|
|
|
foreach($this->results as $result) |
313
|
|
|
{ |
314
|
|
|
$lines[] = ' - '.$result->getType().' #'.$result->getCode().' "'.$result->getMessage($result->getType()).'"'; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
return implode(PHP_EOL, $lines); |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
|