@@ -13,13 +13,6 @@ class DataObjectHelper
13
13
*/
14
14
protected $ objectFactory ;
15
15
16
- /**
17
- * Cached custom attribute codes by metadataServiceInterface
18
- *
19
- * @var array
20
- */
21
- protected $ customAttributesCodes = [];
22
-
23
16
/**
24
17
* @var \Magento\Framework\Reflection\DataObjectProcessor
25
18
*/
@@ -48,50 +41,24 @@ public function __construct(
48
41
/**
49
42
* @param ExtensibleDataInterface $dataObject
50
43
* @param array $data
44
+ * @param string $interfaceName
51
45
* @return $this
52
46
*/
53
- public function populateWithArray (ExtensibleDataInterface $ dataObject , array $ data )
47
+ public function populateWithArray (ExtensibleDataInterface $ dataObject , array $ data, $ interfaceName = null )
54
48
{
55
- $ this ->_setDataValues ($ dataObject , $ data );
49
+ $ this ->_setDataValues ($ dataObject , $ data, $ interfaceName );
56
50
return $ this ;
57
51
}
58
52
59
- /**
60
- * Template method used to configure the attribute codes for the custom attributes
61
- *
62
- * @param ExtensibleDataInterface $dataObject
63
- * @param MetadataServiceInterface metadataService
64
- * @return string[]
65
- */
66
- public function getCustomAttributesCodes (
67
- ExtensibleDataInterface $ dataObject ,
68
- MetadataServiceInterface $ metadataService
69
- ) {
70
- $ metadataServiceClass = get_class ($ metadataService );
71
- if (isset ($ this ->customAttributesCodes [$ metadataServiceClass ])) {
72
- return $ this ->customAttributesCodes [$ metadataServiceClass ];
73
- }
74
- $ attributeCodes = [];
75
- /** @var \Magento\Framework\Api\MetadataObjectInterface[] $customAttributesMetadata */
76
- $ customAttributesMetadata = $ metadataService
77
- ->getCustomAttributesMetadata (get_class ($ dataObject ));
78
- if (is_array ($ customAttributesMetadata )) {
79
- foreach ($ customAttributesMetadata as $ attribute ) {
80
- $ attributeCodes [] = $ attribute ->getAttributeCode ();
81
- }
82
- }
83
- $ this ->customAttributesCodes [$ metadataServiceClass ] = $ attributeCodes ;
84
- return $ attributeCodes ;
85
- }
86
-
87
53
/**
88
54
* Update Data Object with the data from array
89
55
*
90
56
* @param ExtensibleDataInterface $dataObject
91
57
* @param array $data
58
+ * @param string $interfaceName
92
59
* @return $this
93
60
*/
94
- protected function _setDataValues (ExtensibleDataInterface $ dataObject , array $ data )
61
+ protected function _setDataValues (ExtensibleDataInterface $ dataObject , array $ data, $ interfaceName )
95
62
{
96
63
$ dataObjectMethods = get_class_methods (get_class ($ dataObject ));
97
64
foreach ($ data as $ key => $ value ) {
@@ -117,7 +84,7 @@ protected function _setDataValues(ExtensibleDataInterface $dataObject, array $da
117
84
$ dataObject ->$ methodName ($ value );
118
85
} else {
119
86
$ getterMethodName = 'get ' . $ camelCaseKey ;
120
- $ this ->setComplexValue ($ dataObject , $ getterMethodName , $ methodName , $ value );
87
+ $ this ->setComplexValue ($ dataObject , $ getterMethodName , $ methodName , $ value, $ interfaceName );
121
88
}
122
89
} else {
123
90
$ dataObject ->setCustomAttribute ($ key , $ value );
@@ -132,15 +99,20 @@ protected function _setDataValues(ExtensibleDataInterface $dataObject, array $da
132
99
* @param string $getterMethodName
133
100
* @param string $methodName
134
101
* @param array $value
102
+ * @param string $interfaceName
135
103
* @return $this
136
104
*/
137
105
protected function setComplexValue (
138
106
ExtensibleDataInterface $ dataObject ,
139
107
$ getterMethodName ,
140
108
$ methodName ,
141
- array $ value
109
+ array $ value ,
110
+ $ interfaceName
142
111
) {
143
- $ returnType = $ this ->objectProcessor ->getMethodReturnType (get_class ($ dataObject ), $ getterMethodName );
112
+ if ($ interfaceName == null ) {
113
+ $ interfaceName = get_class ($ dataObject );
114
+ }
115
+ $ returnType = $ this ->objectProcessor ->getMethodReturnType ($ interfaceName , $ getterMethodName );
144
116
if ($ this ->typeProcessor ->isTypeSimple ($ returnType )) {
145
117
$ dataObject ->$ methodName ($ value );
146
118
return $ this ;
@@ -151,7 +123,7 @@ protected function setComplexValue(
151
123
$ objects = [];
152
124
foreach ($ value as $ arrayElementData ) {
153
125
$ object = $ this ->objectFactory ->create ($ type , []);
154
- $ this ->populateWithArray ($ object , $ arrayElementData );
126
+ $ this ->populateWithArray ($ object , $ arrayElementData, $ type );
155
127
$ objects [] = $ object ;
156
128
}
157
129
$ dataObject ->$ methodName ($ objects );
@@ -160,7 +132,7 @@ protected function setComplexValue(
160
132
161
133
if (is_subclass_of ($ returnType , '\Magento\Framework\Api\ExtensibleDataInterface ' )) {
162
134
$ object = $ this ->objectFactory ->create ($ returnType , []);
163
- $ this ->populateWithArray ($ object , $ value );
135
+ $ this ->populateWithArray ($ object , $ value, $ returnType );
164
136
} else {
165
137
$ object = $ this ->objectFactory ->create ($ returnType , $ value );
166
138
}
@@ -171,22 +143,22 @@ protected function setComplexValue(
171
143
/**
172
144
* Merges second object onto the first
173
145
*
174
- * @param string $className
146
+ * @param string $interfaceName
175
147
* @param ExtensibleDataInterface $firstDataObject
176
148
* @param ExtensibleDataInterface $secondDataObject
177
149
* @return $this
178
150
* @throws \LogicException
179
151
*/
180
152
public function mergeDataObjects (
181
- $ className ,
153
+ $ interfaceName ,
182
154
ExtensibleDataInterface $ firstDataObject ,
183
155
ExtensibleDataInterface $ secondDataObject
184
156
) {
185
- if (!$ firstDataObject instanceof $ className || !$ secondDataObject instanceof $ className ) {
186
- throw new \LogicException ('Wrong prototype object given. It can only be of " ' . $ className . '" type. ' );
157
+ if (!$ firstDataObject instanceof $ interfaceName || !$ secondDataObject instanceof $ interfaceName ) {
158
+ throw new \LogicException ('Wrong prototype object given. It can only be of " ' . $ interfaceName . '" type. ' );
187
159
}
188
- $ secondObjectArray = $ this ->objectProcessor ->buildOutputDataArray ($ secondDataObject , $ className );
189
- $ this ->_setDataValues ($ firstDataObject , $ secondObjectArray );
160
+ $ secondObjectArray = $ this ->objectProcessor ->buildOutputDataArray ($ secondDataObject , $ interfaceName );
161
+ $ this ->_setDataValues ($ firstDataObject , $ secondObjectArray, $ interfaceName );
190
162
return $ this ;
191
163
}
192
164
}
0 commit comments