1 | <?php |
||
18 | class Worksheet implements WorksheetInterface |
||
19 | { |
||
20 | /** @var \Box\Spout\Writer\Common\Sheet The "external" sheet */ |
||
21 | protected $externalSheet; |
||
22 | |||
23 | /** @var string Path to the XML file that will contain the sheet data */ |
||
24 | protected $worksheetFilePath; |
||
25 | |||
26 | /** @var \Box\Spout\Common\Escaper\ODS Strings escaper */ |
||
27 | protected $stringsEscaper; |
||
28 | |||
29 | /** @var \Box\Spout\Common\Helper\StringHelper To help with string manipulation */ |
||
30 | protected $stringHelper; |
||
31 | |||
32 | /** @var Resource Pointer to the temporary sheet data file (e.g. worksheets-temp/sheet1.xml) */ |
||
33 | protected $sheetFilePointer; |
||
34 | |||
35 | /** @var int Maximum number of columns among all the written rows */ |
||
36 | protected $maxNumColumns = 1; |
||
37 | |||
38 | /** @var int Index of the last written row */ |
||
39 | protected $lastWrittenRowIndex = 0; |
||
40 | |||
41 | /** |
||
42 | * @param \Box\Spout\Writer\Common\Sheet $externalSheet The associated "external" sheet |
||
43 | * @param string $worksheetFilesFolder Temporary folder where the files to create the XLSX will be stored |
||
44 | * @throws \Box\Spout\Common\Exception\IOException If the sheet data file cannot be opened for writing |
||
45 | */ |
||
46 | 114 | public function __construct($externalSheet, $worksheetFilesFolder) |
|
47 | { |
||
48 | 114 | $this->externalSheet = $externalSheet; |
|
49 | /** @noinspection PhpUnnecessaryFullyQualifiedNameInspection */ |
||
50 | 114 | $this->stringsEscaper = \Box\Spout\Common\Escaper\ODS::getInstance(); |
|
51 | 114 | $this->worksheetFilePath = $worksheetFilesFolder . '/sheet' . $externalSheet->getIndex() . '.xml'; |
|
52 | |||
53 | 114 | $this->stringHelper = new StringHelper(); |
|
54 | |||
55 | 114 | $this->startSheet(); |
|
56 | 114 | } |
|
57 | |||
58 | /** |
||
59 | * Prepares the worksheet to accept data |
||
60 | * The XML file does not contain the "<table:table>" node as it contains the sheet's name |
||
61 | * which may change during the execution of the program. It will be added at the end. |
||
62 | * |
||
63 | * @return void |
||
64 | * @throws \Box\Spout\Common\Exception\IOException If the sheet data file cannot be opened for writing |
||
65 | */ |
||
66 | 114 | protected function startSheet() |
|
67 | { |
||
68 | 114 | $this->sheetFilePointer = fopen($this->worksheetFilePath, 'w'); |
|
69 | 114 | $this->throwIfSheetFilePointerIsNotAvailable(); |
|
70 | 114 | } |
|
71 | |||
72 | /** |
||
73 | * Checks if the book has been created. Throws an exception if not created yet. |
||
74 | * |
||
75 | * @return void |
||
76 | * @throws \Box\Spout\Common\Exception\IOException If the sheet data file cannot be opened for writing |
||
77 | */ |
||
78 | 114 | protected function throwIfSheetFilePointerIsNotAvailable() |
|
79 | { |
||
80 | 114 | if (!$this->sheetFilePointer) { |
|
81 | throw new IOException('Unable to open sheet for writing.'); |
||
82 | } |
||
83 | 114 | } |
|
84 | |||
85 | /** |
||
86 | * @return string Path to the temporary sheet content XML file |
||
87 | */ |
||
88 | 84 | public function getWorksheetFilePath() |
|
92 | |||
93 | /** |
||
94 | * Returns the table XML root node as string. |
||
95 | * |
||
96 | * @return string <table> node as string |
||
97 | */ |
||
98 | 84 | public function getTableElementStartAsString() |
|
99 | { |
||
100 | 84 | $escapedSheetName = $this->stringsEscaper->escape($this->externalSheet->getName()); |
|
101 | 84 | $tableStyleName = 'ta' . ($this->externalSheet->getIndex() + 1); |
|
102 | |||
103 | 84 | $tableElement = '<table:table table:style-name="' . $tableStyleName . '" table:name="' . $escapedSheetName . '">'; |
|
104 | 84 | $tableElement .= '<table:table-column table:default-cell-style-name="ce1" table:style-name="co1" table:number-columns-repeated="' . $this->maxNumColumns . '"/>'; |
|
105 | |||
106 | 84 | return $tableElement; |
|
107 | } |
||
108 | |||
109 | /** |
||
110 | * @return \Box\Spout\Writer\Common\Sheet The "external" sheet |
||
111 | */ |
||
112 | 30 | public function getExternalSheet() |
|
116 | |||
117 | /** |
||
118 | * @return int The index of the last written row |
||
119 | */ |
||
120 | 81 | public function getLastWrittenRowIndex() |
|
124 | |||
125 | /** |
||
126 | * Adds data to the worksheet. |
||
127 | * |
||
128 | * @param array $dataRow Array containing data to be written. Cannot be empty. |
||
129 | * Example $dataRow = ['data1', 1234, null, '', 'data5']; |
||
130 | * @param \Box\Spout\Writer\Style\Style $style Style to be applied to the row. NULL means use default style. |
||
131 | * @return void |
||
132 | * @throws \Box\Spout\Common\Exception\IOException If the data cannot be written |
||
133 | * @throws \Box\Spout\Common\Exception\InvalidArgumentException If a cell value's type is not supported |
||
134 | */ |
||
135 | 81 | public function addRow($dataRow, $style) |
|
176 | |||
177 | /** |
||
178 | * Returns the cell XML content, given its value. |
||
179 | * |
||
180 | * @param mixed $cellValue The value to be written |
||
181 | * @param int $styleIndex Index of the used style |
||
182 | * @param int $numTimesValueRepeated Number of times the value is consecutively repeated |
||
183 | * @return string The cell XML content |
||
184 | * @throws \Box\Spout\Common\Exception\InvalidArgumentException If a cell value's type is not supported |
||
185 | */ |
||
186 | 81 | protected function getCellXML($cellValue, $styleIndex, $numTimesValueRepeated) |
|
187 | { |
||
188 | 81 | $data = '<table:table-cell table:style-name="ce' . $styleIndex . '"'; |
|
189 | |||
190 | 81 | if ($numTimesValueRepeated !== 1) { |
|
191 | 12 | $data .= ' table:number-columns-repeated="' . $numTimesValueRepeated . '"'; |
|
192 | 12 | } |
|
193 | |||
194 | 81 | if (CellHelper::isNonEmptyString($cellValue)) { |
|
195 | 69 | $data .= ' office:value-type="string" calcext:value-type="string">'; |
|
196 | |||
197 | 69 | $cellValueLines = explode("\n", $cellValue); |
|
198 | 69 | foreach ($cellValueLines as $cellValueLine) { |
|
199 | 69 | $data .= '<text:p>' . $this->stringsEscaper->escape($cellValueLine) . '</text:p>'; |
|
200 | 69 | } |
|
201 | |||
202 | 69 | $data .= '</table:table-cell>'; |
|
203 | 81 | } else if (CellHelper::isBoolean($cellValue)) { |
|
204 | 6 | $data .= ' office:value-type="boolean" calcext:value-type="boolean" office:boolean-value="' . $cellValue . '">'; |
|
205 | 6 | $data .= '<text:p>' . $cellValue . '</text:p>'; |
|
206 | 6 | $data .= '</table:table-cell>'; |
|
207 | 15 | } else if (CellHelper::isNumeric($cellValue)) { |
|
208 | 6 | $data .= ' office:value-type="float" calcext:value-type="float" office:value="' . $cellValue . '">'; |
|
209 | 6 | $data .= '<text:p>' . $cellValue . '</text:p>'; |
|
210 | 6 | $data .= '</table:table-cell>'; |
|
211 | 12 | } else if (empty($cellValue)) { |
|
212 | 6 | $data .= '/>'; |
|
213 | 6 | } else { |
|
214 | 3 | throw new InvalidArgumentException('Trying to add a value with an unsupported type: ' . gettype($cellValue)); |
|
215 | } |
||
216 | |||
217 | 78 | return $data; |
|
218 | } |
||
219 | |||
220 | /** |
||
221 | * Closes the worksheet |
||
222 | * |
||
223 | * @return void |
||
224 | */ |
||
225 | 84 | public function close() |
|
233 | } |
||
234 |