/** * Convert dom node tree to array * * @param \DOMDocument $source * @return array */ public function convert($source) { $result = []; /** @var \DOMNode $zipNode */ foreach ($source->documentElement->childNodes as $zipNode) { if ($zipNode->nodeType != XML_ELEMENT_NODE) { continue; } $groupName = $zipNode->attributes->getNamedItem('countryCode')->nodeValue; /** @var \DOMNode $codesNode */ foreach ($zipNode->childNodes as $codesNode) { if ($codesNode->nodeType != XML_ELEMENT_NODE) { continue; } /** @var \DOMNode $code */ foreach ($codesNode->childNodes as $code) { if ($code->nodeType != XML_ELEMENT_NODE || !$this->booleanUtils->toBoolean($code->attributes->getNamedItem('active')->nodeValue)) { continue; } $result[$groupName][$code->attributes->getNamedItem('id')->nodeValue] = ['example' => $code->attributes->getNamedItem('example')->nodeValue, 'pattern' => $code->nodeValue]; } } } return $result; }
/** * Compute and return effective value of an argument * * @param array $data * @return mixed * @throws \InvalidArgumentException * @throws \UnexpectedValueException */ public function evaluate(array $data) { $result = array('instance' => $data['value']); if (isset($data['shared'])) { $result['shared'] = $this->booleanUtils->toBoolean($data['shared']); } return $result; }
/** * {@inheritdoc} * @return bool * @throws \InvalidArgumentException */ public function evaluate(array $data) { if (!isset($data['value'])) { throw new \InvalidArgumentException('Boolean value is missing.'); } $value = $data['value']; return $this->booleanUtils->toBoolean($value); }
/** * {@inheritdoc} * @return string * @throws \InvalidArgumentException */ public function evaluate(array $data) { if (isset($data['value'])) { $result = $data['value']; if (!is_string($result)) { throw new \InvalidArgumentException('String value is expected.'); } $needTranslation = isset($data['translate']) ? $this->booleanUtils->toBoolean($data['translate']) : false; if ($needTranslation) { $result = (string) new \Magento\Framework\Phrase($result); } } else { $result = ''; } return $result; }
/** * Validate topic response handler from config data * * @param array $configDataItem * @return void */ private function validateTopicResponseHandler($configDataItem) { $topicName = $configDataItem[ConfigInterface::TOPIC_NAME]; if (!is_array($configDataItem[ConfigInterface::TOPIC_HANDLERS])) { throw new \LogicException(sprintf('Handlers in the topic "%s" must be an array', $topicName)); } if ($this->booleanUtils->toBoolean($configDataItem[ConfigInterface::TOPIC_IS_SYNCHRONOUS]) && count($configDataItem[ConfigInterface::TOPIC_HANDLERS]) != 1) { throw new \LogicException(sprintf('Topic "%s" is configured for synchronous requests, that is why it must have exactly one ' . 'response handler declared. The following handlers declared: %s', $topicName, implode(', ', array_keys($configDataItem[ConfigInterface::TOPIC_HANDLERS])))); } foreach ($configDataItem[ConfigInterface::TOPIC_HANDLERS] as $handlerName => $handler) { $serviceName = $handler[ConfigInterface::HANDLER_TYPE]; $methodName = $handler[ConfigInterface::HANDLER_METHOD]; if (isset($handler[ConfigInterface::HANDLER_DISABLED]) && $this->booleanUtils->toBoolean($handler[ConfigInterface::HANDLER_DISABLED])) { throw new \LogicException(sprintf('Disabled handler "%s" for topic "%s" cannot be added to the config file', $handlerName, $topicName)); } $this->validateResponseHandlersType($serviceName, $methodName, $handlerName, $topicName); } }
/** * Extract response handlers. * * @param \DOMNode $topicNode * @return array List of handlers, each contain service name and method name */ protected function extractTopicResponseHandlers($topicNode) { $topicName = $topicNode->attributes->getNamedItem('name')->nodeValue; $topicChildNodes = $topicNode->childNodes; $handlerNodes = []; /** @var \DOMNode $topicChildNode */ foreach ($topicChildNodes as $topicChildNode) { if ($topicChildNode->nodeName === 'handler') { $handlerAttributes = $topicChildNode->attributes; if ($handlerAttributes->getNamedItem('disabled') && $this->booleanUtils->toBoolean($handlerAttributes->getNamedItem('disabled')->nodeValue)) { continue; } $handlerName = $handlerAttributes->getNamedItem('name')->nodeValue; $serviceType = $handlerAttributes->getNamedItem('type')->nodeValue; $methodName = $handlerAttributes->getNamedItem('method')->nodeValue; $this->xmlValidator->validateResponseHandlersType($serviceType, $methodName, $handlerName, $topicName); $handlerNodes[$handlerName] = [Config::HANDLER_TYPE => $serviceType, Config::HANDLER_METHOD => $methodName]; } } return $handlerNodes; }
/** * 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; }
/** * @param mixed $input * * @dataProvider toBooleanExceptionDataProvider * @expectedException \InvalidArgumentException * @expectedExceptionMessage Boolean value is expected */ public function testToBooleanException($input) { $this->object->toBoolean($input); }