1 | <?php |
||
20 | class WorksheetManager implements WorksheetManagerInterface |
||
21 | { |
||
22 | /** @var \Box\Spout\Common\Escaper\ODS Strings escaper */ |
||
23 | private $stringsEscaper; |
||
24 | |||
25 | /** @var StringHelper String helper */ |
||
26 | private $stringHelper; |
||
27 | |||
28 | /** @var StyleManager Manages styles */ |
||
29 | private $styleManager; |
||
30 | |||
31 | /** |
||
32 | * WorksheetManager constructor. |
||
33 | * @param StyleManager $styleManager |
||
34 | * @param \Box\Spout\Common\Escaper\ODS $stringsEscaper |
||
35 | * @param StringHelper $stringHelper |
||
36 | */ |
||
37 | 41 | public function __construct( |
|
46 | |||
47 | /** |
||
48 | * Prepares the worksheet to accept data |
||
49 | * |
||
50 | * @param Worksheet $worksheet The worksheet to start |
||
51 | * @return void |
||
52 | * @throws \Box\Spout\Common\Exception\IOException If the sheet data file cannot be opened for writing |
||
53 | */ |
||
54 | 41 | public function startSheet(Worksheet $worksheet) |
|
61 | |||
62 | /** |
||
63 | * Checks if the sheet has been sucessfully created. Throws an exception if not. |
||
64 | * |
||
65 | * @param bool|resource $sheetFilePointer Pointer to the sheet data file or FALSE if unable to open the file |
||
66 | * @return void |
||
67 | * @throws IOException If the sheet data file cannot be opened for writing |
||
68 | */ |
||
69 | 41 | private function throwIfSheetFilePointerIsNotAvailable($sheetFilePointer) |
|
75 | |||
76 | /** |
||
77 | * Returns the table XML root node as string. |
||
78 | * |
||
79 | * @param Worksheet $worksheet |
||
80 | * @return string <table> node as string |
||
81 | */ |
||
82 | 34 | public function getTableElementStartAsString(Worksheet $worksheet) |
|
93 | |||
94 | /** |
||
95 | * Adds a row to the worksheet. |
||
96 | * |
||
97 | * @param Worksheet $worksheet The worksheet to add the row to |
||
98 | * @param Row $row The row to be added |
||
99 | * @return void |
||
100 | * |
||
101 | * @throws IOException If the data cannot be written |
||
102 | * @throws InvalidArgumentException If a cell value's type is not supported |
||
103 | */ |
||
104 | 30 | public function addRow(Worksheet $worksheet, Row $row) |
|
105 | { |
||
106 | |||
107 | 30 | $cells = $row->getCells(); |
|
108 | 30 | $cellsCount = count($cells); |
|
109 | |||
110 | 30 | $data = '<table:table-row table:style-name="ro1">'; |
|
111 | |||
112 | 30 | $currentCellIndex = 0; |
|
113 | 30 | $nextCellIndex = 1; |
|
114 | |||
115 | 30 | for ($i = 0; $i < $cellsCount; $i++) { |
|
116 | |||
117 | /** @var Cell $cell */ |
||
118 | 30 | $cell = $cells[$currentCellIndex]; |
|
119 | /** @var Cell|null $nextCell */ |
||
120 | 30 | $nextCell = isset($cells[$nextCellIndex]) ? $cells[$nextCellIndex] : null; |
|
121 | |||
122 | // @TODO refactoring: move this to its own method |
||
123 | 30 | if (null === $nextCell || $cell->getValue() !== $nextCell->getValue()) { |
|
124 | |||
125 | // Apply styles - the row style is merged at this point |
||
126 | 30 | $cell->applyStyle($row->getStyle()); |
|
127 | 30 | $this->styleManager->applyExtraStylesIfNeeded($cell); |
|
128 | 30 | $registeredStyle = $this->styleManager->registerStyle($cell->getStyle()); |
|
|
|||
129 | 30 | $styleIndex = $registeredStyle->getId() + 1; // 1-based |
|
130 | |||
131 | 30 | $numTimesValueRepeated = ($nextCellIndex - $currentCellIndex); |
|
132 | 30 | $data .= $this->getCellXML($cell, $styleIndex, $numTimesValueRepeated); |
|
133 | 29 | $currentCellIndex = $nextCellIndex; |
|
134 | } |
||
135 | |||
136 | 29 | $nextCellIndex++; |
|
137 | } |
||
138 | |||
139 | 29 | $data .= '</table:table-row>'; |
|
140 | |||
141 | 29 | $wasWriteSuccessful = fwrite($worksheet->getFilePointer(), $data); |
|
142 | 29 | if ($wasWriteSuccessful === false) { |
|
143 | throw new IOException("Unable to write data in {$worksheet->getFilePath()}"); |
||
144 | } |
||
145 | |||
146 | // only update the count if the write worked |
||
147 | 29 | $lastWrittenRowIndex = $worksheet->getLastWrittenRowIndex(); |
|
148 | 29 | $worksheet->setLastWrittenRowIndex($lastWrittenRowIndex + 1); |
|
149 | 29 | } |
|
150 | |||
151 | /** |
||
152 | * Returns the cell XML content, given its value. |
||
153 | * |
||
154 | * @param Cell $cell The cell to be written |
||
155 | * @param int $styleIndex Index of the used style |
||
156 | * @param int $numTimesValueRepeated Number of times the value is consecutively repeated |
||
157 | * @return string The cell XML content |
||
158 | * @throws \Box\Spout\Common\Exception\InvalidArgumentException If a cell value's type is not supported |
||
159 | */ |
||
160 | 30 | protected function getCellXML(Cell $cell, $styleIndex, $numTimesValueRepeated) |
|
193 | |||
194 | /** |
||
195 | * Closes the worksheet |
||
196 | * |
||
197 | * @param Worksheet $worksheet |
||
198 | * @return void |
||
199 | */ |
||
200 | 34 | public function close(Worksheet $worksheet) |
|
210 | } |
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: