Skip to content

Commit

Permalink
[ENH] ZugferdKositValidator can now also accept a string (containing …
Browse files Browse the repository at this point in the history
…the XML to be validated) instead of an instance of ZugferdDocument
  • Loading branch information
HorstOeko committed Dec 16, 2024
1 parent 1d0690f commit a0bc48c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 9 deletions.
9 changes: 8 additions & 1 deletion examples/KositValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,14 @@ function showValidationResult(ZugferdKositValidator $kositValidator)

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

$kositValidator = new ZugferdKositValidator($document);
$kositValidator->setDocument($document)->enableRemoteMode()->setRemoteModeHost("127.0.0.1")->setRemoteModePort(8081)->validate();

showValidationResult($kositValidator);

/* ----------------------------------------------------------------------------------
- Validation of a document read by content string (Remote, using running daemon)
---------------------------------------------------------------------------------- */

$kositValidator->setDocument(file_get_contents(dirname(__FILE__) . "/../tests/assets/xml_en16931_5.xml"))->enableRemoteMode()->setRemoteModeHost("127.0.0.1")->setRemoteModePort(8081)->validate();

showValidationResult($kositValidator);
56 changes: 48 additions & 8 deletions src/ZugferdKositValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ZugferdKositValidator
/**
* The invoice document reference
*
* @var ZugferdDocument
* @var ZugferdDocument|string|null
*/
private $document = null;

Expand Down Expand Up @@ -153,25 +153,51 @@ class ZugferdKositValidator
*/
protected const MSG_TYPE_PROCESSOUTPUT = 'processoutput';

/**
* Create a KositValidator-Instance by a given content string
*
* @param string $document
* @return ZugferdKositValidator
*/
public static function fromString(string $document): ZugferdKositValidator
{
return new ZugferdKositValidator($document);
}

/**
* Create a KositValidator-Instance by a given ZugferdDocument (ZugferdDocumentReader, ZugferdDocumentBuilder)
*
* @param ZugferdDocument $document
* @return ZugferdKositValidator
*/
public static function fromZugferdDocument(ZugferdDocument $document): ZugferdKositValidator
{
return new ZugferdKositValidator($document);
}

/**
* Constructor
*
* @param ZugferdDocument|null $document
* @param ZugferdDocument|string|null $document
*/
public function __construct(?ZugferdDocument $document = null)
public function __construct($document = null)
{
$this->document = $document;
$this->baseDirectory = sys_get_temp_dir();
$this->setDocument($document);
}

/**
* Set the ZugferdDocument instance to validate
*
* @param ZugferdDocument $document
* @param ZugferdDocument|string $document
* @return ZugferdKositValidator
*/
public function setDocument(ZugferdDocument $document): ZugferdKositValidator
public function setDocument($document): ZugferdKositValidator
{
if (!is_string($document) && !($document instanceof ZugferdDocument)) {
return $this;
}

$this->document = $document;

return $this;
Expand Down Expand Up @@ -412,6 +438,20 @@ public function validate(): ZugferdKositValidator
return $this;
}

/**
* Internal get the content of the document
*
* @return string
*/
private function getDocumentContent(): string
{
if (is_string($this->document)) {
return $this->document;
}

return $this->document->serializeAsXml();
}

/**
* Internal get (and create) the directory for downloads and file creation
*
Expand Down Expand Up @@ -886,7 +926,7 @@ private function performValidationLocal(): bool
return true;
}

if (file_put_contents($this->resolveFileToValidateFilename(), $this->document->serializeAsXml()) === false) {
if (file_put_contents($this->resolveFileToValidateFilename(), $this->getDocumentContent()) === false) {
$this->addToMessageBag("Cannot create temporary file which contains the XML to validate");
return false;
}
Expand Down Expand Up @@ -932,7 +972,7 @@ private function performValidationRemote(): bool
curl_setopt($httpConnection, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($httpConnection, CURLOPT_TIMEOUT, 120);
curl_setopt($httpConnection, CURLOPT_POST, true);
curl_setopt($httpConnection, CURLOPT_POSTFIELDS, $this->document->serializeAsXml());
curl_setopt($httpConnection, CURLOPT_POSTFIELDS, $this->getDocumentContent());
curl_setopt($httpConnection, CURLOPT_HTTPHEADER, ["Content-Type: application/xml"]);

$response = curl_exec($httpConnection);
Expand Down

0 comments on commit a0bc48c

Please sign in to comment.