1 | <?php |
||
39 | class Writer |
||
40 | { |
||
41 | /** |
||
42 | * The flavor (format) of CSV to write. |
||
43 | * |
||
44 | * @var Flavor |
||
45 | */ |
||
46 | protected $flavor; |
||
47 | |||
48 | /** |
||
49 | * The output stream to write to. |
||
50 | * |
||
51 | * @var Contract\Streamable |
||
52 | */ |
||
53 | protected $output; |
||
54 | |||
55 | /** |
||
56 | * The header row. |
||
57 | * |
||
58 | * @var \Iterator |
||
59 | */ |
||
60 | protected $headers; |
||
61 | |||
62 | /** |
||
63 | * Number of lines written so far (not including header). |
||
64 | * |
||
65 | * @var int |
||
66 | */ |
||
67 | protected $written = 0; |
||
68 | |||
69 | /** |
||
70 | * Class Constructor. |
||
71 | * |
||
72 | * @param Contract\Streamable $output An output source to write to |
||
73 | * @param Flavor|array $flavor A flavor or set of formatting params |
||
74 | */ |
||
75 | 17 | public function __construct(Streamable $output, $flavor = null) |
|
83 | |||
84 | /** |
||
85 | * Get the CSV flavor (or dialect) for this writer. |
||
86 | * |
||
87 | * @param void |
||
88 | * |
||
89 | * @return Flavor |
||
90 | */ |
||
91 | 16 | public function getFlavor() |
|
95 | |||
96 | /** |
||
97 | * Sets the header row |
||
98 | * If any data has been written to the output, it is too late to write the |
||
99 | * header row and an exception will be thrown. Later implementations will |
||
100 | * likely buffer the output so that this may be called after writeRows(). |
||
101 | * |
||
102 | * @param \Iterator|array A list of header values |
||
103 | * @param mixed $headers |
||
104 | * |
||
105 | * @throws Exception\WriterException |
||
106 | * |
||
107 | * @return $this |
||
108 | */ |
||
109 | 2 | public function setHeaderRow($headers) |
|
121 | |||
122 | /** |
||
123 | * Write a single row. |
||
124 | * |
||
125 | * @param \Iterator|array $row The row to write to source |
||
126 | * |
||
127 | * @return int The number or bytes written |
||
128 | */ |
||
129 | 14 | public function writeRow($row) |
|
149 | |||
150 | /** |
||
151 | * Write multiple rows. |
||
152 | * |
||
153 | * @param \Iterator|array $rows List of \Iterable|array |
||
154 | * |
||
155 | * @return int number of lines written |
||
156 | */ |
||
157 | 11 | public function writeRows($rows) |
|
177 | |||
178 | /** |
||
179 | * Write the header row. |
||
180 | * |
||
181 | * @param HeaderRow $row |
||
182 | * |
||
183 | * @return int|false |
||
184 | */ |
||
185 | 3 | protected function writeHeaderRow(HeaderRow $row) |
|
193 | |||
194 | /** |
||
195 | * Prepare a row of data to be written |
||
196 | * This means taking an array of data, and converting it to a Row object. |
||
197 | * |
||
198 | * @param \Iterator $row of data items |
||
199 | * |
||
200 | * @return AbstractRow |
||
201 | */ |
||
202 | 14 | protected function prepareRow(Iterator $row) |
|
212 | |||
213 | /** |
||
214 | * Prepare a cell of data to be written (convert to Data object). |
||
215 | * |
||
216 | * @param string $data A string containing cell data |
||
217 | * |
||
218 | * @return string quoted string data |
||
219 | */ |
||
220 | 14 | protected function prepareData($data) |
|
227 | |||
228 | /** |
||
229 | * Enclose a string in quotes. |
||
230 | * |
||
231 | * Accepts a string and returns it with quotes around it. |
||
232 | * |
||
233 | * @param string $str The string to wrap in quotes |
||
234 | * |
||
235 | * @return string |
||
236 | */ |
||
237 | 14 | protected function quoteString($str) |
|
273 | |||
274 | /** |
||
275 | * Escape a string. |
||
276 | * |
||
277 | * Return a string with all special characters escaped. |
||
278 | * |
||
279 | * @param string $str The string to escape |
||
280 | * @param bool $isQuoted True if string is quoted |
||
281 | * |
||
282 | * @return string |
||
283 | */ |
||
284 | 14 | protected function escapeString($str, $isQuoted = true) |
|
294 | } |
||
295 |