Skip to content

Commit

Permalink
Fix concept complements namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Orlando Charles committed Feb 6, 2019
1 parent 8fe5c7e commit 407fccd
Showing 1 changed file with 35 additions and 14 deletions.
49 changes: 35 additions & 14 deletions src/Common/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class Node
*/
protected $attr = [];

protected $outstandingReferences;

/**
* Create a new node instance.
*
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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;
}
}

/**
Expand All @@ -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.
*
Expand Down

0 comments on commit 407fccd

Please sign in to comment.