/** * Add separate flow for annotated data objects, all other objects are processed by original code. * * @param \Magento\Framework\Webapi\ServiceInputProcessor $subject * @param \Closure $proceed * @param $data * @param $type * @return mixed */ public function aroundConvertValue(\Magento\Framework\Webapi\ServiceInputProcessor $subject, \Closure $proceed, $data, $type) { $result = null; if (is_subclass_of($type, \Flancer32\Lib\DataObject::class)) { if ($this->_typeProcessor->isTypeSimple($type) || $this->_typeProcessor->isTypeAny($type)) { $result = $this->_typeProcessor->processSimpleAndAnyType($data, $type); } else { /** Complex type or array of complex types */ $isArrayType = $this->_typeProcessor->isArrayType($type); if ($isArrayType) { // Initializing the result for array type else it will return null for empty array $itemType = $this->_typeProcessor->getArrayItemType($type); if (is_array($data)) { $result = []; foreach ($data as $key => $item) { $result[$key] = $this->_parser->parseArrayData($itemType, $item); } } } else { if (is_null($data)) { // do nothing, result is null } else { $result = $this->_parser->parseArrayData($type, $data); } } } } else { $result = $proceed($data, $type); } return $result; }
/** * @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; }
/** * Convert data from array to Data Object representation if type is Data Object or array of Data Objects. * * @param mixed $data * @param string $type Convert given value to the this type * @return mixed * @throws \Magento\Framework\Exception\LocalizedException */ public function convertValue($data, $type) { $isArrayType = $this->typeProcessor->isArrayType($type); if ($isArrayType && isset($data['item'])) { $data = $this->_removeSoapItemNode($data); } if ($this->typeProcessor->isTypeSimple($type) || $this->typeProcessor->isTypeAny($type)) { $result = $this->typeProcessor->processSimpleAndAnyType($data, $type); } else { /** Complex type or array of complex types */ if ($isArrayType) { // Initializing the result for array type else it will return null for empty array $result = is_array($data) ? [] : null; $itemType = $this->typeProcessor->getArrayItemType($type); if (is_array($data)) { foreach ($data as $key => $item) { $result[$key] = $this->_createFromArray($itemType, $item); } } } else { $result = $this->_createFromArray($type, $data); } } return $result; }
/** * Convert data from array to Data Object representation if type is Data Object or array of Data Objects. * * @param mixed $value * @param string $type Convert given value to the this type * @return mixed * @throws WebapiException * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ protected function _convertValue($value, $type) { $isArrayType = $this->typeProcessor->isArrayType($type); if ($isArrayType && isset($value['item'])) { $value = $this->_removeSoapItemNode($value); } if ($this->typeProcessor->isTypeSimple($type) || $this->typeProcessor->isTypeAny($type)) { try { $result = $this->typeProcessor->processSimpleAndAnyType($value, $type); } catch (SerializationException $e) { throw new WebapiException(new Phrase($e->getMessage())); } } else { /** Complex type or array of complex types */ if ($isArrayType) { // Initializing the result for array type else it will return null for empty array $result = is_array($value) ? [] : null; $itemType = $this->typeProcessor->getArrayItemType($type); if (is_array($value)) { foreach ($value as $key => $item) { $result[$key] = $this->_createFromArray($itemType, $item); } } } else { $result = $this->_createFromArray($type, $value); } } return $result; }
public function getArrayItemType() { $this->assertEquals('string', $this->_typeProcessor->getArrayItemType('str[]')); $this->assertEquals('string', $this->_typeProcessor->getArrayItemType('string[]')); $this->assertEquals('integer', $this->_typeProcessor->getArrayItemType('int[]')); $this->assertEquals('boolean', $this->_typeProcessor->getArrayItemType('bool[]')); $this->assertEquals('any', $this->_typeProcessor->getArrayItemType('mixed[]')); }
/** * Create and add WSDL Types for complex custom attribute classes * * @param \Magento\Webapi\Model\Soap\Wsdl $wsdl * @return \Magento\Webapi\Model\Soap\Wsdl */ protected function addCustomAttributeTypes($wsdl) { foreach ($this->customAttributeTypeLocator->getAllServiceDataInterfaces() as $customAttributeClass) { $typeName = $this->_typeProcessor->register($customAttributeClass); $wsdl->addComplexType($this->_typeProcessor->getArrayItemType($typeName)); } return $wsdl; }
/** * {@inheritdoc} */ public function resolveObjectType($attributeCode, $value, $context) { if (!is_object($value)) { throw new \InvalidArgumentException('Provided value is not object type'); } $data = $this->config->get(); $context = trim($context, '\\'); $config = isset($data[$context]) ? $data[$context] : []; $output = get_class($value); if (isset($config[$attributeCode])) { $type = $config[$attributeCode]['type']; $output = $this->typeProcessor->getArrayItemType($type); if (!(class_exists($output) || interface_exists($output))) { throw new \LogicException(sprintf('Class "%s" does not exist. Please note that namespace must be specified.', $type)); } } return $output; }
/** * Process array of types. * * @param string $type * @param array $callInfo * @return void */ protected function _processArrayParameter($type, $callInfo = []) { $arrayItemType = $this->_typeProcessor->getArrayItemType($type); $arrayTypeName = $this->_typeProcessor->translateArrayTypeName($type); if (!$this->_typeProcessor->isTypeSimple($arrayItemType) && !$this->_typeProcessor->isTypeAny($arrayItemType)) { $this->addComplexType($arrayItemType, $callInfo); } $arrayTypeParameters = [self::ARRAY_ITEM_KEY_NAME => ['type' => $arrayItemType, 'required' => false, 'isArray' => true, 'documentation' => sprintf('An item of %s.', $arrayTypeName)]]; $arrayTypeData = ['documentation' => sprintf('An array of %s items.', $arrayItemType), 'parameters' => $arrayTypeParameters]; $this->_typeProcessor->setTypeData($arrayTypeName, $arrayTypeData); $this->addComplexType($arrayTypeName, $callInfo); }
/** * @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; }
/** * Ensure that specified type is either a simple type or a valid service data type. * * @param string $typeName * @return $this * @throws \Exception In case when type is invalid */ protected function validateType($typeName) { if ($this->typeProcessor->isTypeSimple($typeName)) { return $this; } if ($this->typeProcessor->isArrayType($typeName)) { $arrayItemType = $this->typeProcessor->getArrayItemType($typeName); $this->methodsMap->getMethodsMap($arrayItemType); } else { $this->methodsMap->getMethodsMap($typeName); } return $this; }
/** * @param string $methodName * @param string $key * @param array $value * @return $this */ protected function setComplexValue($methodName, $key, array $value) { $returnType = $this->objectProcessor->getMethodReturnType($this->_getDataObjectType(), $methodName); if ($this->typeProcessor->isTypeSimple($returnType)) { $this->data[$key] = $value; return $this; } if ($this->typeProcessor->isArrayType($returnType)) { $type = $this->typeProcessor->getArrayItemType($returnType); $dataBuilder = $this->dataBuilderFactory->getDataBuilder($type); $objects = []; foreach ($value as $arrayElementData) { $objects[] = $dataBuilder->populateWithArray($arrayElementData)->create(); } $this->data[$key] = $objects; return $this; } $dataBuilder = $this->dataBuilderFactory->getDataBuilder($returnType); $object = $dataBuilder->populateWithArray($value)->create(); $this->data[$key] = $object; return $this; }