diff --git a/src/Validator.php b/src/Validator.php index 05358e4..86de816 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -1,4 +1,6 @@ -getContents(); - } catch (EmptyFile $e) { - return false; - } catch (FileCouldNotBeOpenedException $e) { - return false; - } catch (FileDoesNotExist $e) { + } catch (EmptyFile | FileCouldNotBeOpenedException | FileDoesNotExist $e) { + $this->error = $e; return false; } @@ -49,6 +53,7 @@ public function isXMLFileValid(string $xmlPath, string $xsdPath = null): bool $xsdPath = (new Xsd($xsdPath)) ->getPath(); } catch (FileDoesNotExist $e) { + $this->error = $e; return false; } } @@ -59,6 +64,7 @@ public function isXMLFileValid(string $xmlPath, string $xsdPath = null): bool /** * @param string $xml * @param string|null $xsdPath + * @param bool $returnError * @return bool */ public function isXMLStringValid(string $xml, string $xsdPath = null): bool @@ -69,6 +75,7 @@ public function isXMLStringValid(string $xml, string $xsdPath = null): bool } return $this->isXMLValid($xml); } catch (InvalidXml $e) { + $this->error = $e; return false; } } @@ -162,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 e828f3b..1371e9d 100644 --- a/src/ValidatorInterface.php +++ b/src/ValidatorInterface.php @@ -1,4 +1,6 @@ -