diff --git a/examples/KositValidator.php b/examples/KositValidator.php index 0cac83a0..a50c801b 100644 --- a/examples/KositValidator.php +++ b/examples/KositValidator.php @@ -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); @@ -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); diff --git a/src/ZugferdDocumentBuilder.php b/src/ZugferdDocumentBuilder.php index 202ae118..f14964c9 100644 --- a/src/ZugferdDocumentBuilder.php +++ b/src/ZugferdDocumentBuilder.php @@ -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 @@ -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 diff --git a/src/ZugferdKositValidator.php b/src/ZugferdKositValidator.php index 8590480a..0e5ea62f 100644 --- a/src/ZugferdKositValidator.php +++ b/src/ZugferdKositValidator.php @@ -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 @@ -34,9 +35,9 @@ class ZugferdKositValidator /** * The invoice document reference * - * @var ZugferdDocument + * @var string */ - private $document = null; + private $documentContent = null; /** * Internal message bag @@ -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; } @@ -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; } @@ -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; } diff --git a/src/ZugferdXsdValidator.php b/src/ZugferdXsdValidator.php index 37ce1a8e..fd61c381 100644 --- a/src/ZugferdXsdValidator.php +++ b/src/ZugferdXsdValidator.php @@ -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