Skip to content

Commit

Permalink
Merge pull request #39 from horstoeko/kositvalidator
Browse files Browse the repository at this point in the history
KositValidator -> Better message handling, New method to retrieve the…
  • Loading branch information
horstoeko authored Apr 13, 2024
2 parents da94b22 + ac81ad1 commit f18db8b
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 28 deletions.
8 changes: 6 additions & 2 deletions examples/KositValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
70 changes: 44 additions & 26 deletions src/ZugferdKositValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ class ZugferdKositValidator
*/
public const MSG_TYPE_VALIDATIONINFORMATION = 'validationinformation';

/**
* Message Type "Process Output"
*/
public const MSG_TYPE_PROCESSOUTPUT = 'processoutput';

/**
* Constructor
*
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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
*
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit f18db8b

Please sign in to comment.