/** * @param RuleDataModel $dataModel * @return $this|Rule * @throws \Magento\Framework\Exception\NoSuchEntityException * @throws \Magento\Framework\Exception\InputException */ public function toModel(RuleDataModel $dataModel) { $ruleId = $dataModel->getRuleId(); if ($ruleId) { $ruleModel = $this->ruleFactory->create()->load($ruleId); if (!$ruleModel->getId()) { throw new \Magento\Framework\Exception\NoSuchEntityException(); } } else { $ruleModel = $this->ruleFactory->create(); } $modelData = $ruleModel->getData(); $data = $this->dataObjectProcessor->buildOutputDataArray($dataModel, '\\Magento\\SalesRule\\Api\\Data\\RuleInterface'); $mergedData = array_merge($modelData, $data); $validateResult = $ruleModel->validateData(new \Magento\Framework\DataObject($mergedData)); if ($validateResult !== true) { $text = ''; /** @var \Magento\Framework\Phrase $errorMessage */ foreach ($validateResult as $errorMessage) { $text .= $errorMessage->getText(); $text .= '; '; } throw new \Magento\Framework\Exception\InputException(new \Magento\Framework\Phrase($text)); } $ruleModel->setData($mergedData); $this->mapFields($ruleModel, $dataModel); return $ruleModel; }
/** * @param RuleDataModel $dataModel * @return $this */ protected function mapCouponType(RuleDataModel $dataModel) { if ($dataModel->getCouponType()) { $mappedValue = ''; switch ((int) $dataModel->getCouponType()) { case \Magento\SalesRule\Model\Rule::COUPON_TYPE_NO_COUPON: $mappedValue = RuleInterface::COUPON_TYPE_NO_COUPON; break; case \Magento\SalesRule\Model\Rule::COUPON_TYPE_SPECIFIC: $mappedValue = RuleInterface::COUPON_TYPE_SPECIFIC_COUPON; break; case \Magento\SalesRule\Model\Rule::COUPON_TYPE_AUTO: $mappedValue = RuleInterface::COUPON_TYPE_AUTO; break; default: } $dataModel->setCouponType($mappedValue); } return $this; }