1 | <?php |
||
8 | class Writer extends AbstractCsv |
||
9 | { |
||
10 | /** @var string */ |
||
11 | protected $fopenMode = 'w+'; |
||
12 | /** @var string */ |
||
13 | protected $lineEnding = "\r\n"; |
||
14 | /** @var int */ |
||
15 | protected $bytesWritten = 0; |
||
16 | |||
17 | public static function write($file, $data) |
||
23 | |||
24 | /** |
||
25 | * @inheritDoc |
||
26 | */ |
||
27 | 1 | public function closeDocument() |
|
28 | { |
||
29 | 1 | $this->trimTrailingLineEnding(); |
|
30 | |||
31 | 1 | parent::closeDocument(); |
|
32 | 1 | } |
|
33 | |||
34 | /** |
||
35 | * Sets headers and all rows on the CSV file and |
||
36 | * then closes the file handle. |
||
37 | * |
||
38 | * @param $data |
||
39 | */ |
||
40 | public function setData($data) |
||
48 | |||
49 | /** |
||
50 | * @param array $headers |
||
51 | */ |
||
52 | public function setHeaders(array $headers) |
||
58 | |||
59 | /** |
||
60 | * Adds multiple rows of data to the CSV file. |
||
61 | * |
||
62 | * @param array $rows |
||
63 | */ |
||
64 | public function addRows($rows) |
||
70 | |||
71 | /** |
||
72 | * Adds a row of data to the CSV file. |
||
73 | * |
||
74 | * @param array $row Row of data to add to the file. |
||
75 | * |
||
76 | * @return bool Whether the row was written to the file. |
||
77 | */ |
||
78 | public function addRow($row) |
||
95 | |||
96 | /** |
||
97 | * Returns the line ending delimiter used to separate rows |
||
98 | * in the CSV file. |
||
99 | * |
||
100 | * @return string |
||
101 | */ |
||
102 | public function getLineEnding() |
||
106 | |||
107 | /** |
||
108 | * Sets the line ending delimiter used to separate rows |
||
109 | * in the CSV file. |
||
110 | * |
||
111 | * @param string $lineEnding |
||
112 | * |
||
113 | * @return Writer |
||
114 | */ |
||
115 | public function setLineEnding($lineEnding) |
||
121 | |||
122 | /** |
||
123 | * Returns a string representation of a row to be written |
||
124 | * as a line in a CSV file. |
||
125 | * |
||
126 | * @param array $row |
||
127 | * |
||
128 | * @return string |
||
129 | */ |
||
130 | protected function getCsvString($row) |
||
142 | |||
143 | /** |
||
144 | * Trims the line ending delimiter from the end of the CSV file. |
||
145 | * RFC-4180 states CSV files should not contain a trailing new line. |
||
146 | */ |
||
147 | 1 | protected function trimTrailingLineEnding() |
|
161 | |||
162 | /** |
||
163 | * Escapes the enclosure character recursively. |
||
164 | * RFC-4180 states the enclosure character (usually double quotes) should be |
||
165 | * escaped by itself, so " becomes "". |
||
166 | * |
||
167 | * @param mixed $data Array or string of data to escape. |
||
168 | * |
||
169 | * @return mixed Escaped data |
||
170 | */ |
||
171 | protected function escapeEnclosure($data) |
||
183 | } |
||
184 |