Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 48 additions & 5 deletions lib/internal/Magento/Framework/Api/DataObjectHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@

namespace Magento\Framework\Api;

use Magento\Framework\DataObject;
use Magento\Framework\Reflection\MethodsMap;

use function get_class;
use function is_subclass_of;

/**
* Service class allow populating object from array data
*
Expand Down Expand Up @@ -220,16 +224,15 @@ protected function setComplexValue(
$value[$extensionAttributeKey][$key] = $extensionAttribute;
}
} else {
$value[$extensionAttributeKey] = $this->objectFactory->create(
$extensionAttributeType,
['data' => $extensionAttributeValue]
);
$object = $this->createSubObject($extensionAttributeType, $extensionAttributeValue);

$value[$extensionAttributeKey] = $object;
}
}
}
$object = $this->extensionFactory->create(get_class($dataObject), ['data' => $value]);
} else {
$object = $this->objectFactory->create($returnType, $value);
$object = $this->createSubObject($returnType, $value);
}
$dataObject->$methodName($object);
return $this;
Expand Down Expand Up @@ -339,4 +342,44 @@ public function setCustomAttributes(mixed $dataObject, array $data, array $attri
}
return $data;
}

/**
* Check if the object is a DataObject or similar, then its value is already prepared. Expand object instead.
*
* @param string $returnType
* @param array $value
*
* @return object
*/
private function createSubObject(string $returnType, array $value): object
{
$object = $this->objectFactory->create($returnType, []);

if ($this->isValueForDataObject(get_class($object), $value)) {
$value = $value['data'];
}

$this->populateWithArray(
$object,
$value,
$returnType
);

return $object;
}

/**
* Check if the value is already prepared to be created for a DataObject.
*
* @param string $returnType
* @param array $value
* @return bool
*/
private function isValueForDataObject(string $returnType, array $value): bool
{
$isDataObject = is_subclass_of($returnType, DataObject::class) ||
is_subclass_of($returnType, AbstractSimpleObject::class);

return $isDataObject && isset($value['data']);
}
}