/**
  * Convert custom attribute data array to array of AttributeValue Data Object
  *
  * @param array $customAttributesValueArray
  * @param string $dataObjectClassName
  * @return AttributeValue[]
  */
 protected function convertCustomAttributeValue($customAttributesValueArray, $dataObjectClassName)
 {
     $result = [];
     $dataObjectClassName = ltrim($dataObjectClassName, '\\');
     $camelCaseAttributeCodeKey = lcfirst(SimpleDataObjectConverter::snakeCaseToUpperCamelCase(AttributeValue::ATTRIBUTE_CODE));
     foreach ($customAttributesValueArray as $key => $customAttribute) {
         if (!is_array($customAttribute)) {
             $customAttribute = [AttributeValue::ATTRIBUTE_CODE => $key, AttributeValue::VALUE => $customAttribute];
         }
         if (isset($customAttribute[AttributeValue::ATTRIBUTE_CODE])) {
             $customAttributeCode = $customAttribute[AttributeValue::ATTRIBUTE_CODE];
         } elseif (isset($customAttribute[$camelCaseAttributeCodeKey])) {
             $customAttributeCode = $customAttribute[$camelCaseAttributeCodeKey];
         } else {
             $customAttributeCode = null;
         }
         //Check if type is defined, else default to string
         $type = $this->customAttributeTypeLocator->getType($customAttributeCode, $dataObjectClassName);
         $type = $type ? $type : TypeProcessor::ANY_TYPE;
         $customAttributeValue = $customAttribute[AttributeValue::VALUE];
         if (is_array($customAttributeValue)) {
             //If type for AttributeValue's value as array is mixed, further processing is not possible
             if ($type === TypeProcessor::ANY_TYPE) {
                 $attributeValue = $customAttributeValue;
             } else {
                 $attributeValue = $this->_createDataObjectForTypeAndArrayValue($type, $customAttributeValue);
             }
         } else {
             $attributeValue = $this->convertValue($customAttributeValue, $type);
         }
         //Populate the attribute value data object once the value for custom attribute is derived based on type
         $result[$customAttributeCode] = $this->attributeValueFactory->create()->setAttributeCode($customAttributeCode)->setValue($attributeValue);
     }
     return $result;
 }
Example #2
0
 /**
  * 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($typeName);
     }
     return $wsdl;
 }
 protected function setUp()
 {
     $this->serviceMetadataMock = $this->getMockBuilder('Magento\\Webapi\\Model\\ServiceMetadata')->disableOriginalConstructor()->getMock();
     $this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
     $swagger = $this->objectManager->getObject('Magento\\Webapi\\Model\\Rest\\Swagger');
     $this->swaggerFactoryMock = $this->getMockBuilder('Magento\\Webapi\\Model\\Rest\\SwaggerFactory')->setMethods(['create'])->disableOriginalConstructor()->getMock();
     $this->swaggerFactoryMock->expects($this->any())->method('create')->will($this->returnValue($swagger));
     $this->cacheMock = $this->getMockBuilder('Magento\\Framework\\App\\Cache\\Type\\Webapi')->disableOriginalConstructor()->getMock();
     $this->cacheMock->expects($this->any())->method('load')->will($this->returnValue(false));
     $this->cacheMock->expects($this->any())->method('save')->will($this->returnValue(true));
     $this->typeProcessorMock = $this->getMockBuilder('Magento\\Framework\\Reflection\\TypeProcessor')->disableOriginalConstructor()->getMock();
     $this->typeProcessorMock->expects($this->any())->method('getOperationName')->will($this->returnValue(self::OPERATION_NAME));
     $this->customAttributeTypeLocatorMock = $this->getMockBuilder('Magento\\Framework\\Webapi\\CustomAttributeTypeLocatorInterface')->disableOriginalConstructor()->getMock();
     $this->customAttributeTypeLocatorMock->expects($this->any())->method('getAllServiceDataInterfaces')->willReturn(['$customAttributeClass']);
     $this->storeManagerMock = $this->getMockBuilder('Magento\\Store\\Model\\StoreManagerInterface')->setMethods(['getStore'])->disableOriginalConstructor()->getMockForAbstractClass();
     $storeMock = $this->getMockBuilder('Magento\\Store\\Model\\Store')->disableOriginalConstructor()->getMock();
     $this->storeManagerMock->expects($this->any())->method('getStore')->will($this->returnValue($storeMock));
     $storeMock->expects($this->any())->method('getCode')->will($this->returnValue('store_code'));
     $this->generator = $this->objectManager->getObject('Magento\\Webapi\\Model\\Rest\\Swagger\\Generator', ['swaggerFactory' => $this->swaggerFactoryMock, 'cache' => $this->cacheMock, 'typeProcessor' => $this->typeProcessorMock, 'storeManager' => $this->storeManagerMock, 'serviceMetadata' => $this->serviceMetadataMock, 'customAttributeTypeLocator' => $this->customAttributeTypeLocatorMock]);
 }
 /**
  * Convert custom attribute data array to array of AttributeValue Data Object
  *
  * @param array $customAttributesValueArray
  * @param string $dataObjectClassName
  * @return AttributeValue[]
  * @throws SerializationException
  */
 protected function convertCustomAttributeValue($customAttributesValueArray, $dataObjectClassName)
 {
     $result = [];
     $dataObjectClassName = ltrim($dataObjectClassName, '\\');
     foreach ($customAttributesValueArray as $key => $customAttribute) {
         if (!is_array($customAttribute)) {
             $customAttribute = [AttributeValue::ATTRIBUTE_CODE => $key, AttributeValue::VALUE => $customAttribute];
         }
         list($customAttributeCode, $customAttributeValue) = $this->processCustomAttribute($customAttribute);
         $type = $this->customAttributeTypeLocator->getType($customAttributeCode, $dataObjectClassName);
         if ($this->typeProcessor->isTypeAny($type) || $this->typeProcessor->isTypeSimple($type) || !is_array($customAttributeValue)) {
             try {
                 $attributeValue = $this->convertValue($customAttributeValue, $type);
             } catch (SerializationException $e) {
                 throw new SerializationException(new Phrase('Attribute "%attribute_code" has invalid value. %details', ['attribute_code' => $customAttributeCode, 'details' => $e->getMessage()]));
             }
         } else {
             $attributeValue = $this->_createDataObjectForTypeAndArrayValue($type, $customAttributeValue);
         }
         //Populate the attribute value data object once the value for custom attribute is derived based on type
         $result[$customAttributeCode] = $this->attributeValueFactory->create()->setAttributeCode($customAttributeCode)->setValue($attributeValue);
     }
     return $result;
 }