Skip to content

Commit

Permalink
Merge pull request #508 from magento-api/MAGETWO-40641-Reusable-Data-…
Browse files Browse the repository at this point in the history
…Object-Encoding-Decoding

[API] Sprint54 Make Data Object Encoding-Decoding Reusable
  • Loading branch information
Paliarush, Alexander(apaliarush) committed Aug 6, 2015
2 parents 15a9e52 + 4cc6ddd commit 12f560b
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 47 deletions.
4 changes: 2 additions & 2 deletions lib/internal/Magento/Framework/Reflection/TypeProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -591,9 +591,9 @@ protected function findAccessorMethodName(
$methodName = $boolAccessorName;
return $methodName;
} else {
throw new \Exception(
throw new \LogicException(
sprintf(
'Property :"%s" does not exist in the provided class: "%s".',
'Property "%s" does not have corresponding setter in class "%s".',
$camelCaseProperty,
$class->getName()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ interface ConfigOptionsListInterface
public function getOptions();

/**
* Creates array of ConfigData objects from user inputted data.
* Creates array of ConfigData objects from user input data.
* Data in these objects will be stored in array form in deployment config file.
*
* @param array $options
Expand Down
54 changes: 32 additions & 22 deletions lib/internal/Magento/Framework/Webapi/ServiceInputProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public function __construct(
* @param array $inputArray data to send to method in key-value format
* @return array list of parameters that can be used to call the service method
* @throws InputException if no value is provided for required parameters
* @throws WebapiException
*/
public function process($serviceClassName, $serviceMethodName, array $inputArray)
{
Expand All @@ -97,7 +98,11 @@ public function process($serviceClassName, $serviceMethodName, array $inputArray
? $inputArray[$paramName]
: $inputArray[$snakeCaseParamName];

$inputData[] = $this->_convertValue($paramValue, $param['type']);
try {
$inputData[] = $this->convertValue($paramValue, $param['type']);
} catch (SerializationException $e) {
throw new WebapiException(new Phrase($e->getMessage()));
}
} else {
if ($param['isDefaultValueAvailable']) {
$inputData[] = $param['defaultValue'];
Expand All @@ -106,17 +111,7 @@ public function process($serviceClassName, $serviceMethodName, array $inputArray
}
}
}

if (!empty($inputError)) {
$exception = new InputException();
foreach ($inputError as $errorParamField) {
$exception->addError(new Phrase(InputException::REQUIRED_FIELD, ['fieldName' => $errorParamField]));
}
if ($exception->wasErrorAdded()) {
throw $exception;
}
}

$this->processInputError($inputError);
return $inputData;
}

Expand Down Expand Up @@ -159,7 +154,7 @@ protected function _createFromArray($className, $data)
if ($camelCaseProperty === 'CustomAttributes') {
$setterValue = $this->convertCustomAttributeValue($value, $className);
} else {
$setterValue = $this->_convertValue($value, $returnType);
$setterValue = $this->convertValue($value, $returnType);
}
$object->{$setterName}($setterValue);
}
Expand Down Expand Up @@ -206,7 +201,7 @@ protected function convertCustomAttributeValue($customAttributesValueArray, $dat
$attributeValue = $this->_createDataObjectForTypeAndArrayValue($type, $customAttributeValue);
}
} else {
$attributeValue = $this->_convertValue($customAttributeValue, $type);
$attributeValue = $this->convertValue($customAttributeValue, $type);
}
//Populate the attribute value data object once the value for custom attribute is derived based on type
$result[$customAttributeCode] = $this->attributeValueFactory->create()
Expand Down Expand Up @@ -245,21 +240,16 @@ protected function _createDataObjectForTypeAndArrayValue($type, $customAttribute
* @param mixed $value
* @param string $type Convert given value to the this type
* @return mixed
* @throws WebapiException
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* @throws \Magento\Framework\Exception\LocalizedException
*/
protected function _convertValue($value, $type)
public 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()));
}
$result = $this->typeProcessor->processSimpleAndAnyType($value, $type);
} else {
/** Complex type or array of complex types */
if ($isArrayType) {
Expand Down Expand Up @@ -336,4 +326,24 @@ protected function getMethodParams($serviceClassName, $serviceMethodName)
$this->cache->save(serialize($params), $cacheId, [WebapiCache::CACHE_TAG]);
return $params;
}

/**
* Process an input error
*
* @param array $inputError
* @return void
* @throws InputException
*/
protected function processInputError($inputError)
{
if (!empty($inputError)) {
$exception = new InputException();
foreach ($inputError as $errorParamField) {
$exception->addError(new Phrase(InputException::REQUIRED_FIELD, ['fieldName' => $errorParamField]));
}
if ($exception->wasErrorAdded()) {
throw $exception;
}
}
}
}
56 changes: 34 additions & 22 deletions lib/internal/Magento/Framework/Webapi/ServiceOutputProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,28 +55,7 @@ public function process($data, $serviceClassName, $serviceMethodName)
{
/** @var string $dataType */
$dataType = $this->methodsMapProcessor->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;
}
return $this->convertValue($data, $dataType);
}

/**
Expand All @@ -100,4 +79,37 @@ protected function processDataObject($dataObjectArray)
}
return $dataObjectArray;
}

/**
* Convert associative array into proper data object.
*
* @param array $data
* @param string $dataType
* @return array|object
*/
public function convertValue($data, $dataType)
{
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;
}
}
}

0 comments on commit 12f560b

Please sign in to comment.