1 | <?php |
||
23 | class WorksheetManager implements WorksheetManagerInterface |
||
24 | { |
||
25 | /** |
||
26 | * Maximum number of characters a cell can contain |
||
27 | * @see https://support.office.com/en-us/article/Excel-specifications-and-limits-16c69c74-3d6a-4aaf-ba35-e6eb276e8eaa [Excel 2007] |
||
28 | * @see https://support.office.com/en-us/article/Excel-specifications-and-limits-1672b34d-7043-467e-8e27-269d656771c3 [Excel 2010] |
||
29 | * @see https://support.office.com/en-us/article/Excel-specifications-and-limits-ca36e2dc-1f09-4620-b726-67c00b05040f [Excel 2013/2016] |
||
30 | */ |
||
31 | const MAX_CHARACTERS_PER_CELL = 32767; |
||
32 | |||
33 | const SHEET_XML_FILE_HEADER = <<<EOD |
||
34 | <?xml version="1.0" encoding="UTF-8" standalone="yes"?> |
||
35 | <worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"> |
||
36 | EOD; |
||
37 | |||
38 | /** @var bool Whether inline or shared strings should be used */ |
||
39 | protected $shouldUseInlineStrings; |
||
40 | |||
41 | /** @var StyleManager Manages styles */ |
||
42 | private $styleManager; |
||
43 | |||
44 | /** @var SharedStringsManager Helper to write shared strings */ |
||
45 | private $sharedStringsManager; |
||
46 | |||
47 | /** @var \Box\Spout\Common\Escaper\XLSX Strings escaper */ |
||
48 | private $stringsEscaper; |
||
49 | |||
50 | /** @var StringHelper String helper */ |
||
51 | private $stringHelper; |
||
52 | |||
53 | /** |
||
54 | * WorksheetManager constructor. |
||
55 | * |
||
56 | * @param OptionsManagerInterface $optionsManager |
||
57 | * @param StyleManager $styleManager |
||
58 | * @param SharedStringsManager $sharedStringsManager |
||
59 | * @param \Box\Spout\Common\Escaper\XLSX $stringsEscaper |
||
60 | * @param StringHelper $stringHelper |
||
61 | */ |
||
62 | 46 | public function __construct( |
|
75 | |||
76 | /** |
||
77 | * @return SharedStringsManager |
||
78 | */ |
||
79 | 36 | public function getSharedStringsManager() |
|
83 | |||
84 | |||
85 | /** |
||
86 | * Prepares the worksheet to accept data |
||
87 | * |
||
88 | * @param Worksheet $worksheet The worksheet to start |
||
89 | * @return void |
||
90 | * @throws \Box\Spout\Common\Exception\IOException If the sheet data file cannot be opened for writing |
||
91 | */ |
||
92 | 46 | public function startSheet(Worksheet $worksheet) |
|
102 | |||
103 | /** |
||
104 | * Checks if the sheet has been sucessfully created. Throws an exception if not. |
||
105 | * |
||
106 | * @param bool|resource $sheetFilePointer Pointer to the sheet data file or FALSE if unable to open the file |
||
107 | * @return void |
||
108 | * @throws IOException If the sheet data file cannot be opened for writing |
||
109 | */ |
||
110 | 46 | private function throwIfSheetFilePointerIsNotAvailable($sheetFilePointer) |
|
116 | |||
117 | /** |
||
118 | * Adds a row to the worksheet. |
||
119 | * |
||
120 | * @param Worksheet $worksheet The worksheet to add the row to |
||
121 | * @param Row $row The row to be added |
||
122 | * @return void |
||
123 | * @throws IOException If the data cannot be written |
||
124 | * @throws InvalidArgumentException If a cell value's type is not supported |
||
125 | */ |
||
126 | 33 | public function addRow(Worksheet $worksheet, Row $row) |
|
134 | |||
135 | /** |
||
136 | * Adds non empty row to the worksheet. |
||
137 | * |
||
138 | * @param Row $row The row to be written |
||
139 | * @return void |
||
140 | * |
||
141 | * @throws \Box\Spout\Common\Exception\IOException If the data cannot be written |
||
142 | * @throws \Box\Spout\Common\Exception\InvalidArgumentException If a cell value's type is not supported |
||
143 | */ |
||
144 | 33 | private function addNonEmptyRow(Worksheet $worksheet, Row $row) |
|
169 | |||
170 | /** |
||
171 | * Build and return xml for a single cell. |
||
172 | * |
||
173 | * @param int $rowIndex |
||
174 | * @param int $cellNumber |
||
175 | * @param Cell $cell |
||
176 | * @param int $styleId |
||
177 | * @return string |
||
178 | * @throws InvalidArgumentException If the given value cannot be processed |
||
179 | */ |
||
180 | 33 | private function getCellXML($rowIndex, $cellNumber, Cell $cell, $styleId) |
|
206 | |||
207 | /** |
||
208 | * Returns the XML fragment for a cell containing a non empty string |
||
209 | * |
||
210 | * @param string $cellValue The cell value |
||
211 | * @return string The XML fragment representing the cell |
||
212 | * @throws InvalidArgumentException If the string exceeds the maximum number of characters allowed per cell |
||
213 | */ |
||
214 | 32 | private function getCellXMLFragmentForNonEmptyString($cellValue) |
|
229 | |||
230 | /** |
||
231 | * Closes the worksheet |
||
232 | * |
||
233 | * @param Worksheet $worksheet |
||
234 | * @return void |
||
235 | */ |
||
236 | 36 | public function close(Worksheet $worksheet) |
|
248 | } |
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: