argumentParser = $argumentParser; } /** * 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; } /** * Convert configuration * * @param \DOMDocument|null $source * @return array */ public function convert($source) { if ($source === null) { return []; } return $this->toArray($source); } }