Skip to content

Commit

Permalink
Improved ZugferdKositValidator -> added direct content validation, de…
Browse files Browse the repository at this point in the history
…precated direct call of constructor, added new methods fromDocument and fromString
  • Loading branch information
HorstOeko committed Nov 1, 2024
1 parent 7914d1c commit 6a9ba32
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 25 deletions.
26 changes: 21 additions & 5 deletions examples/KositValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,24 @@ function showValidationResult(ZugferdKositValidator $kositValidator)
}

/* ----------------------------------------------------------------------------------
- Get instance of the Validator
- Validation of a document read by ZugferdDocumentPdfReader
- The direct call of the constructor is deprecated
---------------------------------------------------------------------------------- */

$kositValidator = new ZugferdKositValidator();
$document = ZugferdDocumentPdfReader::readAndGuessFromFile(dirname(__FILE__) . "/invoice_1.pdf");

$kositValidator = new ZugferdKositValidator($document);
$kositValidator->disableCleanup()->validate();

showValidationResult($kositValidator);

/* ----------------------------------------------------------------------------------
- Validation of a document read by ZugferdDocumentPdfReader
---------------------------------------------------------------------------------- */

$document = ZugferdDocumentPdfReader::readAndGuessFromFile(dirname(__FILE__) . "/invoice_1.pdf");
$kositValidator->setDocument($document)->disableCleanup()->validate();

$kositValidator = ZugferdKositValidator::fromDocument($document)->disableCleanup()->validate();

showValidationResult($kositValidator);

Expand All @@ -54,7 +61,16 @@ function showValidationResult(ZugferdKositValidator $kositValidator)

$document = ZugferdDocumentReader::readAndGuessFromFile(dirname(__FILE__) . "/../tests/assets/xml_en16931_5.xml");

$kositValidator = new ZugferdKositValidator($document);
$kositValidator->setDocument($document)->disableCleanup()->validate();
$kositValidator = ZugferdKositValidator::fromDocument($document)->disableCleanup()->validate();

showValidationResult($kositValidator);

/* ----------------------------------------------------------------------------------
- Validation of a document read by content
---------------------------------------------------------------------------------- */

$content = file_get_contents(dirname(__FILE__) . "/../tests/assets/xml_en16931_4.xml");

$kositValidator = ZugferdKositValidator::fromString($content)->disableCleanup()->validate();

showValidationResult($kositValidator);
8 changes: 4 additions & 4 deletions src/ZugferdDocumentBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2761,13 +2761,13 @@ public function addNewPosition(string $lineid, ?string $lineStatusCode = null, ?
/**
* Adds a new text-only position (line) to document
*
* @param string $lineid
* @param string $lineid
* A unique identifier for the relevant item within the invoice (item number)
* @param string|null $lineStatusCode
* @param string|null $lineStatusCode
* Indicates whether the invoice item contains prices that must be taken into account when
* calculating the invoice amount, or whether it only contains information.
* The following code should be used: TYPE_LINE
* @param string|null $lineStatusReasonCode
* @param string|null $lineStatusReasonCode
* Adds the type to specify whether the invoice line is:
* - detail (normal position)
* - Subtotal
Expand All @@ -2776,7 +2776,7 @@ public function addNewPosition(string $lineid, ?string $lineStatusCode = null, ?
* - detail
* - grouping
* - information
* @return ZugferdDocumentBuilder
* @return ZugferdDocumentBuilder
* @deprecated 1.0.75
*/
public function addNewTextPosition(string $lineid, ?string $lineStatusCode = null, ?string $lineStatusReasonCode = null): ZugferdDocumentBuilder
Expand Down
73 changes: 60 additions & 13 deletions src/ZugferdKositValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@
use DOMDocument;
use DOMXPath;
use Exception;
use Throwable;
use ZipArchive;
use horstoeko\stringmanagement\FileUtils;
use horstoeko\stringmanagement\PathUtils;
use horstoeko\stringmanagement\StringUtils;
use JMS\Serializer\Exception\RuntimeException;
use Symfony\Component\Process\ExecutableFinder;
use Symfony\Component\Process\Process;
use Throwable;
use ZipArchive;

/**
* Class representing the validator against Schematron (Kosit) for documents
Expand All @@ -34,9 +35,9 @@ class ZugferdKositValidator
/**
* The invoice document reference
*
* @var ZugferdDocument
* @var string
*/
private $document = null;
private $documentContent = null;

/**
* Internal message bag
Expand Down Expand Up @@ -134,25 +135,71 @@ class ZugferdKositValidator
protected const MSG_TYPE_PROCESSOUTPUT = 'processoutput';

/**
* Constructor
* Constructo
*
* @param ZugferdDocument|null $document
* @deprecated 1.0.76 Use static::fromDocument or static::fromString instead
*/
public function __construct(?ZugferdDocument $document = null)
{
$this->document = $document;
$this->baseDirectory = sys_get_temp_dir();

if (!is_null($document)) {
$this->setDocumentContent($document->serializeAsXml());
}
}

/**
* Set the ZugferdDocument instance to validate
* Initialize from a ZugferdDocument to validate
*
* @param ZugferdDocument $document
* @return ZugferdKositValidator
* @return static
*/
public static function fromDocument(ZugferdDocument $document)
{
$instance = new static();
$instance->setDocumentContent($document->serializeAsXml());

return $instance;
}

/**
* Initialize from a string which contains the XML to validate
*
* @param string $documentContent
* @return static
*/
public static function fromString(string $documentContent)
{
$instance = new static();
$instance->setDocumentContent($documentContent);

return $instance;
}

/**
* Set the ZugferdDocument instance to validate
*
* @param ZugferdDocument $document
* @return ZugferdKositValidator
* @throws RuntimeException
* @deprecated 1.0.76 Use static::fromDocument or static::fromString instead
*/
public function setDocument(ZugferdDocument $document): ZugferdKositValidator
{
$this->document = $document;
$this->setDocumentContent($document->serializeAsXml());

return $this;
}

/**
* Sets the content to validate
*
* @param string $documentContent
* @return ZugferdKositValidator
*/
public function setDocumentContent(string $documentContent): ZugferdKositValidator
{
$this->documentContent = $documentContent;

return $this;
}
Expand Down Expand Up @@ -577,8 +624,8 @@ public function getProcessOutput(): array
*/
private function checkRequirements(): bool
{
if (is_null($this->document)) {
$this->addToMessageBag("You must specify an instance of the ZugferdDocument class");
if (empty($this->documentContent)) {
$this->addToMessageBag("You must specify the content to validate");
return false;
}

Expand Down Expand Up @@ -683,7 +730,7 @@ private function unpackRequiredFile(string $filename): bool
*/
private function performValidation(): bool
{
if (file_put_contents($this->resolveFileToValidateFilename(), $this->document->serializeAsXml()) === false) {
if (file_put_contents($this->resolveFileToValidateFilename(), $this->documentContent) === false) {
$this->addToMessageBag("Cannot create temporary file which contains the XML to validate");
return false;
}
Expand Down
5 changes: 2 additions & 3 deletions src/ZugferdXsdValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@

namespace horstoeko\zugferd;

use Exception;
use DOMDocument;
use Exception;
use LibXMLError;
use Throwable;
use horstoeko\stringmanagement\PathUtils;
use horstoeko\zugferd\exception\ZugferdFileNotFoundException;
use horstoeko\zugferd\ZugferdDocument;
use horstoeko\zugferd\ZugferdSettings;
use PhpParser\Node\Expr\Throw_;
use Throwable;

/**
* Class representing the validator against XSD for documents
Expand Down

0 comments on commit 6a9ba32

Please sign in to comment.