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 string */ |
11
|
|
|
protected $openMode = 'w+'; |
12
|
|
|
/** @var array */ |
13
|
|
|
protected $headers = []; |
14
|
|
|
|
15
|
|
|
public static function write($file, $data) |
16
|
|
|
{ |
17
|
|
|
$writer = new static($file); |
18
|
|
|
$writer->setData($data); |
19
|
|
|
$writer->closeDocument(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Sets headers and all rows on the CSV file and |
24
|
|
|
* then closes the file handle. |
25
|
|
|
* |
26
|
|
|
* Uses the first row's keys as headers. |
27
|
|
|
* |
28
|
|
|
* @param $data |
29
|
|
|
* |
30
|
|
|
* @return $this |
31
|
|
|
*/ |
32
|
|
|
public function setData(array $data) |
33
|
|
|
{ |
34
|
|
|
if ($this->hasHeaders()) { |
35
|
|
|
$this->setHeaders(array_keys(reset($data))); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$this->addRows($data); |
39
|
|
|
|
40
|
|
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param array $headers |
45
|
|
|
* |
46
|
|
|
* @return $this |
47
|
|
|
*/ |
48
|
|
|
public function setHeaders(array $headers) |
49
|
|
|
{ |
50
|
|
|
$this->createDocument(); |
51
|
|
|
|
52
|
|
|
$this->headers = $headers; |
53
|
|
|
|
54
|
|
|
$this->addRow($this->headers); |
55
|
|
|
|
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $header |
61
|
|
|
* |
62
|
|
|
* @return Writer |
63
|
|
|
*/ |
64
|
|
|
public function addHeader($header) |
65
|
|
|
{ |
66
|
|
|
$headers = $this->headers; |
67
|
|
|
|
68
|
|
|
$headers[] = $header; |
69
|
|
|
|
70
|
|
|
$this->setHeaders($headers); |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Adds multiple rows of data to the CSV file. |
77
|
|
|
* |
78
|
|
|
* @param array $rows |
79
|
|
|
*/ |
80
|
|
|
public function addRows(array $rows) |
81
|
|
|
{ |
82
|
|
|
foreach ($rows as $row) { |
83
|
|
|
$this->addRow($row); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Adds a row of data to the CSV file. |
89
|
|
|
* |
90
|
|
|
* @param array $row Row of data to add to the file. |
91
|
|
|
* |
92
|
|
|
* @return bool Whether the row was written to the file. |
93
|
|
|
*/ |
94
|
|
|
public function addRow(array $row) |
95
|
|
|
{ |
96
|
|
|
$result = $this->getDocument()->fwriteCsv($row); |
97
|
|
|
|
98
|
|
|
if ($result === false) { |
99
|
|
|
// @todo: handle error |
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return true; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
|
|
public function getContents() |
110
|
|
|
{ |
111
|
|
|
$this->getDocument()->trimFinalLineEnding(); |
112
|
|
|
$this->getDocument()->fseek(0); |
113
|
|
|
|
114
|
|
|
return $this->getDocument()->fread($this->getDocument()->getSize()); |
|
|
|
|
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.