@@ -22,12 +22,15 @@ public function __construct(XlsxFastEditor $editor, int $sheetNumber, \DOMElemen
2222 $ this ->c = $ c ;
2323 }
2424
25+ /**
26+ * @throws XlsxFastEditorXmlException
27+ */
2528 private function getXPath (): \DOMXPath
2629 {
2730 if ($ this ->xpath === null ) {
2831 $ dom = $ this ->c ->ownerDocument ;
2932 if ($ dom === null ) {
30- throw new XlsxFastEditorInputException ("Internal error accessing cell {$ this ->name ()}! " );
33+ throw new XlsxFastEditorXmlException ("Internal error accessing cell {$ this ->name ()}! " );
3134 }
3235 $ xpath = new \DOMXPath ($ dom );
3336 $ xpath ->registerNamespace ('o ' , XlsxFastEditor::_OXML_NAMESPACE );
@@ -47,6 +50,7 @@ public function name(): string
4750
4851 /**
4952 * Column name (e.g., `'D'`).
53+ * @throws XlsxFastEditorXmlException
5054 */
5155 public function column (): string
5256 {
@@ -88,6 +92,7 @@ public function getNextCell(): ?XlsxFastEditorCell
8892
8993 /**
9094 * Access the parent row of the cell.
95+ * @throws XlsxFastEditorXmlException
9196 */
9297 public function getRow (): XlsxFastEditorRow
9398 {
@@ -100,6 +105,7 @@ public function getRow(): XlsxFastEditorRow
100105
101106 /**
102107 * Read a formula in the given worksheet at the given cell location.
108+ * @throws XlsxFastEditorXmlException
103109 */
104110 public function readFormula (): ?string
105111 {
@@ -119,10 +125,9 @@ private function value(): ?string
119125 $ vs = $ this ->c ->getElementsByTagName ('v ' );
120126 if ($ vs ->length > 0 ) {
121127 $ 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 ;
124130 }
125- return $ v ->nodeValue ;
126131 }
127132 return null ;
128133 }
@@ -141,14 +146,21 @@ public function readFloat(): ?float
141146
142147 /**
143148 * Read the date/time value of the cell, if any.
149+ * @throws XlsxFastEditorFileFormatException
150+ * @throws XlsxFastEditorXmlException
144151 */
145152 public function readDateTime (): ?\DateTimeImmutable
146153 {
147154 $ value = $ this ->readFloat ();
148155 if ($ value === null ) {
149156 return null ;
150157 }
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+ }
152164 }
153165
154166 /**
@@ -166,6 +178,8 @@ public function readInt(): ?int
166178 /**
167179 * Read the string value of the cell,
168180 * compatible with the shared string approach.
181+ * @throws XlsxFastEditorFileFormatException
182+ * @throws XlsxFastEditorXmlException
169183 */
170184 public function readString (): ?string
171185 {
@@ -188,6 +202,8 @@ public function readString(): ?string
188202
189203 /**
190204 * Read the hyperlink value of the cell, if any.
205+ * @throws XlsxFastEditorFileFormatException
206+ * @throws XlsxFastEditorXmlException
191207 */
192208 public function readHyperlink (): ?string
193209 {
@@ -196,12 +212,17 @@ public function readHyperlink(): ?string
196212 if (!is_string ($ rid ) || $ rid === '' ) {
197213 return null ;
198214 }
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+ }
200220 }
201221
202222 /**
203223 * Clean the cell to have its value written.
204224 * @return \DOMElement The `<v>` value element of the provided cell, or null in case of error.
225+ * @throws XlsxFastEditorXmlException
205226 */
206227 private function initCellValue (): \DOMElement
207228 {
@@ -224,9 +245,13 @@ private function initCellValue(): \DOMElement
224245 }
225246 if ($ v === null ) {
226247 // 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+ }
228253 if ($ v == false ) {
229- throw new XlsxFastEditorXmlException (' Error creating value for cell! ' );
254+ throw new XlsxFastEditorXmlException (" Error creating value for cell { $ this -> name ()} ! " );
230255 }
231256 $ this ->c ->appendChild ($ v );
232257 }
@@ -237,6 +262,7 @@ private function initCellValue(): \DOMElement
237262 /**
238263 * Write a formulat, without changing the type/style of the cell.
239264 * Removes the formulas of the cell, if any.
265+ * @throws XlsxFastEditorXmlException
240266 */
241267 public function writeFormula (string $ value ): void
242268 {
@@ -260,9 +286,13 @@ public function writeFormula(string $value): void
260286
261287 $ dom = $ this ->c ->ownerDocument ;
262288 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 );
264295 }
265- $ f = $ dom ->createElement ('f ' , $ value );
266296 $ this ->c ->appendChild ($ f );
267297
268298 $ this ->editor ->_clearCalcChain ();
@@ -272,8 +302,8 @@ public function writeFormula(string $value): void
272302 /**
273303 * Write a number, without changing the type/style of the cell.
274304 * Removes the formulas of the cell, if any.
275- *
276305 * @param int|float $value
306+ * @throws XlsxFastEditorXmlException
277307 */
278308 private function writeNumber ($ value ): void
279309 {
@@ -286,6 +316,7 @@ private function writeNumber($value): void
286316 * Write a float, without changing the type/style of the cell.
287317 * Removes the formulas of the cell, if any.
288318 * @param float $value
319+ * @throws XlsxFastEditorXmlException
289320 */
290321 public function writeFloat (float $ value ): void
291322 {
@@ -296,6 +327,7 @@ public function writeFloat(float $value): void
296327 * Write an integer, without changing the type/style of the cell.
297328 * Removes the formulas of the cell, if any.
298329 * @param int $value
330+ * @throws XlsxFastEditorXmlException
299331 */
300332 public function writeInt (int $ value ): void
301333 {
@@ -305,6 +337,8 @@ public function writeInt(int $value): void
305337 /**
306338 * Write a string, without changing the type/style of the cell.
307339 * Removes the formulas of the cell, if any.
340+ * @throws XlsxFastEditorFileFormatException
341+ * @throws XlsxFastEditorXmlException
308342 */
309343 public function writeString (string $ value ): void
310344 {
@@ -319,6 +353,8 @@ public function writeString(string $value): void
319353 * Replace the hyperlink of the cell, if that cell already has an hyperlink.
320354 * Warning: does not support the creation of a new hyperlink.
321355 * @return bool True if the hyperlink could be replaced, false otherwise.
356+ * @throws XlsxFastEditorFileFormatException
357+ * @throws XlsxFastEditorXmlException
322358 */
323359 public function writeHyperlink (string $ value ): bool
324360 {
@@ -327,6 +363,10 @@ public function writeHyperlink(string $value): bool
327363 if (!is_string ($ rId ) || $ rId === '' ) {
328364 return false ;
329365 }
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+ }
331371 }
332372}
0 commit comments