Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ToDataModel.php
Go to the documentation of this file.
1 <?php
7 
8 use Magento\SalesRule\Api\Data\RuleExtensionFactory;
9 use Magento\SalesRule\Api\Data\RuleExtensionInterface;
12 use Magento\SalesRule\Model\Data\Rule as RuleDataModel;
15 
17 {
21  protected $ruleFactory;
22 
26  protected $ruleDataFactory;
27 
32 
37 
41  protected $ruleLabelFactory;
42 
46  private $serializer;
47 
51  private $extensionFactory;
52 
62  public function __construct(
63  \Magento\SalesRule\Model\RuleFactory $ruleFactory,
64  \Magento\SalesRule\Api\Data\RuleInterfaceFactory $ruleDataFactory,
65  \Magento\SalesRule\Api\Data\ConditionInterfaceFactory $conditionDataFactory,
66  \Magento\SalesRule\Api\Data\RuleLabelInterfaceFactory $ruleLabelFactory,
67  \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor,
68  Json $serializer = null,
69  RuleExtensionFactory $extensionFactory = null
70  ) {
71  $this->ruleFactory = $ruleFactory;
72  $this->ruleDataFactory = $ruleDataFactory;
73  $this->conditionDataFactory = $conditionDataFactory;
74  $this->ruleLabelFactory = $ruleLabelFactory;
75  $this->dataObjectProcessor = $dataObjectProcessor;
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);
79  }
80 
87  public function toDataModel(Rule $ruleModel)
88  {
89  $modelData = $ruleModel->getData();
90  $modelData = $this->convertExtensionAttributesToObject($modelData);
91 
93  $dataModel = $this->ruleDataFactory->create(['data' => $modelData]);
94 
95  $this->mapFields($dataModel, $ruleModel);
96 
97  return $dataModel;
98  }
99 
105  protected function mapConditions(RuleDataModel $dataModel, Rule $ruleModel)
106  {
107  $conditionSerialized = $ruleModel->getConditionsSerialized();
108  if ($conditionSerialized) {
109  $conditionArray = $this->serializer->unserialize($conditionSerialized);
110  $conditionDataModel = $this->arrayToConditionDataModel($conditionArray);
111  $dataModel->setCondition($conditionDataModel);
112  } else {
113  $dataModel->setCondition(null);
114  }
115  return $this;
116  }
117 
123  protected function mapActionConditions(RuleDataModel $dataModel, Rule $ruleModel)
124  {
125  $actionConditionSerialized = $ruleModel->getActionsSerialized();
126  if ($actionConditionSerialized) {
127  $actionConditionArray = $this->serializer->unserialize($actionConditionSerialized);
128  $actionConditionDataModel = $this->arrayToConditionDataModel($actionConditionArray);
129  $dataModel->setActionCondition($actionConditionDataModel);
130  } else {
131  $dataModel->setActionCondition(null);
132  }
133  return $this;
134  }
135 
140  protected function mapStoreLabels(RuleDataModel $dataModel)
141  {
142  //translate store labels into objects
143  if ($dataModel->getStoreLabels() !== null) {
144  $storeLabels = [];
145  foreach ($dataModel->getStoreLabels() as $storeId => $storeLabel) {
146  $storeLabelObj = $this->ruleLabelFactory->create();
147  $storeLabelObj->setStoreId($storeId);
148  $storeLabelObj->setStoreLabel($storeLabel);
149  $storeLabels[] = $storeLabelObj;
150  }
151  $dataModel->setStoreLabels($storeLabels);
152  }
153  return $this;
154  }
155 
160  protected function mapCouponType(RuleDataModel $dataModel)
161  {
162  if ($dataModel->getCouponType()) {
163  $mappedValue = '';
164  switch ((int)$dataModel->getCouponType()) {
165  case \Magento\SalesRule\Model\Rule::COUPON_TYPE_NO_COUPON:
167  break;
168  case \Magento\SalesRule\Model\Rule::COUPON_TYPE_SPECIFIC:
170  break;
171  case \Magento\SalesRule\Model\Rule::COUPON_TYPE_AUTO:
172  $mappedValue = RuleInterface::COUPON_TYPE_AUTO;
173  break;
174  default:
175  }
176  $dataModel->setCouponType($mappedValue);
177  }
178  return $this;
179  }
180 
187  private function convertExtensionAttributesToObject(array $data)
188  {
189  if (isset($data['extension_attributes']) && is_array($data['extension_attributes'])) {
191  $data['extension_attributes'] = $this->extensionFactory->create(['data' => $data['extension_attributes']]);
192  }
193  return $data;
194  }
195 
201  protected function mapFields(RuleDataModel $dataModel, Rule $ruleModel)
202  {
203  $this->mapConditions($dataModel, $ruleModel);
204  $this->mapActionConditions($dataModel, $ruleModel);
205  $this->mapStoreLabels($dataModel);
206  $this->mapCouponType($dataModel);
207  return $this;
208  }
209 
216  public function arrayToConditionDataModel(array $input)
217  {
219  $conditionDataModel = $this->conditionDataFactory->create();
220  foreach ($input as $key => $value) {
221  switch ($key) {
222  case 'type':
223  $conditionDataModel->setConditionType($value);
224  break;
225  case 'attribute':
226  $conditionDataModel->setAttributeName($value);
227  break;
228  case 'operator':
229  $conditionDataModel->setOperator($value);
230  break;
231  case 'value':
232  $conditionDataModel->setValue($value);
233  break;
234  case 'aggregator':
235  $conditionDataModel->setAggregatorType($value);
236  break;
237  case 'conditions':
238  $conditions = [];
239  foreach ($value as $condition) {
240  $conditions[] = $this->arrayToConditionDataModel($condition);
241  }
242  $conditionDataModel->setConditions($conditions);
243  break;
244  default:
245  }
246  }
247  return $conditionDataModel;
248  }
249 }
mapActionConditions(RuleDataModel $dataModel, Rule $ruleModel)
__construct(\Magento\SalesRule\Model\RuleFactory $ruleFactory, \Magento\SalesRule\Api\Data\RuleInterfaceFactory $ruleDataFactory, \Magento\SalesRule\Api\Data\ConditionInterfaceFactory $conditionDataFactory, \Magento\SalesRule\Api\Data\RuleLabelInterfaceFactory $ruleLabelFactory, \Magento\Framework\Reflection\DataObjectProcessor $dataObjectProcessor, Json $serializer=null, RuleExtensionFactory $extensionFactory=null)
Definition: ToDataModel.php:62
$value
Definition: gender.phtml:16
mapConditions(RuleDataModel $dataModel, Rule $ruleModel)
mapFields(RuleDataModel $dataModel, Rule $ruleModel)