/** * {@inheritdoc} * @return array * @throws \InvalidArgumentException */ public function evaluate(array $data) { if (!isset($data['item']) || !is_array($data['item'])) { throw new \InvalidArgumentException('Array items are expected.'); } $result = array(); $items = $data['item']; foreach ($items as $itemKey => $itemData) { $result[$itemKey] = $this->itemInterpreter->evaluate($itemData); } return $result; }
/** * {@inheritdoc} * @throws \InvalidArgumentException */ public function evaluate(array $data) { $updaters = !empty($data['updater']) ? $data['updater'] : array(); unset($data['updater']); if (!is_array($updaters)) { throw new \InvalidArgumentException('Layout argument updaters are expected to be an array of classes.'); } $result = $this->subject->evaluate($data); foreach ($updaters as $updaterClass) { $result = $this->applyUpdater($result, $updaterClass); } return $result; }
/** * {@inheritdoc} * @return array * @throws \InvalidArgumentException */ public function evaluate(array $data) { $params = isset($data['param']) ? $data['param'] : []; if (!is_array($params)) { throw new \InvalidArgumentException('Layout argument parameters are expected to be an array.'); } $result = []; foreach ($params as $paramKey => $paramData) { if (!is_array($paramData)) { throw new \InvalidArgumentException('Parameter data of layout argument is expected to be an array.'); } $result[$paramKey] = $this->paramInterpreter->evaluate($paramData); } return $result; }
/** * {@inheritdoc} */ public function evaluate(array $data) { if (!isset($data['argument'])) { throw new \InvalidArgumentException('Node "argument" required for this type.'); } foreach ($data['argument'] as $name => $argument) { $arguments[$name] = $this->argumentInterpreter->evaluate($argument); } if (!isset($arguments['class'])) { throw new \InvalidArgumentException('Node "argument" with name "class" is required for this type.'); } $className = $arguments['class']; unset($arguments['class']); return $this->objectManager->create($className, $arguments); }
/** * Compute and return argument values * * @param array $arguments * @return array */ protected function evaluateArguments(array $arguments) { $result = []; foreach ($arguments as $argumentName => $argumentData) { $result[$argumentName] = $this->argumentInterpreter->evaluate($argumentData); } 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()); } } }
/** * Compute and return argument values * * @param array $arguments * @return array */ protected function evaluateArguments(array $arguments) { $result = []; foreach ($arguments as $argumentName => $argumentData) { if (!isset($argumentData[Dom::TYPE_ATTRIBUTE])) { $result[$argumentName] = $argumentData; continue; } $result[$argumentName] = $this->argumentInterpreter->evaluate($argumentData); } return $result; }
/** * To create the raw data components * * @param string $component * @param bool $evaluated * @return array */ public function createRawComponentData($component, $evaluated = true) { $componentData = $this->componentConfigProvider->getComponentData($component); $componentData[Converter::DATA_ATTRIBUTES_KEY] = isset($componentData[Converter::DATA_ATTRIBUTES_KEY]) ? $componentData[Converter::DATA_ATTRIBUTES_KEY] : []; $componentData[Converter::DATA_ARGUMENTS_KEY] = isset($componentData[Converter::DATA_ARGUMENTS_KEY]) ? $componentData[Converter::DATA_ARGUMENTS_KEY] : []; if ($evaluated) { foreach ($componentData[Converter::DATA_ARGUMENTS_KEY] as $argumentName => $argument) { $componentData[Converter::DATA_ARGUMENTS_KEY][$argumentName] = $this->argumentInterpreter->evaluate($argument); } } return [ManagerInterface::COMPONENT_ATTRIBUTES_KEY => $componentData[Converter::DATA_ATTRIBUTES_KEY], ManagerInterface::COMPONENT_ARGUMENTS_KEY => $componentData[Converter::DATA_ARGUMENTS_KEY]]; }
/** * Compute argument values * * @param Layout\Element $blockElement * @param array $data */ protected function evaluateArguments(Layout\Element $blockElement, array &$data) { $arguments = $this->getArguments($blockElement); foreach ($arguments as $argumentName => $argumentData) { if (isset($argumentData['updater'])) { continue; } $result = $this->argumentInterpreter->evaluate($argumentData); if (is_array($result)) { $data['arguments'][$argumentName] = isset($data['arguments'][$argumentName]) ? array_replace_recursive($data['arguments'][$argumentName], $result) : $result; } else { $data['arguments'][$argumentName] = $result; } } }
/** * Resolve arguments * * @param string $identifier * @param array $componentData * @return array */ protected function argumentsResolver($identifier, array $componentData) { $attributes = $componentData[ManagerInterface::COMPONENT_ATTRIBUTES_KEY]; $className = $attributes['class']; unset($attributes['class']); $arguments = []; foreach ($componentData[ManagerInterface::COMPONENT_ARGUMENTS_KEY] as $name => $argument) { $arguments[$name] = $this->argumentInterpreter->evaluate($argument); } if (!isset($arguments['data'])) { $arguments['data'] = []; } $arguments['data'] = array_merge($arguments['data'], ['name' => $identifier], $attributes); return [$className, $arguments]; }
/** * Convert configuration in DOM format to assoc array that can be used by object manager * * @param \DOMDocument $config * @return array * @throws \Exception * @todo this method has high cyclomatic complexity in order to avoid performance issues * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function convert($config) { $output = array(); /** @var \DOMNode $node */ foreach ($config->documentElement->childNodes as $node) { if ($node->nodeType != XML_ELEMENT_NODE) { continue; } switch ($node->nodeName) { case 'preference': $output['preferences'][$node->attributes->getNamedItem('for')->nodeValue] = $node->attributes->getNamedItem('type')->nodeValue; break; case 'type': case 'virtualType': $typeData = array(); $typeNodeAttributes = $node->attributes; $typeNodeShared = $typeNodeAttributes->getNamedItem('shared'); if ($typeNodeShared) { $typeData['shared'] = $this->booleanUtils->toBoolean($typeNodeShared->nodeValue); } if ($node->nodeName == 'virtualType') { $attributeType = $typeNodeAttributes->getNamedItem('type'); // attribute type is required for virtual type only in merged configuration if ($attributeType) { $typeData['type'] = $attributeType->nodeValue; } } $typeArguments = array(); $typePlugins = array(); /** @var \DOMNode $typeChildNode */ foreach ($node->childNodes as $typeChildNode) { if ($typeChildNode->nodeType != XML_ELEMENT_NODE) { continue; } switch ($typeChildNode->nodeName) { case 'arguments': /** @var \DOMNode $argumentNode */ foreach ($typeChildNode->childNodes as $argumentNode) { if ($argumentNode->nodeType != XML_ELEMENT_NODE) { continue; } $argumentName = $argumentNode->attributes->getNamedItem('name')->nodeValue; $argumentData = $this->argumentParser->parse($argumentNode); $typeArguments[$argumentName] = $this->argumentInterpreter->evaluate($argumentData); } break; case 'plugin': $pluginAttributes = $typeChildNode->attributes; $pluginDisabledNode = $pluginAttributes->getNamedItem('disabled'); $pluginSortOrderNode = $pluginAttributes->getNamedItem('sortOrder'); $pluginTypeNode = $pluginAttributes->getNamedItem('type'); $pluginData = array('sortOrder' => $pluginSortOrderNode ? (int) $pluginSortOrderNode->nodeValue : 0); if ($pluginDisabledNode) { $pluginData['disabled'] = $this->booleanUtils->toBoolean($pluginDisabledNode->nodeValue); } if ($pluginTypeNode) { $pluginData['instance'] = $pluginTypeNode->nodeValue; } $typePlugins[$pluginAttributes->getNamedItem('name')->nodeValue] = $pluginData; break; default: throw new \Exception("Invalid application config. Unknown node: {$typeChildNode->nodeName}."); } } $typeData['arguments'] = $typeArguments; if (!empty($typePlugins)) { $typeData['plugins'] = $typePlugins; } $output[$typeNodeAttributes->getNamedItem('name')->nodeValue] = $typeData; break; default: throw new \Exception("Invalid application config. Unknown node: {$node->nodeName}."); } } return $output; }