|
Apache/2.4.41 (Ubuntu) Linux vmi616275.contaboserver.net 5.4.0-84-generic #94-Ubuntu SMP Thu Aug 26 20:27:37 UTC 2021 x86_64 uid=33(www-data) gid=33(www-data) groups=33(www-data) server ip : 62.171.164.128 | your ip : 127.0.0.1 safemode OFF > / home / Mirasvit / ReportApi / Config / Loader / |
Filename | /home/Mirasvit/ReportApi/Config/Loader/Converter.php |
Size | 3.88 kb |
Permission | rw-r--r-- |
Owner | root : root |
Create time | 01-Jul-2024 20:52 |
Last modified | 06-Apr-2021 18:06 |
Last accessed | 22-Aug-2025 11:09 |
Actions | edit | rename | delete | download (gzip) |
View | text | code | image |
<?php
/**
* Mirasvit
*
* This source file is subject to the Mirasvit Software License, which is available at https://mirasvit.com/license/.
* Do not edit or add to this file if you wish to upgrade the to newer versions in the future.
* If you wish to customize this module for your needs.
* Please refer to http://www.magentocommerce.com for more information.
*
* @category Mirasvit
* @package mirasvit/module-report-api
* @version 1.0.41
* @copyright Copyright (C) 2020 Mirasvit (https://mirasvit.com/)
*/
namespace Mirasvit\ReportApi\Config\Loader;
use Magento\Framework\Config\ConverterInterface;
class Converter implements ConverterInterface
{
/**
* The key attributes of a node
*/
const DATA_ATTRIBUTES_KEY = '@attributes';
/**
* The key for the data arguments
*/
const DATA_ARGUMENTS_KEY = '@arguments';
/**
* @var array
*/
protected $mergeNames = ['config', 'tables', 'relations', 'columns'];
/**
* Convert configuration
* @param \DOMDocument|null $source
* @return array
*/
public function convert($source)
{
if ($source === null) {
return [];
}
$array = $this->toArray($source);
$array = $this->simplifyArray($array);
return $array;
}
/**
* Transform Xml to array @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @param \DOMNode $node
* @return array|string
*/
protected function toArray(\DOMNode $node)
{
$result = [];
$attributes = [];
// Collect data from attributes
if ($node->hasAttributes()) {
foreach ($node->attributes as $attribute) {
$attributes[$attribute->name] = $attribute->value;
}
}
switch ($node->nodeType) {
case XML_TEXT_NODE:
case XML_COMMENT_NODE:
case XML_CDATA_SECTION_NODE:
break;
default:
$arguments = [];
for ($i = 0, $iLength = $node->childNodes->length; $i < $iLength; ++$i) {
$itemNode = $node->childNodes->item($i);
if (empty($itemNode->localName)) {
continue;
}
if ($itemNode->childNodes->length == 1) {
if ($itemNode->localName == 'column') {
$arguments[] = $itemNode->textContent;
} else {
$arguments[$itemNode->localName] = $itemNode->textContent;
}
} else {
$result[$itemNode->localName][] = $this->toArray($itemNode);
}
}
if (!empty($arguments)) {
$result[static::DATA_ARGUMENTS_KEY] = $arguments;
}
if (!empty($attributes)) {
$result[static::DATA_ATTRIBUTES_KEY] = $attributes;
}
}
return $result;
}
/**
* Simplify array
* @param array $array
* @return array
*/
protected function simplifyArray($array)
{
if (!is_array($array)) {
return $array;
}
$result = [];
foreach ($array as $name => $values) {
if (is_string($name) && in_array($name, $this->mergeNames)) {
$merged = [];
if (is_array($values)) {
foreach ($values as $value) {
$merged = array_merge_recursive($merged, $value);
}
$merged = $this->simplifyArray($merged);
} else {
$merged = $values;
}
$result[$name] = $merged;
} else {
$result[$name] = $this->simplifyArray($values);
}
}
return $result;
}
}
/**
* Mirasvit
*
* This source file is subject to the Mirasvit Software License, which is available at https://mirasvit.com/license/.
* Do not edit or add to this file if you wish to upgrade the to newer versions in the future.
* If you wish to customize this module for your needs.
* Please refer to http://www.magentocommerce.com for more information.
*
* @category Mirasvit
* @package mirasvit/module-report-api
* @version 1.0.41
* @copyright Copyright (C) 2020 Mirasvit (https://mirasvit.com/)
*/
namespace Mirasvit\ReportApi\Config\Loader;
use Magento\Framework\Config\ConverterInterface;
class Converter implements ConverterInterface
{
/**
* The key attributes of a node
*/
const DATA_ATTRIBUTES_KEY = '@attributes';
/**
* The key for the data arguments
*/
const DATA_ARGUMENTS_KEY = '@arguments';
/**
* @var array
*/
protected $mergeNames = ['config', 'tables', 'relations', 'columns'];
/**
* Convert configuration
* @param \DOMDocument|null $source
* @return array
*/
public function convert($source)
{
if ($source === null) {
return [];
}
$array = $this->toArray($source);
$array = $this->simplifyArray($array);
return $array;
}
/**
* Transform Xml to array @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @param \DOMNode $node
* @return array|string
*/
protected function toArray(\DOMNode $node)
{
$result = [];
$attributes = [];
// Collect data from attributes
if ($node->hasAttributes()) {
foreach ($node->attributes as $attribute) {
$attributes[$attribute->name] = $attribute->value;
}
}
switch ($node->nodeType) {
case XML_TEXT_NODE:
case XML_COMMENT_NODE:
case XML_CDATA_SECTION_NODE:
break;
default:
$arguments = [];
for ($i = 0, $iLength = $node->childNodes->length; $i < $iLength; ++$i) {
$itemNode = $node->childNodes->item($i);
if (empty($itemNode->localName)) {
continue;
}
if ($itemNode->childNodes->length == 1) {
if ($itemNode->localName == 'column') {
$arguments[] = $itemNode->textContent;
} else {
$arguments[$itemNode->localName] = $itemNode->textContent;
}
} else {
$result[$itemNode->localName][] = $this->toArray($itemNode);
}
}
if (!empty($arguments)) {
$result[static::DATA_ARGUMENTS_KEY] = $arguments;
}
if (!empty($attributes)) {
$result[static::DATA_ATTRIBUTES_KEY] = $attributes;
}
}
return $result;
}
/**
* Simplify array
* @param array $array
* @return array
*/
protected function simplifyArray($array)
{
if (!is_array($array)) {
return $array;
}
$result = [];
foreach ($array as $name => $values) {
if (is_string($name) && in_array($name, $this->mergeNames)) {
$merged = [];
if (is_array($values)) {
foreach ($values as $value) {
$merged = array_merge_recursive($merged, $value);
}
$merged = $this->simplifyArray($merged);
} else {
$merged = $values;
}
$result[$name] = $merged;
} else {
$result[$name] = $this->simplifyArray($values);
}
}
return $result;
}
}