From c126b7369f475a47cc6bbda4c09f27f58d7f679b Mon Sep 17 00:00:00 2001 From: Okan Date: Sat, 6 Jul 2024 17:52:54 +0300 Subject: [PATCH] Enabled output formatting in XMLReader. Fixed potentially polymorphic calls. --- src/Http/MyDataXmlRequest.php | 7 ++----- src/Models/TypeArray.php | 2 +- src/Xml/BookInfoReader.php | 3 +++ src/Xml/InvoicesDocWriter.php | 7 +++++-- src/Xml/PaymentMethodsDocWriter.php | 7 +++++-- src/Xml/RequestedDocReader.php | 3 +++ src/Xml/ResponseDocReader.php | 3 +++ src/Xml/VatInfoReader.php | 3 +++ src/Xml/XMLReader.php | 16 +++++++++++++--- src/Xml/XMLWriter.php | 11 ++++++++++- 10 files changed, 48 insertions(+), 14 deletions(-) diff --git a/src/Http/MyDataXmlRequest.php b/src/Http/MyDataXmlRequest.php index 3af2fff..cfa7f32 100644 --- a/src/Http/MyDataXmlRequest.php +++ b/src/Http/MyDataXmlRequest.php @@ -21,17 +21,14 @@ protected function request(XMLWriter $writer, XMLReader $reader, mixed $data): R { // Create the request XML $requestXML = $writer->asXML($data); + $this->requestDom = $writer->getDomDocument(); // Get the response XML $response = $this->post(body: $requestXML); $responseXML = $response->getBody()->getContents(); - - // Parse the response XML $responseDoc = $reader->parseXML($responseXML); - $this->responseDom = $reader->getDomDocument(); - $this->requestDom = $writer->getDomDocument(); - + return $responseDoc; } } \ No newline at end of file diff --git a/src/Models/TypeArray.php b/src/Models/TypeArray.php index 39a55ef..ba3d238 100644 --- a/src/Models/TypeArray.php +++ b/src/Models/TypeArray.php @@ -8,7 +8,7 @@ use IteratorAggregate; use Traversable; -/** * +/** * @template TType * * @template-implements IteratorAggregate diff --git a/src/Xml/BookInfoReader.php b/src/Xml/BookInfoReader.php index 7be93d3..8b9fde1 100644 --- a/src/Xml/BookInfoReader.php +++ b/src/Xml/BookInfoReader.php @@ -4,6 +4,9 @@ use Firebed\AadeMyData\Models\RequestedBookInfo; +/** + * @extends XMLReader + */ class BookInfoReader extends XMLReader { public function parseXML(string $xmlString): RequestedBookInfo diff --git a/src/Xml/InvoicesDocWriter.php b/src/Xml/InvoicesDocWriter.php index c71022b..bcc9f7a 100644 --- a/src/Xml/InvoicesDocWriter.php +++ b/src/Xml/InvoicesDocWriter.php @@ -4,6 +4,9 @@ use Firebed\AadeMyData\Models\InvoicesDoc; +/** + * @extends XMLWriter + */ class InvoicesDocWriter extends XMLWriter { private const DOC_VERSION = 'v1.0.8'; @@ -20,7 +23,7 @@ class InvoicesDocWriter extends XMLWriter ]; /** @noinspection PhpUnhandledExceptionInspection */ - public function asXML(InvoicesDoc $invoicesDoc): string + public function asXML($data): string { $rootNode = $this->document->createElementNS(self::XMLNS, 'InvoicesDoc'); $this->document->appendChild($rootNode); @@ -30,7 +33,7 @@ public function asXML(InvoicesDoc $invoicesDoc): string $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:ecls', self::ECLS); // $rootNode->setAttributeNS('http://www.w3.org/2001/XMLSchema-instance', 'xsi:schemaLocation', self::SCHEMA_LOCATION); - $this->buildArray($rootNode, 'invoice', iterator_to_array($invoicesDoc)); + $this->buildArray($rootNode, 'invoice', iterator_to_array($data)); return $this->document->saveXML(); } diff --git a/src/Xml/PaymentMethodsDocWriter.php b/src/Xml/PaymentMethodsDocWriter.php index 71c7bc2..628e6dc 100644 --- a/src/Xml/PaymentMethodsDocWriter.php +++ b/src/Xml/PaymentMethodsDocWriter.php @@ -2,6 +2,9 @@ namespace Firebed\AadeMyData\Xml; +/** + * @extends XMLWriter + */ class PaymentMethodsDocWriter extends XMLWriter { private const XMLNS = 'https://www.aade.gr/myDATA/paymentMethod/v1.0'; @@ -17,7 +20,7 @@ class PaymentMethodsDocWriter extends XMLWriter ]; /** @noinspection PhpUnhandledExceptionInspection */ - public function asXML(array $paymentMethods): string + public function asXML($data): string { $rootNode = $this->document->createElementNS(self::XMLNS, 'PaymentMethodsDoc'); $this->document->appendChild($rootNode); @@ -25,7 +28,7 @@ public function asXML(array $paymentMethods): string $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xsi', self::XSI); $rootNode->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:inv', self::INV); - $this->buildArray($rootNode, 'paymentMethods', $paymentMethods); + $this->buildArray($rootNode, 'paymentMethods', $data); return $this->document->saveXML(); } diff --git a/src/Xml/RequestedDocReader.php b/src/Xml/RequestedDocReader.php index 5cf459a..a009a99 100644 --- a/src/Xml/RequestedDocReader.php +++ b/src/Xml/RequestedDocReader.php @@ -4,6 +4,9 @@ use Firebed\AadeMyData\Models\RequestedDoc; +/** + * @extends XMLReader + */ class RequestedDocReader extends XMLReader { public function parseXML(string $xmlString): RequestedDoc diff --git a/src/Xml/ResponseDocReader.php b/src/Xml/ResponseDocReader.php index 7e464e0..7ad3183 100644 --- a/src/Xml/ResponseDocReader.php +++ b/src/Xml/ResponseDocReader.php @@ -4,6 +4,9 @@ use Firebed\AadeMyData\Models\ResponseDoc; +/** + * @extends XMLReader + */ class ResponseDocReader extends XMLReader { public function parseXML(string $xmlString): ResponseDoc diff --git a/src/Xml/VatInfoReader.php b/src/Xml/VatInfoReader.php index 2558b1c..afe351c 100644 --- a/src/Xml/VatInfoReader.php +++ b/src/Xml/VatInfoReader.php @@ -4,6 +4,9 @@ use Firebed\AadeMyData\Models\RequestedVatInfo; +/** + * @extends XMLReader + */ class VatInfoReader extends XMLReader { public function parseXML(string $xmlString): RequestedVatInfo diff --git a/src/Xml/XMLReader.php b/src/Xml/XMLReader.php index 449e246..bafe6a8 100644 --- a/src/Xml/XMLReader.php +++ b/src/Xml/XMLReader.php @@ -7,14 +7,18 @@ use Firebed\AadeMyData\Models\Type; use IteratorAggregate; -class XMLReader +/** + * @template T of Type + */ +abstract class XMLReader { private DOMDocument $document; - + protected function loadXML(string $xmlString, Type $parent): void { $this->document = new DOMDocument(); $this->document->preserveWhiteSpace = false; + $this->document->formatOutput = true; $this->document->loadXML($xmlString); $this->parseDOMElement($this->document->documentElement->childNodes, $parent); @@ -64,9 +68,15 @@ protected function createType(Type $parent, string $name): mixed $cast = $parent->getCast($name); return $cast ? new $cast() : null; } - + public function getDomDocument(): DOMDocument { return $this->document; } + + /** + * @param string $xmlString + * @return T + */ + public abstract function parseXml(string $xmlString): mixed; } \ No newline at end of file diff --git a/src/Xml/XMLWriter.php b/src/Xml/XMLWriter.php index 00f7b4c..1720ebc 100644 --- a/src/Xml/XMLWriter.php +++ b/src/Xml/XMLWriter.php @@ -8,7 +8,10 @@ use DOMNode; use Firebed\AadeMyData\Models\Type; -class XMLWriter +/** + * @template T + */ +abstract class XMLWriter { // DOMDocument object for XML manipulation protected DOMDocument $document; @@ -128,4 +131,10 @@ public function getDomDocument(): DOMDocument { return $this->document; } + + /** + * @param T $data + * @return string + */ + public abstract function asXml($data): string; } \ No newline at end of file