1 | <?php |
||
26 | class WorksheetManager implements WorksheetManagerInterface |
||
27 | { |
||
28 | /** |
||
29 | * Maximum number of characters a cell can contain |
||
30 | * @see https://support.office.com/en-us/article/Excel-specifications-and-limits-16c69c74-3d6a-4aaf-ba35-e6eb276e8eaa [Excel 2007] |
||
31 | * @see https://support.office.com/en-us/article/Excel-specifications-and-limits-1672b34d-7043-467e-8e27-269d656771c3 [Excel 2010] |
||
32 | * @see https://support.office.com/en-us/article/Excel-specifications-and-limits-ca36e2dc-1f09-4620-b726-67c00b05040f [Excel 2013/2016] |
||
33 | */ |
||
34 | const MAX_CHARACTERS_PER_CELL = 32767; |
||
35 | |||
36 | const SHEET_XML_FILE_HEADER = <<<'EOD' |
||
37 | <?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
||
38 | <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"> |
||
39 | EOD; |
||
40 | |||
41 | /** @var bool Whether inline or shared strings should be used */ |
||
42 | protected $shouldUseInlineStrings; |
||
43 | |||
44 | /** @var RowManager Manages rows */ |
||
45 | private $rowManager; |
||
46 | |||
47 | /** @var StyleManager Manages styles */ |
||
48 | private $styleManager; |
||
49 | |||
50 | /** @var StyleMerger Helper to merge styles together */ |
||
51 | private $styleMerger; |
||
52 | |||
53 | /** @var SharedStringsManager Helper to write shared strings */ |
||
54 | private $sharedStringsManager; |
||
55 | |||
56 | /** @var XLSXEscaper Strings escaper */ |
||
57 | private $stringsEscaper; |
||
58 | |||
59 | /** @var StringHelper String helper */ |
||
60 | private $stringHelper; |
||
61 | |||
62 | /** @var InternalEntityFactory Factory to create entities */ |
||
63 | private $entityFactory; |
||
64 | |||
65 | /** |
||
66 | * WorksheetManager constructor. |
||
67 | * |
||
68 | * @param OptionsManagerInterface $optionsManager |
||
69 | * @param RowManager $rowManager |
||
70 | * @param StyleManager $styleManager |
||
71 | * @param StyleMerger $styleMerger |
||
72 | * @param SharedStringsManager $sharedStringsManager |
||
73 | * @param XLSXEscaper $stringsEscaper |
||
74 | * @param StringHelper $stringHelper |
||
75 | * @param InternalEntityFactory $entityFactory |
||
76 | */ |
||
77 | 39 | public function __construct( |
|
96 | |||
97 | /** |
||
98 | * @return SharedStringsManager |
||
99 | */ |
||
100 | 35 | public function getSharedStringsManager() |
|
104 | |||
105 | /** |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | 39 | public function startSheet(Worksheet $worksheet) |
|
118 | |||
119 | /** |
||
120 | * Checks if the sheet has been sucessfully created. Throws an exception if not. |
||
121 | * |
||
122 | * @param bool|resource $sheetFilePointer Pointer to the sheet data file or FALSE if unable to open the file |
||
123 | * @throws IOException If the sheet data file cannot be opened for writing |
||
124 | * @return void |
||
125 | */ |
||
126 | 39 | private function throwIfSheetFilePointerIsNotAvailable($sheetFilePointer) |
|
132 | |||
133 | /** |
||
134 | * {@inheritdoc} |
||
135 | */ |
||
136 | 32 | public function addRow(Worksheet $worksheet, Row $row) |
|
144 | |||
145 | /** |
||
146 | * Adds non empty row to the worksheet. |
||
147 | * |
||
148 | * @param Worksheet $worksheet The worksheet to add the row to |
||
149 | * @param Row $row The row to be written |
||
150 | * @throws IOException If the data cannot be written |
||
151 | * @throws InvalidArgumentException If a cell value's type is not supported |
||
152 | * @return void |
||
153 | */ |
||
154 | 32 | private function addNonEmptyRow(Worksheet $worksheet, Row $row) |
|
155 | { |
||
156 | 32 | $rowStyle = $row->getStyle(); |
|
157 | 32 | $rowIndexOneBased = $worksheet->getLastWrittenRowIndex() + 1; |
|
158 | 32 | $numCells = $row->getNumCells(); |
|
159 | |||
160 | 32 | $rowXML = '<row r="' . $rowIndexOneBased . '" spans="1:' . $numCells . '">'; |
|
161 | |||
162 | 32 | foreach ($row->getCells() as $columnIndexZeroBased => $cell) { |
|
163 | 32 | $rowXML .= $this->applyStyleAndGetCellXML($cell, $rowStyle, $rowIndexOneBased, $columnIndexZeroBased); |
|
164 | } |
||
165 | |||
166 | 31 | $rowXML .= '</row>'; |
|
167 | |||
168 | 31 | $wasWriteSuccessful = fwrite($worksheet->getFilePointer(), $rowXML); |
|
169 | 31 | if ($wasWriteSuccessful === false) { |
|
170 | throw new IOException("Unable to write data in {$worksheet->getFilePath()}"); |
||
171 | } |
||
172 | 31 | } |
|
173 | |||
174 | /** |
||
175 | * Applies styles to the given style, merging the cell's style with its row's style |
||
176 | * Then builds and returns xml for the cell. |
||
177 | * |
||
178 | * @param Cell $cell |
||
179 | * @param Style $rowStyle |
||
180 | * @param int $rowIndexOneBased |
||
181 | * @param int $columnIndexZeroBased |
||
182 | * |
||
183 | * @throws InvalidArgumentException If the given value cannot be processed |
||
184 | * @return string |
||
185 | */ |
||
186 | 32 | private function applyStyleAndGetCellXML(Cell $cell, Style $rowStyle, $rowIndexOneBased, $columnIndexZeroBased) |
|
187 | { |
||
188 | // Apply row and extra styles |
||
189 | 32 | $mergedCellAndRowStyle = $this->styleMerger->merge($cell->getStyle(), $rowStyle); |
|
190 | 32 | $cell->setStyle($mergedCellAndRowStyle); |
|
191 | 32 | $newCellStyle = $this->styleManager->applyExtraStylesIfNeeded($cell); |
|
192 | |||
193 | 32 | $registeredStyle = $this->styleManager->registerStyle($newCellStyle); |
|
194 | |||
195 | 32 | return $this->getCellXML($rowIndexOneBased, $columnIndexZeroBased, $cell, $registeredStyle->getId()); |
|
196 | } |
||
197 | |||
198 | /** |
||
199 | * Builds and returns xml for a single cell. |
||
200 | * |
||
201 | * @param int $rowIndexOneBased |
||
202 | * @param int $columnIndexZeroBased |
||
203 | * @param Cell $cell |
||
204 | * @param int $styleId |
||
205 | * |
||
206 | * @throws InvalidArgumentException If the given value cannot be processed |
||
207 | * @return string |
||
208 | */ |
||
209 | 32 | private function getCellXML($rowIndexOneBased, $columnIndexZeroBased, Cell $cell, $styleId) |
|
235 | |||
236 | /** |
||
237 | * Returns the XML fragment for a cell containing a non empty string |
||
238 | * |
||
239 | * @param string $cellValue The cell value |
||
240 | * @throws InvalidArgumentException If the string exceeds the maximum number of characters allowed per cell |
||
241 | * @return string The XML fragment representing the cell |
||
242 | */ |
||
243 | 31 | private function getCellXMLFragmentForNonEmptyString($cellValue) |
|
258 | |||
259 | /** |
||
260 | * {@inheritdoc} |
||
261 | */ |
||
262 | 35 | public function close(Worksheet $worksheet) |
|
274 | } |
||
275 |