Example #1
0
 /**
  * @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;
 }
Example #2
0
 /**
  * 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
  */
 protected function _createFromArray($className, $data)
 {
     $data = is_array($data) ? $data : [];
     $class = new ClassReflection($className);
     $builder = $this->builderFactory->getDataBuilder($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'];
             $setterName = 'set' . $camelCaseProperty;
             if ($camelCaseProperty === 'CustomAttributes') {
                 $setterValue = $this->convertCustomAttributeValue($value, $returnType, $className);
             } else {
                 $setterValue = $this->_convertValue($value, $returnType);
             }
             $builder->{$setterName}($setterValue);
         }
     }
     return $builder->create();
 }