ToModel.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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\Model\Data\Condition;
  8. use Magento\SalesRule\Api\Data\RuleInterface;
  9. use Magento\SalesRule\Model\Data\Rule as RuleDataModel;
  10. use Magento\SalesRule\Model\Rule;
  11. class ToModel
  12. {
  13. /**
  14. * @var \Magento\SalesRule\Model\RuleFactory
  15. */
  16. protected $ruleFactory;
  17. /**
  18. * @var \Magento\Framework\Reflection\DataObjectProcessor
  19. */
  20. protected $dataObjectProcessor;
  21. /**
  22. * @param \Magento\SalesRule\Model\RuleFactory $ruleFactory
  23. * @param \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor
  24. */
  25. public function __construct(
  26. \Magento\SalesRule\Model\RuleFactory $ruleFactory,
  27. \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor
  28. ) {
  29. $this->ruleFactory = $ruleFactory;
  30. $this->dataObjectProcessor = $dataObjectProcessor;
  31. }
  32. /**
  33. * @param \Magento\SalesRule\Model\Rule $ruleModel
  34. * @param RuleDataModel $dataModel
  35. * @return $this
  36. */
  37. protected function mapConditions(\Magento\SalesRule\Model\Rule $ruleModel, RuleDataModel $dataModel)
  38. {
  39. $condition = $dataModel->getCondition();
  40. if ($condition) {
  41. $array = $this->dataModelToArray($condition);
  42. $ruleModel->getConditions()->setConditions([])->loadArray($array);
  43. }
  44. return $this;
  45. }
  46. /**
  47. * @param \Magento\SalesRule\Model\Rule $ruleModel
  48. * @param RuleDataModel $dataModel
  49. * @return $this
  50. */
  51. protected function mapActionConditions(\Magento\SalesRule\Model\Rule $ruleModel, RuleDataModel $dataModel)
  52. {
  53. $condition = $dataModel->getActionCondition();
  54. if ($condition) {
  55. $array = $this->dataModelToArray($condition, 'actions');
  56. $ruleModel->getActions()->setActions([])->loadArray($array, 'actions');
  57. }
  58. return $this;
  59. }
  60. /**
  61. * @param Rule $ruleModel
  62. * @param RuleDataModel $dataModel
  63. * @return $this
  64. */
  65. protected function mapStoreLabels(Rule $ruleModel, RuleDataModel $dataModel)
  66. {
  67. //translate store labels object into array
  68. if ($dataModel->getStoreLabels() !== null) {
  69. $storeLabels = [];
  70. /** @var \Magento\SalesRule\Api\Data\RuleLabelInterface $ruleLabel */
  71. foreach ($dataModel->getStoreLabels() as $ruleLabel) {
  72. $storeLabels[$ruleLabel->getStoreId()] = $ruleLabel->getStoreLabel();
  73. }
  74. $ruleModel->setStoreLabels($storeLabels);
  75. }
  76. return $this;
  77. }
  78. /**
  79. * @param Rule $ruleModel
  80. * @return $this
  81. */
  82. protected function mapCouponType(Rule $ruleModel)
  83. {
  84. if ($ruleModel->getCouponType()) {
  85. $mappedValue = '';
  86. switch ($ruleModel->getCouponType()) {
  87. case RuleInterface::COUPON_TYPE_NO_COUPON:
  88. $mappedValue = \Magento\SalesRule\Model\Rule::COUPON_TYPE_NO_COUPON;
  89. break;
  90. case RuleInterface::COUPON_TYPE_SPECIFIC_COUPON:
  91. $mappedValue = \Magento\SalesRule\Model\Rule::COUPON_TYPE_SPECIFIC;
  92. break;
  93. case RuleInterface::COUPON_TYPE_AUTO:
  94. $mappedValue = \Magento\SalesRule\Model\Rule::COUPON_TYPE_AUTO;
  95. break;
  96. default:
  97. }
  98. $ruleModel->setCouponType($mappedValue);
  99. }
  100. return $this;
  101. }
  102. /**
  103. * @param \Magento\SalesRule\Model\Rule $ruleModel
  104. * @param RuleDataModel $dataModel
  105. * @return $this
  106. */
  107. protected function mapFields(\Magento\SalesRule\Model\Rule $ruleModel, RuleDataModel $dataModel)
  108. {
  109. $this->mapConditions($ruleModel, $dataModel);
  110. $this->mapActionConditions($ruleModel, $dataModel);
  111. $this->mapStoreLabels($ruleModel, $dataModel);
  112. $this->mapCouponType($ruleModel);
  113. return $this;
  114. }
  115. /**
  116. * Convert recursive array into condition data model
  117. *
  118. * @param Condition $condition
  119. * @param string $key
  120. * @return array
  121. */
  122. public function dataModelToArray(Condition $condition, $key = 'conditions')
  123. {
  124. $output = [];
  125. $output['type'] = $condition->getConditionType();
  126. $output['value'] = $condition->getValue();
  127. $output['attribute'] = $condition->getAttributeName();
  128. $output['operator'] = $condition->getOperator();
  129. if ($condition->getAggregatorType()) {
  130. $output['aggregator'] = $condition->getAggregatorType();
  131. }
  132. if ($condition->getConditions()) {
  133. $conditions = [];
  134. foreach ($condition->getConditions() as $subCondition) {
  135. $conditions[] = $this->dataModelToArray($subCondition, $key);
  136. }
  137. $output[$key] = $conditions;
  138. }
  139. return $output;
  140. }
  141. /**
  142. * @param RuleDataModel $dataModel
  143. * @return $this|Rule
  144. * @throws \Magento\Framework\Exception\NoSuchEntityException
  145. * @throws \Magento\Framework\Exception\InputException
  146. */
  147. public function toModel(RuleDataModel $dataModel)
  148. {
  149. $ruleId = $dataModel->getRuleId();
  150. if ($ruleId) {
  151. $ruleModel = $this->ruleFactory->create()->load($ruleId);
  152. if (!$ruleModel->getId()) {
  153. throw new \Magento\Framework\Exception\NoSuchEntityException();
  154. }
  155. } else {
  156. $ruleModel = $this->ruleFactory->create();
  157. $dataModel->setFromDate(
  158. $this->formattingDate($dataModel->getFromDate())
  159. );
  160. $dataModel->setToDate(
  161. $this->formattingDate($dataModel->getToDate())
  162. );
  163. }
  164. $modelData = $ruleModel->getData();
  165. $data = $this->dataObjectProcessor->buildOutputDataArray(
  166. $dataModel,
  167. \Magento\SalesRule\Api\Data\RuleInterface::class
  168. );
  169. $mergedData = array_merge($modelData, $data);
  170. $validateResult = $ruleModel->validateData(new \Magento\Framework\DataObject($mergedData));
  171. if ($validateResult !== true) {
  172. $text = '';
  173. /** @var \Magento\Framework\Phrase $errorMessage */
  174. foreach ($validateResult as $errorMessage) {
  175. $text .= $errorMessage->getText();
  176. $text .= '; ';
  177. }
  178. throw new \Magento\Framework\Exception\InputException(new \Magento\Framework\Phrase($text));
  179. }
  180. $ruleModel->setData($mergedData);
  181. $this->mapFields($ruleModel, $dataModel);
  182. return $ruleModel;
  183. }
  184. /**
  185. * Convert date to ISO8601
  186. *
  187. * @param string|null $date
  188. * @return string|null
  189. */
  190. private function formattingDate($date)
  191. {
  192. if ($date) {
  193. $fromDate = new \DateTime($date);
  194. $date = $fromDate->format(\DateTime::ISO8601);
  195. }
  196. return $date;
  197. }
  198. }