Skip to content

Commit

Permalink
Add validation errors to message bag
Browse files Browse the repository at this point in the history
  • Loading branch information
HorstOeko committed Nov 3, 2024
1 parent 1ffdb58 commit 2cfa7a4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
17 changes: 11 additions & 6 deletions src/ZugferdMailReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,24 +275,29 @@ protected function checkSingleMessageAttachment(ZugferdMailAccount $account, Fol
private function validateDocument(ZugferdDocument $document, array $messageAdditionalData): void
{
if ($this->config->getSymfonyValidationEnabled()) {
$validator = new ZugferdDocumentValidator($document);
$this->raiseRuntimeExceptionIf($validator->validateDocument()->count() != 0, "Validation against Symfony-Validation failed");
$validator = (new ZugferdDocumentValidator($document))->validateDocument();
$this->raiseRuntimeExceptionIf($validator->count() != 0, "Validation against Symfony-Validation failed");
$this->addSuccessMessageToMessageBag('The document was successfully validated against Symfony validator', $messageAdditionalData);
} else {
$this->addLogSecondaryMessageToMessageBag('The document was not validated against Symfony validator (Disabled)', $messageAdditionalData);
}

if ($this->config->getXsdValidationEnabled()) {
$validator = new ZugferdXsdValidator($document);
$this->raiseRuntimeExceptionIf($validator->validate()->hasValidationErrors(), "Validation against XSD-Validation failed");
$validator = (new ZugferdXsdValidator($document))->validate();
$this->addMultipleErrorMessagesToMessageBagIf($validator->hasValidationErrors(), $validator->validationErrors());
$this->raiseRuntimeExceptionIf($validator->hasValidationErrors(), "Validation against XSD-Validation failed");
$this->addSuccessMessageToMessageBag('The document was successfully validated against XSD scheme', $messageAdditionalData);
} else {
$this->addLogSecondaryMessageToMessageBag('The document was not validated against XSD scheme (Disabled)', $messageAdditionalData);
}

if ($this->config->getKositValidationEnabled()) {
$validator = new ZugferdKositValidator($document);
$this->raiseRuntimeExceptionIf($validator->validate()->hasValidationErrors(), "Validation against KosIT Validation failed");
$validator = (new ZugferdKositValidator($document))->validate();
$this->addMultipleLogSecondaryMessagesToMessageBagIf($validator->hasNoValidationInformation(), $validator->getValidationInformation());
$this->addMultipleWarningMessagesToMessageBagIf($validator->hasValidationWarnings(), $validator->getValidationWarnings());
$this->addMultipleErrorMessagesToMessageBagIf($validator->hasValidationErrors(), $validator->getValidationErrors());
$this->addMultipleErrorMessagesToMessageBagIf($validator->hasProcessErrors(), $validator->getProcessErrors());
$this->raiseRuntimeExceptionIf($validator->hasValidationErrors(), "Validation against KosIT Validation failed");
$this->addSuccessMessageToMessageBag('The document was successfully validated against the KosIT validator', $messageAdditionalData);
} else {
$this->addLogSecondaryMessageToMessageBag('The document was not validated against the KosIT validator (Disabled)', $messageAdditionalData);
Expand Down
19 changes: 13 additions & 6 deletions src/concerns/ZugferdMailConsoleOutputsMessageBagMessages.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,22 +96,29 @@ function ($message, int $messageKey) use ($messages) {
*/
private function formatMessageBagMessage(array $message): string
{
//$messageText = ZugferdMailStringHelper::truncateString($message["message"], 100);
$messageText = wordwrap($message["message"], 100);

if ($message["type"] === ZugferdMailMessageBagType::MESSAGETYPE_LOG_SECONDARY) {
return sprintf("<gray>%s</gray>", $message["message"]);
return sprintf("<gray>%s</gray>", $messageText);
}

if ($message["type"] === ZugferdMailMessageBagType::MESSAGETYPE_WARN) {
return sprintf("<comment>%s</comment>", $message["message"]);
return sprintf("<comment>%s</comment>", $messageText);
}

if ($message["type"] === ZugferdMailMessageBagType::MESSAGETYPE_ERROR) {
if (isset($message["additionalData"]["errno"])) {
return sprintf("<error>%s in %s:%s</error>", $message["message"], $message["additionalData"]["errfile"], $message["additionalData"]["errline"]);
return sprintf("<red-text>%s in %s:%s</red-text>", $messageText, $message["additionalData"]["errfile"], $message["additionalData"]["errline"]);
} else {
return sprintf("<error>%s</error>", $message["message"]);
return sprintf("<red-text>%s</red-text>", $messageText);
}
}

if ($message["type"] === ZugferdMailMessageBagType::MESSAGETYPE_SUCCESS) {
return sprintf("<info>%s</info>", $message["message"]);
return sprintf("<info>%s</info>", $messageText);
}
return $message["message"];

return $messageText;
}
}
2 changes: 2 additions & 0 deletions src/console/ZugferdMailBaseConsoleCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ private function setCustomColors(): void
{
$outputStyle = new OutputFormatterStyle('bright-blue', '#000', []);
$this->outputInterface->getFormatter()->setStyle('gray', $outputStyle);
$outputStyle = new OutputFormatterStyle('red', '#000', []);
$this->outputInterface->getFormatter()->setStyle('red-text', $outputStyle);
}

/**
Expand Down

0 comments on commit 2cfa7a4

Please sign in to comment.