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