1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Box\Spout\Writer\ODS\Manager; |
4
|
|
|
|
5
|
|
|
use Box\Spout\Common\Entity\Cell; |
6
|
|
|
use Box\Spout\Common\Entity\Row; |
7
|
|
|
use Box\Spout\Common\Entity\Style\EmptyStyle; |
8
|
|
|
use Box\Spout\Common\Entity\Style\Style; |
9
|
|
|
use Box\Spout\Common\Exception\InvalidArgumentException; |
10
|
|
|
use Box\Spout\Common\Exception\IOException; |
11
|
|
|
use Box\Spout\Common\Helper\Escaper\ODS as ODSEscaper; |
12
|
|
|
use Box\Spout\Common\Helper\StringHelper; |
13
|
|
|
use Box\Spout\Writer\Common\Entity\Worksheet; |
14
|
|
|
use Box\Spout\Writer\Common\Manager\Style\StyleMerger; |
15
|
|
|
use Box\Spout\Writer\Common\Manager\WorksheetManagerInterface; |
16
|
|
|
use Box\Spout\Writer\ODS\Manager\Style\StyleManager; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Class WorksheetManager |
20
|
|
|
* ODS worksheet manager, providing the interfaces to work with ODS worksheets. |
21
|
|
|
*/ |
22
|
|
|
class WorksheetManager implements WorksheetManagerInterface |
23
|
|
|
{ |
24
|
|
|
/** @var \Box\Spout\Common\Helper\Escaper\ODS Strings escaper */ |
25
|
|
|
private $stringsEscaper; |
26
|
|
|
|
27
|
|
|
/** @var StringHelper String helper */ |
28
|
|
|
private $stringHelper; |
29
|
|
|
|
30
|
|
|
/** @var StyleManager Manages styles */ |
31
|
|
|
private $styleManager; |
32
|
|
|
|
33
|
|
|
/** @var StyleMerger Helper to merge styles together */ |
34
|
|
|
private $styleMerger; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* WorksheetManager constructor. |
38
|
|
|
* |
39
|
|
|
* @param StyleManager $styleManager |
40
|
|
|
* @param StyleMerger $styleMerger |
41
|
|
|
* @param ODSEscaper $stringsEscaper |
42
|
|
|
* @param StringHelper $stringHelper |
43
|
|
|
*/ |
44
|
38 |
|
public function __construct( |
45
|
|
|
StyleManager $styleManager, |
46
|
|
|
StyleMerger $styleMerger, |
47
|
|
|
ODSEscaper $stringsEscaper, |
48
|
|
|
StringHelper $stringHelper |
49
|
|
|
) { |
50
|
38 |
|
$this->styleManager = $styleManager; |
51
|
38 |
|
$this->styleMerger = $styleMerger; |
52
|
38 |
|
$this->stringsEscaper = $stringsEscaper; |
53
|
38 |
|
$this->stringHelper = $stringHelper; |
54
|
38 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Prepares the worksheet to accept data |
58
|
|
|
* |
59
|
|
|
* @param Worksheet $worksheet The worksheet to start |
60
|
|
|
* @throws \Box\Spout\Common\Exception\IOException If the sheet data file cannot be opened for writing |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
38 |
|
public function startSheet(Worksheet $worksheet) |
64
|
|
|
{ |
65
|
38 |
|
$sheetFilePointer = \fopen($worksheet->getFilePath(), 'w'); |
66
|
38 |
|
$this->throwIfSheetFilePointerIsNotAvailable($sheetFilePointer); |
67
|
|
|
|
68
|
38 |
|
$worksheet->setFilePointer($sheetFilePointer); |
69
|
38 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Checks if the sheet has been sucessfully created. Throws an exception if not. |
73
|
|
|
* |
74
|
|
|
* @param bool|resource $sheetFilePointer Pointer to the sheet data file or FALSE if unable to open the file |
75
|
|
|
* @throws IOException If the sheet data file cannot be opened for writing |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
38 |
|
private function throwIfSheetFilePointerIsNotAvailable($sheetFilePointer) |
79
|
|
|
{ |
80
|
38 |
|
if (!$sheetFilePointer) { |
81
|
|
|
throw new IOException('Unable to open sheet for writing.'); |
82
|
|
|
} |
83
|
38 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns the table XML root node as string. |
87
|
|
|
* |
88
|
|
|
* @param Worksheet $worksheet |
89
|
|
|
* @return string <table> node as string |
90
|
|
|
*/ |
91
|
35 |
|
public function getTableElementStartAsString(Worksheet $worksheet) |
92
|
|
|
{ |
93
|
35 |
|
$externalSheet = $worksheet->getExternalSheet(); |
94
|
35 |
|
$escapedSheetName = $this->stringsEscaper->escape($externalSheet->getName()); |
95
|
35 |
|
$tableStyleName = 'ta' . ($externalSheet->getIndex() + 1); |
96
|
|
|
|
97
|
35 |
|
$tableElement = '<table:table table:style-name="' . $tableStyleName . '" table:name="' . $escapedSheetName . '">'; |
98
|
35 |
|
$tableElement .= '<table:table-column table:default-cell-style-name="ce1" table:style-name="co1" table:number-columns-repeated="' . $worksheet->getMaxNumColumns() . '"/>'; |
99
|
|
|
|
100
|
35 |
|
return $tableElement; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Adds a row to the given worksheet. |
105
|
|
|
* |
106
|
|
|
* @param Worksheet $worksheet The worksheet to add the row to |
107
|
|
|
* @param Row $row The row to be added |
108
|
|
|
* @throws IOException If the data cannot be written |
109
|
|
|
* @throws InvalidArgumentException If a cell value's type is not supported |
110
|
|
|
* @return void |
111
|
|
|
*/ |
112
|
33 |
|
public function addRow(Worksheet $worksheet, Row $row) |
113
|
|
|
{ |
114
|
33 |
|
$cells = $row->getCells(); |
115
|
33 |
|
$rowStyle = $row->getStyle(); |
116
|
|
|
|
117
|
33 |
|
$data = '<table:table-row table:style-name="ro1">'; |
118
|
|
|
|
119
|
33 |
|
$currentCellIndex = 0; |
120
|
33 |
|
$nextCellIndex = 1; |
121
|
|
|
|
122
|
33 |
|
for ($i = 0; $i < $row->getNumCells(); $i++) { |
123
|
|
|
/** @var Cell $cell */ |
124
|
33 |
|
$cell = $cells[$currentCellIndex]; |
125
|
|
|
/** @var Cell|null $nextCell */ |
126
|
33 |
|
$nextCell = isset($cells[$nextCellIndex]) ? $cells[$nextCellIndex] : null; |
127
|
|
|
|
128
|
33 |
|
if ($nextCell === null || $cell->getValue() !== $nextCell->getValue()) { |
129
|
33 |
|
$data .= $this->applyStyleAndGetCellXML($cell, $rowStyle, $currentCellIndex, $nextCellIndex); |
130
|
32 |
|
$currentCellIndex = $nextCellIndex; |
131
|
|
|
} |
132
|
|
|
|
133
|
32 |
|
$nextCellIndex++; |
134
|
|
|
} |
135
|
|
|
|
136
|
32 |
|
$data .= '</table:table-row>'; |
137
|
|
|
|
138
|
32 |
|
$wasWriteSuccessful = \fwrite($worksheet->getFilePointer(), $data); |
139
|
32 |
|
if ($wasWriteSuccessful === false) { |
140
|
|
|
throw new IOException("Unable to write data in {$worksheet->getFilePath()}"); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// only update the count if the write worked |
144
|
32 |
|
$lastWrittenRowIndex = $worksheet->getLastWrittenRowIndex(); |
145
|
32 |
|
$worksheet->setLastWrittenRowIndex($lastWrittenRowIndex + 1); |
146
|
32 |
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Applies styles to the given style, merging the cell's style with its row's style |
150
|
|
|
* Then builds and returns xml for the cell. |
151
|
|
|
* |
152
|
|
|
* @param Cell $cell |
153
|
|
|
* @param Style $rowStyle |
154
|
|
|
* @param int $currentCellIndex |
155
|
|
|
* @param int $nextCellIndex |
156
|
|
|
* @throws InvalidArgumentException If a cell value's type is not supported |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
33 |
|
private function applyStyleAndGetCellXML(Cell $cell, Style &$rowStyle, $currentCellIndex, $nextCellIndex) |
160
|
|
|
{ |
161
|
33 |
|
if ($cell->getStyle() instanceof EmptyStyle) { |
162
|
32 |
|
$cell->setStyle($rowStyle); |
163
|
|
|
|
164
|
32 |
|
$extraStyle = $this->styleManager->applyExtraStylesIfNeeded($cell); |
165
|
|
|
|
166
|
32 |
|
if ($extraStyle) { |
167
|
2 |
|
$registeredStyle = $this->styleManager->registerStyle($extraStyle); |
168
|
|
|
} else { |
169
|
32 |
|
$registeredStyle = $rowStyle = $this->styleManager->registerStyle($rowStyle); |
170
|
|
|
} |
171
|
|
|
} else { |
172
|
2 |
|
$mergedCellAndRowStyle = $this->styleMerger->merge($cell->getStyle(), $rowStyle); |
173
|
2 |
|
$cell->setStyle($mergedCellAndRowStyle); |
174
|
|
|
|
175
|
2 |
|
$newCellStyle = $this->styleManager->applyExtraStylesIfNeeded($cell) ?: $mergedCellAndRowStyle; |
176
|
2 |
|
$registeredStyle = $this->styleManager->registerStyle($newCellStyle); |
177
|
|
|
} |
178
|
|
|
|
179
|
33 |
|
$styleIndex = $registeredStyle->getId() + 1; // 1-based |
180
|
|
|
|
181
|
33 |
|
$numTimesValueRepeated = ($nextCellIndex - $currentCellIndex); |
182
|
|
|
|
183
|
33 |
|
return $this->getCellXML($cell, $styleIndex, $numTimesValueRepeated); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Returns the cell XML content, given its value. |
188
|
|
|
* |
189
|
|
|
* @param Cell $cell The cell to be written |
190
|
|
|
* @param int $styleIndex Index of the used style |
191
|
|
|
* @param int $numTimesValueRepeated Number of times the value is consecutively repeated |
192
|
|
|
* @throws InvalidArgumentException If a cell value's type is not supported |
193
|
|
|
* @return string The cell XML content |
194
|
|
|
*/ |
195
|
33 |
|
private function getCellXML(Cell $cell, $styleIndex, $numTimesValueRepeated) |
196
|
|
|
{ |
197
|
33 |
|
$data = '<table:table-cell table:style-name="ce' . $styleIndex . '"'; |
198
|
|
|
|
199
|
33 |
|
if ($numTimesValueRepeated !== 1) { |
200
|
4 |
|
$data .= ' table:number-columns-repeated="' . $numTimesValueRepeated . '"'; |
201
|
|
|
} |
202
|
|
|
|
203
|
33 |
|
if ($cell->isString()) { |
204
|
28 |
|
$data .= ' office:value-type="string" calcext:value-type="string">'; |
205
|
|
|
|
206
|
28 |
|
$cellValueLines = \explode("\n", $cell->getValue()); |
207
|
28 |
|
foreach ($cellValueLines as $cellValueLine) { |
208
|
28 |
|
$data .= '<text:p>' . $this->stringsEscaper->escape($cellValueLine) . '</text:p>'; |
209
|
|
|
} |
210
|
|
|
|
211
|
28 |
|
$data .= '</table:table-cell>'; |
212
|
7 |
|
} elseif ($cell->isBoolean()) { |
213
|
2 |
|
$value = $cell->getValue() ? 'true' : 'false'; // boolean-value spec: http://docs.oasis-open.org/office/v1.2/os/OpenDocument-v1.2-os-part1.html#datatype-boolean |
214
|
2 |
|
$data .= ' office:value-type="boolean" calcext:value-type="boolean" office:boolean-value="' . $value . '">'; |
215
|
2 |
|
$data .= '<text:p>' . $cell->getValue() . '</text:p>'; |
216
|
2 |
|
$data .= '</table:table-cell>'; |
217
|
6 |
|
} elseif ($cell->isNumeric()) { |
218
|
2 |
|
$data .= ' office:value-type="float" calcext:value-type="float" office:value="' . $cell->getValue() . '">'; |
219
|
2 |
|
$data .= '<text:p>' . $cell->getValue() . '</text:p>'; |
220
|
2 |
|
$data .= '</table:table-cell>'; |
221
|
5 |
|
} elseif ($cell->isError() && is_string($cell->getValueEvenIfError())) { |
222
|
|
|
// only writes the error value if it's a string |
223
|
1 |
|
$data .= ' office:value-type="string" calcext:value-type="error" office:value="">'; |
224
|
1 |
|
$data .= '<text:p>' . $cell->getValueEvenIfError() . '</text:p>'; |
225
|
1 |
|
$data .= '</table:table-cell>'; |
226
|
4 |
|
} elseif ($cell->isEmpty()) { |
227
|
2 |
|
$data .= '/>'; |
228
|
|
|
} else { |
229
|
2 |
|
throw new InvalidArgumentException('Trying to add a value with an unsupported type: ' . \gettype($cell->getValue())); |
230
|
|
|
} |
231
|
|
|
|
232
|
32 |
|
return $data; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Closes the worksheet |
237
|
|
|
* |
238
|
|
|
* @param Worksheet $worksheet |
239
|
|
|
* @return void |
240
|
|
|
*/ |
241
|
35 |
|
public function close(Worksheet $worksheet) |
242
|
|
|
{ |
243
|
35 |
|
$worksheetFilePointer = $worksheet->getFilePointer(); |
244
|
|
|
|
245
|
35 |
|
if (!\is_resource($worksheetFilePointer)) { |
246
|
|
|
return; |
247
|
|
|
} |
248
|
|
|
|
249
|
35 |
|
\fclose($worksheetFilePointer); |
250
|
35 |
|
} |
251
|
|
|
} |
252
|
|
|
|