3
3
* Copyright © Magento, Inc. All rights reserved.
4
4
* See COPYING.txt for license details.
5
5
*/
6
+ declare (strict_types=1 );
6
7
7
8
namespace Magento \Framework \DataObject ;
8
9
10
+ use Magento \Framework \Api \AbstractSimpleObject ;
11
+ use Magento \Framework \Api \ExtensibleDataInterface ;
12
+ use Magento \Framework \Api \ExtensionAttributesFactory ;
13
+ use Magento \Framework \DataObject ;
14
+ use Magento \Framework \DataObject \Copy \Config ;
15
+ use Magento \Framework \Event \ManagerInterface ;
16
+
9
17
/**
10
18
* Utility class for copying data sets between objects
11
19
*/
12
20
class Copy
13
21
{
14
22
/**
15
- * @var \Magento\Framework\DataObject\Copy\ Config
23
+ * @var Config
16
24
*/
17
25
protected $ fieldsetConfig ;
18
26
19
27
/**
20
28
* Core event manager proxy
21
29
*
22
- * @var \Magento\Framework\Event\ ManagerInterface
30
+ * @var ManagerInterface
23
31
*/
24
32
protected $ eventManager = null ;
25
33
26
34
/**
27
- * @var \Magento\Framework\Api\ ExtensionAttributesFactory
35
+ * @var ExtensionAttributesFactory
28
36
*/
29
37
protected $ extensionAttributesFactory ;
30
38
31
39
/**
32
- * @param \Magento\Framework\Event\ ManagerInterface $eventManager
33
- * @param \Magento\Framework\DataObject\Copy\ Config $fieldsetConfig
34
- * @param \Magento\Framework\Api\ ExtensionAttributesFactory $extensionAttributesFactory
40
+ * @param ManagerInterface $eventManager
41
+ * @param Config $fieldsetConfig
42
+ * @param ExtensionAttributesFactory $extensionAttributesFactory
35
43
*/
36
44
public function __construct (
37
- \ Magento \ Framework \ Event \ ManagerInterface $ eventManager ,
38
- \ Magento \ Framework \ DataObject \ Copy \ Config $ fieldsetConfig ,
39
- \ Magento \ Framework \ Api \ ExtensionAttributesFactory $ extensionAttributesFactory
45
+ ManagerInterface $ eventManager ,
46
+ Config $ fieldsetConfig ,
47
+ ExtensionAttributesFactory $ extensionAttributesFactory
40
48
) {
41
49
$ this ->eventManager = $ eventManager ;
42
50
$ this ->fieldsetConfig = $ fieldsetConfig ;
@@ -51,10 +59,11 @@ public function __construct(
51
59
*
52
60
* @param string $fieldset
53
61
* @param string $aspect
54
- * @param array|\Magento\Framework\ DataObject $source
55
- * @param array|\Magento\Framework\ DataObject $target
62
+ * @param array|DataObject $source
63
+ * @param array|DataObject $target
56
64
* @param string $root
57
- * @return array|\Magento\Framework\DataObject|null the value of $target
65
+ *
66
+ * @return array|DataObject|null the value of $target
58
67
* @throws \InvalidArgumentException
59
68
*
60
69
* @api
@@ -93,17 +102,18 @@ public function copyFieldsetToTarget($fieldset, $aspect, $source, $target, $root
93
102
*
94
103
* @param string $fieldset
95
104
* @param string $aspect
96
- * @param array|\Magento\Framework\ DataObject $source
97
- * @param array|\Magento\Framework\ DataObject $target
105
+ * @param array|DataObject $source
106
+ * @param array|DataObject $target
98
107
* @param string $root
99
108
* @param bool $targetIsArray
100
- * @return \Magento\Framework\DataObject|mixed
109
+ *
110
+ * @return DataObject|mixed
101
111
*/
102
112
protected function dispatchCopyFieldSetEvent ($ fieldset , $ aspect , $ source , $ target , $ root , $ targetIsArray )
103
113
{
104
114
$ eventName = sprintf ('core_copy_fieldset_%s_%s ' , $ fieldset , $ aspect );
105
115
if ($ targetIsArray ) {
106
- $ target = new \ Magento \ Framework \ DataObject ($ target );
116
+ $ target = new DataObject ($ target );
107
117
}
108
118
$ this ->eventManager ->dispatch (
109
119
$ eventName ,
@@ -120,17 +130,19 @@ protected function dispatchCopyFieldSetEvent($fieldset, $aspect, $source, $targe
120
130
*
121
131
* @param string $fieldset
122
132
* @param string $aspect a field name
123
- * @param array|\Magento\Framework\ DataObject $source
133
+ * @param array|DataObject $source
124
134
* @param string $root
135
+ *
125
136
* @return array
126
137
*
127
138
* @api
128
139
*/
129
140
public function getDataFromFieldset ($ fieldset , $ aspect , $ source , $ root = 'global ' )
130
141
{
131
- if (!( is_array ( $ source ) || $ source instanceof \ Magento \ Framework \DataObject )) {
142
+ if ((! $ this -> isInputArgumentValid ( $ source ))) {
132
143
return null ;
133
144
}
145
+
134
146
$ fields = $ this ->fieldsetConfig ->getFieldset ($ fieldset , $ root );
135
147
if ($ fields === null ) {
136
148
return null ;
@@ -155,18 +167,28 @@ public function getDataFromFieldset($fieldset, $aspect, $source, $root = 'global
155
167
/**
156
168
* Check if source and target are valid input for converting using fieldset
157
169
*
158
- * @param array|\Magento\Framework\DataObject $source
159
- * @param array|\Magento\Framework\DataObject $target
170
+ * @param array|DataObject $source
171
+ * @param array|DataObject $target
172
+ *
160
173
* @return bool
161
174
*/
162
175
protected function _isFieldsetInputValid ($ source , $ target )
163
176
{
164
- return (is_array ($ source ) || $ source instanceof \Magento \Framework \DataObject ||
165
- $ source instanceof \Magento \Framework \Api \ExtensibleDataInterface ||
166
- $ source instanceof \Magento \Framework \Api \AbstractSimpleObject) && (
167
- is_array ($ target ) || $ target instanceof \Magento \Framework \DataObject ||
168
- $ target instanceof \Magento \Framework \Api \ExtensibleDataInterface ||
169
- $ target instanceof \Magento \Framework \Api \AbstractSimpleObject);
177
+ return $ this ->isInputArgumentValid ($ source ) && $ this ->isInputArgumentValid ($ target );
178
+ }
179
+
180
+ /**
181
+ * Verify that we can access data from input object.
182
+ *
183
+ * @param $object
184
+ *
185
+ * @return bool
186
+ */
187
+ private function isInputArgumentValid ($ object ): bool
188
+ {
189
+ return (is_array ($ object ) || $ object instanceof DataObject ||
190
+ $ object instanceof ExtensibleDataInterface ||
191
+ $ object instanceof AbstractSimpleObject);
170
192
}
171
193
172
194
/**
@@ -180,20 +202,27 @@ protected function _isFieldsetInputValid($source, $target)
180
202
*/
181
203
protected function _getFieldsetFieldValue ($ source , $ code )
182
204
{
183
- if (is_array ($ source )) {
184
- $ value = isset ($ source [$ code ]) ? $ source [$ code ] : null ;
185
- } elseif ($ source instanceof \Magento \Framework \DataObject) {
186
- $ value = $ source ->getDataUsingMethod ($ code );
187
- } elseif ($ source instanceof \Magento \Framework \Api \ExtensibleDataInterface) {
188
- $ value = $ this ->getAttributeValueFromExtensibleDataObject ($ source , $ code );
189
- } elseif ($ source instanceof \Magento \Framework \Api \AbstractSimpleObject) {
190
- $ sourceArray = $ source ->__toArray ();
191
- $ value = isset ($ sourceArray [$ code ]) ? $ sourceArray [$ code ] : null ;
192
- } else {
193
- throw new \InvalidArgumentException (
194
- 'Source should be array, Magento Object, ExtensibleDataInterface, or AbstractSimpleObject '
195
- );
205
+
206
+ switch (true ) {
207
+ case is_array ($ source ):
208
+ $ value = isset ($ source [$ code ]) ? $ source [$ code ] : null ;
209
+ break ;
210
+ case $ source instanceof ExtensibleDataInterface:
211
+ $ value = $ this ->getAttributeValueFromExtensibleObject ($ source , $ code );
212
+ break ;
213
+ case $ source instanceof DataObject:
214
+ $ value = $ source ->getDataUsingMethod ($ code );
215
+ break ;
216
+ case $ source instanceof AbstractSimpleObject:
217
+ $ sourceArray = $ source ->__toArray ();
218
+ $ value = isset ($ sourceArray [$ code ]) ? $ sourceArray [$ code ] : null ;
219
+ break ;
220
+ default :
221
+ throw new \InvalidArgumentException (
222
+ 'Source should be array, Magento Object, ExtensibleDataInterface, or AbstractSimpleObject '
223
+ );
196
224
}
225
+
197
226
return $ value ;
198
227
}
199
228
@@ -209,20 +238,23 @@ protected function _getFieldsetFieldValue($source, $code)
209
238
*/
210
239
protected function _setFieldsetFieldValue ($ target , $ targetCode , $ value )
211
240
{
212
- $ targetIsArray = is_array ($ target );
213
-
214
- if ($ targetIsArray ) {
215
- $ target [$ targetCode ] = $ value ;
216
- } elseif ($ target instanceof \Magento \Framework \DataObject) {
217
- $ target ->setDataUsingMethod ($ targetCode , $ value );
218
- } elseif ($ target instanceof \Magento \Framework \Api \ExtensibleDataInterface) {
219
- $ this ->setAttributeValueFromExtensibleDataObject ($ target , $ targetCode , $ value );
220
- } elseif ($ target instanceof \Magento \Framework \Api \AbstractSimpleObject) {
221
- $ target ->setData ($ targetCode , $ value );
222
- } else {
223
- throw new \InvalidArgumentException (
224
- 'Source should be array, Magento Object, ExtensibleDataInterface, or AbstractSimpleObject '
225
- );
241
+ switch (true ) {
242
+ case is_array ($ target ):
243
+ $ target [$ targetCode ] = $ value ;
244
+ break ;
245
+ case $ target instanceof ExtensibleDataInterface:
246
+ $ this ->setAttributeValueFromExtensibleObject ($ target , $ targetCode , $ value );
247
+ break ;
248
+ case $ target instanceof DataObject:
249
+ $ target ->setDataUsingMethod ($ targetCode , $ value );
250
+ break ;
251
+ case $ target instanceof AbstractSimpleObject:
252
+ $ target ->setData ($ targetCode , $ value );
253
+ break ;
254
+ default :
255
+ throw new \InvalidArgumentException (
256
+ 'Source should be array, Magento Object, ExtensibleDataInterface, or AbstractSimpleObject '
257
+ );
226
258
}
227
259
228
260
return $ target ;
@@ -231,68 +263,119 @@ protected function _setFieldsetFieldValue($target, $targetCode, $value)
231
263
/**
232
264
* Access the extension get method
233
265
*
234
- * @param \Magento\Framework\Api\ ExtensibleDataInterface $source
266
+ * @param ExtensibleDataInterface $source
235
267
* @param string $code
236
268
*
237
269
* @return mixed
238
270
* @throws \InvalidArgumentException
271
+ *
272
+ * @deprecated
273
+ * @see \Magento\Framework\DataObject\Copy::getAttributeValueFromExtensibleObject
239
274
*/
240
275
protected function getAttributeValueFromExtensibleDataObject ($ source , $ code )
276
+ {
277
+ return $ this ->getAttributeValueFromExtensibleObject ($ source , $ code );
278
+ }
279
+
280
+ /**
281
+ * Get Attribute Value from Extensible Object Data with fallback to DataObject or AbstractSimpleObject.
282
+ *
283
+ * @param ExtensibleDataInterface $source
284
+ * @param string $code
285
+ *
286
+ * @return mixed|null
287
+ */
288
+ private function getAttributeValueFromExtensibleObject (ExtensibleDataInterface $ source , string $ code )
241
289
{
242
290
$ method = 'get ' . str_replace (' ' , '' , ucwords (str_replace ('_ ' , ' ' , $ code )));
243
291
244
292
$ methodExists = method_exists ($ source , $ method );
245
- if ($ methodExists == true ) {
246
- $ value = $ source ->{$ method }();
247
- } else {
248
- // If we couldn't find the method, check if we can get it from the extension attributes
249
- $ extensionAttributes = $ source ->getExtensionAttributes ();
250
- if ($ extensionAttributes == null ) {
251
- throw new \InvalidArgumentException ('Method in extension does not exist. ' );
252
- } else {
253
- $ extensionMethodExists = method_exists ($ extensionAttributes , $ method );
254
- if ($ extensionMethodExists == true ) {
255
- $ value = $ extensionAttributes ->{$ method }();
256
- } else {
257
- throw new \InvalidArgumentException ('Attribute in object does not exist. ' );
258
- }
293
+
294
+ if ($ methodExists === true ) {
295
+ return $ source ->{$ method }();
296
+ }
297
+
298
+ $ extensionAttributes = $ source ->getExtensionAttributes ();
299
+
300
+ if ($ extensionAttributes ) {
301
+ $ methodExists = method_exists ($ extensionAttributes , $ method );
302
+ if ($ methodExists ) {
303
+ return $ extensionAttributes ->{$ method }();
259
304
}
260
305
}
261
- return $ value ;
306
+
307
+ if ($ source instanceof DataObject) {
308
+ return $ source ->getDataUsingMethod ($ code );
309
+ }
310
+
311
+ if ($ source instanceof AbstractSimpleObject) {
312
+ $ sourceArray = $ source ->__toArray ();
313
+ return isset ($ sourceArray [$ code ]) ? $ sourceArray [$ code ] : null ;
314
+ }
315
+
316
+ throw new \InvalidArgumentException ('Attribute in object does not exist. ' );
262
317
}
263
318
264
319
/**
265
320
* Access the extension set method
266
321
*
267
- * @param \Magento\Framework\Api\ ExtensibleDataInterface $target
322
+ * @param ExtensibleDataInterface $target
268
323
* @param string $code
269
324
* @param mixed $value
270
325
*
271
- * @return null
326
+ * @return void
327
+ * @throws \InvalidArgumentException
328
+ *
329
+ * @deprecated
330
+ * @see \Magento\Framework\DataObject\Copy::setAttributeValueFromExtensibleObject
331
+ */
332
+ protected function setAttributeValueFromExtensibleDataObject (ExtensibleDataInterface $ target , $ code , $ value )
333
+ {
334
+ $ this ->setAttributeValueFromExtensibleObject ($ target , $ code , $ value );
335
+ }
336
+
337
+ /**
338
+ * Set Attribute Value for Extensible Object Data with fallback to DataObject or AbstractSimpleObject.
339
+ *
340
+ * @param ExtensibleDataInterface $target
341
+ * @param string $code
342
+ * @param $value
343
+ *
344
+ * @return void
272
345
* @throws \InvalidArgumentException
273
346
*/
274
- protected function setAttributeValueFromExtensibleDataObject ( $ target , $ code , $ value )
347
+ private function setAttributeValueFromExtensibleObject ( ExtensibleDataInterface $ target , string $ code , $ value ): void
275
348
{
276
349
$ method = 'set ' . str_replace (' ' , '' , ucwords (str_replace ('_ ' , ' ' , $ code )));
277
350
278
351
$ methodExists = method_exists ($ target , $ method );
279
- if ($ methodExists == true ) {
352
+ if ($ methodExists ) {
280
353
$ target ->{$ method }($ value );
281
- } else {
282
- // If we couldn't find the method, check if we can set it from the extension attributes
283
- $ extensionAttributes = $ target ->getExtensionAttributes ();
284
- if ($ extensionAttributes == null ) {
285
- $ extensionAttributes = $ this ->extensionAttributesFactory ->create (get_class ($ target ));
286
- }
287
- $ extensionMethodExists = method_exists ($ extensionAttributes , $ method );
288
- if ($ extensionMethodExists == true ) {
289
- $ extensionAttributes ->{$ method }($ value );
290
- $ target ->setExtensionAttributes ($ extensionAttributes );
291
- } else {
292
- throw new \InvalidArgumentException ('Attribute in object does not exist. ' );
293
- }
354
+ return ;
355
+ }
356
+
357
+ $ extensionAttributes = $ target ->getExtensionAttributes ();
358
+ if ($ extensionAttributes === null ) {
359
+ $ extensionAttributes = $ this ->extensionAttributesFactory ->create (get_class ($ target ));
360
+ }
361
+
362
+ if (method_exists ($ extensionAttributes , $ method )) {
363
+ $ extensionAttributes ->{$ method }($ value );
364
+ $ target ->setExtensionAttributes ($ extensionAttributes );
365
+ return ;
366
+ }
367
+
368
+
369
+ if ($ target instanceof DataObject) {
370
+ $ target ->setDataUsingMethod ($ code , $ value );
371
+ return ;
372
+ }
373
+
374
+ if ($ target instanceof AbstractSimpleObject) {
375
+ $ target ->setData ($ code , $ value );
376
+ return ;
294
377
}
295
378
296
- return null ;
379
+ throw new \ InvalidArgumentException ( ' Attribute in object does not exist. ' ) ;
297
380
}
298
381
}
0 commit comments