Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Flat.php
Go to the documentation of this file.
1 <?php
7 
9 
16 class Flat
17 {
21  protected $arrayNodeConfig;
22 
29  {
30  $this->arrayNodeConfig = $arrayNodeConfig;
31  }
32 
56  public function convert(\DOMNode $source, $basePath = '')
57  {
58  $value = [];
60  foreach ($source->childNodes as $node) {
61  if ($node->nodeType == XML_ELEMENT_NODE) {
62  $nodeName = $node->nodeName;
63  $nodePath = $basePath . '/' . $nodeName;
64 
65  $arrayKeyAttribute = $this->arrayNodeConfig->getAssocArrayKeyAttribute($nodePath);
66  $isNumericArrayNode = $this->arrayNodeConfig->isNumericArray($nodePath);
67  $isArrayNode = $isNumericArrayNode || $arrayKeyAttribute;
68 
69  if (isset($value[$nodeName]) && !$isArrayNode) {
70  throw new \UnexpectedValueException(
71  "Node path '{$nodePath}' is not unique, but it has not been marked as array."
72  );
73  }
74 
75  $nodeData = $this->convert($node, $nodePath);
76 
77  if ($isArrayNode) {
78  if ($isNumericArrayNode) {
79  $value[$nodeName][] = $nodeData;
80  } elseif (isset($nodeData[$arrayKeyAttribute])) {
81  $arrayKeyValue = $nodeData[$arrayKeyAttribute];
82  $value[$nodeName][$arrayKeyValue] = $nodeData;
83  } else {
84  throw new \UnexpectedValueException(
85  "Array is expected to contain value for key '{$arrayKeyAttribute}'."
86  );
87  }
88  } else {
89  $value[$nodeName] = $nodeData;
90  }
91  } elseif ($node->nodeType == XML_CDATA_SECTION_NODE
92  || ($node->nodeType == XML_TEXT_NODE && trim($node->nodeValue) != '')
93  ) {
94  $value = $node->nodeValue;
95  break;
96  }
97  }
98  $result = $this->getNodeAttributes($source);
99  if (is_array($value)) {
100  $result = array_merge($result, $value);
101  if (!$result) {
102  $result = '';
103  }
104  } else {
105  if ($result) {
106  $result['value'] = trim($value);
107  } else {
108  $result = trim($value);
109  }
110  }
111  return $result;
112  }
113 
120  protected function getNodeAttributes(\DOMNode $node)
121  {
122  $result = [];
123  $attributes = $node->attributes ?: [];
125  foreach ($attributes as $attribute) {
126  if ($attribute->nodeType == XML_ATTRIBUTE_NODE) {
127  $result[$attribute->nodeName] = $attribute->nodeValue;
128  }
129  }
130  return $result;
131  }
132 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$source
Definition: source.php:23
__construct(ArrayNodeConfig $arrayNodeConfig)
Definition: Flat.php:28
$value
Definition: gender.phtml:16
$attributes
Definition: matrix.phtml:13