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 | * {@inheritdoc} |
||
123 | */ |
||
124 | public function openToBrowser($outputFileName) |
||
154 | |||
155 | /** |
||
156 | * Checks if the pointer to the file/stream to write to is available. |
||
157 | * Will throw an exception if not available. |
||
158 | * |
||
159 | * @throws \Box\Spout\Common\Exception\IOException If the pointer is not available |
||
160 | * @return void |
||
161 | */ |
||
162 | 97 | protected function throwIfFilePointerIsNotAvailable() |
|
168 | |||
169 | /** |
||
170 | * Checks if the writer has already been opened, since some actions must be done before it gets opened. |
||
171 | * Throws an exception if already opened. |
||
172 | * |
||
173 | * @param string $message Error message |
||
174 | * @throws \Box\Spout\Writer\Exception\WriterAlreadyOpenedException If the writer was already opened and must not be. |
||
175 | * @return void |
||
176 | */ |
||
177 | 53 | protected function throwIfWriterAlreadyOpened($message) |
|
183 | |||
184 | /** |
||
185 | * {@inheritdoc} |
||
186 | */ |
||
187 | 81 | public function addRow(Row $row) |
|
209 | |||
210 | /** |
||
211 | * {@inheritdoc} |
||
212 | */ |
||
213 | public function withRow(\Closure $callback) |
||
217 | |||
218 | /** |
||
219 | * {@inheritdoc} |
||
220 | */ |
||
221 | 63 | public function addRows(array $dataRows) |
|
234 | |||
235 | /** |
||
236 | * @param array $dataRow |
||
237 | * @param Style|null $style |
||
238 | * @return Row |
||
239 | */ |
||
240 | protected function createRowFromArray(array $dataRow, Style $style = null) |
||
256 | |||
257 | /** |
||
258 | * @TODO: Move this into styleMerger |
||
259 | * |
||
260 | * @param Row $row |
||
261 | * @return $this |
||
262 | */ |
||
263 | 70 | private function applyDefaultRowStyle(Row $row) |
|
273 | |||
274 | /** |
||
275 | * Closes the writer. This will close the streamer as well, preventing new data |
||
276 | * to be written to the file. |
||
277 | * |
||
278 | * @return void |
||
279 | */ |
||
280 | 80 | public function close() |
|
294 | |||
295 | /** |
||
296 | * Closes the writer and attempts to cleanup all files that were |
||
297 | * created during the writing process (temp files & final file). |
||
298 | * |
||
299 | * @return void |
||
300 | */ |
||
301 | 6 | private function closeAndAttemptToCleanupAllFiles() |
|
313 | } |
||
314 |
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: