Skip to content

Commit

Permalink
More generalization
Browse files Browse the repository at this point in the history
  • Loading branch information
ruff committed Jun 3, 2024
1 parent 9702b08 commit 641ad22
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 62 deletions.
100 changes: 99 additions & 1 deletion src/XmlConverterBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace horstoeko\zugferdublbridge;

use RuntimeException;
use horstoeko\zugferdublbridge\XmlDocumentReader;
use horstoeko\zugferdublbridge\XmlDocumentWriter;

Expand All @@ -21,7 +22,7 @@
* @license https://opensource.org/licenses/MIT MIT
* @link https://github.com/horstoeko/zugferdublbridge
*/
class XmlConverterBase
abstract class XmlConverterBase
{
/**
* The input document
Expand All @@ -36,4 +37,101 @@ class XmlConverterBase
* @var XmlDocumentWriter
*/
protected $out = null;

/**
* Constructor
*/
protected final function __construct()
{
$this->in = (new XmlDocumentReader());
$this->out = (new XmlDocumentWriter($this->getDestinationRoot()));

foreach ($this->getSourceNamespaces() as $namespace => $namespaceUri) {
$this->in->addNamespace($namespace, $namespaceUri);
}

foreach ($this->getDestinationNamespaces() as $namespace => $namespaceUri) {
$this->out->addNamespace($namespace, $namespaceUri);
}
}

/**
* Load source from XML string
*
* @param string $source
* @return static
*/
public function loadFromXmlString(string $source): XmlConverterCiiToUbl
{
$this->in->loadFromXmlString($source);

return $this;
}

/**
* Load from XML file
*
* @param string $filename
* @return static
* @throws RuntimeException
*/
public function loadFromXmlFile(string $filename): XmlConverterCiiToUbl
{
if (!is_file($filename)) {
throw new RuntimeException("File $filename does not exists");
}

$this->in->loadFromXmlFile($filename);

return $this;
}

/**
* Save converted XML to a string containing XML data
*
* @return string
*/
public function saveXmlString(): string
{
return $this->out->saveXmlString();
}

/**
* Save converted XML to a file
*
* @param string $filename
* @return int|false
*/
public function saveXmlFile(string $filename)
{
return $this->out->saveXmlFile($filename);
}

/**
* Get the root namespace for the destination document
*
* @return string
*/
protected abstract function getDestinationRoot(): string;

/**
* Get namespaces for the source document
*
* @return array
*/
protected abstract function getSourceNamespaces(): array;

/**
* Get namespaces for the destination document
*
* @return array
*/
protected abstract function getDestinationNamespaces(): array;

/**
* Perform convert
*
* @return static
*/
protected abstract function convert();
}
86 changes: 25 additions & 61 deletions src/XmlConverterCiiToUbl.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ class XmlConverterCiiToUbl extends XmlConverterBase
* Factory: Load from XML file
*
* @param string $filename
* @return XmlConverterCiiToUbl
* @return static
*/
public static function fromFile(string $filename): XmlConverterCiiToUbl
public static function fromFile(string $filename)
{
return (new static())->loadFromXmlFile($filename);
}
Expand All @@ -66,92 +66,56 @@ public static function fromFile(string $filename): XmlConverterCiiToUbl
* Factory: Load from XML stream
*
* @param string $xmlData
* @return XmlConverterCiiToUbl
* @return static
*/
public static function fromString(string $xmlData): XmlConverterCiiToUbl
public static function fromString(string $xmlData)
{
return (new static())->loadFromXmlString($xmlData);
}

/**
* Constructor
*/
public function __construct()
{
$this->in = (new XmlDocumentReader())
->addNamespace('rsm', 'urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100')
->addNamespace('ram', 'urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100')
->addNamespace('qdt', 'urn:un:unece:uncefact:data:Standard:QualifiedDataType:100')
->addNamespace('udt', 'urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100')
->addNamespace('xsi', 'http://www.w3.org/2001/XMLSchema-instance');

$this->out = (new XmlDocumentWriter("ubl:Invoice"))
->addNamespace('ubl', 'urn:oasis:names:specification:ubl:schema:xsd:Invoice-2')
->addNamespace('cac', 'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2')
->addNamespace('cbc', 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2');
}

/**
* Load source from XML string
*
* @param string $source
* @return XmlConverterCiiToUbl
*/
public function loadFromXmlString(string $source): XmlConverterCiiToUbl
{
$this->in->loadFromXmlString($source);

return $this;
}

/**
* Load from XML file
*
* @param string $filename
* @return XmlConverterCiiToUbl
* @throws RuntimeException
* @inheritDoc
*/
public function loadFromXmlFile(string $filename): XmlConverterCiiToUbl
protected function getDestinationRoot(): string
{
if (!is_file($filename)) {
throw new RuntimeException("File $filename does not exists");
}

$this->in->loadFromXmlFile($filename);

return $this;
return "ubl:Invoice";
}

/**
* Save converted XML to a string containing XML data
*
* @return string
* @inheritDoc
*/
public function saveXmlString(): string
protected function getSourceNamespaces(): array
{
return $this->out->saveXmlString();
return [
'rsm' => 'urn:un:unece:uncefact:data:standard:CrossIndustryInvoice:100',
'ram' => 'urn:un:unece:uncefact:data:standard:ReusableAggregateBusinessInformationEntity:100',
'qdt' => 'urn:un:unece:uncefact:data:Standard:QualifiedDataType:100',
'udt' => 'urn:un:unece:uncefact:data:standard:UnqualifiedDataType:100',
'xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
];
}

/**
* Save converted XML to a file
*
* @param string $filename
* @return int|false
* @inheritDoc
*/
public function saveXmlFile(string $filename)
protected function getDestinationNamespaces(): array
{
return $this->out->saveXmlFile($filename);
return [
'ubl' => 'urn:oasis:names:specification:ubl:schema:xsd:Invoice-2',
'cac' => 'urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2',
'cbc' => 'urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2',
];
}

/**
* Perform conversion
*
* @return XmlConverterCiiToUbl
* @return static
* @throws DOMException
* @throws Exception
* @throws RuntimeException
*/
public function convert(): XmlConverterCiiToUbl
public function convert()
{
$this->checkValidSource();
$this->convertGeneral();
Expand Down

0 comments on commit 641ad22

Please sign in to comment.