From 407fccdd3c7c537f7e011eec564db3a735efc6e1 Mon Sep 17 00:00:00 2001 From: Orlando Charles Date: Wed, 6 Feb 2019 16:27:07 -0600 Subject: [PATCH] Fix concept complements namespaces --- src/Common/Node.php | 49 ++++++++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/src/Common/Node.php b/src/Common/Node.php index 5b098ba..d766cb1 100644 --- a/src/Common/Node.php +++ b/src/Common/Node.php @@ -52,6 +52,8 @@ class Node */ protected $attr = []; + protected $outstandingReferences; + /** * Create a new node instance. * @@ -82,6 +84,14 @@ public function add(Node $node) { $wrapperElement = null; + if ($node->hasOutstandingReferences()) { + $schemaDefinition = $node->outstandingReferences['schema_definition']; + $namespace = $node->outstandingReferences['namespace']; + + $this->setSchemaDefinition($schemaDefinition); + $this->setNamespace($namespace); + } + if (isset($node->schemaDefinition) && $node->globalSchemaLocation) { $this->setSchemaDefinition( $node->getSchemaLocation() @@ -161,27 +171,31 @@ public function setNamespace(Node $node) { $element = $this->element; - $namespaceKey = "xmlns:{$node->namespaceKey}"; - $namespaceValue = $node->getNamespace(); + if ($element->tagName == 'cfdi:Comprobante') { + $namespaceKey = "xmlns:{$node->namespaceKey}"; + $namespaceValue = $node->getNamespace(); - $elementAttr = []; + $elementAttr = []; - if ($element->hasAttributes()) { - $lastAttrName = null; + if ($element->hasAttributes()) { + $lastAttrName = null; - foreach (iterator_to_array($element->attributes) as $attr) { - if ((substr($lastAttrName, 0, 5) == 'xmlns') && - (substr($attr->name, 0, 5) != 'xmlns')) { - $elementAttr[$namespaceKey] = $namespaceValue; - } + foreach (iterator_to_array($element->attributes) as $attr) { + if ((substr($lastAttrName, 0, 5) == 'xmlns') && + (substr($attr->name, 0, 5) != 'xmlns')) { + $elementAttr[$namespaceKey] = $namespaceValue; + } - $elementAttr[$lastAttrName = $attr->name] = $attr->value; + $elementAttr[$lastAttrName = $attr->name] = $attr->value; - $element->removeAttributeNode($attr); + $element->removeAttributeNode($attr); + } } - } - $this->setAttr($element, $elementAttr); + $this->setAttr($element, $elementAttr); + } else { + $this->outstandingReferences['namespace'] = $node; + } } /** @@ -199,9 +213,16 @@ public function setSchemaDefinition(string $schemaDefinition) $value = "{$value} {$schemaDefinition}"; $node->setAttribute($attrName, $value); + } else { + $this->outstandingReferences['schema_definition'] = $schemaDefinition; } } + private function hasOutstandingReferences(): bool + { + return !! $this->outstandingReferences; + } + /** * Get node attributes. *