/** * Transform Xml to array * * @param \DOMNode $node * @return array|string * * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ 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: if ($node->localName === static::ARGUMENT_KEY) { if (!isset($attributes[static::NAME_ATTRIBUTE_KEY])) { throw new \InvalidArgumentException('Attribute "' . static::NAME_ATTRIBUTE_KEY . '" is absent in the attributes node.'); } $result[$attributes[static::NAME_ATTRIBUTE_KEY]] = $this->argumentParser->parse($node); } else { $arguments = []; for ($i = 0, $iLength = $node->childNodes->length; $i < $iLength; ++$i) { $itemNode = $node->childNodes->item($i); if (empty($itemNode->localName)) { continue; } if ($itemNode->nodeName === static::ARGUMENT_KEY) { $arguments += $this->toArray($itemNode); } 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; } } break; } return $result; }
/** * @param string $area * @param string $layoutFile * @dataProvider layoutArgumentsDataProvider */ public function testLayoutArguments($area, $layoutFile) { \Magento\TestFramework\Helper\Bootstrap::getInstance()->loadArea($area); $dom = new \DOMDocument(); $dom->load($layoutFile); $xpath = new \DOMXPath($dom); $argumentNodes = $xpath->query('/layout//arguments/argument | /layout//action/argument'); /** @var \DOMNode $argumentNode */ foreach ($argumentNodes as $argumentNode) { try { $argumentData = $this->_argParser->parse($argumentNode); if ($this->isSkippedArgument($argumentData)) { continue; } $this->_argInterpreter->evaluate($argumentData); } catch (\Exception $e) { $this->fail($e->getMessage()); } } }
/** * Parse argument nodes and return their array representation * * @param Layout\Element $node * @return array */ protected function parseArguments(Layout\Element $node) { $nodeDom = dom_import_simplexml($node); $result = []; foreach ($nodeDom->childNodes as $argumentNode) { if ($argumentNode instanceof \DOMElement && $argumentNode->nodeName == 'argument') { $argumentName = $argumentNode->getAttribute('name'); $result[$argumentName] = $this->argumentParser->parse($argumentNode); } } return $result; }