Skip to content

Commit

Permalink
Reverted changes to ZugferdKositValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
HorstOeko committed Nov 2, 2024
1 parent 6a9ba32 commit f3b99d7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 81 deletions.
26 changes: 5 additions & 21 deletions examples/KositValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,17 @@ function showValidationResult(ZugferdKositValidator $kositValidator)
}

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

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

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

showValidationResult($kositValidator);
$kositValidator = new ZugferdKositValidator();

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

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

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

showValidationResult($kositValidator);

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

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

$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();
$kositValidator = new ZugferdKositValidator($document);
$kositValidator->setDocument($document)->disableCleanup()->validate();

showValidationResult($kositValidator);
73 changes: 13 additions & 60 deletions src/ZugferdKositValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@
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 @@ -35,9 +34,9 @@ class ZugferdKositValidator
/**
* The invoice document reference
*
* @var string
* @var ZugferdDocument
*/
private $documentContent = null;
private $document = null;

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

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

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

/**
* Initialize from a ZugferdDocument to validate
*
* @param ZugferdDocument $document
* @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->setDocumentContent($document->serializeAsXml());

return $this;
}

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

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

Expand Down Expand Up @@ -730,7 +683,7 @@ private function unpackRequiredFile(string $filename): bool
*/
private function performValidation(): bool
{
if (file_put_contents($this->resolveFileToValidateFilename(), $this->documentContent) === false) {
if (file_put_contents($this->resolveFileToValidateFilename(), $this->document->serializeAsXml()) === false) {
$this->addToMessageBag("Cannot create temporary file which contains the XML to validate");
return false;
}
Expand Down

0 comments on commit f3b99d7

Please sign in to comment.