|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* CollectionWriter.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 on 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
|
|
|
|
|
18
|
|
|
namespace Pvra\Result; |
|
19
|
|
|
|
|
20
|
|
|
use Pvra\Result\Collection as ResultCollection; |
|
21
|
|
|
use Pvra\Result\Exceptions\ResultFileWriterException; |
|
22
|
|
|
use Pvra\Result\ResultFormatter\ResultFormatter; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Class CollectionWriter |
|
26
|
|
|
* |
|
27
|
|
|
* @package Pvra\Result |
|
28
|
|
|
*/ |
|
29
|
|
|
class CollectionWriter |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* @var \Pvra\Result\Collection |
|
33
|
|
|
*/ |
|
34
|
|
|
private $result; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param \Pvra\Result\Collection $result |
|
38
|
|
|
*/ |
|
39
|
24 |
|
public function __construct(ResultCollection $result) |
|
40
|
|
|
{ |
|
41
|
24 |
|
$this->result = $result; |
|
42
|
24 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param $fileName |
|
46
|
|
|
* @param \Pvra\Result\ResultFormatter\ResultFormatter $formatter |
|
47
|
|
|
* @param bool $overrideExistingFile |
|
48
|
|
|
* @throws \Pvra\Result\Exceptions\ResultFileWriterException |
|
49
|
|
|
*/ |
|
50
|
14 |
|
public function write($fileName, ResultFormatter $formatter, $overrideExistingFile = false) |
|
51
|
|
|
{ |
|
52
|
14 |
|
if ($overrideExistingFile !== true && file_exists($fileName)) { |
|
53
|
6 |
|
throw new ResultFileWriterException($fileName . ' already exists. Cannot override an already existing file!'); |
|
54
|
|
|
} |
|
55
|
8 |
|
$formatted = $formatter->makePrintable($this->result); |
|
56
|
8 |
|
file_put_contents($fileName, $formatted); |
|
57
|
8 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param $stream |
|
61
|
|
|
* @param \Pvra\Result\ResultFormatter\ResultFormatter $formatter |
|
62
|
|
|
*/ |
|
63
|
6 |
|
public function writeToStream($stream, ResultFormatter $formatter) |
|
64
|
|
|
{ |
|
65
|
6 |
|
if (!is_resource($stream)) { |
|
66
|
2 |
|
throw new \InvalidArgumentException(sprintf('Parameter 1 of method CollectionWriter::writeToStream has to be a resource. %s given', |
|
67
|
1 |
|
gettype($stream))); |
|
68
|
|
|
} |
|
69
|
4 |
|
$formatted = $formatter->makePrintable($this->result); |
|
70
|
4 |
|
fwrite($stream, $formatted); |
|
71
|
4 |
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|