From 040b6ff389982c9a0604c614a76fda5bff85fd2d Mon Sep 17 00:00:00 2001 From: Duncan Pronk Date: Thu, 2 Feb 2023 11:47:30 +0100 Subject: [PATCH 1/3] feat: allow returning of error message --- src/Validator.php | 14 ++++++++++---- src/ValidatorInterface.php | 9 ++++++--- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/Validator.php b/src/Validator.php index 05358e4..354f519 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -1,4 +1,6 @@ -isXMLStringValid($string, $xsdPath); + return $this->isXMLStringValid($string, $xsdPath, $returnError); } /** * @param string $xml * @param string|null $xsdPath + * @param bool $returnError * @return bool */ - public function isXMLStringValid(string $xml, string $xsdPath = null): bool + public function isXMLStringValid(string $xml, string $xsdPath = null, bool $returnError = false): bool|string { try { if (is_string($xsdPath)) { @@ -69,6 +72,9 @@ public function isXMLStringValid(string $xml, string $xsdPath = null): bool } return $this->isXMLValid($xml); } catch (InvalidXml $e) { + if (true === $returnError) { + return $e->getMessage(); + } return false; } } diff --git a/src/ValidatorInterface.php b/src/ValidatorInterface.php index e828f3b..90c4604 100644 --- a/src/ValidatorInterface.php +++ b/src/ValidatorInterface.php @@ -1,4 +1,6 @@ - Date: Thu, 2 Feb 2023 11:52:52 +0100 Subject: [PATCH 2/3] fix: added missing semicolon --- src/ValidatorInterface.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ValidatorInterface.php b/src/ValidatorInterface.php index 90c4604..d402ccc 100644 --- a/src/ValidatorInterface.php +++ b/src/ValidatorInterface.php @@ -16,7 +16,7 @@ interface ValidatorInterface * @param string|null $xsdPath * @return bool */ - public function isXMLFileValid(string $xmlPath, string $xsdPath = null, bool $returnError = false): bool|string + public function isXMLFileValid(string $xmlPath, string $xsdPath = null, bool $returnError = false): bool|string; /** * @param string $xml From ae58aa986839c5e437d8c4a3a1bd3041cfd7b140 Mon Sep 17 00:00:00 2001 From: Duncan Date: Thu, 16 May 2024 13:07:05 +0200 Subject: [PATCH 3/3] fix: revert return error and added functions to get last error message and added functions to get errors --- src/Validator.php | 56 ++++++++++++++++++++++++++++++-------- src/ValidatorInterface.php | 10 +++++-- 2 files changed, 53 insertions(+), 13 deletions(-) diff --git a/src/Validator.php b/src/Validator.php index 354f519..86de816 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -33,16 +33,18 @@ final class Validator implements ValidatorInterface */ private $encoding = Strings::UTF_8; - public function isXMLFileValid(string $xmlPath, string $xsdPath = null, bool $returnError = false): bool|string + /** + * @var null|\Exception + */ + private $error = null; + + public function isXMLFileValid(string $xmlPath, string $xsdPath = null): bool { try { $string = (new Xml($xmlPath)) ->getContents(); - } catch (EmptyFile $e) { - return false; - } catch (FileCouldNotBeOpenedException $e) { - return false; - } catch (FileDoesNotExist $e) { + } catch (EmptyFile | FileCouldNotBeOpenedException | FileDoesNotExist $e) { + $this->error = $e; return false; } @@ -51,11 +53,12 @@ public function isXMLFileValid(string $xmlPath, string $xsdPath = null, bool $re $xsdPath = (new Xsd($xsdPath)) ->getPath(); } catch (FileDoesNotExist $e) { + $this->error = $e; return false; } } - return $this->isXMLStringValid($string, $xsdPath, $returnError); + return $this->isXMLStringValid($string, $xsdPath); } /** @@ -64,7 +67,7 @@ public function isXMLFileValid(string $xmlPath, string $xsdPath = null, bool $re * @param bool $returnError * @return bool */ - public function isXMLStringValid(string $xml, string $xsdPath = null, bool $returnError = false): bool|string + public function isXMLStringValid(string $xml, string $xsdPath = null): bool { try { if (is_string($xsdPath)) { @@ -72,9 +75,7 @@ public function isXMLStringValid(string $xml, string $xsdPath = null, bool $retu } return $this->isXMLValid($xml); } catch (InvalidXml $e) { - if (true === $returnError) { - return $e->getMessage(); - } + $this->error = $e; return false; } } @@ -168,4 +169,37 @@ public function setEncoding(string $encoding): void { $this->encoding = $encoding; } + + /** + * @return int Will return 0 when no error has occurred + */ + public function getErrorCode(): int + { + if (null !== $this->error) { + return $this->error->getCode(); + } + + return 0; + } + + /** + * @return string Will return empty string when no error has occurred + */ + public function getErrorMessage(): string + { + if (null !== $this->error) { + return $this->error->getMessage(); + } + + return ''; + } + + public function getErrorType(): null|string + { + if (null !== $this->error) { + return get_class($this->error); + } + + return null; + } } diff --git a/src/ValidatorInterface.php b/src/ValidatorInterface.php index d402ccc..1371e9d 100644 --- a/src/ValidatorInterface.php +++ b/src/ValidatorInterface.php @@ -16,7 +16,7 @@ interface ValidatorInterface * @param string|null $xsdPath * @return bool */ - public function isXMLFileValid(string $xmlPath, string $xsdPath = null, bool $returnError = false): bool|string; + public function isXMLFileValid(string $xmlPath, string $xsdPath = null): bool; /** * @param string $xml @@ -24,7 +24,7 @@ public function isXMLFileValid(string $xmlPath, string $xsdPath = null, bool $re * @param bool $returnError * @return bool */ - public function isXMLStringValid(string $xml, string $xsdPath = null, bool $returnError = false): bool|string; + public function isXMLStringValid(string $xml, string $xsdPath = null): bool; /** * @return string @@ -45,4 +45,10 @@ public function getEncoding(): string; * @param string $encoding */ public function setEncoding(string $encoding): void; + + public function getErrorCode(): int; + + public function getErrorMessage(): string; + + public function getErrorType(): null|string; }