Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Converter.php
Go to the documentation of this file.
1 <?php
7 
10 
16 class Converter
17 {
22 
27 
31  private $format;
32 
38  public function __construct(
39  \Magento\Tax\Api\Data\TaxRateInterfaceFactory $taxRateDataObjectFactory,
40  \Magento\Tax\Api\Data\TaxRateTitleInterfaceFactory $taxRateTitleDataObjectFactory,
41  FormatInterface $format = null
42  ) {
43  $this->taxRateDataObjectFactory = $taxRateDataObjectFactory;
44  $this->taxRateTitleDataObjectFactory = $taxRateTitleDataObjectFactory;
45  $this->format = $format ?: ObjectManager::getInstance()->get(FormatInterface::class);
46  }
47 
55  {
56  $titles = $taxRate->getTitles();
57  $titleData = [];
58  if ($titles) {
59  foreach ($titles as $title) {
60  $titleData[$title->getStoreId()] = $title->getValue();
61  }
62  }
63  return $titleData;
64  }
65 
76  \Magento\Tax\Api\Data\TaxRateInterface $taxRate,
77  $returnNumericLogic = false
78  ) {
79  $taxRateFormData = [
80  'tax_calculation_rate_id' => $taxRate->getId(),
81  'tax_country_id' => $taxRate->getTaxCountryId(),
82  'tax_region_id' => $taxRate->getTaxRegionId(),
83  'tax_postcode' => $taxRate->getTaxPostcode(),
84  'code' => $taxRate->getCode(),
85  'rate' => $taxRate->getRate(),
86  'zip_is_range' => $returnNumericLogic ? 0 : false,
87  ];
88 
89  if ($taxRateFormData['tax_region_id'] === '0') {
90  $taxRateFormData['tax_region_id'] = '';
91  }
92 
93  if ($taxRate->getZipFrom() && $taxRate->getZipTo()) {
94  $taxRateFormData['zip_is_range'] = $returnNumericLogic ? 1 : true;
95  $taxRateFormData['zip_from'] = $taxRate->getZipFrom();
96  $taxRateFormData['zip_to'] = $taxRate->getZipTo();
97  }
98 
99  if ($returnNumericLogic) {
100  //format for the ajax on multiple sites titles
101  $titleArray=($this->createTitleArrayFromServiceObject($taxRate));
102  if (is_array($titleArray)) {
103  foreach ($titleArray as $storeId => $title) {
104  $taxRateFormData['title[' . $storeId . ']']=$title;
105  }
106  }
107  } else {
108  //format for the form array on multiple sites titles
109  $titleArray=($this->createTitleArrayFromServiceObject($taxRate));
110  if (is_array($titleArray)) {
111  $titleData = [];
112  foreach ($titleArray as $storeId => $title) {
113  $titleData[] = [$storeId => $title];
114  }
115  if (count($titleArray)>0) {
116  $taxRateFormData['title'] = $titleData;
117  }
118  }
119  }
120 
121  return $taxRateFormData;
122  }
123 
130  public function populateTaxRateData($formData)
131  {
132  $taxRate = $this->taxRateDataObjectFactory->create();
133  $taxRate->setId($this->extractFormData($formData, 'tax_calculation_rate_id'))
134  ->setTaxCountryId($this->extractFormData($formData, 'tax_country_id'))
135  ->setTaxRegionId($this->extractFormData($formData, 'tax_region_id'))
136  ->setTaxPostcode($this->extractFormData($formData, 'tax_postcode'))
137  ->setCode($this->extractFormData($formData, 'code'))
138  ->setRate($this->format->getNumber($this->extractFormData($formData, 'rate')));
139  if (isset($formData['zip_is_range']) && $formData['zip_is_range']) {
140  $taxRate->setZipFrom($this->extractFormData($formData, 'zip_from'))
141  ->setZipTo($this->extractFormData($formData, 'zip_to'))->setZipIsRange(1);
142  }
143 
144  if (isset($formData['title'])) {
145  $titles = [];
146  foreach ($formData['title'] as $storeId => $value) {
147  $titles[] = $this->taxRateTitleDataObjectFactory->create()->setStoreId($storeId)->setValue($value);
148  }
149  $taxRate->setTitles($titles);
150  }
151 
152  return $taxRate;
153  }
154 
162  protected function extractFormData($formData, $fieldName)
163  {
164  if (isset($formData[$fieldName])) {
165  return $formData[$fieldName];
166  }
167  return null;
168  }
169 }
$title
Definition: default.phtml:14
createArrayFromServiceObject(\Magento\Tax\Api\Data\TaxRateInterface $taxRate, $returnNumericLogic=false)
Definition: Converter.php:75
$value
Definition: gender.phtml:16
__construct(\Magento\Tax\Api\Data\TaxRateInterfaceFactory $taxRateDataObjectFactory, \Magento\Tax\Api\Data\TaxRateTitleInterfaceFactory $taxRateTitleDataObjectFactory, FormatInterface $format=null)
Definition: Converter.php:38
createTitleArrayFromServiceObject(\Magento\Tax\Api\Data\TaxRateInterface $taxRate)
Definition: Converter.php:54
$taxRate
Definition: tax_rule.php:12