From a0bc48cf47a20a6c15159769cbdc217cf08fcc5a Mon Sep 17 00:00:00 2001 From: HorstOeko Date: Mon, 16 Dec 2024 06:04:39 +0100 Subject: [PATCH] [ENH] ZugferdKositValidator can now also accept a string (containing the XML to be validated) instead of an instance of ZugferdDocument --- examples/KositValidator.php | 9 +++++- src/ZugferdKositValidator.php | 56 ++++++++++++++++++++++++++++++----- 2 files changed, 56 insertions(+), 9 deletions(-) diff --git a/examples/KositValidator.php b/examples/KositValidator.php index db520fb..27ee102 100644 --- a/examples/KositValidator.php +++ b/examples/KositValidator.php @@ -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); diff --git a/src/ZugferdKositValidator.php b/src/ZugferdKositValidator.php index 5888873..d357b63 100644 --- a/src/ZugferdKositValidator.php +++ b/src/ZugferdKositValidator.php @@ -33,7 +33,7 @@ class ZugferdKositValidator /** * The invoice document reference * - * @var ZugferdDocument + * @var ZugferdDocument|string|null */ private $document = null; @@ -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; @@ -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 * @@ -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; } @@ -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);