Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added a way to get error messages #138

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 46 additions & 6 deletions src/Validator.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Hyperized\Xml;

Expand Down Expand Up @@ -31,16 +33,18 @@ final class Validator implements ValidatorInterface
*/
private $encoding = Strings::UTF_8;

/**
* @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;
}

Expand All @@ -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;
}
}
Expand All @@ -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
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
}
11 changes: 10 additions & 1 deletion src/ValidatorInterface.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace Hyperized\Xml;

Expand All @@ -19,6 +21,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;
Expand All @@ -42,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;
}