ToDataModel.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\SalesRule\Model\Converter;
  7. use Magento\SalesRule\Api\Data\RuleExtensionFactory;
  8. use Magento\SalesRule\Api\Data\RuleExtensionInterface;
  9. use Magento\SalesRule\Model\Data\Condition;
  10. use Magento\SalesRule\Api\Data\RuleInterface;
  11. use Magento\SalesRule\Model\Data\Rule as RuleDataModel;
  12. use Magento\SalesRule\Model\Rule;
  13. use Magento\Framework\Serialize\Serializer\Json;
  14. class ToDataModel
  15. {
  16. /**
  17. * @var \Magento\SalesRule\Model\RuleFactory
  18. */
  19. protected $ruleFactory;
  20. /**
  21. * @var \Magento\SalesRule\Api\Data\RuleInterfaceFactory
  22. */
  23. protected $ruleDataFactory;
  24. /**
  25. * @var \Magento\SalesRule\Api\Data\ConditionInterfaceFactory
  26. */
  27. protected $conditionDataFactory;
  28. /**
  29. * @var \Magento\Framework\Reflection\DataObjectProcessor
  30. */
  31. protected $dataObjectProcessor;
  32. /**
  33. * @var \Magento\SalesRule\Api\Data\RuleLabelInterfaceFactory
  34. */
  35. protected $ruleLabelFactory;
  36. /**
  37. * @var Json $serializer
  38. */
  39. private $serializer;
  40. /**
  41. * @var RuleExtensionFactory
  42. */
  43. private $extensionFactory;
  44. /**
  45. * @param \Magento\SalesRule\Model\RuleFactory $ruleFactory
  46. * @param \Magento\SalesRule\Api\Data\RuleInterfaceFactory $ruleDataFactory
  47. * @param \Magento\SalesRule\Api\Data\ConditionInterfaceFactory $conditionDataFactory
  48. * @param \Magento\SalesRule\Api\Data\RuleLabelInterfaceFactory $ruleLabelFactory
  49. * @param \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor
  50. * @param Json $serializer Optional parameter for backward compatibility
  51. * @param RuleExtensionFactory|null $extensionFactory
  52. */
  53. public function __construct(
  54. \Magento\SalesRule\Model\RuleFactory $ruleFactory,
  55. \Magento\SalesRule\Api\Data\RuleInterfaceFactory $ruleDataFactory,
  56. \Magento\SalesRule\Api\Data\ConditionInterfaceFactory $conditionDataFactory,
  57. \Magento\SalesRule\Api\Data\RuleLabelInterfaceFactory $ruleLabelFactory,
  58. \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor,
  59. Json $serializer = null,
  60. RuleExtensionFactory $extensionFactory = null
  61. ) {
  62. $this->ruleFactory = $ruleFactory;
  63. $this->ruleDataFactory = $ruleDataFactory;
  64. $this->conditionDataFactory = $conditionDataFactory;
  65. $this->ruleLabelFactory = $ruleLabelFactory;
  66. $this->dataObjectProcessor = $dataObjectProcessor;
  67. $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()->get(Json::class);
  68. $this->extensionFactory = $extensionFactory ?:
  69. \Magento\Framework\App\ObjectManager::getInstance()->get(RuleExtensionFactory::class);
  70. }
  71. /**
  72. * Converts Sale Rule model to Sale Rule DTO
  73. *
  74. * @param Rule $ruleModel
  75. * @return RuleDataModel
  76. */
  77. public function toDataModel(Rule $ruleModel)
  78. {
  79. $modelData = $ruleModel->getData();
  80. $modelData = $this->convertExtensionAttributesToObject($modelData);
  81. /** @var \Magento\SalesRule\Model\Data\Rule $dataModel */
  82. $dataModel = $this->ruleDataFactory->create(['data' => $modelData]);
  83. $this->mapFields($dataModel, $ruleModel);
  84. return $dataModel;
  85. }
  86. /**
  87. * @param RuleDataModel $dataModel
  88. * @param Rule $ruleModel
  89. * @return $this
  90. */
  91. protected function mapConditions(RuleDataModel $dataModel, Rule $ruleModel)
  92. {
  93. $conditionSerialized = $ruleModel->getConditionsSerialized();
  94. if ($conditionSerialized) {
  95. $conditionArray = $this->serializer->unserialize($conditionSerialized);
  96. $conditionDataModel = $this->arrayToConditionDataModel($conditionArray);
  97. $dataModel->setCondition($conditionDataModel);
  98. } else {
  99. $dataModel->setCondition(null);
  100. }
  101. return $this;
  102. }
  103. /**
  104. * @param RuleDataModel $dataModel
  105. * @param Rule $ruleModel
  106. * @return $this
  107. */
  108. protected function mapActionConditions(RuleDataModel $dataModel, Rule $ruleModel)
  109. {
  110. $actionConditionSerialized = $ruleModel->getActionsSerialized();
  111. if ($actionConditionSerialized) {
  112. $actionConditionArray = $this->serializer->unserialize($actionConditionSerialized);
  113. $actionConditionDataModel = $this->arrayToConditionDataModel($actionConditionArray);
  114. $dataModel->setActionCondition($actionConditionDataModel);
  115. } else {
  116. $dataModel->setActionCondition(null);
  117. }
  118. return $this;
  119. }
  120. /**
  121. * @param RuleDataModel $dataModel
  122. * @return $this
  123. */
  124. protected function mapStoreLabels(RuleDataModel $dataModel)
  125. {
  126. //translate store labels into objects
  127. if ($dataModel->getStoreLabels() !== null) {
  128. $storeLabels = [];
  129. foreach ($dataModel->getStoreLabels() as $storeId => $storeLabel) {
  130. $storeLabelObj = $this->ruleLabelFactory->create();
  131. $storeLabelObj->setStoreId($storeId);
  132. $storeLabelObj->setStoreLabel($storeLabel);
  133. $storeLabels[] = $storeLabelObj;
  134. }
  135. $dataModel->setStoreLabels($storeLabels);
  136. }
  137. return $this;
  138. }
  139. /**
  140. * @param RuleDataModel $dataModel
  141. * @return $this
  142. */
  143. protected function mapCouponType(RuleDataModel $dataModel)
  144. {
  145. if ($dataModel->getCouponType()) {
  146. $mappedValue = '';
  147. switch ((int)$dataModel->getCouponType()) {
  148. case \Magento\SalesRule\Model\Rule::COUPON_TYPE_NO_COUPON:
  149. $mappedValue = RuleInterface::COUPON_TYPE_NO_COUPON;
  150. break;
  151. case \Magento\SalesRule\Model\Rule::COUPON_TYPE_SPECIFIC:
  152. $mappedValue = RuleInterface::COUPON_TYPE_SPECIFIC_COUPON;
  153. break;
  154. case \Magento\SalesRule\Model\Rule::COUPON_TYPE_AUTO:
  155. $mappedValue = RuleInterface::COUPON_TYPE_AUTO;
  156. break;
  157. default:
  158. }
  159. $dataModel->setCouponType($mappedValue);
  160. }
  161. return $this;
  162. }
  163. /**
  164. * Convert extension attributes of model to object if it is an array
  165. *
  166. * @param array $data
  167. * @return array
  168. */
  169. private function convertExtensionAttributesToObject(array $data)
  170. {
  171. if (isset($data['extension_attributes']) && is_array($data['extension_attributes'])) {
  172. /** @var RuleExtensionInterface $attributes */
  173. $data['extension_attributes'] = $this->extensionFactory->create(['data' => $data['extension_attributes']]);
  174. }
  175. return $data;
  176. }
  177. /**
  178. * @param RuleDataModel $dataModel
  179. * @param Rule $ruleModel
  180. * @return $this
  181. */
  182. protected function mapFields(RuleDataModel $dataModel, Rule $ruleModel)
  183. {
  184. $this->mapConditions($dataModel, $ruleModel);
  185. $this->mapActionConditions($dataModel, $ruleModel);
  186. $this->mapStoreLabels($dataModel);
  187. $this->mapCouponType($dataModel);
  188. return $this;
  189. }
  190. /**
  191. * Convert recursive array into condition data model
  192. *
  193. * @param array $input
  194. * @return Condition
  195. */
  196. public function arrayToConditionDataModel(array $input)
  197. {
  198. /** @var \Magento\SalesRule\Model\Data\Condition $conditionDataModel */
  199. $conditionDataModel = $this->conditionDataFactory->create();
  200. foreach ($input as $key => $value) {
  201. switch ($key) {
  202. case 'type':
  203. $conditionDataModel->setConditionType($value);
  204. break;
  205. case 'attribute':
  206. $conditionDataModel->setAttributeName($value);
  207. break;
  208. case 'operator':
  209. $conditionDataModel->setOperator($value);
  210. break;
  211. case 'value':
  212. $conditionDataModel->setValue($value);
  213. break;
  214. case 'aggregator':
  215. $conditionDataModel->setAggregatorType($value);
  216. break;
  217. case 'conditions':
  218. $conditions = [];
  219. foreach ($value as $condition) {
  220. $conditions[] = $this->arrayToConditionDataModel($condition);
  221. }
  222. $conditionDataModel->setConditions($conditions);
  223. break;
  224. default:
  225. }
  226. }
  227. return $conditionDataModel;
  228. }
  229. }