@@ -97,6 +97,18 @@ public function populateWithArray($dataObject, array $data, $interfaceName)
97
97
*/
98
98
protected function _setDataValues ($ dataObject , array $ data , $ interfaceName )
99
99
{
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
+
100
112
if ($ dataObject instanceof ExtensibleDataInterface
101
113
&& !empty ($ data [CustomAttributesDataInterface::CUSTOM_ATTRIBUTES ])
102
114
) {
@@ -108,40 +120,37 @@ protected function _setDataValues($dataObject, array $data, $interfaceName)
108
120
}
109
121
unset($ data [CustomAttributesDataInterface::CUSTOM_ATTRIBUTES ]);
110
122
}
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 ) {
114
125
return is_scalar ($ e ) || is_null ($ e );
115
126
});
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 );
117
132
$ dataObject ->addData ($ simpleData );
118
133
$ data = array_diff_key ($ data , $ simpleData );
119
134
if (\count ($ data ) === 0 ) {
120
135
return $ this ;
121
136
}
122
137
}
138
+ foreach (array_intersect_key ($ data , $ setMethods ) as $ key => $ value ) {
139
+ $ methodName = SimpleDataObjectConverter::snakeCaseToUpperCamelCase ($ key );
123
140
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 );
143
144
}
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) {
145
154
$ dataObject ->setCustomAttribute ($ key , $ value );
146
155
}
147
156
}
@@ -192,7 +201,7 @@ protected function setComplexValue(
192
201
} elseif (is_subclass_of ($ returnType , \Magento \Framework \Api \ExtensionAttributesInterface::class)) {
193
202
foreach ($ value as $ extensionAttributeKey => $ extensionAttributeValue ) {
194
203
$ extensionAttributeGetterMethodName
195
- = 'get ' . \ Magento \ Framework \ Api \ SimpleDataObjectConverter::snakeCaseToUpperCamelCase (
204
+ = 'get ' . SimpleDataObjectConverter::snakeCaseToUpperCamelCase (
196
205
$ extensionAttributeKey
197
206
);
198
207
$ methodReturnType = $ this ->methodsMapProcessor ->getMethodReturnType (
0 commit comments