123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\Config;
- use Magento\Framework\View\Xsd\Media\TypeDataExtractorPool;
- /**
- * Class Converter convert xml to appropriate array
- *
- * @package Magento\Framework\Config
- */
- class Converter implements \Magento\Framework\Config\ConverterInterface
- {
- /**
- * @var \Magento\Framework\View\Xsd\Media\TypeDataExtractorPool
- */
- protected $extractorPool;
- /**
- * @param TypeDataExtractorPool $extractorPool
- */
- public function __construct(TypeDataExtractorPool $extractorPool)
- {
- $this->extractorPool = $extractorPool;
- }
- /**
- * Convert dom node tree to array
- *
- * @param \DOMDocument $source
- * @return array
- * @throws \InvalidArgumentException
- */
- public function convert($source)
- {
- $xpath = new \DOMXPath($source);
- $output = [];
- foreach ($xpath->evaluate('/view') as $typeNode) {
- foreach ($typeNode->childNodes as $childNode) {
- if ($childNode->nodeType != XML_ELEMENT_NODE) {
- continue;
- }
- $result = $this->parseNodes($childNode);
- $output = array_merge_recursive($output, $result);
- }
- }
- return $output;
- }
- /**
- * Parse node values from xml nodes
- *
- * @param \DOMElement $childNode
- * @return array
- */
- protected function parseNodes($childNode)
- {
- $output = [];
- switch ($childNode->nodeName) {
- case 'vars':
- $moduleName = $childNode->getAttribute('module');
- $output[$childNode->tagName][$moduleName] = $this->parseVarElement($childNode);
- break;
- case 'exclude':
- /** @var $itemNode \DOMElement */
- foreach ($childNode->getElementsByTagName('item') as $itemNode) {
- $itemType = $itemNode->getAttribute('type');
- $output[$childNode->tagName][$itemType][] = $itemNode->nodeValue;
- }
- break;
- case 'media':
- foreach ($childNode->childNodes as $mediaNode) {
- if ($mediaNode instanceof \DOMElement) {
- $mediaNodesArray =
- $this->extractorPool->nodeProcessor($mediaNode->tagName)->process(
- $mediaNode,
- $childNode->tagName
- );
- $output = array_merge_recursive($output, $mediaNodesArray);
- }
- }
- break;
- }
- return $output;
- }
- /**
- * Recursive parser for <var> nodes
- *
- * @param \DOMElement $node
- * @return string|boolean|number|null|[]
- */
- protected function parseVarElement(\DOMElement $node)
- {
- $result = [];
- for ($varNode = $node->firstChild; $varNode !== null; $varNode = $varNode->nextSibling) {
- if ($varNode instanceof \DOMElement && $varNode->tagName == "var") {
- $varName = $varNode->getAttribute('name');
- $result[$varName] = $this->parseVarElement($varNode);
- }
- }
- if (!count($result)) {
- $result = (strtolower($node->nodeValue) !== 'true' && strtolower($node->nodeValue) !== 'false')
- ? $node->nodeValue
- : filter_var($node->nodeValue, FILTER_VALIDATE_BOOLEAN);
- }
- return $result;
- }
- }
|