<?php
namespace ModulesAbstract\ElectronicInvoice\UBL30Exporter; abstract class ExporterAbstract extends \ModulesAbstract\ElectronicInvoice\UBLExporter\Exporter { protected $invoice = null; protected $document = null; protected $peppolStructure = null; protected $namespaces = [ 'root' => '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', ]; public function __construct(\Entity\Invoice $invoice, \Entity\Document $document, \ModulesAbstract\ElectronicInvoice\UBL30Structure\AbstractPeppolElement $peppolStructure) { $this->options = [ 'hideEmptyChilds' => null, 'hideEmptyNodes' => null, ]; $this->invoice = $invoice; $this->document = $document; $this->peppolStructure = $peppolStructure; } public function getInvoice() { return $this->invoice; } public function getDocument() { return $this->document; } public function getStructure() { return $this->peppolStructure; } abstract public function generate(); protected function generateObjectTag(\Engine\SimpleXmlElement $node, $object, array $parents=[]) { if ($this->options['hideEmptyChilds']) { if (!$this->objectHasValue($object, true, $this->options['hideEmptyChilds'], $parents)) { return; } } foreach ($object->getAttributes() as $k=>$label) { $p = $parents; $p[]= $k; $v = $object->{$k}; if ($v===null || is_scalar($v)) { if ($this->options['hideEmptyNodes']) { if ($this->isValue($v, $this->options['hideEmptyNodes'], $object, $p)) { $node[$label] = $this->renderField($object, $k, $v, $parents); } } else { $node[$label] = $this->renderField($object, $k, $v, $parents); } } else { throw new \Exception("Trying to report an unsupported value!"); } } foreach ($object->getChilds() as $k=>$label) { $p = $parents; $p[]= $k; $v = $object->{$k}; if (is_array($v)) { if ($label===null) { $node->asDOMElement()->nodeValue = $this->renderField($object, $k, $v, $parents); } else { foreach ($v as $child) { if ($this->options['hideEmptyNodes']) { if ($this->objectHasValue($child, true, $this->options['hideEmptyNodes'], $p)) { $this->generateObjectTag($node->addChild($label, null, $this->determineNamespace($child, $p)), $child, $p); } } else { $this->generateObjectTag($node->addChild($label, null, $this->determineNamespace($child, $p)), $child, $p); } } } } elseif ($v===null || is_scalar($v)) { $shouldAdd = false; if ($this->options['hideEmptyNodes']) { if ($this->isValue($v, $this->options['hideEmptyNodes'], $object, $p)) { $shouldAdd = true; } } else { $shouldAdd = true; } if ($shouldAdd) { if ($label===null) { $node->asDOMElement()->nodeValue = $this->renderField($object, $k, $v, $parents); } else { $node->addChild($label, $this->renderField($object, $k, $v, $parents), $this->determineNamespace($v, $p)); } } } elseif (is_object($v)) { if ($this->options['hideEmptyNodes']) { if ($this->objectHasValue($v, true, $this->options['hideEmptyNodes'], $p)) { $this->generateObjectTag($node->addChild($label, null, $this->determineNamespace($v, $p)), $v, $p); } } else { $this->generateObjectTag($node->addChild($label, null, $this->determineNamespace($v, $p)), $v, $p); } } else { throw new \Exception("Trying to report an unsupported value!"); } } } private function determineNamespace($object, $parents) { if ($object===null || is_scalar($object) || $object instanceof \ModulesAbstract\ElectronicInvoice\UBL30Structure\AbstractPeppolElementWithOneValue) { return $this->namespaces['cbc']; } return $this->namespaces['cac']; } private function objectHasValue($object, bool $recurse=true, $comparisonMode='strict', array $parents=[]) { if ($object instanceof \ModulesAbstract\ElectronicInvoice\UBL21Structure\TypeSignature) { return true; } if ($object instanceof \ModulesAbstract\ElectronicInvoice\UBL21Structure\TypeExtension) { return true; } $hasValue = false; foreach ($object as $k=>$v) { $p = $parents; $p[]= $k; if ($v instanceOf \ModulesAbstract\ElectronicInvoice\UBL21Structure\AbstractPeppolElement) { $hasChildsWithValue = $this->objectHasValue($v, $recurse, $comparisonMode, $p); if ($hasChildsWithValue) { $hasValue = true; break; } } elseif ($this->isValue($v, $comparisonMode, $object, $p)) { $hasValue = true; break; } } return $hasValue; } private function isValue($value, string $comparisonMode, $object, array $parents) { $ret = false; if ($object instanceof \ModulesAbstract\ElectronicInvoice\UBL21Structure\AbstractPeppolElementWithOneValue) { if ($value!==null) { $ret = true; } } else { $ret = $object->tagHasValue(end($parents), $value, $comparisonMode, $object, $parents); } return $ret; } protected function renderField($object, $k, $v, $parents) { return $object->renderField($k, $v); } } 