Example #1
0
 /**
  * @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 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;
 }
 /**
  * @param array $data1
  * @param array $data2
  * @dataProvider dataProviderForTestMergeDataObjects
  */
 public function testMergeDataObjects($data1, $data2)
 {
     /** @var \Magento\Customer\Model\Data\Address $addressDataObject */
     $firstAddressDataObject = $this->objectManager->getObject('Magento\\Customer\\Model\\Data\\Address', ['dataObjectHelper' => $this->dataObjectHelper]);
     /** @var \Magento\Customer\Model\Data\Region $regionDataObject */
     $firstRegionDataObject = $this->objectManager->getObject('Magento\\Customer\\Model\\Data\\Region', ['dataObjectHelper' => $this->dataObjectHelper]);
     $firstRegionDataObject->setRegionId($data1['region']['region_id']);
     $firstRegionDataObject->setRegion($data1['region']['region']);
     if (isset($data1['id'])) {
         $firstAddressDataObject->setId($data1['id']);
     }
     if (isset($data1['country_id'])) {
         $firstAddressDataObject->setCountryId($data1['country_id']);
     }
     $firstAddressDataObject->setStreet($data1['street']);
     $firstAddressDataObject->setIsDefaultShipping($data1['default_shipping']);
     $firstAddressDataObject->setRegion($firstRegionDataObject);
     $secondAddressDataObject = $this->objectManager->getObject('Magento\\Customer\\Model\\Data\\Address', ['dataObjectHelper' => $this->dataObjectHelper]);
     /** @var \Magento\Customer\Model\Data\Region $regionDataObject */
     $secondRegionDataObject = $this->objectManager->getObject('Magento\\Customer\\Model\\Data\\Region', ['dataObjectHelper' => $this->dataObjectHelper]);
     $secondRegionDataObject->setRegionId($data2['region']['region_id']);
     $secondRegionDataObject->setRegion($data2['region']['region']);
     if (isset($data2['id'])) {
         $secondAddressDataObject->setId($data2['id']);
     }
     if (isset($data2['country_id'])) {
         $secondAddressDataObject->setCountryId($data2['country_id']);
     }
     $secondAddressDataObject->setStreet($data2['street']);
     $secondAddressDataObject->setIsDefaultShipping($data2['default_shipping']);
     $secondAddressDataObject->setRegion($secondRegionDataObject);
     $this->objectProcessorMock->expects($this->once())->method('buildOutputDataArray')->with($secondAddressDataObject, get_class($firstAddressDataObject))->willReturn($data2);
     $this->methodsMapProcessor->expects($this->at(0))->method('getMethodReturnType')->with('Magento\\Customer\\Model\\Data\\Address', 'getStreet')->willReturn('string[]');
     $this->methodsMapProcessor->expects($this->at(1))->method('getMethodReturnType')->with('Magento\\Customer\\Model\\Data\\Address', 'getRegion')->willReturn('\\Magento\\Customer\\Api\\Data\\RegionInterface');
     $this->objectFactoryMock->expects($this->once())->method('create')->with('\\Magento\\Customer\\Api\\Data\\RegionInterface', [])->willReturn($secondRegionDataObject);
     $this->dataObjectHelper->mergeDataObjects(get_class($firstAddressDataObject), $firstAddressDataObject, $secondAddressDataObject);
     $this->assertSame($firstAddressDataObject->getId(), $secondAddressDataObject->getId());
     $this->assertSame($firstAddressDataObject->getCountryId(), $secondAddressDataObject->getCountryId());
     $this->assertSame($firstAddressDataObject->getStreet(), $secondAddressDataObject->getStreet());
     $this->assertSame($firstAddressDataObject->isDefaultShipping(), $secondAddressDataObject->isDefaultShipping());
     $this->assertSame($firstAddressDataObject->getRegion(), $secondAddressDataObject->getRegion());
 }