Skip to content

Commit

Permalink
Update XMLReader.php
Browse files Browse the repository at this point in the history
  • Loading branch information
rez1dent3 committed Jan 12, 2018
1 parent de654ae commit 431e9b9
Showing 1 changed file with 93 additions and 25 deletions.
118 changes: 93 additions & 25 deletions src/XMLReader/XMLReader.php
Expand Up @@ -16,25 +16,38 @@ class XMLReader
/**
* @var array
*/
protected $copyright = [
'created-with' => 'https://github.com/bavix/xml'
];
protected $copyright = [];

/**
* @var array
*/
protected $namespaces = [];

/**
* @var \DOMDocument
*/
protected $document;

public function __construct($copyright = true)
{
if ($copyright)
{
$this->copyright = [
'created-with' => 'https://github.com/bavix/xml',
'created-at' => \date('Y-m-d H:i:s')
];
}
}

/**
* @return \DOMDocument
*/
protected function document(): \DOMDocument
{
if (!$this->document)
{
$this->document = new \DOMDocument('1.0', 'utf-8');
$this->document->formatOutput = true;
$this->copyright['created-at'] = \date('Y-m-d H:i:s');
$this->document = new \DOMDocument('1.0', 'utf-8');
$this->document->formatOutput = true;
}

return $this->document;
Expand All @@ -47,6 +60,17 @@ protected function document(): \DOMDocument
*/
protected function element($name): \DOMElement
{
if (strpos($name, ':') !== false)
{

$keys = explode(':', $name);

return $this->document()->createElementNS(
$this->namespaces['xmlns:' . \current($keys)],
$name
);
}

return $this->document()->createElement($name);
}

Expand Down Expand Up @@ -229,6 +253,7 @@ protected function _convertStorage($storage): array
*/
public function asXML($storage, string $name = 'bavix', array $attributes = []): string
{
$storage = $this->fragments($storage);
$element = $this->element($name);

$this->addAttributes($element, $attributes);
Expand All @@ -252,6 +277,15 @@ public function asXML($storage, string $name = 'bavix', array $attributes = []):
*/
protected function convert(DOMElement $element, $storage)
{
if (\is_object($storage))
{
$element->appendChild(
$element->ownerDocument->importNode($storage)
);

return;
}

if (!\is_array($storage))
{
$element->nodeValue = htmlspecialchars($storage);
Expand Down Expand Up @@ -279,6 +313,35 @@ protected function convert(DOMElement $element, $storage)
}
}

protected function fragments(array $storage)
{

Arr::walkRecursive($storage, function (&$value) {

if (\is_object($value)) {

if ($value instanceof CData)
{
$value = $this->document()->createCDATASection((string)$value);
return;
}

if ($value instanceof Raw)
{
$fragment = $this->document()->createDocumentFragment();
$fragment->appendXML((string)$value);
$value = $fragment;
return;
}

}

});

return $storage;

}

/**
* @param string $key
* @param DOMElement $element
Expand All @@ -290,17 +353,12 @@ protected function addNodeWithKey($key, DOMElement $element, $storage)
{
if ($key === '@attributes')
{

$this->addAttributes($element, $storage);
} else if ($key === '@value')
{

if (\is_object($storage) && $storage instanceof Raw)
{
$fragment = $element->ownerDocument->createDocumentFragment();
$fragment->appendXML((string)$storage);
$element->appendChild($fragment);
return;
}
}
else if ($key === '@value')
{

if (\is_string($storage))
{
Expand All @@ -309,22 +367,27 @@ protected function addNodeWithKey($key, DOMElement $element, $storage)
return;
}

$dom = new \DOMDocument();
$dom = new \DOMDocument();
$fragment = $element->ownerDocument->createDocumentFragment();

$dom->loadXML(
(new XMLReader())->asXML($storage)
(new XMLReader())->asXML($storage, 'root', $this->namespaces)
);

$fragment = $element->ownerDocument->createDocumentFragment();

foreach ($dom->firstChild->childNodes as $value)
/**
* @var $childNode \DOMText
*/
foreach ($dom->firstChild->childNodes as $childNode)
{
$fragment->appendXML(
$value->ownerDocument->saveXML($value)
$childNode->ownerDocument->saveXML($childNode)
);
}

$element->appendChild($fragment);
} else

}
else
{
$this->addNode($element, $key, $storage);
}
Expand Down Expand Up @@ -360,7 +423,7 @@ protected function sequential(DOMElement $element, $storage)
protected function addNode(DOMElement $element, $key, $value)
{
$key = \str_replace(' ', '-', $key);
$child = $this->document()->createElement($key);
$child = $this->element($key);
$element->appendChild($child);
$this->convert($child, $value);
}
Expand All @@ -385,7 +448,7 @@ protected function addCollectionNode(DOMElement $element, $value)
/**
* @var $child DOMElement
*/
$child = $this->document()->createElement($element->nodeName);
$child = $this->element($element->nodeName);
// $child = $element->cloneNode();
$element->parentNode->appendChild($child);
$this->convert($child, $value);
Expand All @@ -406,7 +469,7 @@ protected function addSequentialNode(DOMElement $element, $value)
return;
}

$child = $this->document()->createElement($element->nodeName);
$child = $this->element($element->nodeName);
// $child = $element->cloneNode();
$child->nodeValue = \htmlspecialchars($value);
$element->parentNode->appendChild($child);
Expand All @@ -422,6 +485,11 @@ protected function addAttributes(DOMElement $element, $storage)
{
foreach ($storage as $attrKey => $attrVal)
{
if (strpos($attrKey, ':') !== false)
{
$this->namespaces[$attrKey] = $attrVal;
}

$element->setAttribute($attrKey, $attrVal);
}
}
Expand Down

0 comments on commit 431e9b9

Please sign in to comment.