Skip to content

Commit 711635c

Browse files
committed
Se puede asignar el XmlDocument en las excepciones.
1 parent c71e9b2 commit 711635c

1 file changed

Lines changed: 18 additions & 11 deletions

File tree

src/Exception/XmlException.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
namespace Derafu\Xml\Exception;
1414

15+
use Derafu\Xml\Contract\XmlDocumentInterface;
1516
use Exception;
1617
use LibXMLError;
1718
use Throwable;
@@ -21,34 +22,29 @@
2122
*/
2223
class 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

Comments
 (0)