1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Palmtree\Csv; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Writes an array to a CSV file. |
7
|
|
|
*/ |
8
|
|
|
class Writer extends AbstractCsv |
9
|
|
|
{ |
10
|
|
|
/** @var array */ |
11
|
|
|
private $headers = []; |
12
|
|
|
|
13
|
1 |
|
public function getOpenMode() |
14
|
|
|
{ |
15
|
1 |
|
return 'w+'; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public static function write($file, $data) |
19
|
|
|
{ |
20
|
|
|
$writer = new static($file); |
21
|
|
|
$writer->setData($data); |
22
|
|
|
$writer->closeDocument(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Sets headers and all rows on the CSV file and |
27
|
|
|
* then closes the file handle. |
28
|
|
|
* |
29
|
|
|
* Uses the first row's keys as headers. |
30
|
|
|
* |
31
|
|
|
* @param $data |
32
|
|
|
* |
33
|
|
|
* @return $this |
34
|
|
|
*/ |
35
|
|
|
public function setData(array $data) |
36
|
|
|
{ |
37
|
|
|
if ($this->hasHeaders()) { |
38
|
|
|
$this->setHeaders(\array_keys(\reset($data))); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$this->addRows($data); |
42
|
|
|
|
43
|
|
|
return $this; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param array $headers |
48
|
|
|
* |
49
|
|
|
* @return $this |
50
|
|
|
*/ |
51
|
|
|
public function setHeaders(array $headers) |
52
|
|
|
{ |
53
|
|
|
$this->createDocument(); |
54
|
|
|
|
55
|
|
|
$this->headers = $headers; |
56
|
|
|
|
57
|
|
|
$this->addRow($this->headers); |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $header |
64
|
|
|
* |
65
|
|
|
* @return Writer |
66
|
|
|
*/ |
67
|
|
|
public function addHeader($header) |
68
|
|
|
{ |
69
|
|
|
$headers = $this->headers; |
70
|
|
|
|
71
|
|
|
$headers[] = $header; |
72
|
|
|
|
73
|
|
|
$this->setHeaders($headers); |
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Adds multiple rows of data to the CSV file. |
80
|
|
|
* |
81
|
|
|
* @param array $rows |
82
|
|
|
*/ |
83
|
|
|
public function addRows(array $rows) |
84
|
|
|
{ |
85
|
|
|
foreach ($rows as $row) { |
86
|
|
|
$this->addRow($row); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Adds a row of data to the CSV file. |
92
|
|
|
* |
93
|
|
|
* @param array $row Row of data to add to the file. |
94
|
|
|
* |
95
|
|
|
* @return bool Whether the row was written to the file. |
96
|
|
|
*/ |
97
|
|
|
public function addRow(array $row) |
98
|
|
|
{ |
99
|
|
|
$result = $this->getDocument()->fwriteCsv($row); |
100
|
|
|
|
101
|
|
|
if ($result === false) { |
102
|
|
|
// @todo: handle error |
103
|
|
|
return false; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return true; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public function getContents() |
113
|
|
|
{ |
114
|
|
|
$this->getDocument()->trimFinalLineEnding(); |
115
|
|
|
$this->getDocument()->fseek(0); |
116
|
|
|
|
117
|
|
|
return $this->getDocument()->fread($this->getDocument()->getSize()); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|