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\Helper\Escaper\XLSX Strings escaper */ |
||
48 | private $stringsEscaper; |
||
49 | |||
50 | /** @var StringHelper String helper */ |
||
51 | private $stringHelper; |
||
52 | |||
53 | /** @var EntityFactory Factory to create entities */ |
||
54 | private $entityFactory; |
||
55 | |||
56 | /** |
||
57 | * WorksheetManager constructor. |
||
58 | * |
||
59 | * @param OptionsManagerInterface $optionsManager |
||
60 | * @param StyleManager $styleManager |
||
61 | * @param SharedStringsManager $sharedStringsManager |
||
62 | * @param \Box\Spout\Common\Helper\Escaper\XLSX $stringsEscaper |
||
63 | * @param StringHelper $stringHelper |
||
64 | * @param EntityFactory $entityFactory |
||
65 | */ |
||
66 | 43 | public function __construct( |
|
67 | OptionsManagerInterface $optionsManager, |
||
68 | StyleManager $styleManager, |
||
69 | SharedStringsManager $sharedStringsManager, |
||
70 | \Box\Spout\Common\Helper\Escaper\XLSX $stringsEscaper, |
||
71 | StringHelper $stringHelper, |
||
72 | EntityFactory $entityFactory |
||
73 | ) { |
||
74 | 43 | $this->shouldUseInlineStrings = $optionsManager->getOption(Options::SHOULD_USE_INLINE_STRINGS); |
|
75 | 43 | $this->styleManager = $styleManager; |
|
76 | 43 | $this->sharedStringsManager = $sharedStringsManager; |
|
77 | 43 | $this->stringsEscaper = $stringsEscaper; |
|
78 | 43 | $this->stringHelper = $stringHelper; |
|
79 | 43 | $this->entityFactory = $entityFactory; |
|
80 | 43 | } |
|
81 | |||
82 | /** |
||
83 | * @return SharedStringsManager |
||
84 | */ |
||
85 | 35 | public function getSharedStringsManager() |
|
86 | { |
||
87 | 35 | return $this->sharedStringsManager; |
|
88 | } |
||
89 | |||
90 | /** |
||
91 | * Prepares the worksheet to accept data |
||
92 | * |
||
93 | * @param Worksheet $worksheet The worksheet to start |
||
94 | * @throws \Box\Spout\Common\Exception\IOException If the sheet data file cannot be opened for writing |
||
95 | * @return void |
||
96 | */ |
||
97 | 43 | public function startSheet(Worksheet $worksheet) |
|
98 | { |
||
99 | 43 | $sheetFilePointer = fopen($worksheet->getFilePath(), 'w'); |
|
100 | 43 | $this->throwIfSheetFilePointerIsNotAvailable($sheetFilePointer); |
|
101 | |||
102 | 43 | $worksheet->setFilePointer($sheetFilePointer); |
|
103 | |||
104 | 43 | fwrite($sheetFilePointer, self::SHEET_XML_FILE_HEADER); |
|
105 | 43 | fwrite($sheetFilePointer, '<sheetData>'); |
|
106 | 43 | } |
|
107 | |||
108 | /** |
||
109 | * Checks if the sheet has been sucessfully created. Throws an exception if not. |
||
110 | * |
||
111 | * @param bool|resource $sheetFilePointer Pointer to the sheet data file or FALSE if unable to open the file |
||
112 | * @throws IOException If the sheet data file cannot be opened for writing |
||
113 | * @return void |
||
114 | */ |
||
115 | 43 | private function throwIfSheetFilePointerIsNotAvailable($sheetFilePointer) |
|
116 | { |
||
117 | 43 | if (!$sheetFilePointer) { |
|
118 | throw new IOException('Unable to open sheet for writing.'); |
||
119 | } |
||
120 | 43 | } |
|
121 | |||
122 | /** |
||
123 | * Adds a row to the worksheet. |
||
124 | * |
||
125 | * @param Worksheet $worksheet The worksheet to add the row to |
||
126 | * @param Row $row The row to be added |
||
127 | * @return void |
||
128 | * @throws IOException If the data cannot be written |
||
129 | * @throws InvalidArgumentException If a cell value's type is not supported |
||
130 | * @return void |
||
131 | */ |
||
132 | 31 | public function addRow(Worksheet $worksheet, Row $row) |
|
140 | |||
141 | /** |
||
142 | * Adds non empty row to the worksheet. |
||
143 | * |
||
144 | * @param Row $row The row to be written |
||
145 | * @return void |
||
146 | * |
||
147 | * @throws \Box\Spout\Common\Exception\IOException If the data cannot be written |
||
148 | * @throws \Box\Spout\Common\Exception\InvalidArgumentException If a cell value's type is not supported |
||
149 | * @return void |
||
150 | */ |
||
151 | 31 | private function addNonEmptyRow(Worksheet $worksheet, Row $row) |
|
177 | |||
178 | /** |
||
179 | * Build and return xml for a single cell. |
||
180 | * |
||
181 | * @param int $rowIndex |
||
182 | * @param int $cellNumber |
||
183 | * @param Cell $cell |
||
184 | * @param int $styleId |
||
185 | * @throws InvalidArgumentException If the given value cannot be processed |
||
186 | * @return string |
||
187 | */ |
||
188 | 31 | private function getCellXML($rowIndex, $cellNumber, Cell $cell, $styleId) |
|
214 | |||
215 | /** |
||
216 | * Returns the XML fragment for a cell containing a non empty string |
||
217 | * |
||
218 | * @param string $cellValue The cell value |
||
219 | * @throws InvalidArgumentException If the string exceeds the maximum number of characters allowed per cell |
||
220 | * @return string The XML fragment representing the cell |
||
221 | */ |
||
222 | 30 | private function getCellXMLFragmentForNonEmptyString($cellValue) |
|
237 | |||
238 | /** |
||
239 | * Closes the worksheet |
||
240 | * |
||
241 | * @param Worksheet $worksheet |
||
242 | * @return void |
||
243 | */ |
||
244 | 35 | public function close(Worksheet $worksheet) |
|
256 | } |
||
257 |
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: