1212
1313namespace Derafu \Xml \Exception ;
1414
15+ use Derafu \Xml \Contract \XmlDocumentInterface ;
1516use Exception ;
1617use LibXMLError ;
1718use Throwable ;
2122 */
2223class XmlException extends Exception
2324{
24- /**
25- * Array with the errors.
26- *
27- * @var array
28- */
29- private array $ errors ;
30-
3125 /**
3226 * Constructor of the exception.
3327 *
3428 * @param string $message The exception message.
3529 * @param array $errors The array with the errors.
3630 * @param int $code The exception code (optional).
3731 * @param Throwable|null $previous The previous exception (optional).
32+ * @param XmlDocumentInterface|null $xmlDocument The XML document that
33+ * caused the exception or `null` if it is not present in the exception.
3834 */
3935 public function __construct (
4036 string $ message ,
41- array $ errors = [],
37+ private array $ errors = [],
4238 int $ code = 0 ,
43- ?Throwable $ previous = null
39+ ?Throwable $ previous = null ,
40+ private ?XmlDocumentInterface $ xmlDocument = null
4441 ) {
4542 $ message = trim (sprintf (
4643 '%s %s ' ,
4744 $ message ,
48- implode (' ' , $ this ->libXmlErrorToString ($ errors ))
45+ implode (' ' , $ this ->libXmlErrorToString ($ this -> errors ))
4946 ));
5047
51- $ this ->errors = $ errors ;
5248 parent ::__construct ($ message , $ code , $ previous );
5349 }
5450
@@ -62,6 +58,17 @@ public function getErrors(): array
6258 return $ this ->errors ;
6359 }
6460
61+ /**
62+ * Gets the XML document that caused the exception.
63+ *
64+ * @return XmlDocumentInterface|null The XML document that caused the
65+ * exception or `null` if it is not present in the exception.
66+ */
67+ public function getXmlDocument (): ?XmlDocumentInterface
68+ {
69+ return $ this ->xmlDocument ;
70+ }
71+
6572 /**
6673 * Processes an array of errors, probably of LibXMLError, and returns it as
6774 * an array of strings.
0 commit comments