Skip to content

Commit

Permalink
Support for validation (Part 1)
Browse files Browse the repository at this point in the history
  • Loading branch information
HorstOeko committed Oct 4, 2024
1 parent 3b42a5d commit 67b3f3e
Show file tree
Hide file tree
Showing 5 changed files with 330 additions and 149 deletions.
21 changes: 15 additions & 6 deletions src/ZugferdMailReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,11 @@ protected function checkSingleMessageAttachment(ZugferdMailAccount $account, Fol
$document = ZugferdDocumentPdfReader::readAndGuessFromContent($attachment->getContent());
$this->raiseRuntimeExceptionIf(is_null($document), "No document returned");
$this->addSuccessMessageToMessageBag('Mail contains a ZUGFeRD compatible PDF', $messageAdditionalData);
$validator = new ZugferdXsdValidator($document);
$this->raiseRuntimeExceptionIf($validator->validate()->validationFailed(), "XSD-Validation of document failed");
if ($this->config->getXsdValidationEnabled()) {
$validator = new ZugferdXsdValidator($document);
$this->raiseRuntimeExceptionIf($validator->validate()->validationFailed(), "XSD-Validation of document failed");
$this->addSuccessMessageToMessageBag('The document was successfully validated againt XSD scheme', $messageAdditionalData);
}
$this->triggerHandlers($account, $folder, $message, $attachment, $document, ZugferdMailReaderRecognitionType::ZFMAIL_RECOGNITION_TYPE_PDF_CII);
} catch (Throwable $e) {
$this->addWarningMessageToMessageBag(sprintf("No ZUGFeRD compatible PDF found (%s)", $e->getMessage()), $messageAdditionalData);
Expand All @@ -207,8 +210,11 @@ protected function checkSingleMessageAttachment(ZugferdMailAccount $account, Fol
$this->addLogMessageToMessageBag('Checking for ZUGFeRD compatible XML', $messageAdditionalData);
$document = ZugferdDocumentReader::readAndGuessFromContent($attachment->getContent());
$this->addSuccessMessageToMessageBag('Mail contains a ZUGFeRD compatible XML', $messageAdditionalData);
$validator = new ZugferdXsdValidator($document);
$this->raiseRuntimeExceptionIf($validator->validate()->validationFailed(), "XSD-Validation of document failed");
if ($this->config->getXsdValidationEnabled()) {
$validator = new ZugferdXsdValidator($document);
$this->raiseRuntimeExceptionIf($validator->validate()->validationFailed(), "XSD-Validation of document failed");
$this->addSuccessMessageToMessageBag('The document was successfully validated againt XSD scheme', $messageAdditionalData);
}
$this->triggerHandlers($account, $folder, $message, $attachment, $document, ZugferdMailReaderRecognitionType::ZFMAIL_RECOGNITION_TYPE_XML_CII);
} catch (Throwable $e) {
$this->addWarningMessageToMessageBag(sprintf("No ZUGFeRD compatible XML found (%s)", $e->getMessage()), $messageAdditionalData);
Expand All @@ -223,8 +229,11 @@ protected function checkSingleMessageAttachment(ZugferdMailAccount $account, Fol
XmlConverterUblToCii::fromString($attachment->getContent())->convert()->saveXmlString()
);
$this->addSuccessMessageToMessageBag('Mail contains a UBL compatible XML', $messageAdditionalData);
$validator = new ZugferdXsdValidator($document);
$this->raiseRuntimeExceptionIf($validator->validate()->validationFailed(), "XSD-Validation of document failed");
if ($this->config->getXsdValidationEnabled()) {
$validator = new ZugferdXsdValidator($document);
$this->raiseRuntimeExceptionIf($validator->validate()->validationFailed(), "XSD-Validation of document failed");
$this->addSuccessMessageToMessageBag('The document was successfully validated againt XSD scheme', $messageAdditionalData);
}
$this->triggerHandlers($account, $folder, $message, $attachment, $document, ZugferdMailReaderRecognitionType::ZFMAIL_RECOGNITION_TYPE_XML_UBL);
} catch (Throwable $e) {
$this->addWarningMessageToMessageBag(sprintf("No UBL compatible XML found (%s)", $e->getMessage()), $messageAdditionalData);
Expand Down
112 changes: 112 additions & 0 deletions src/config/ZugferdMailConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,20 @@ class ZugferdMailConfig
*/
protected $ublSupportEnabled = false;

/**
* XSD validation enabled
*
* @var boolean
*/
protected $xsdValidationEnabled = false;

/**
* Kosit validation enabled (JAVA is required)
*
* @var boolean
*/
protected $kositValidationEnabled = false;

/**
* List of defined accounts
*
Expand Down Expand Up @@ -124,6 +138,100 @@ public function deactivateUblSupport(): ZugferdMailConfig
return $this;
}

/**
* Returns true if the XSD validation is enabled
*
* @return boolean
*/
public function getXsdValidationEnabled(): bool
{
return $this->xsdValidationEnabled;
}

/**
* Activate or deactivate XSD validation
*
* @param boolean $xsdValidationEnabled
* @return ZugferdMailConfig
*/
public function setXsdValidationEnabled(bool $xsdValidationEnabled): ZugferdMailConfig
{
$this->xsdValidationEnabled = $xsdValidationEnabled;

return $this;
}

/**
* Activate the UBL-Syntax support
*
* @return ZugferdMailConfig
*/
public function activateXsdValidation(): ZugferdMailConfig
{
$this->setXsdValidationEnabled(true);

return $this;
}

/**
* Deactivate the UBL-Syntax support
*
* @return ZugferdMailConfig
*/
public function deactivateXsdValidation(): ZugferdMailConfig
{
$this->setXsdValidationEnabled(false);

return $this;
}

/**
* Returns true if the Kosit validation is enabled
*
* @return boolean
*/
public function getKositValidationEnabled(): bool
{
return $this->kositValidationEnabled;
}

/**
* Activate or deactivate Kosit validation
*
* @param boolean $kositValidationEnabled
* @return ZugferdMailConfig
*/
public function setKositValidationEnabled(bool $kositValidationEnabled): ZugferdMailConfig
{
$this->kositValidationEnabled = $kositValidationEnabled;

return $this;
}

/**
* Activate the UBL-Syntax support
*
* @return ZugferdMailConfig
*/
public function activateKositValidation(): ZugferdMailConfig
{
$this->setKositValidationEnabled(true);

return $this;
}

/**
* Deactivate the UBL-Syntax support
*
* @return ZugferdMailConfig
*/
public function deactivateKositValidation(): ZugferdMailConfig
{
$this->setKositValidationEnabled(false);

return $this;
}

/**
* Add an account definition
*
Expand Down Expand Up @@ -257,6 +365,8 @@ public static function loadFromFile(string $filename): ZugferdMailConfig
$config = new ZugferdMailConfig;
$config->setDateFormat($jsonObject->dateFormat);
$config->setUblSupportEnabled($jsonObject->ublSupportEnabled);
$config->setXsdValidationEnabled($jsonObject->xsdValidationEnabled);
$config->setKositValidationEnabled($jsonObject->kositValidationEnabled);

foreach ($jsonObject->accounts as $accountDefinition) {
$account = new ZugferdMailAccount();
Expand Down Expand Up @@ -296,6 +406,8 @@ public function saveToFile(string $filename): ZugferdMailConfig
$jsonObject = new stdClass;
$jsonObject->dateFormat = $this->getDateFormat();
$jsonObject->ublSupportEnabled = $this->getUblSupportEnabled();
$jsonObject->xsdValidationEnabled = $this->getXsdValidationEnabled();
$jsonObject->kositValidationEnabled = $this->getKositValidationEnabled();
$jsonObject->accounts = [];

foreach ($this->getAccounts() as $account) {
Expand Down
Loading

0 comments on commit 67b3f3e

Please sign in to comment.