Skip to content

Commit

Permalink
Improvements, Restructuring on ZugferdKositValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
HorstOeko committed Apr 9, 2024
1 parent 5b25a3d commit 0ff5c09
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 13 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ _backup*
/nbproject/

!/setup/.htaccess
.vscode
.vscode
.idea

/vendor/
Expand All @@ -83,4 +83,5 @@ _backup*
/composer.lock
/examples/fullpdf.pdf
/examples/factur-x.xml
/examples/filetovalidate-report.xml
myfile_dbg.xml
80 changes: 68 additions & 12 deletions src/ZugferdKositValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\ExecutableFinder;
use Throwable;
use ZipArchive;

/**
* Class representing the validator against Schematron (Kosit) for documents
Expand Down Expand Up @@ -231,7 +232,7 @@ public function validate(): ZugferdKositValidator
}
}

$this->cleanupBaseDirectory();
//$this->cleanupBaseDirectory();

return $this;
}
Expand All @@ -252,6 +253,36 @@ private function resolveBaseDirectory(): string
return $baseDirectory;
}

/**
* Get the full filename of the validator application jar file
*
* @return string
*/
private function resolveAppJarFilename(): string
{
return PathUtils::combineAllPaths($this->resolveBaseDirectory(), $this->validatorAppJarFilename);
}

/**
* Get the full filename of the validator application scenario file
*
* @return string
*/
private function resolveAppScenarioFilename(): string
{
return PathUtils::combinePathWithFile($this->resolveBaseDirectory(), 'scenarios.xml');
}

/**
* Get the full filename which contains the xml to validate
*
* @return string
*/
private function resolveFileToValidateFilename(): string
{
return PathUtils::combinePathWithFile($this->resolveBaseDirectory(), 'filetovalidate.xml', '');
}

/**
* Clear the internal error bag
*
Expand Down Expand Up @@ -325,13 +356,13 @@ private function checkRequirements(): bool
return false;
}

$executeableFinder = new ExecutableFinder();

if (is_null($executeableFinder->find('unzip'))) {
$this->addToErrorBag("UNZIP not installed on this machine");
if (!extension_loaded('zip')) {
$this->addToErrorBag("ZIP extension not installed");
return false;
}

$executeableFinder = new ExecutableFinder();

if (is_null($executeableFinder->find('java'))) {
$this->addToErrorBag("JAVA not installed on this machine");
return false;
Expand Down Expand Up @@ -383,11 +414,13 @@ private function unpackRequiredFiles(): bool
$validatorAppFile = PathUtils::combinePathWithFile($this->resolveBaseDirectory(), $this->validatorAppZipFilename);
$validatorScenarioFile = PathUtils::combinePathWithFile($this->resolveBaseDirectory(), $this->validatorScenarioZipFilename);

if (!$this->runProcess(['unzip', $validatorAppFile, '-d', $this->resolveBaseDirectory()])) {
if ($this->unpackRequiredFile($validatorAppFile) !== true) {
$this->addToErrorBag("Unable to unpack ZIP archive $validatorAppFile");
return false;
}

if (!$this->runProcess(['unzip', $validatorScenarioFile, '-d', $this->resolveBaseDirectory()])) {
if ($this->unpackRequiredFile($validatorScenarioFile) !== true) {
$this->addToErrorBag("Unable to unpack ZIP archive $validatorScenarioFile");
return false;
}

Expand All @@ -396,23 +429,46 @@ private function unpackRequiredFiles(): bool
return true;
}

/**
* Unpack single required file
*
* @param string $filename
* @return boolean
*/
private function unpackRequiredFile(string $filename): bool
{
$zip = new ZipArchive();

if ($zip->open($filename) !== true) {
return false;
}

if ($zip->extractTo($this->resolveBaseDirectory()) !== true) {;
$zip->close();
return false;
}

$zip->close();
return true;
}

/**
* Runs the validator java application
*
* @return boolean
*/
private function runValidator(): bool
{
$jarFilename = PathUtils::combineAllPaths($this->resolveBaseDirectory(), $this->validatorAppJarFilename);
$scenarioFilename = PathUtils::combinePathWithFile($this->resolveBaseDirectory(), 'scenarios.xml', '');
$xmlFilename = PathUtils::combinePathWithFile($this->resolveBaseDirectory(), 'filetovalidate.xml', '');
$jarFilename = $this->resolveAppJarFilename();
$scenarioFilename = $this->resolveAppScenarioFilename();
$fileToValidateFilename = $this->resolveFileToValidateFilename();

if (file_put_contents($xmlFilename, $this->document->serializeAsXml()) === false) {
if (file_put_contents($fileToValidateFilename, $this->document->serializeAsXml()) === false) {
$this->addToErrorBag("Cannot create temporary file which contains the XML to validate");
return false;
}

if (!$this->runProcess(['java', '-jar', $jarFilename, '-r', $this->resolveBaseDirectory(), '-s', $scenarioFilename, $xmlFilename])) {
if (!$this->runProcess(['java', '-jar', $jarFilename, '-r', $this->resolveBaseDirectory(), '-s', $scenarioFilename, $fileToValidateFilename])) {
return false;
}

Expand Down

0 comments on commit 0ff5c09

Please sign in to comment.