Exemplo n.º 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->objectProcessor->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;
 }
 /**
  * Converts the incoming data into scalar or an array of scalars format.
  *
  * If the data provided is null, then an empty array is returned.  Otherwise, if the data is an object, it is
  * assumed to be a Data Object and converted to an associative array with keys representing the properties of the
  * Data Object.
  * Nested Data Objects are also converted.  If the data provided is itself an array, then we iterate through the
  * contents and convert each piece individually.
  *
  * @param mixed $data
  * @param string $serviceClassName
  * @param string $serviceMethodName
  * @return array|int|string|bool|float Scalar or array of scalars
  */
 public function process($data, $serviceClassName, $serviceMethodName)
 {
     /** @var string $dataType */
     $dataType = $this->dataObjectProcessor->getMethodReturnType($serviceClassName, $serviceMethodName);
     if (is_array($data)) {
         $result = [];
         $arrayElementType = substr($dataType, 0, -2);
         foreach ($data as $datum) {
             if (is_object($datum)) {
                 $datum = $this->processDataObject($this->dataObjectProcessor->buildOutputDataArray($datum, $arrayElementType));
             }
             $result[] = $datum;
         }
         return $result;
     } elseif (is_object($data)) {
         return $this->processDataObject($this->dataObjectProcessor->buildOutputDataArray($data, $dataType));
     } elseif ($data === null) {
         return [];
     } else {
         /** No processing is required for scalar types */
         return $data;
     }
 }
Exemplo n.º 3
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;
 }
Exemplo n.º 4
0
 /**
  * Convert service response into format acceptable by SoapServer.
  *
  * @param object|array|string|int|float|null $data
  * @param string $serviceClassName
  * @param string $serviceMethodName
  * @return array
  * @throws \InvalidArgumentException
  */
 protected function _prepareResponseData($data, $serviceClassName, $serviceMethodName)
 {
     /** @var string $dataType */
     $dataType = $this->_dataObjectProcessor->getMethodReturnType($serviceClassName, $serviceMethodName);
     $result = null;
     if (is_object($data)) {
         $result = $this->_dataObjectConverter->convertKeysToCamelCase($this->_dataObjectProcessor->buildOutputDataArray($data, $dataType));
     } elseif (is_array($data)) {
         $dataType = substr($dataType, 0, -2);
         foreach ($data as $key => $value) {
             if ($value instanceof ExtensibleDataInterface || $value instanceof MetadataObjectInterface) {
                 $result[] = $this->_dataObjectConverter->convertKeysToCamelCase($this->_dataObjectProcessor->buildOutputDataArray($value, $dataType));
             } else {
                 $result[$key] = $value;
             }
         }
     } elseif (is_scalar($data) || $data === null) {
         $result = $data;
     } else {
         throw new \InvalidArgumentException("Service returned result in invalid format.");
     }
     return [self::RESULT_NODE_NAME => $result];
 }