|
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 | 87 | public function toDataModel(\Magento\SalesRule\Model\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 |
|
@@ -162,6 +178,21 @@ 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) : array |
| 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 | 198 | * @param \Magento\SalesRule\Model\Rule $ruleModel
|
|
0 commit comments