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 | 107 | 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) |
|
103 | |||
104 | /** |
||
105 | * @inheritdoc |
||
106 | */ |
||
107 | 96 | public function openToFile($outputFilePath) |
|
119 | |||
120 | /** |
||
121 | * @inheritdoc |
||
122 | */ |
||
123 | public function openToBrowser($outputFileName) |
||
153 | |||
154 | /** |
||
155 | * Checks if the pointer to the file/stream to write to is available. |
||
156 | * Will throw an exception if not available. |
||
157 | * |
||
158 | * @throws \Box\Spout\Common\Exception\IOException If the pointer is not available |
||
159 | * @return void |
||
160 | */ |
||
161 | 96 | protected function throwIfFilePointerIsNotAvailable() |
|
167 | |||
168 | /** |
||
169 | * Checks if the writer has already been opened, since some actions must be done before it gets opened. |
||
170 | * Throws an exception if already opened. |
||
171 | * |
||
172 | * @param string $message Error message |
||
173 | * @throws \Box\Spout\Writer\Exception\WriterAlreadyOpenedException If the writer was already opened and must not be. |
||
174 | * @return void |
||
175 | */ |
||
176 | 52 | protected function throwIfWriterAlreadyOpened($message) |
|
182 | |||
183 | /** |
||
184 | * @inheritdoc |
||
185 | */ |
||
186 | 80 | public function addRow(Row $row) |
|
206 | |||
207 | /** |
||
208 | * @inheritdoc |
||
209 | */ |
||
210 | public function withRow(\Closure $callback) |
||
214 | |||
215 | /** |
||
216 | * @inheritdoc |
||
217 | */ |
||
218 | 62 | public function addRows(array $dataRows) |
|
231 | |||
232 | /** |
||
233 | * @param array $dataRow |
||
234 | * @param Style|null $style |
||
235 | * @return Row |
||
236 | */ |
||
237 | protected function createRowFromArray(array $dataRow, Style $style = null) |
||
252 | |||
253 | /** |
||
254 | * @TODO: Move this into styleMerger |
||
255 | * |
||
256 | * @param Row $row |
||
257 | * @return $this |
||
258 | */ |
||
259 | 69 | private function applyDefaultRowStyle(Row $row) |
|
268 | |||
269 | /** |
||
270 | * Closes the writer. This will close the streamer as well, preventing new data |
||
271 | * to be written to the file. |
||
272 | * |
||
273 | * @api |
||
274 | * @return void |
||
275 | */ |
||
276 | 79 | public function close() |
|
290 | |||
291 | /** |
||
292 | * Closes the writer and attempts to cleanup all files that were |
||
293 | * created during the writing process (temp files & final file). |
||
294 | * |
||
295 | * @return void |
||
296 | */ |
||
297 | 6 | private function closeAndAttemptToCleanupAllFiles() |
|
309 | } |
||
310 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: