1 | <?php |
||
27 | class WorksheetManager implements WorksheetManagerInterface |
||
28 | { |
||
29 | /** |
||
30 | * Maximum number of characters a cell can contain |
||
31 | * @see https://support.office.com/en-us/article/Excel-specifications-and-limits-16c69c74-3d6a-4aaf-ba35-e6eb276e8eaa [Excel 2007] |
||
32 | * @see https://support.office.com/en-us/article/Excel-specifications-and-limits-1672b34d-7043-467e-8e27-269d656771c3 [Excel 2010] |
||
33 | * @see https://support.office.com/en-us/article/Excel-specifications-and-limits-ca36e2dc-1f09-4620-b726-67c00b05040f [Excel 2013/2016] |
||
34 | */ |
||
35 | const MAX_CHARACTERS_PER_CELL = 32767; |
||
36 | |||
37 | const SHEET_XML_FILE_HEADER = <<<'EOD' |
||
38 | <?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
||
39 | <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"> |
||
40 | EOD; |
||
41 | |||
42 | /** @var bool Whether inline or shared strings should be used */ |
||
43 | protected $shouldUseInlineStrings; |
||
44 | |||
45 | /** @var RowManager Manages rows */ |
||
46 | private $rowManager; |
||
47 | |||
48 | /** @var StyleManager Manages styles */ |
||
49 | private $styleManager; |
||
50 | |||
51 | /** @var StyleMerger Helper to merge styles together */ |
||
52 | private $styleMerger; |
||
53 | |||
54 | /** @var SharedStringsManager Helper to write shared strings */ |
||
55 | private $sharedStringsManager; |
||
56 | |||
57 | /** @var XLSXEscaper Strings escaper */ |
||
58 | private $stringsEscaper; |
||
59 | |||
60 | /** @var StringHelper String helper */ |
||
61 | private $stringHelper; |
||
62 | |||
63 | /** @var InternalEntityFactory Factory to create entities */ |
||
64 | private $entityFactory; |
||
65 | |||
66 | /** @var array Locale info, used for number formatting */ |
||
67 | private $localeInfo; |
||
68 | |||
69 | /** |
||
70 | * WorksheetManager constructor. |
||
71 | * |
||
72 | * @param OptionsManagerInterface $optionsManager |
||
73 | * @param RowManager $rowManager |
||
74 | * @param StyleManager $styleManager |
||
75 | * @param StyleMerger $styleMerger |
||
76 | * @param SharedStringsManager $sharedStringsManager |
||
77 | * @param XLSXEscaper $stringsEscaper |
||
78 | * @param StringHelper $stringHelper |
||
79 | * @param InternalEntityFactory $entityFactory |
||
80 | */ |
||
81 | 42 | public function __construct( |
|
101 | |||
102 | /** |
||
103 | * @return SharedStringsManager |
||
104 | */ |
||
105 | 38 | public function getSharedStringsManager() |
|
109 | |||
110 | /** |
||
111 | * {@inheritdoc} |
||
112 | */ |
||
113 | 42 | public function startSheet(Worksheet $worksheet) |
|
123 | |||
124 | /** |
||
125 | * Checks if the sheet has been sucessfully created. Throws an exception if not. |
||
126 | * |
||
127 | * @param bool|resource $sheetFilePointer Pointer to the sheet data file or FALSE if unable to open the file |
||
128 | * @throws IOException If the sheet data file cannot be opened for writing |
||
129 | * @return void |
||
130 | */ |
||
131 | 42 | private function throwIfSheetFilePointerIsNotAvailable($sheetFilePointer) |
|
137 | |||
138 | /** |
||
139 | * {@inheritdoc} |
||
140 | */ |
||
141 | 35 | public function addRow(Worksheet $worksheet, Row $row) |
|
149 | |||
150 | /** |
||
151 | * Adds non empty row to the worksheet. |
||
152 | * |
||
153 | * @param Worksheet $worksheet The worksheet to add the row to |
||
154 | * @param Row $row The row to be written |
||
155 | * @throws IOException If the data cannot be written |
||
156 | * @throws InvalidArgumentException If a cell value's type is not supported |
||
157 | * @return void |
||
158 | */ |
||
159 | 35 | private function addNonEmptyRow(Worksheet $worksheet, Row $row) |
|
183 | |||
184 | /** |
||
185 | * Applies styles to the given style, merging the cell's style with its row's style |
||
186 | * |
||
187 | * @param Cell $cell |
||
188 | * @param Style $rowStyle |
||
189 | * |
||
190 | * @throws InvalidArgumentException If the given value cannot be processed |
||
191 | * @return RegisteredStyle |
||
192 | */ |
||
193 | 35 | private function applyStyleAndRegister(Cell $cell, Style $rowStyle) : RegisteredStyle |
|
224 | |||
225 | /** |
||
226 | * Builds and returns xml for a single cell. |
||
227 | * |
||
228 | * @param int $rowIndexOneBased |
||
229 | * @param int $columnIndexZeroBased |
||
230 | * @param Cell $cell |
||
231 | * @param int $styleId |
||
232 | * |
||
233 | * @throws InvalidArgumentException If the given value cannot be processed |
||
234 | * @return string |
||
235 | */ |
||
236 | 35 | private function getCellXML($rowIndexOneBased, $columnIndexZeroBased, Cell $cell, $styleId) |
|
277 | |||
278 | /** |
||
279 | * Returns the XML fragment for a cell containing a non empty string |
||
280 | * |
||
281 | * @param string $cellValue The cell value |
||
282 | * @throws InvalidArgumentException If the string exceeds the maximum number of characters allowed per cell |
||
283 | * @return string The XML fragment representing the cell |
||
284 | */ |
||
285 | 32 | private function getCellXMLFragmentForNonEmptyString($cellValue) |
|
300 | |||
301 | /** |
||
302 | * {@inheritdoc} |
||
303 | */ |
||
304 | 38 | public function close(Worksheet $worksheet) |
|
316 | } |
||
317 |