/** * Creates a new instance of the given class and populates it with the array of data. The data can * be in different forms depending on the adapter being used, REST vs. SOAP. For REST, the data is * in snake_case (e.g. tax_class_id) while for SOAP the data is in camelCase (e.g. taxClassId). * * @param string $className * @param array $data * @return object the newly created and populated object * @throws \Exception */ protected function _createFromArray($className, $data) { $data = is_array($data) ? $data : []; $class = new ClassReflection($className); if (is_subclass_of($className, self::EXTENSION_ATTRIBUTES_TYPE)) { $className = substr($className, 0, -strlen('Interface')); } $object = $this->objectManager->create($className); foreach ($data as $propertyName => $value) { // Converts snake_case to uppercase CamelCase to help form getter/setter method names // This use case is for REST only. SOAP request data is already camel cased $camelCaseProperty = SimpleDataObjectConverter::snakeCaseToUpperCamelCase($propertyName); $methodName = $this->typeProcessor->findGetterMethodName($class, $camelCaseProperty); $methodReflection = $class->getMethod($methodName); if ($methodReflection->isPublic()) { $returnType = $this->typeProcessor->getGetterReturnType($methodReflection)['type']; try { $setterName = $this->typeProcessor->findSetterMethodName($class, $camelCaseProperty); } catch (\Exception $e) { if (empty($value)) { continue; } else { throw $e; } } if ($camelCaseProperty === 'CustomAttributes') { $setterValue = $this->convertCustomAttributeValue($value, $className); } else { $setterValue = $this->convertValue($value, $returnType); } $object->{$setterName}($setterValue); } } return $object; }
/** * @expectedException \Exception * @expectedExceptionMessageRegExp /Property :"InvalidAttribute" does not exist in the provided class: \w+/ */ public function testFindSetterMethodNameInvalidAttribute() { $class = new ClassReflection("\\Magento\\Framework\\Reflection\\Test\\Unit\\DataObject"); $this->_typeProcessor->findSetterMethodName($class, 'InvalidAttribute'); }