Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
RowParser.php
Go to the documentation of this file.
1 <?php
8 
11 
12 class RowParser
13 {
17  private $locationDirectory;
18 
23  public function __construct(LocationDirectory $locationDirectory)
24  {
25  $this->locationDirectory = $locationDirectory;
26  }
27 
31  public function getColumns()
32  {
33  return [
34  'website_id',
35  'dest_country_id',
36  'dest_region_id',
37  'dest_zip',
38  'condition_name',
39  'condition_value',
40  'price',
41  ];
42  }
43 
55  public function parse(
56  array $rowData,
57  $rowNumber,
58  $websiteId,
59  $conditionShortName,
60  $conditionFullName,
61  ColumnResolver $columnResolver
62  ) {
63  // validate row
64  if (count($rowData) < 5) {
65  throw new RowException(
66  __(
67  'The Table Rates File Format is incorrect in row number "%1". Verify the format and try again.',
68  $rowNumber
69  )
70  );
71  }
72 
73  $countryId = $this->getCountryId($rowData, $rowNumber, $columnResolver);
74  $regionId = $this->getRegionId($rowData, $rowNumber, $columnResolver, $countryId);
75  $zipCode = $this->getZipCode($rowData, $columnResolver);
76  $conditionValue = $this->getConditionValue($rowData, $rowNumber, $conditionFullName, $columnResolver);
77  $price = $this->getPrice($rowData, $rowNumber, $columnResolver);
78 
79  return [
80  'website_id' => $websiteId,
81  'dest_country_id' => $countryId,
82  'dest_region_id' => $regionId,
83  'dest_zip' => $zipCode,
84  'condition_name' => $conditionShortName,
85  'condition_value' => $conditionValue,
86  'price' => $price,
87  ];
88  }
89 
98  private function getCountryId(array $rowData, $rowNumber, ColumnResolver $columnResolver)
99  {
100  $countryCode = $columnResolver->getColumnValue(ColumnResolver::COLUMN_COUNTRY, $rowData);
101  // validate country
102  if ($this->locationDirectory->hasCountryId($countryCode)) {
103  $countryId = $this->locationDirectory->getCountryId($countryCode);
104  } elseif ($countryCode === '*' || $countryCode === '') {
105  $countryId = '0';
106  } else {
107  throw new RowException(
108  __(
109  'The "%1" country in row number "%2" is incorrect. Verify the country and try again.',
110  $countryCode,
111  $rowNumber
112  )
113  );
114  }
115  return $countryId;
116  }
117 
127  private function getRegionId(array $rowData, $rowNumber, ColumnResolver $columnResolver, $countryId)
128  {
129  $regionCode = $columnResolver->getColumnValue(ColumnResolver::COLUMN_REGION, $rowData);
130  if ($countryId !== '0' && $this->locationDirectory->hasRegionId($countryId, $regionCode)) {
131  $regionId = $this->locationDirectory->getRegionId($countryId, $regionCode);
132  } elseif ($regionCode === '*' || $regionCode === '') {
133  $regionId = 0;
134  } else {
135  throw new RowException(
136  __(
137  'The "%1" region or state in row number "%2" is incorrect. '
138  . 'Verify the region or state and try again.',
139  $regionCode,
140  $rowNumber
141  )
142  );
143  }
144  return $regionId;
145  }
146 
153  private function getZipCode(array $rowData, ColumnResolver $columnResolver)
154  {
155  $zipCode = $columnResolver->getColumnValue(ColumnResolver::COLUMN_ZIP, $rowData);
156  if ($zipCode === '') {
157  $zipCode = '*';
158  }
159  return $zipCode;
160  }
161 
171  private function getConditionValue(array $rowData, $rowNumber, $conditionFullName, ColumnResolver $columnResolver)
172  {
173  // validate condition value
174  $conditionValue = $columnResolver->getColumnValue($conditionFullName, $rowData);
175  $value = $this->_parseDecimalValue($conditionValue);
176  if ($value === false) {
177  throw new RowException(
178  __(
179  'Please correct %1 "%2" in the Row #%3.',
180  $conditionFullName,
181  $conditionValue,
182  $rowNumber
183  )
184  );
185  }
186  return $value;
187  }
188 
197  private function getPrice(array $rowData, $rowNumber, ColumnResolver $columnResolver)
198  {
199  $priceValue = $columnResolver->getColumnValue(ColumnResolver::COLUMN_PRICE, $rowData);
200  $price = $this->_parseDecimalValue($priceValue);
201  if ($price === false) {
202  throw new RowException(
203  __(
204  'The "%1" shipping price in row number "%2" is incorrect. Verify the shipping price and try again.',
205  $priceValue,
206  $rowNumber
207  )
208  );
209  }
210  return $price;
211  }
212 
220  private function _parseDecimalValue($value)
221  {
222  $result = false;
223  if (is_numeric($value)) {
224  $value = (double)sprintf('%.4F', $value);
225  if ($value >= 0.0000) {
226  $result = $value;
227  }
228  }
229  return $result;
230  }
231 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__()
Definition: __.php:13
$price
$value
Definition: gender.phtml:16
parse(array $rowData, $rowNumber, $websiteId, $conditionShortName, $conditionFullName, ColumnResolver $columnResolver)
Definition: RowParser.php:55