1 | <?php |
||
25 | abstract class WriterAbstract implements WriterInterface |
||
26 | { |
||
27 | /** @var string Path to the output file */ |
||
28 | protected $outputFilePath; |
||
29 | |||
30 | /** @var resource Pointer to the file/stream we will write to */ |
||
31 | protected $filePointer; |
||
32 | |||
33 | /** @var bool Indicates whether the writer has been opened or not */ |
||
34 | protected $isWriterOpened = false; |
||
35 | |||
36 | /** @var GlobalFunctionsHelper Helper to work with global functions */ |
||
37 | protected $globalFunctionsHelper; |
||
38 | |||
39 | /** @var HelperFactory $helperFactory */ |
||
40 | protected $helperFactory; |
||
41 | |||
42 | /** @var OptionsManagerInterface Writer options manager */ |
||
43 | protected $optionsManager; |
||
44 | |||
45 | /** @var StyleMerger Helps merge styles together */ |
||
46 | protected $styleMerger; |
||
47 | |||
48 | /** @var string Content-Type value for the header - to be defined by child class */ |
||
49 | protected static $headerContentType; |
||
50 | |||
51 | /** |
||
52 | * @param OptionsManagerInterface $optionsManager |
||
53 | * @param StyleMerger $styleMerger |
||
54 | * @param GlobalFunctionsHelper $globalFunctionsHelper |
||
55 | * @param HelperFactory $helperFactory |
||
56 | */ |
||
57 | 108 | public function __construct( |
|
68 | |||
69 | /** |
||
70 | * Opens the streamer and makes it ready to accept data. |
||
71 | * |
||
72 | * @throws \Box\Spout\Common\Exception\IOException If the writer cannot be opened |
||
73 | * @return void |
||
74 | */ |
||
75 | abstract protected function openWriter(); |
||
76 | |||
77 | /** |
||
78 | * Adds a row to the currently opened writer. |
||
79 | * |
||
80 | * @param Row $row The row containing cells and styles |
||
81 | * @return void |
||
82 | */ |
||
83 | abstract protected function addRowToWriter(Row $row); |
||
84 | |||
85 | /** |
||
86 | * Closes the streamer, preventing any additional writing. |
||
87 | * |
||
88 | * @return void |
||
89 | */ |
||
90 | abstract protected function closeWriter(); |
||
91 | |||
92 | /** |
||
93 | * Sets the default styles for all rows added with "addRow" |
||
94 | * |
||
95 | * @param Style $defaultStyle |
||
96 | * @return WriterAbstract |
||
97 | */ |
||
98 | 2 | public function setDefaultRowStyle($defaultStyle) |
|
104 | |||
105 | /** |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | 97 | public function openToFile($outputFilePath) |
|
120 | |||
121 | /** |
||
122 | * @codeCoverageIgnore |
||
123 | * |
||
124 | * {@inheritdoc} |
||
125 | */ |
||
126 | public function openToBrowser($outputFileName) |
||
156 | |||
157 | /** |
||
158 | * Checks if the pointer to the file/stream to write to is available. |
||
159 | * Will throw an exception if not available. |
||
160 | * |
||
161 | * @throws \Box\Spout\Common\Exception\IOException If the pointer is not available |
||
162 | * @return void |
||
163 | */ |
||
164 | 97 | protected function throwIfFilePointerIsNotAvailable() |
|
170 | |||
171 | /** |
||
172 | * Checks if the writer has already been opened, since some actions must be done before it gets opened. |
||
173 | * Throws an exception if already opened. |
||
174 | * |
||
175 | * @param string $message Error message |
||
176 | * @throws \Box\Spout\Writer\Exception\WriterAlreadyOpenedException If the writer was already opened and must not be. |
||
177 | * @return void |
||
178 | */ |
||
179 | 53 | protected function throwIfWriterAlreadyOpened($message) |
|
185 | |||
186 | /** |
||
187 | * {@inheritdoc} |
||
188 | */ |
||
189 | 81 | public function addRow(Row $row) |
|
211 | |||
212 | /** |
||
213 | * {@inheritdoc} |
||
214 | */ |
||
215 | 63 | public function addRows(array $dataRows) |
|
228 | |||
229 | /** |
||
230 | * @TODO: Move this into styleMerger |
||
231 | * |
||
232 | * @param Row $row |
||
233 | * @return $this |
||
234 | */ |
||
235 | 70 | private function applyDefaultRowStyle(Row $row) |
|
245 | |||
246 | /** |
||
247 | * Closes the writer. This will close the streamer as well, preventing new data |
||
248 | * to be written to the file. |
||
249 | * |
||
250 | * @return void |
||
251 | */ |
||
252 | 80 | public function close() |
|
266 | |||
267 | /** |
||
268 | * Closes the writer and attempts to cleanup all files that were |
||
269 | * created during the writing process (temp files & final file). |
||
270 | * |
||
271 | * @return void |
||
272 | */ |
||
273 | 6 | private function closeAndAttemptToCleanupAllFiles() |
|
285 | } |
||
286 |