Skip to content

Commit 7089a80

Browse files
committed
Optimize object fill for AbstractModel case
- Refactor setDataValues method logic;
1 parent 51c0317 commit 7089a80

File tree

1 file changed

+34
-25
lines changed

1 file changed

+34
-25
lines changed

lib/internal/Magento/Framework/Api/DataObjectHelper.php

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,18 @@ public function populateWithArray($dataObject, array $data, $interfaceName)
9797
*/
9898
protected function _setDataValues($dataObject, array $data, $interfaceName)
9999
{
100+
if (empty($data)) {
101+
return $this;
102+
}
103+
$dataObjectMethods = get_class_methods(get_class($dataObject));
104+
105+
$setMethods = array_filter($dataObjectMethods, static function ($e) {
106+
return 0 === strncmp($e, 'set', 3);
107+
});
108+
$setMethods = array_flip(array_map(static function ($e) {
109+
return SimpleDataObjectConverter::camelCaseToSnakeCase(substr($e, 3));
110+
}, $setMethods));
111+
100112
if ($dataObject instanceof ExtensibleDataInterface
101113
&& !empty($data[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES])
102114
) {
@@ -108,40 +120,37 @@ protected function _setDataValues($dataObject, array $data, $interfaceName)
108120
}
109121
unset($data[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES]);
110122
}
111-
if ($dataObject instanceof \Magento\Framework\Model\AbstractModel//) {
112-
&& !$dataObject instanceof \Magento\Quote\Api\Data\AddressInterface) {
113-
$simpleData = array_filter($data, function ($e) {
123+
if ($dataObject instanceof \Magento\Framework\Model\AbstractModel) {
124+
$simpleData = array_filter($data, static function ($e) {
114125
return is_scalar($e) || is_null($e);
115126
});
116-
unset($simpleData['id']);
127+
if (isset($simpleData['id'])) {
128+
$dataObject->setId($simpleData['id']);
129+
unset($simpleData['id']);
130+
}
131+
$simpleData = array_intersect_key($simpleData, $setMethods);
117132
$dataObject->addData($simpleData);
118133
$data = array_diff_key($data, $simpleData);
119134
if (\count($data) === 0) {
120135
return $this;
121136
}
122137
}
138+
foreach (array_intersect_key($data, $setMethods) as $key => $value) {
139+
$methodName = SimpleDataObjectConverter::snakeCaseToUpperCamelCase($key);
123140

124-
$dataObjectMethods = get_class_methods(get_class($dataObject));
125-
foreach ($data as $key => $value) {
126-
/* First, verify is there any setter for the key on the Service Data Object */
127-
$camelCaseKey = \Magento\Framework\Api\SimpleDataObjectConverter::snakeCaseToUpperCamelCase($key);
128-
$possibleMethods = [
129-
'set' . $camelCaseKey,
130-
'setIs' . $camelCaseKey,
131-
];
132-
if ($methodNames = array_intersect($possibleMethods, $dataObjectMethods)) {
133-
$methodName = array_values($methodNames)[0];
134-
if (!is_array($value)) {
135-
if ($methodName === 'setExtensionAttributes' && $value === null) {
136-
// Cannot pass a null value to a method with a typed parameter
137-
} else {
138-
$dataObject->$methodName($value);
139-
}
140-
} else {
141-
$getterMethodName = 'get' . $camelCaseKey;
142-
$this->setComplexValue($dataObject, $getterMethodName, $methodName, $value, $interfaceName);
141+
if (!is_array($value)) {
142+
if ($methodName !== 'setExtensionAttributes' || $value !== null) {
143+
$dataObject->{'set' . $methodName}($value);
143144
}
144-
} elseif ($dataObject instanceof CustomAttributesDataInterface) {
145+
} else {
146+
$getterMethodName = 'get' . $methodName;
147+
$this->setComplexValue($dataObject, $getterMethodName, 'set' . $methodName, $value, $interfaceName);
148+
}
149+
unset($data[$key]);
150+
}
151+
152+
foreach ($data as $key => $value) {
153+
if ($dataObject instanceof CustomAttributesDataInterface) {
145154
$dataObject->setCustomAttribute($key, $value);
146155
}
147156
}
@@ -192,7 +201,7 @@ protected function setComplexValue(
192201
} elseif (is_subclass_of($returnType, \Magento\Framework\Api\ExtensionAttributesInterface::class)) {
193202
foreach ($value as $extensionAttributeKey => $extensionAttributeValue) {
194203
$extensionAttributeGetterMethodName
195-
= 'get' . \Magento\Framework\Api\SimpleDataObjectConverter::snakeCaseToUpperCamelCase(
204+
= 'get' . SimpleDataObjectConverter::snakeCaseToUpperCamelCase(
196205
$extensionAttributeKey
197206
);
198207
$methodReturnType = $this->methodsMapProcessor->getMethodReturnType(

0 commit comments

Comments
 (0)