1 | <?php |
||
20 | abstract class WriterAbstract implements WriterInterface |
||
21 | { |
||
22 | /** @var string Path to the output file */ |
||
23 | protected $outputFilePath; |
||
24 | |||
25 | /** @var resource Pointer to the file/stream we will write to */ |
||
26 | protected $filePointer; |
||
27 | |||
28 | /** @var bool Indicates whether the writer has been opened or not */ |
||
29 | protected $isWriterOpened = false; |
||
30 | |||
31 | /** @var \Box\Spout\Common\Helper\GlobalFunctionsHelper Helper to work with global functions */ |
||
32 | protected $globalFunctionsHelper; |
||
33 | |||
34 | /** @var \Box\Spout\Writer\Common\Manager\OptionsManagerInterface Writer options manager */ |
||
35 | protected $optionsManager; |
||
36 | |||
37 | /** @var Style\Style Style to be applied to the next written row(s) */ |
||
38 | protected $rowStyle; |
||
39 | |||
40 | /** @var string Content-Type value for the header - to be defined by child class */ |
||
41 | protected static $headerContentType; |
||
42 | |||
43 | /** |
||
44 | * Opens the streamer and makes it ready to accept data. |
||
45 | * |
||
46 | * @return void |
||
47 | * @throws \Box\Spout\Common\Exception\IOException If the writer cannot be opened |
||
48 | */ |
||
49 | abstract protected function openWriter(); |
||
50 | |||
51 | /** |
||
52 | * Adds data to the currently openned writer. |
||
53 | * |
||
54 | * @param array $dataRow Array containing data to be streamed. |
||
55 | * Example $dataRow = ['data1', 1234, null, '', 'data5']; |
||
56 | * @param Style\Style $style Style to be applied to the written row |
||
57 | * @return void |
||
58 | */ |
||
59 | abstract protected function addRowToWriter(array $dataRow, $style); |
||
60 | |||
61 | /** |
||
62 | * Closes the streamer, preventing any additional writing. |
||
63 | * |
||
64 | * @return void |
||
65 | */ |
||
66 | abstract protected function closeWriter(); |
||
67 | |||
68 | /** |
||
69 | * @param \Box\Spout\Writer\Common\Manager\OptionsManagerInterface $optionsManager |
||
70 | */ |
||
71 | 112 | public function __construct(OptionsManagerInterface $optionsManager) |
|
76 | |||
77 | /** |
||
78 | * Sets the default styles for all rows added with "addRow". |
||
79 | * Overriding the default style instead of using "addRowWithStyle" improves performance by 20%. |
||
80 | * @see https://github.com/box/spout/issues/272 |
||
81 | * |
||
82 | * @param Style\Style $defaultStyle |
||
83 | * @return WriterAbstract |
||
84 | */ |
||
85 | 2 | public function setDefaultRowStyle($defaultStyle) |
|
91 | |||
92 | /** |
||
93 | * @param \Box\Spout\Common\Helper\GlobalFunctionsHelper $globalFunctionsHelper |
||
94 | * @return WriterAbstract |
||
95 | */ |
||
96 | 112 | public function setGlobalFunctionsHelper($globalFunctionsHelper) |
|
101 | |||
102 | /** |
||
103 | * Inits the writer and opens it to accept data. |
||
104 | * By using this method, the data will be written to a file. |
||
105 | * |
||
106 | * @api |
||
107 | * @param string $outputFilePath Path of the output file that will contain the data |
||
108 | * @return WriterAbstract |
||
109 | * @throws \Box\Spout\Common\Exception\IOException If the writer cannot be opened or if the given path is not writable |
||
110 | */ |
||
111 | 101 | public function openToFile($outputFilePath) |
|
123 | |||
124 | /** |
||
125 | * Inits the writer and opens it to accept data. |
||
126 | * By using this method, the data will be outputted directly to the browser. |
||
127 | * |
||
128 | * @codeCoverageIgnore |
||
129 | * |
||
130 | * @api |
||
131 | * @param string $outputFileName Name of the output file that will contain the data. If a path is passed in, only the file name will be kept |
||
132 | * @return WriterAbstract |
||
133 | * @throws \Box\Spout\Common\Exception\IOException If the writer cannot be opened |
||
134 | */ |
||
135 | public function openToBrowser($outputFileName) |
||
165 | |||
166 | /** |
||
167 | * Checks if the pointer to the file/stream to write to is available. |
||
168 | * Will throw an exception if not available. |
||
169 | * |
||
170 | * @return void |
||
171 | * @throws \Box\Spout\Common\Exception\IOException If the pointer is not available |
||
172 | */ |
||
173 | 101 | protected function throwIfFilePointerIsNotAvailable() |
|
179 | |||
180 | /** |
||
181 | * Checks if the writer has already been opened, since some actions must be done before it gets opened. |
||
182 | * Throws an exception if already opened. |
||
183 | * |
||
184 | * @param string $message Error message |
||
185 | * @return void |
||
186 | * @throws \Box\Spout\Writer\Exception\WriterAlreadyOpenedException If the writer was already opened and must not be. |
||
187 | */ |
||
188 | 53 | protected function throwIfWriterAlreadyOpened($message) |
|
194 | |||
195 | /** |
||
196 | * Write given data to the output. New data will be appended to end of stream. |
||
197 | * |
||
198 | * @param array $dataRow Array containing data to be streamed. |
||
199 | * If empty, no data is added (i.e. not even as a blank row) |
||
200 | * Example: $dataRow = ['data1', 1234, null, '', 'data5', false]; |
||
201 | * @api |
||
202 | * @return WriterAbstract |
||
203 | * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If this function is called before opening the writer |
||
204 | * @throws \Box\Spout\Common\Exception\IOException If unable to write data |
||
205 | * @throws \Box\Spout\Common\Exception\SpoutException If anything else goes wrong while writing data |
||
206 | */ |
||
207 | 82 | public function addRow(array $dataRow) |
|
229 | |||
230 | /** |
||
231 | * Write given data to the output and apply the given style. |
||
232 | * @see addRow |
||
233 | * |
||
234 | * @api |
||
235 | * @param array $dataRow Array of array containing data to be streamed. |
||
236 | * @param Style\Style $style Style to be applied to the row. |
||
237 | * @return WriterAbstract |
||
238 | * @throws \Box\Spout\Common\Exception\InvalidArgumentException If the input param is not valid |
||
239 | * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If this function is called before opening the writer |
||
240 | * @throws \Box\Spout\Common\Exception\IOException If unable to write data |
||
241 | */ |
||
242 | 19 | public function addRowWithStyle(array $dataRow, $style) |
|
254 | |||
255 | /** |
||
256 | * Write given data to the output. New data will be appended to end of stream. |
||
257 | * |
||
258 | * @api |
||
259 | * @param array $dataRows Array of array containing data to be streamed. |
||
260 | * If a row is empty, it won't be added (i.e. not even as a blank row) |
||
261 | * Example: $dataRows = [ |
||
262 | * ['data11', 12, , '', 'data13'], |
||
263 | * ['data21', 'data22', null, false], |
||
264 | * ]; |
||
265 | * @return WriterAbstract |
||
266 | * @throws \Box\Spout\Common\Exception\InvalidArgumentException If the input param is not valid |
||
267 | * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If this function is called before opening the writer |
||
268 | * @throws \Box\Spout\Common\Exception\IOException If unable to write data |
||
269 | */ |
||
270 | 61 | public function addRows(array $dataRows) |
|
285 | |||
286 | /** |
||
287 | * Write given data to the output and apply the given style. |
||
288 | * @see addRows |
||
289 | * |
||
290 | * @api |
||
291 | * @param array $dataRows Array of array containing data to be streamed. |
||
292 | * @param Style\Style $style Style to be applied to the rows. |
||
293 | * @return WriterAbstract |
||
294 | * @throws \Box\Spout\Common\Exception\InvalidArgumentException If the input param is not valid |
||
295 | * @throws \Box\Spout\Writer\Exception\WriterNotOpenedException If this function is called before opening the writer |
||
296 | * @throws \Box\Spout\Common\Exception\IOException If unable to write data |
||
297 | */ |
||
298 | 16 | public function addRowsWithStyle(array $dataRows, $style) |
|
310 | |||
311 | /** |
||
312 | * Sets the style to be applied to the next written rows |
||
313 | * until it is changed or reset. |
||
314 | * |
||
315 | * @param Style\Style $style |
||
316 | * @return void |
||
317 | */ |
||
318 | 23 | private function setRowStyle($style) |
|
324 | |||
325 | /** |
||
326 | * Resets the style to be applied to the next written rows. |
||
327 | * |
||
328 | * @return void |
||
329 | */ |
||
330 | 112 | private function resetRowStyleToDefault() |
|
334 | |||
335 | /** |
||
336 | * Closes the writer. This will close the streamer as well, preventing new data |
||
337 | * to be written to the file. |
||
338 | * |
||
339 | * @api |
||
340 | * @return void |
||
341 | */ |
||
342 | 79 | public function close() |
|
356 | |||
357 | /** |
||
358 | * Closes the writer and attempts to cleanup all files that were |
||
359 | * created during the writing process (temp files & final file). |
||
360 | * |
||
361 | * @return void |
||
362 | */ |
||
363 | 5 | private function closeAndAttemptToCleanupAllFiles() |
|
375 | } |
||
376 |