|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Csv-Machine package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Dan McAdams <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace RoadBunch\Csv; |
|
13
|
|
|
|
|
14
|
|
|
use RoadBunch\Csv\Exception\InvalidInputArrayException; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class Writer |
|
18
|
|
|
* |
|
19
|
|
|
* @author Dan McAdams |
|
20
|
|
|
* @package RoadBunch\Csv |
|
21
|
|
|
*/ |
|
22
|
|
|
class Writer extends Csv implements WriterInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var array */ |
|
25
|
|
|
protected $header; |
|
26
|
|
|
|
|
27
|
|
|
/** @var string */ |
|
28
|
|
|
protected $filename; |
|
29
|
|
|
|
|
30
|
|
|
/** @var array */ |
|
31
|
|
|
protected $rows = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param array $header |
|
35
|
|
|
*/ |
|
36
|
2 |
|
public function setHeader(array $header) |
|
37
|
|
|
{ |
|
38
|
2 |
|
$this->header = $header; |
|
39
|
2 |
|
array_unshift($this->rows, $header); |
|
40
|
2 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Writer constructor. |
|
44
|
|
|
* |
|
45
|
|
|
* @param string $filename |
|
46
|
|
|
* @throws \Exception |
|
47
|
|
|
*/ |
|
48
|
6 |
|
public function __construct(string $filename) |
|
49
|
|
|
{ |
|
50
|
6 |
|
$this->filename = $filename; |
|
51
|
|
|
|
|
52
|
|
|
// attempt to open the file one time |
|
53
|
|
|
// this will create the file if it doesn't exist |
|
54
|
6 |
|
$handle = $this->openStream(); |
|
55
|
6 |
|
$this->closeStream($handle); |
|
56
|
6 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param array $row |
|
60
|
|
|
* @return WriterInterface |
|
61
|
|
|
*/ |
|
62
|
1 |
|
public function addRow(array $row): WriterInterface |
|
63
|
|
|
{ |
|
64
|
1 |
|
$this->rows[] = $row; |
|
65
|
1 |
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @param array $rows |
|
70
|
|
|
* @throws InvalidInputArrayException |
|
71
|
|
|
*/ |
|
72
|
2 |
|
public function addRows(array $rows) |
|
73
|
|
|
{ |
|
74
|
2 |
|
foreach ($rows as $row) { |
|
75
|
2 |
|
if (!is_array($row)) { |
|
76
|
1 |
|
throw new InvalidInputArrayException('Element must be an array'); |
|
77
|
|
|
} |
|
78
|
1 |
|
$this->rows[] = $row; |
|
79
|
|
|
} |
|
80
|
1 |
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return bool|resource |
|
84
|
|
|
* @throws \Exception |
|
85
|
|
|
*/ |
|
86
|
6 |
|
private function openStream() |
|
87
|
|
|
{ |
|
88
|
6 |
|
$handle = fopen($this->filename, 'a'); |
|
89
|
6 |
|
if (false === $handle) { |
|
90
|
|
|
throw new \Exception(sprintf('Cannot open steam: %s', $this->filename)); |
|
91
|
|
|
} |
|
92
|
6 |
|
return $handle; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
6 |
|
private function closeStream($handle) |
|
96
|
|
|
{ |
|
97
|
6 |
|
fclose($handle); |
|
98
|
6 |
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|