Skip to content

Commit

Permalink
Enabled output formatting in XMLReader.
Browse files Browse the repository at this point in the history
Fixed potentially polymorphic calls.
  • Loading branch information
firebed committed Jul 6, 2024
1 parent c66f1b5 commit c126b73
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 14 deletions.
7 changes: 2 additions & 5 deletions src/Http/MyDataXmlRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion src/Models/TypeArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use IteratorAggregate;
use Traversable;

/** *
/**
* @template TType
*
* @template-implements IteratorAggregate<int, TType>
Expand Down
3 changes: 3 additions & 0 deletions src/Xml/BookInfoReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use Firebed\AadeMyData\Models\RequestedBookInfo;

/**
* @extends XMLReader<RequestedBookInfo>
*/
class BookInfoReader extends XMLReader
{
public function parseXML(string $xmlString): RequestedBookInfo
Expand Down
7 changes: 5 additions & 2 deletions src/Xml/InvoicesDocWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use Firebed\AadeMyData\Models\InvoicesDoc;

/**
* @extends XMLWriter<InvoicesDoc>
*/
class InvoicesDocWriter extends XMLWriter
{
private const DOC_VERSION = 'v1.0.8';
Expand All @@ -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);
Expand All @@ -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();
}
Expand Down
7 changes: 5 additions & 2 deletions src/Xml/PaymentMethodsDocWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Firebed\AadeMyData\Xml;

/**
* @extends XMLWriter<array>
*/
class PaymentMethodsDocWriter extends XMLWriter
{
private const XMLNS = 'https://www.aade.gr/myDATA/paymentMethod/v1.0';
Expand All @@ -17,15 +20,15 @@ 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);

$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();
}
Expand Down
3 changes: 3 additions & 0 deletions src/Xml/RequestedDocReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use Firebed\AadeMyData\Models\RequestedDoc;

/**
* @extends XMLReader<RequestedDoc>
*/
class RequestedDocReader extends XMLReader
{
public function parseXML(string $xmlString): RequestedDoc
Expand Down
3 changes: 3 additions & 0 deletions src/Xml/ResponseDocReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use Firebed\AadeMyData\Models\ResponseDoc;

/**
* @extends XMLReader<ResponseDoc>
*/
class ResponseDocReader extends XMLReader
{
public function parseXML(string $xmlString): ResponseDoc
Expand Down
3 changes: 3 additions & 0 deletions src/Xml/VatInfoReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use Firebed\AadeMyData\Models\RequestedVatInfo;

/**
* @extends XMLReader<RequestedVatInfo>
*/
class VatInfoReader extends XMLReader
{
public function parseXML(string $xmlString): RequestedVatInfo
Expand Down
16 changes: 13 additions & 3 deletions src/Xml/XMLReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
11 changes: 10 additions & 1 deletion src/Xml/XMLWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -128,4 +131,10 @@ public function getDomDocument(): DOMDocument
{
return $this->document;
}

/**
* @param T $data
* @return string
*/
public abstract function asXml($data): string;
}

0 comments on commit c126b73

Please sign in to comment.