@@ -201,39 +201,25 @@ protected function convertCustomAttributeValue($customAttributesValueArray, $dat
201
201
$ result = [];
202
202
$ dataObjectClassName = ltrim ($ dataObjectClassName , '\\' );
203
203
204
- $ camelCaseAttributeCodeKey = lcfirst (
205
- SimpleDataObjectConverter::snakeCaseToUpperCamelCase (AttributeValue::ATTRIBUTE_CODE )
206
- );
207
204
foreach ($ customAttributesValueArray as $ key => $ customAttribute ) {
208
205
if (!is_array ($ customAttribute )) {
209
206
$ customAttribute = [AttributeValue::ATTRIBUTE_CODE => $ key , AttributeValue::VALUE => $ customAttribute ];
210
207
}
211
- if (isset ($ customAttribute [AttributeValue::ATTRIBUTE_CODE ])) {
212
- $ customAttributeCode = $ customAttribute [AttributeValue::ATTRIBUTE_CODE ];
213
- } elseif (isset ($ customAttribute [$ camelCaseAttributeCodeKey ])) {
214
- $ customAttributeCode = $ customAttribute [$ camelCaseAttributeCodeKey ];
215
- } else {
216
- $ customAttributeCode = null ;
217
- }
208
+
209
+ list ($ customAttributeCode , $ customAttributeValue ) = $ this ->processCustomAttribute ($ customAttribute );
218
210
219
211
$ type = $ this ->customAttributeTypeLocator ->getType ($ customAttributeCode , $ dataObjectClassName );
220
- $ customAttributeValue = $ customAttribute [AttributeValue:: VALUE ] ;
212
+ $ type = $ type ? $ type : TypeProcessor:: ANY_TYPE ;
221
213
222
- if ($ this ->typeProcessor ->isTypeAny ($ type ) || $ this ->typeProcessor ->isTypeSimple ($ type )
223
- || !is_array ($ customAttributeValue )
224
- ) {
225
- try {
226
- $ attributeValue = $ this ->convertValue ($ customAttributeValue , $ type );
227
- } catch (SerializationException $ e ) {
228
- throw new SerializationException (
229
- new Phrase (
230
- 'Attribute "%attribute_code" has invalid value. %details ' ,
231
- ['attribute_code ' => $ customAttributeCode , 'details ' => $ e ->getMessage ()]
232
- )
233
- );
214
+ if (is_array ($ customAttributeValue )) {
215
+ //If type for AttributeValue's value as array is mixed, further processing is not possible
216
+ if ($ type === TypeProcessor::ANY_TYPE ) {
217
+ $ attributeValue = $ customAttributeValue ;
218
+ } else {
219
+ $ attributeValue = $ this ->_createDataObjectForTypeAndArrayValue ($ type , $ customAttributeValue );
234
220
}
235
221
} else {
236
- $ attributeValue = $ this ->_createDataObjectForTypeAndArrayValue ( $ type , $ customAttributeValue );
222
+ $ attributeValue = $ this ->convertValue ( $ customAttributeValue , $ type );
237
223
}
238
224
//Populate the attribute value data object once the value for custom attribute is derived based on type
239
225
$ result [$ customAttributeCode ] = $ this ->attributeValueFactory ->create ()
@@ -244,6 +230,39 @@ protected function convertCustomAttributeValue($customAttributesValueArray, $dat
244
230
return $ result ;
245
231
}
246
232
233
+ /**
234
+ * Derive the custom attribute code and value.
235
+ *
236
+ * @param string[] $customAttribute
237
+ * @return string[]
238
+ */
239
+ private function processCustomAttribute ($ customAttribute )
240
+ {
241
+ $ camelCaseAttributeCodeKey = lcfirst (
242
+ SimpleDataObjectConverter::snakeCaseToUpperCamelCase (AttributeValue::ATTRIBUTE_CODE )
243
+ );
244
+ // attribute code key could be snake or camel case, depending on whether SOAP or REST is used.
245
+ if (isset ($ customAttribute [AttributeValue::ATTRIBUTE_CODE ])) {
246
+ $ customAttributeCode = $ customAttribute [AttributeValue::ATTRIBUTE_CODE ];
247
+ } elseif (isset ($ customAttribute [$ camelCaseAttributeCodeKey ])) {
248
+ $ customAttributeCode = $ customAttribute [$ camelCaseAttributeCodeKey ];
249
+ } else {
250
+ $ customAttributeCode = null ;
251
+ }
252
+
253
+ if (!$ customAttributeCode && !isset ($ customAttribute [AttributeValue::VALUE ])) {
254
+ throw new SerializationException (new Phrase ('There is an empty custom attribute specified. ' ));
255
+ } else if (!$ customAttributeCode ) {
256
+ throw new SerializationException (new Phrase ('A custom attribute is specified without an attribute code. ' ));
257
+ } else if (!isset ($ customAttribute [AttributeValue::VALUE ])) {
258
+ throw new SerializationException (
259
+ new Phrase ('Value is not set for attribute code " ' . $ customAttributeCode . '" ' )
260
+ );
261
+ }
262
+
263
+ return [$ customAttributeCode , $ customAttribute [AttributeValue::VALUE ]];
264
+ }
265
+
247
266
/**
248
267
* Creates a data object type from a given type name and a PHP array.
249
268
*
0 commit comments