Skip to content

Commit 9647d87

Browse files
committed
Optimize object fill for AbstractModel case
1 parent 9599f40 commit 9647d87

File tree

1 file changed

+36
-19
lines changed

1 file changed

+36
-19
lines changed

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

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -100,24 +100,7 @@ protected function _setDataValues($dataObject, array $data, $interfaceName)
100100
if (empty($data)) {
101101
return $this;
102102
}
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_map(static function ($e) {
109-
return SimpleDataObjectConverter::camelCaseToSnakeCase(substr($e, 3));
110-
}, $setMethods);
111-
$setMethods = array_merge(
112-
$setMethods,
113-
array_map(
114-
function ($e) {
115-
return str_replace('is_', '', $e);
116-
},
117-
$setMethods
118-
)
119-
);
120-
$setMethods = array_flip($setMethods);
103+
$setMethods = $this->getSetters($dataObject);
121104
if ($dataObject instanceof ExtensibleDataInterface
122105
&& !empty($data[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES])
123106
) {
@@ -148,7 +131,7 @@ function ($e) {
148131
$methodName = SimpleDataObjectConverter::snakeCaseToUpperCamelCase($key);
149132

150133
if (!is_array($value)) {
151-
if ($methodName !== 'setExtensionAttributes' || $value !== null) {
134+
if ($methodName !== 'ExtensionAttributes' || $value !== null) {
152135
if (method_exists($dataObject, 'set' . $methodName)) {
153136
$dataObject->{'set' . $methodName}($value);
154137
} else {
@@ -295,4 +278,38 @@ public function getCustomAttributeValueByType(array $attributeValues, $type)
295278
}
296279
return $attributeValueArray;
297280
}
281+
282+
/** @var array */
283+
private $settersCache = [];
284+
285+
/**
286+
* Get list of setters for object
287+
*
288+
* @param object $dataObject
289+
* @return array
290+
*/
291+
private function getSetters(object $dataObject): array
292+
{
293+
$class = get_class($dataObject);
294+
if (!isset($this->settersCache[$class])) {
295+
$dataObjectMethods = get_class_methods($class);
296+
// use regexp to manipulate with method list as it use jit starting with PHP 7.3
297+
$setters = explode(
298+
',',
299+
strtolower(
300+
// (0) remove all not setter
301+
// (1) add _ before upper letter
302+
// (2) remove set_ in start of name
303+
// (3) add name without is_ prefix
304+
preg_replace(
305+
['/(^|,)(?!set)[^,]*/S','/(.)([A-Z])/S', '/(^|,)set_/iS', '/(^|,)is_([^,]+)/is'],
306+
['', '$1_$2', '$1', '$1$2,is_$2'],
307+
implode(',', $dataObjectMethods)
308+
)
309+
)
310+
);
311+
$this->settersCache[$class] = array_flip($setters);
312+
}
313+
return $this->settersCache[$class];
314+
}
298315
}

0 commit comments

Comments
 (0)