@@ -22,12 +22,15 @@ public function __construct(XlsxFastEditor $editor, int $sheetNumber, \DOMElemen
22
22
$ this ->c = $ c ;
23
23
}
24
24
25
+ /**
26
+ * @throws XlsxFastEditorXmlException
27
+ */
25
28
private function getXPath (): \DOMXPath
26
29
{
27
30
if ($ this ->xpath === null ) {
28
31
$ dom = $ this ->c ->ownerDocument ;
29
32
if ($ dom === null ) {
30
- throw new XlsxFastEditorInputException ("Internal error accessing cell {$ this ->name ()}! " );
33
+ throw new XlsxFastEditorXmlException ("Internal error accessing cell {$ this ->name ()}! " );
31
34
}
32
35
$ xpath = new \DOMXPath ($ dom );
33
36
$ xpath ->registerNamespace ('o ' , XlsxFastEditor::_OXML_NAMESPACE );
@@ -47,6 +50,7 @@ public function name(): string
47
50
48
51
/**
49
52
* Column name (e.g., `'D'`).
53
+ * @throws XlsxFastEditorXmlException
50
54
*/
51
55
public function column (): string
52
56
{
@@ -88,6 +92,7 @@ public function getNextCell(): ?XlsxFastEditorCell
88
92
89
93
/**
90
94
* Access the parent row of the cell.
95
+ * @throws XlsxFastEditorXmlException
91
96
*/
92
97
public function getRow (): XlsxFastEditorRow
93
98
{
@@ -100,6 +105,7 @@ public function getRow(): XlsxFastEditorRow
100
105
101
106
/**
102
107
* Read a formula in the given worksheet at the given cell location.
108
+ * @throws XlsxFastEditorXmlException
103
109
*/
104
110
public function readFormula (): ?string
105
111
{
@@ -119,10 +125,9 @@ private function value(): ?string
119
125
$ vs = $ this ->c ->getElementsByTagName ('v ' );
120
126
if ($ vs ->length > 0 ) {
121
127
$ v = $ vs [0 ];
122
- if (!( $ v instanceof \DOMElement) ) {
123
- throw new XlsxFastEditorXmlException ( " Error querying XML value for cell { $ this -> name ()} ! " ) ;
128
+ if ($ v instanceof \DOMElement) {
129
+ return $ v -> nodeValue ;
124
130
}
125
- return $ v ->nodeValue ;
126
131
}
127
132
return null ;
128
133
}
@@ -141,14 +146,21 @@ public function readFloat(): ?float
141
146
142
147
/**
143
148
* Read the date/time value of the cell, if any.
149
+ * @throws XlsxFastEditorFileFormatException
150
+ * @throws XlsxFastEditorXmlException
144
151
*/
145
152
public function readDateTime (): ?\DateTimeImmutable
146
153
{
147
154
$ value = $ this ->readFloat ();
148
155
if ($ value === null ) {
149
156
return null ;
150
157
}
151
- return XlsxFastEditor::excelDateToDateTime ($ value , $ this ->editor ->getWorkbookDateSystem ());
158
+ try {
159
+ return XlsxFastEditor::excelDateToDateTime ($ value , $ this ->editor ->getWorkbookDateSystem ());
160
+ } catch (\InvalidArgumentException $ iaex ) {
161
+ // Never happens
162
+ return null ;
163
+ }
152
164
}
153
165
154
166
/**
@@ -166,6 +178,8 @@ public function readInt(): ?int
166
178
/**
167
179
* Read the string value of the cell,
168
180
* compatible with the shared string approach.
181
+ * @throws XlsxFastEditorFileFormatException
182
+ * @throws XlsxFastEditorXmlException
169
183
*/
170
184
public function readString (): ?string
171
185
{
@@ -188,6 +202,8 @@ public function readString(): ?string
188
202
189
203
/**
190
204
* Read the hyperlink value of the cell, if any.
205
+ * @throws XlsxFastEditorFileFormatException
206
+ * @throws XlsxFastEditorXmlException
191
207
*/
192
208
public function readHyperlink (): ?string
193
209
{
@@ -196,12 +212,17 @@ public function readHyperlink(): ?string
196
212
if (!is_string ($ rid ) || $ rid === '' ) {
197
213
return null ;
198
214
}
199
- return $ this ->editor ->_getHyperlink ($ this ->sheetNumber , $ rid );
215
+ try {
216
+ return $ this ->editor ->_getHyperlink ($ this ->sheetNumber , $ rid );
217
+ } catch (\InvalidArgumentException $ iax ) {
218
+ throw new XlsxFastEditorXmlException ("Error querying XML fragment for hyperlink in cell {$ this ->name ()}! " , $ iax ->getCode (), $ iax );
219
+ }
200
220
}
201
221
202
222
/**
203
223
* Clean the cell to have its value written.
204
224
* @return \DOMElement The `<v>` value element of the provided cell, or null in case of error.
225
+ * @throws XlsxFastEditorXmlException
205
226
*/
206
227
private function initCellValue (): \DOMElement
207
228
{
@@ -224,9 +245,13 @@ private function initCellValue(): \DOMElement
224
245
}
225
246
if ($ v === null ) {
226
247
// There was no existing <v>
227
- $ v = $ this ->c ->ownerDocument === null ? null : $ this ->c ->ownerDocument ->createElement ('v ' );
248
+ try {
249
+ $ v = $ this ->c ->ownerDocument === null ? null : $ this ->c ->ownerDocument ->createElement ('v ' );
250
+ } catch (\DOMException $ dex ) {
251
+ throw new XlsxFastEditorXmlException ("Error creating value for cell {$ this ->name ()}! " , $ dex ->code , $ dex );
252
+ }
228
253
if ($ v == false ) {
229
- throw new XlsxFastEditorXmlException (' Error creating value for cell! ' );
254
+ throw new XlsxFastEditorXmlException (" Error creating value for cell { $ this -> name ()} ! " );
230
255
}
231
256
$ this ->c ->appendChild ($ v );
232
257
}
@@ -237,6 +262,7 @@ private function initCellValue(): \DOMElement
237
262
/**
238
263
* Write a formulat, without changing the type/style of the cell.
239
264
* Removes the formulas of the cell, if any.
265
+ * @throws XlsxFastEditorXmlException
240
266
*/
241
267
public function writeFormula (string $ value ): void
242
268
{
@@ -260,9 +286,13 @@ public function writeFormula(string $value): void
260
286
261
287
$ dom = $ this ->c ->ownerDocument ;
262
288
if ($ dom === null ) {
263
- throw new XlsxFastEditorInputException ("Internal error accessing cell {$ this ->name ()}! " );
289
+ throw new XlsxFastEditorXmlException ("Internal error accessing cell {$ this ->name ()}! " );
290
+ }
291
+ try {
292
+ $ f = $ dom ->createElement ('f ' , $ value );
293
+ } catch (\DOMException $ dex ) {
294
+ throw new XlsxFastEditorXmlException ("Error creating formulat for cell {$ this ->name ()}! " , $ dex ->code , $ dex );
264
295
}
265
- $ f = $ dom ->createElement ('f ' , $ value );
266
296
$ this ->c ->appendChild ($ f );
267
297
268
298
$ this ->editor ->_clearCalcChain ();
@@ -272,8 +302,8 @@ public function writeFormula(string $value): void
272
302
/**
273
303
* Write a number, without changing the type/style of the cell.
274
304
* Removes the formulas of the cell, if any.
275
- *
276
305
* @param int|float $value
306
+ * @throws XlsxFastEditorXmlException
277
307
*/
278
308
private function writeNumber ($ value ): void
279
309
{
@@ -286,6 +316,7 @@ private function writeNumber($value): void
286
316
* Write a float, without changing the type/style of the cell.
287
317
* Removes the formulas of the cell, if any.
288
318
* @param float $value
319
+ * @throws XlsxFastEditorXmlException
289
320
*/
290
321
public function writeFloat (float $ value ): void
291
322
{
@@ -296,6 +327,7 @@ public function writeFloat(float $value): void
296
327
* Write an integer, without changing the type/style of the cell.
297
328
* Removes the formulas of the cell, if any.
298
329
* @param int $value
330
+ * @throws XlsxFastEditorXmlException
299
331
*/
300
332
public function writeInt (int $ value ): void
301
333
{
@@ -305,6 +337,8 @@ public function writeInt(int $value): void
305
337
/**
306
338
* Write a string, without changing the type/style of the cell.
307
339
* Removes the formulas of the cell, if any.
340
+ * @throws XlsxFastEditorFileFormatException
341
+ * @throws XlsxFastEditorXmlException
308
342
*/
309
343
public function writeString (string $ value ): void
310
344
{
@@ -319,6 +353,8 @@ public function writeString(string $value): void
319
353
* Replace the hyperlink of the cell, if that cell already has an hyperlink.
320
354
* Warning: does not support the creation of a new hyperlink.
321
355
* @return bool True if the hyperlink could be replaced, false otherwise.
356
+ * @throws XlsxFastEditorFileFormatException
357
+ * @throws XlsxFastEditorXmlException
322
358
*/
323
359
public function writeHyperlink (string $ value ): bool
324
360
{
@@ -327,6 +363,10 @@ public function writeHyperlink(string $value): bool
327
363
if (!is_string ($ rId ) || $ rId === '' ) {
328
364
return false ;
329
365
}
330
- return $ this ->editor ->_setHyperlink ($ this ->sheetNumber , $ rId , $ value );
366
+ try {
367
+ return $ this ->editor ->_setHyperlink ($ this ->sheetNumber , $ rId , $ value );
368
+ } catch (\InvalidArgumentException $ iax ) {
369
+ throw new XlsxFastEditorXmlException ("Error querying XML fragment for hyperlink in cell {$ this ->name ()}! " , $ iax ->getCode (), $ iax );
370
+ }
331
371
}
332
372
}
0 commit comments