1 | <?php |
||
22 | abstract class WriterAbstract implements WriterInterface |
||
23 | { |
||
24 | /** @var string Path to the output file */ |
||
25 | protected $outputFilePath; |
||
26 | |||
27 | /** @var resource Pointer to the file/stream we will write to */ |
||
28 | protected $filePointer; |
||
29 | |||
30 | /** @var bool Indicates whether the writer has been opened or not */ |
||
31 | protected $isWriterOpened = false; |
||
32 | |||
33 | /** @var GlobalFunctionsHelper Helper to work with global functions */ |
||
34 | protected $globalFunctionsHelper; |
||
35 | |||
36 | /** @var HelperFactory $helperFactory */ |
||
37 | protected $helperFactory; |
||
38 | |||
39 | /** @var OptionsManagerInterface Writer options manager */ |
||
40 | protected $optionsManager; |
||
41 | |||
42 | /** @var string Content-Type value for the header - to be defined by child class */ |
||
43 | protected static $headerContentType; |
||
44 | |||
45 | /** |
||
46 | * @param OptionsManagerInterface $optionsManager |
||
47 | * @param GlobalFunctionsHelper $globalFunctionsHelper |
||
48 | * @param HelperFactory $helperFactory |
||
49 | */ |
||
50 | 94 | public function __construct( |
|
51 | OptionsManagerInterface $optionsManager, |
||
52 | GlobalFunctionsHelper $globalFunctionsHelper, |
||
53 | HelperFactory $helperFactory |
||
54 | ) { |
||
55 | 94 | $this->optionsManager = $optionsManager; |
|
56 | 94 | $this->globalFunctionsHelper = $globalFunctionsHelper; |
|
57 | 94 | $this->helperFactory = $helperFactory; |
|
58 | 94 | } |
|
59 | |||
60 | /** |
||
61 | * Opens the streamer and makes it ready to accept data. |
||
62 | * |
||
63 | * @throws IOException If the writer cannot be opened |
||
64 | * @return void |
||
65 | */ |
||
66 | abstract protected function openWriter(); |
||
67 | |||
68 | /** |
||
69 | * Adds a row to the currently opened writer. |
||
70 | * |
||
71 | * @param Row $row The row containing cells and styles |
||
72 | * @throws WriterNotOpenedException If the workbook is not created yet |
||
73 | * @throws IOException If unable to write data |
||
74 | * @return void |
||
75 | */ |
||
76 | abstract protected function addRowToWriter(Row $row); |
||
77 | |||
78 | /** |
||
79 | * Closes the streamer, preventing any additional writing. |
||
80 | * |
||
81 | * @return void |
||
82 | */ |
||
83 | abstract protected function closeWriter(); |
||
84 | |||
85 | /** |
||
86 | * {@inheritdoc} |
||
87 | */ |
||
88 | 2 | public function setDefaultRowStyle(Style $defaultStyle) |
|
94 | |||
95 | /** |
||
96 | * {@inheritdoc} |
||
97 | */ |
||
98 | 83 | public function openToFile($outputFilePath) |
|
110 | |||
111 | /** |
||
112 | * @codeCoverageIgnore |
||
113 | * {@inheritdoc} |
||
114 | */ |
||
115 | public function openToBrowser($outputFileName) |
||
145 | |||
146 | /** |
||
147 | * Checks if the pointer to the file/stream to write to is available. |
||
148 | * Will throw an exception if not available. |
||
149 | * |
||
150 | * @throws IOException If the pointer is not available |
||
151 | * @return void |
||
152 | */ |
||
153 | 83 | protected function throwIfFilePointerIsNotAvailable() |
|
159 | |||
160 | /** |
||
161 | * Checks if the writer has already been opened, since some actions must be done before it gets opened. |
||
162 | * Throws an exception if already opened. |
||
163 | * |
||
164 | * @param string $message Error message |
||
165 | * @throws WriterAlreadyOpenedException If the writer was already opened and must not be. |
||
166 | * @return void |
||
167 | */ |
||
168 | 49 | protected function throwIfWriterAlreadyOpened($message) |
|
174 | |||
175 | /** |
||
176 | * {@inheritdoc} |
||
177 | */ |
||
178 | 75 | public function addRow(Row $row) |
|
197 | |||
198 | /** |
||
199 | * {@inheritdoc} |
||
200 | */ |
||
201 | 63 | public function addRows(array $rows) |
|
214 | |||
215 | /** |
||
216 | * {@inheritdoc} |
||
217 | */ |
||
218 | 74 | public function close() |
|
232 | |||
233 | /** |
||
234 | * Closes the writer and attempts to cleanup all files that were |
||
235 | * created during the writing process (temp files & final file). |
||
236 | * |
||
237 | * @return void |
||
238 | */ |
||
239 | 6 | private function closeAndAttemptToCleanupAllFiles() |
|
251 | } |
||
252 |