|
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 array */ |
|
28
|
|
|
protected $rows = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param array $header |
|
32
|
|
|
*/ |
|
33
|
3 |
|
public function setHeader(array $header) |
|
34
|
|
|
{ |
|
35
|
3 |
|
$this->header = $header; |
|
36
|
3 |
|
array_unshift($this->rows, $header); |
|
37
|
3 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param array $row |
|
41
|
|
|
* @return WriterInterface |
|
42
|
|
|
*/ |
|
43
|
1 |
|
public function addRow(array $row): WriterInterface |
|
44
|
|
|
{ |
|
45
|
1 |
|
$this->rows[] = $row; |
|
46
|
1 |
|
return $this; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param array $rows |
|
51
|
|
|
* @throws InvalidInputArrayException |
|
52
|
|
|
*/ |
|
53
|
3 |
|
public function addRows(array $rows) |
|
54
|
|
|
{ |
|
55
|
3 |
|
foreach ($rows as $row) { |
|
56
|
3 |
|
if (!is_array($row)) { |
|
57
|
1 |
|
throw new InvalidInputArrayException('Element must be an array'); |
|
58
|
|
|
} |
|
59
|
2 |
|
$this->rows[] = $row; |
|
60
|
|
|
} |
|
61
|
2 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Write the CSV to the stream |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $filename |
|
67
|
|
|
* @throws \Exception |
|
68
|
|
|
*/ |
|
69
|
2 |
|
public function write(string $filename) |
|
70
|
|
|
{ |
|
71
|
2 |
|
$handle = $this->openStream($filename); |
|
72
|
|
|
|
|
73
|
2 |
|
foreach ($this->rows as $row) { |
|
74
|
2 |
|
fputcsv($handle, $row, $this->delimiter, $this->enclosure, $this->escape); |
|
75
|
|
|
|
|
76
|
2 |
|
if (is_file($filename)) { |
|
77
|
2 |
|
$this->updateNewLine($handle); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
2 |
|
$this->closeStream($handle); |
|
82
|
2 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param string $filename |
|
86
|
|
|
* @return bool|resource |
|
87
|
|
|
* @throws \Exception |
|
88
|
|
|
*/ |
|
89
|
2 |
|
private function openStream(string $filename) |
|
90
|
|
|
{ |
|
91
|
2 |
|
if ($handle = fopen($filename, 'w+')) { |
|
92
|
2 |
|
return $handle; |
|
93
|
|
|
} |
|
94
|
|
|
throw new \Exception(sprintf('Cannot open steam: %s', $filename)); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param $handle |
|
99
|
|
|
*/ |
|
100
|
2 |
|
private function closeStream($handle) |
|
101
|
|
|
{ |
|
102
|
2 |
|
fclose($handle); |
|
103
|
2 |
|
} |
|
104
|
|
|
|
|
105
|
2 |
|
private function updateNewLine($handle) |
|
106
|
|
|
{ |
|
107
|
2 |
|
if ((Newline::NEWLINE_LF !== $this->newline) && (0 === fseek($handle, -1, SEEK_CUR))) { |
|
108
|
1 |
|
fwrite($handle, $this->newline); |
|
109
|
|
|
} |
|
110
|
2 |
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|