/** * @param ExtensibleDataInterface $dataObject * @param string $getterMethodName * @param string $methodName * @param array $value * @param string $interfaceName * @return $this */ protected function setComplexValue(ExtensibleDataInterface $dataObject, $getterMethodName, $methodName, array $value, $interfaceName) { if ($interfaceName == null) { $interfaceName = get_class($dataObject); } $returnType = $this->methodsMapProcessor->getMethodReturnType($interfaceName, $getterMethodName); if ($this->typeProcessor->isTypeSimple($returnType)) { $dataObject->{$methodName}($value); return $this; } if ($this->typeProcessor->isArrayType($returnType)) { $type = $this->typeProcessor->getArrayItemType($returnType); $objects = []; foreach ($value as $arrayElementData) { $object = $this->objectFactory->create($type, []); $this->populateWithArray($object, $arrayElementData, $type); $objects[] = $object; } $dataObject->{$methodName}($objects); return $this; } if (is_subclass_of($returnType, '\\Magento\\Framework\\Api\\ExtensibleDataInterface')) { $object = $this->objectFactory->create($returnType, []); $this->populateWithArray($object, $value, $returnType); } else { if (is_subclass_of($returnType, '\\Magento\\Framework\\Api\\ExtensionAttributesInterface')) { $object = $this->extensionFactory->create(get_class($dataObject), $value); } else { $object = $this->objectFactory->create($returnType, $value); } } $dataObject->{$methodName}($object); return $this; }
/** * @param CartTotalRepository $subject * @param callable $proceed * @param int $cartId * @return \Magento\Quote\Model\Cart\Totals * @throws \Magento\Framework\Exception\NoSuchEntityException * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function aroundGet(CartTotalRepository $subject, \Closure $proceed, $cartId) { $result = $proceed($cartId); $quote = $this->quoteRepository->getActive($cartId); $totals = $quote->getTotals(); if (!array_key_exists('tax', $totals)) { return $result; } $taxes = $totals['tax']->getData(); if (!array_key_exists('full_info', $taxes)) { return $result; } $detailsId = 1; $finalData = []; foreach ($taxes['full_info'] as $info) { if (array_key_exists('hidden', $info) && $info['hidden'] || $info['amount'] == 0 && $this->taxConfig->displayCartZeroTax()) { continue; } $taxDetails = $this->detailsFactory->create([]); $taxDetails->setAmount($info['amount']); $taxRates = $this->getRatesData($info['rates']); $taxDetails->setRates($taxRates); $taxDetails->setGroupId($detailsId); $finalData[] = $taxDetails; $detailsId++; } $taxInfo = $this->extensionFactory->create('\\Magento\\Quote\\Model\\Cart\\Totals', []); $taxInfo->setTaxGrandtotalDetails($finalData); /** @var $result \Magento\Quote\Model\Cart\Totals */ $result->setExtensionAttributes($taxInfo); $result->setTaxAmount($taxes['value']); return $result; }
/** * Determine if the type is an actual extensible data interface. * * @param string $typeName * @return bool */ private function isExtensibleAttributesImplemented($typeName) { try { $this->extensionAttributesFactory->getExtensibleInterfaceName($typeName); return true; } catch (\LogicException $e) { return false; } }
/** * @param mixed $dataObject * @param string $getterMethodName * @param string $methodName * @param array $value * @param string $interfaceName * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ protected function setComplexValue($dataObject, $getterMethodName, $methodName, array $value, $interfaceName) { if ($interfaceName == null) { $interfaceName = get_class($dataObject); } $returnType = $this->methodsMapProcessor->getMethodReturnType($interfaceName, $getterMethodName); if ($this->typeProcessor->isTypeSimple($returnType)) { $dataObject->{$methodName}($value); return $this; } if ($this->typeProcessor->isArrayType($returnType)) { $type = $this->typeProcessor->getArrayItemType($returnType); $objects = []; foreach ($value as $arrayElementData) { $object = $this->objectFactory->create($type, []); $this->populateWithArray($object, $arrayElementData, $type); $objects[] = $object; } $dataObject->{$methodName}($objects); return $this; } if (is_subclass_of($returnType, '\\Magento\\Framework\\Api\\ExtensibleDataInterface')) { $object = $this->objectFactory->create($returnType, []); $this->populateWithArray($object, $value, $returnType); } else { if (is_subclass_of($returnType, '\\Magento\\Framework\\Api\\ExtensionAttributesInterface')) { foreach ($value as $extensionAttributeKey => $extensionAttributeValue) { $extensionAttributeGetterMethodName = 'get' . \Magento\Framework\Api\SimpleDataObjectConverter::snakeCaseToUpperCamelCase($extensionAttributeKey); $methodReturnType = $this->methodsMapProcessor->getMethodReturnType($returnType, $extensionAttributeGetterMethodName); $extensionAttributeType = $this->typeProcessor->isArrayType($methodReturnType) ? $this->typeProcessor->getArrayItemType($methodReturnType) : $methodReturnType; if ($this->typeProcessor->isTypeSimple($extensionAttributeType)) { $value[$extensionAttributeKey] = $extensionAttributeValue; } else { if ($this->typeProcessor->isArrayType($methodReturnType)) { foreach ($extensionAttributeValue as $key => $extensionAttributeArrayValue) { $extensionAttribute = $this->objectFactory->create($extensionAttributeType, []); $this->populateWithArray($extensionAttribute, $extensionAttributeArrayValue, $extensionAttributeType); $value[$extensionAttributeKey][$key] = $extensionAttribute; } } else { $value[$extensionAttributeKey] = $this->objectFactory->create($extensionAttributeType, ['data' => $extensionAttributeValue]); } } } $object = $this->extensionFactory->create(get_class($dataObject), ['data' => $value]); } else { $object = $this->objectFactory->create($returnType, $value); } } $dataObject->{$methodName}($object); return $this; }
/** * Access the extension set method * * @param \Magento\Framework\Api\ExtensibleDataInterface $object * @param string $code * @param mixed $value * * @return null * @throws \InvalidArgumentException */ protected function setAttributeValueFromExtensibleDataObject($target, $code, $value) { $method = 'set' . str_replace(' ', '', ucwords(str_replace('_', ' ', $code))); $methodExists = method_exists($target, $method); if ($methodExists == true) { $target->{$method}($value); } else { // If we couldn't find the method, check if we can set it from the extension attributes $extensionAttributes = $target->getExtensionAttributes(); if ($extensionAttributes == null) { $extensionAttributes = $this->extensionAttributesFactory->create(get_class($target)); } $extensionMethodExists = method_exists($extensionAttributes, $method); if ($extensionMethodExists == true) { $extensionAttributes->{$method}($value); $target->setExtensionAttributes($extensionAttributes); } else { throw new \InvalidArgumentException('Attribute in object does not exist.'); } } return null; }
public function testCreateWithLogicException() { $this->setExpectedException('LogicException', "Class 'Magento\\Framework\\Api\\ExtensionAttributesFactoryTest' must implement an interface, " . "which extends from 'Magento\\Framework\\Api\\ExtensibleDataInterface'"); $this->factory->create(get_class($this)); }
public function testCreate() { $this->assertInstanceOf('Magento\\Wonderland\\Api\\Data\\FakeRegionExtension', $this->factory->create('Magento\\Wonderland\\Model\\Data\\FakeRegion')); }