diff --git a/examples/KositValidator.php b/examples/KositValidator.php index 206931df..bfda916f 100644 --- a/examples/KositValidator.php +++ b/examples/KositValidator.php @@ -14,15 +14,19 @@ */ function showValidationResult(ZugferdKositValidator $kositValidator) { + foreach ($kositValidator->getProcessOutput() as $output) { + echo $output . PHP_EOL; + } + if ($kositValidator->hasProcessErrors()) { echo "\033[01;31mProcess failed\e[0m\n"; foreach ($kositValidator->getProcessErrors() as $processError) { - echo " - " . $processError["message"] . PHP_EOL; + echo " - " . $processError . PHP_EOL; } } elseif ($kositValidator->hasValidationErrors()) { echo "\033[01;31mValidation failed\e[0m\n"; foreach ($kositValidator->getValidationErrors() as $validationError) { - echo " - " . $validationError["message"] . PHP_EOL; + echo " - " . $validationError . PHP_EOL; } } else { echo "\033[01;32mValidation passed\e[0m\n"; diff --git a/src/ZugferdKositValidator.php b/src/ZugferdKositValidator.php index eda8ec38..a27e4f79 100644 --- a/src/ZugferdKositValidator.php +++ b/src/ZugferdKositValidator.php @@ -128,6 +128,11 @@ class ZugferdKositValidator */ public const MSG_TYPE_VALIDATIONINFORMATION = 'validationinformation'; + /** + * Message Type "Process Output" + */ + public const MSG_TYPE_PROCESSOUTPUT = 'processoutput'; + /** * Constructor * @@ -416,20 +421,36 @@ private function addToMessageBag($error, string $messageType = ""): void } /** - * Returns an array of all validation errors + * Get messages from messagebag filtered by message type * + * @param string $messageType * @return array */ - public function getValidationErrors(): array + private function getMessageBagFiltered(string $messageType): array { - return array_filter( - $this->messageBag, + return array_map( function ($data) { - return $data['type'] == static::MSG_TYPE_VALIDATIONERROR; - } + return $data["message"]; + }, + array_filter( + $this->messageBag, + function ($data) use ($messageType) { + return $data['type'] == $messageType; + } + ) ); } + /** + * Returns an array of all validation errors + * + * @return array + */ + public function getValidationErrors(): array + { + return $this->getMessageBagFiltered(static::MSG_TYPE_VALIDATIONERROR); + } + /** * Returns true if __no__ validation errors are present otherwise false * @@ -457,12 +478,7 @@ public function hasValidationErrors(): bool */ public function getValidationWarnings(): array { - return array_filter( - $this->messageBag, - function ($data) { - return $data['type'] == static::MSG_TYPE_VALIDATIONWARNING; - } - ); + return $this->getMessageBagFiltered(static::MSG_TYPE_VALIDATIONWARNING); } /** @@ -492,12 +508,7 @@ public function hasValidationWarnings(): bool */ public function getValidationInformation(): array { - return array_filter( - $this->messageBag, - function ($data) { - return $data['type'] == static::MSG_TYPE_VALIDATIONINFORMATION; - } - ); + return $this->getMessageBagFiltered(static::MSG_TYPE_VALIDATIONINFORMATION); } /** @@ -527,12 +538,7 @@ public function hasValidationInformation(): bool */ public function getProcessErrors(): array { - return array_filter( - $this->messageBag, - function ($data) { - return $data['type'] == static::MSG_TYPE_INTERNALERROR; - } - ); + return $this->getMessageBagFiltered(static::MSG_TYPE_INTERNALERROR); } /** @@ -555,6 +561,16 @@ public function hasProcessErrors(): bool return !$this->hasNoProcessErrors(); } + /** + * Returns an array of all messages from process system (calling external applications) + * + * @return array + */ + public function getProcessOutput(): array + { + return $this->getMessageBagFiltered(static::MSG_TYPE_PROCESSOUTPUT); + } + /** * Check Requirements * @@ -802,9 +818,11 @@ private function runValidationApplication(array $command, string $workingdirecto $process->setWorkingDirectory($workingdirectory); $process->run(); + foreach (preg_split("/\r\n|\n|\r/", $process->getOutput()) as $outputLine) { + $this->addToMessageBag($outputLine, static::MSG_TYPE_PROCESSOUTPUT); + } + if (!$process->isSuccessful()) { - echo "Dir " . $this->resolveBaseDirectory(); - echo "Exitcode " . $process->getExitCode(); if ($process->getExitCode() == -1) { $this->addToMessageBag("Parsing error. The commandline arguments specified are incorrect", static::MSG_TYPE_VALIDATIONERROR); }