From 641ad22eee41d2f2aa3820447404309caa59e592 Mon Sep 17 00:00:00 2001 From: ruff Date: Mon, 3 Jun 2024 17:42:30 +0200 Subject: [PATCH] More generalization --- src/XmlConverterBase.php | 100 ++++++++++++++++++++++++++++++++++- src/XmlConverterCiiToUbl.php | 86 +++++++++--------------------- 2 files changed, 124 insertions(+), 62 deletions(-) diff --git a/src/XmlConverterBase.php b/src/XmlConverterBase.php index 04cf382..32d5477 100644 --- a/src/XmlConverterBase.php +++ b/src/XmlConverterBase.php @@ -9,6 +9,7 @@ namespace horstoeko\zugferdublbridge; +use RuntimeException; use horstoeko\zugferdublbridge\XmlDocumentReader; use horstoeko\zugferdublbridge\XmlDocumentWriter; @@ -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 @@ -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(); } diff --git a/src/XmlConverterCiiToUbl.php b/src/XmlConverterCiiToUbl.php index dafbea4..6c1460a 100644 --- a/src/XmlConverterCiiToUbl.php +++ b/src/XmlConverterCiiToUbl.php @@ -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); } @@ -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();