5
5
*/
6
6
namespace Magento \SalesRule \Model \Converter ;
7
7
8
+ use Magento \SalesRule \Api \Data \RuleExtensionFactory ;
9
+ use Magento \SalesRule \Api \Data \RuleExtensionInterface ;
8
10
use Magento \SalesRule \Model \Data \Condition ;
9
11
use Magento \SalesRule \Api \Data \RuleInterface ;
10
12
use Magento \SalesRule \Model \Data \Rule as RuleDataModel ;
@@ -43,38 +45,52 @@ class ToDataModel
43
45
*/
44
46
private $ serializer ;
45
47
48
+ /**
49
+ * @var RuleExtensionFactory
50
+ */
51
+ private $ extensionFactory ;
52
+
46
53
/**
47
54
* @param \Magento\SalesRule\Model\RuleFactory $ruleFactory
48
55
* @param \Magento\SalesRule\Api\Data\RuleInterfaceFactory $ruleDataFactory
49
56
* @param \Magento\SalesRule\Api\Data\ConditionInterfaceFactory $conditionDataFactory
50
57
* @param \Magento\SalesRule\Api\Data\RuleLabelInterfaceFactory $ruleLabelFactory
51
58
* @param \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor
52
59
* @param Json $serializer Optional parameter for backward compatibility
60
+ * @param RuleExtensionFactory|null $extensionFactory
53
61
*/
54
62
public function __construct (
55
63
\Magento \SalesRule \Model \RuleFactory $ ruleFactory ,
56
64
\Magento \SalesRule \Api \Data \RuleInterfaceFactory $ ruleDataFactory ,
57
65
\Magento \SalesRule \Api \Data \ConditionInterfaceFactory $ conditionDataFactory ,
58
66
\Magento \SalesRule \Api \Data \RuleLabelInterfaceFactory $ ruleLabelFactory ,
59
67
\Magento \Framework \Reflection \DataObjectProcessor $ dataObjectProcessor ,
60
- Json $ serializer = null
68
+ Json $ serializer = null ,
69
+ RuleExtensionFactory $ extensionFactory = null
61
70
) {
62
71
$ this ->ruleFactory = $ ruleFactory ;
63
72
$ this ->ruleDataFactory = $ ruleDataFactory ;
64
73
$ this ->conditionDataFactory = $ conditionDataFactory ;
65
74
$ this ->ruleLabelFactory = $ ruleLabelFactory ;
66
75
$ this ->dataObjectProcessor = $ dataObjectProcessor ;
67
76
$ this ->serializer = $ serializer ?: \Magento \Framework \App \ObjectManager::getInstance ()->get (Json::class);
77
+ $ this ->extensionFactory = $ extensionFactory ?:
78
+ \Magento \Framework \App \ObjectManager::getInstance ()->get (RuleExtensionFactory::class);
68
79
}
69
80
70
81
/**
82
+ * Converts Sale Rule model to Sale Rule DTO
83
+ *
71
84
* @param Rule $ruleModel
72
85
* @return RuleDataModel
73
86
*/
74
- public function toDataModel (\ Magento \ SalesRule \ Model \ Rule $ ruleModel )
87
+ public function toDataModel (Rule $ ruleModel )
75
88
{
89
+ $ modelData = $ ruleModel ->getData ();
90
+ $ modelData = $ this ->convertExtensionAttributesToObject ($ modelData );
91
+
76
92
/** @var \Magento\SalesRule\Model\Data\Rule $dataModel */
77
- $ dataModel = $ this ->ruleDataFactory ->create (['data ' => $ ruleModel -> getData () ]);
93
+ $ dataModel = $ this ->ruleDataFactory ->create (['data ' => $ modelData ]);
78
94
79
95
$ this ->mapFields ($ dataModel , $ ruleModel );
80
96
@@ -83,10 +99,10 @@ public function toDataModel(\Magento\SalesRule\Model\Rule $ruleModel)
83
99
84
100
/**
85
101
* @param RuleDataModel $dataModel
86
- * @param \Magento\SalesRule\Model\ Rule $ruleModel
102
+ * @param Rule $ruleModel
87
103
* @return $this
88
104
*/
89
- protected function mapConditions (RuleDataModel $ dataModel , \ Magento \ SalesRule \ Model \ Rule $ ruleModel )
105
+ protected function mapConditions (RuleDataModel $ dataModel , Rule $ ruleModel )
90
106
{
91
107
$ conditionSerialized = $ ruleModel ->getConditionsSerialized ();
92
108
if ($ conditionSerialized ) {
@@ -101,10 +117,10 @@ protected function mapConditions(RuleDataModel $dataModel, \Magento\SalesRule\Mo
101
117
102
118
/**
103
119
* @param RuleDataModel $dataModel
104
- * @param \Magento\SalesRule\Model\ Rule $ruleModel
120
+ * @param Rule $ruleModel
105
121
* @return $this
106
122
*/
107
- protected function mapActionConditions (RuleDataModel $ dataModel , \ Magento \ SalesRule \ Model \ Rule $ ruleModel )
123
+ protected function mapActionConditions (RuleDataModel $ dataModel , Rule $ ruleModel )
108
124
{
109
125
$ actionConditionSerialized = $ ruleModel ->getActionsSerialized ();
110
126
if ($ actionConditionSerialized ) {
@@ -162,12 +178,27 @@ protected function mapCouponType(RuleDataModel $dataModel)
162
178
return $ this ;
163
179
}
164
180
181
+ /**
182
+ * Convert extension attributes of model to object if it is an array
183
+ *
184
+ * @param array $data
185
+ * @return array
186
+ */
187
+ private function convertExtensionAttributesToObject (array $ data )
188
+ {
189
+ if (isset ($ data ['extension_attributes ' ]) && is_array ($ data ['extension_attributes ' ])) {
190
+ /** @var RuleExtensionInterface $attributes */
191
+ $ data ['extension_attributes ' ] = $ this ->extensionFactory ->create (['data ' => $ data ['extension_attributes ' ]]);
192
+ }
193
+ return $ data ;
194
+ }
195
+
165
196
/**
166
197
* @param RuleDataModel $dataModel
167
- * @param \Magento\SalesRule\Model\ Rule $ruleModel
198
+ * @param Rule $ruleModel
168
199
* @return $this
169
200
*/
170
- protected function mapFields (RuleDataModel $ dataModel , \ Magento \ SalesRule \ Model \ Rule $ ruleModel )
201
+ protected function mapFields (RuleDataModel $ dataModel , Rule $ ruleModel )
171
202
{
172
203
$ this ->mapConditions ($ dataModel , $ ruleModel );
173
204
$ this ->mapActionConditions ($ dataModel , $ ruleModel );
0 commit comments