/**
  * Add separate flow for annotated data objects, all other objects are processed by original code.
  *
  * @param \Magento\Framework\Webapi\ServiceInputProcessor $subject
  * @param \Closure $proceed
  * @param $data
  * @param $type
  * @return mixed
  */
 public function aroundConvertValue(\Magento\Framework\Webapi\ServiceInputProcessor $subject, \Closure $proceed, $data, $type)
 {
     $result = null;
     if (is_subclass_of($type, \Flancer32\Lib\DataObject::class)) {
         if ($this->_typeProcessor->isTypeSimple($type) || $this->_typeProcessor->isTypeAny($type)) {
             $result = $this->_typeProcessor->processSimpleAndAnyType($data, $type);
         } else {
             /** Complex type or array of complex types */
             $isArrayType = $this->_typeProcessor->isArrayType($type);
             if ($isArrayType) {
                 // Initializing the result for array type else it will return null for empty array
                 $itemType = $this->_typeProcessor->getArrayItemType($type);
                 if (is_array($data)) {
                     $result = [];
                     foreach ($data as $key => $item) {
                         $result[$key] = $this->_parser->parseArrayData($itemType, $item);
                     }
                 }
             } else {
                 if (is_null($data)) {
                     // do nothing, result is null
                 } else {
                     $result = $this->_parser->parseArrayData($type, $data);
                 }
             }
         }
     } else {
         $result = $proceed($data, $type);
     }
     return $result;
 }
 /**
  * Convert data from array to Data Object representation if type is Data Object or array of Data Objects.
  *
  * @param mixed $data
  * @param string $type Convert given value to the this type
  * @return mixed
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 public function convertValue($data, $type)
 {
     $isArrayType = $this->typeProcessor->isArrayType($type);
     if ($isArrayType && isset($data['item'])) {
         $data = $this->_removeSoapItemNode($data);
     }
     if ($this->typeProcessor->isTypeSimple($type) || $this->typeProcessor->isTypeAny($type)) {
         $result = $this->typeProcessor->processSimpleAndAnyType($data, $type);
     } else {
         /** Complex type or array of complex types */
         if ($isArrayType) {
             // Initializing the result for array type else it will return null for empty array
             $result = is_array($data) ? [] : null;
             $itemType = $this->typeProcessor->getArrayItemType($type);
             if (is_array($data)) {
                 foreach ($data as $key => $item) {
                     $result[$key] = $this->_createFromArray($itemType, $item);
                 }
             }
         } else {
             $result = $this->_createFromArray($type, $data);
         }
     }
     return $result;
 }
Exemplo n.º 3
0
 /**
  * Convert data from array to Data Object representation if type is Data Object or array of Data Objects.
  *
  * @param mixed $value
  * @param string $type Convert given value to the this type
  * @return mixed
  * @throws WebapiException
  * @SuppressWarnings(PHPMD.CyclomaticComplexity)
  */
 protected function _convertValue($value, $type)
 {
     $isArrayType = $this->typeProcessor->isArrayType($type);
     if ($isArrayType && isset($value['item'])) {
         $value = $this->_removeSoapItemNode($value);
     }
     if ($this->typeProcessor->isTypeSimple($type) || $this->typeProcessor->isTypeAny($type)) {
         try {
             $result = $this->typeProcessor->processSimpleAndAnyType($value, $type);
         } catch (SerializationException $e) {
             throw new WebapiException(new Phrase($e->getMessage()));
         }
     } else {
         /** Complex type or array of complex types */
         if ($isArrayType) {
             // Initializing the result for array type else it will return null for empty array
             $result = is_array($value) ? [] : null;
             $itemType = $this->typeProcessor->getArrayItemType($type);
             if (is_array($value)) {
                 foreach ($value as $key => $item) {
                     $result[$key] = $this->_createFromArray($itemType, $item);
                 }
             }
         } else {
             $result = $this->_createFromArray($type, $value);
         }
     }
     return $result;
 }
 /**
  * @expectedException \Magento\Framework\Exception\SerializationException
  * @expectedExceptionMessage Invalid type for value: "integer". Expected Type: "int[]".
  */
 public function testProcessSimpleTypeInvalidType()
 {
     $value = 1;
     $type = 'int[]';
     $this->_typeProcessor->processSimpleAndAnyType($value, $type);
 }