99use Magento \Framework \Phrase ;
1010use Magento \OfflineShipping \Model \ResourceModel \Carrier \Tablerate \LocationDirectory ;
1111
12+ /**
13+ * Row parser.
14+ */
1215class RowParser
1316{
1417 /**
@@ -26,6 +29,8 @@ public function __construct(LocationDirectory $locationDirectory)
2629 }
2730
2831 /**
32+ * Retrieve columns.
33+ *
2934 * @return array
3035 */
3136 public function getColumns ()
@@ -42,6 +47,8 @@ public function getColumns()
4247 }
4348
4449 /**
50+ * Parse provided row data.
51+ *
4552 * @param array $rowData
4653 * @param int $rowNumber
4754 * @param int $websiteId
@@ -62,27 +69,39 @@ public function parse(
6269 ) {
6370 // validate row
6471 if (count ($ rowData ) < 5 ) {
65- throw new RowException (__ ('Please correct Table Rates format in the Row #%1. ' , $ rowNumber ));
72+ throw new RowException (
73+ __ (
74+ 'The Table Rates File Format is incorrect in row number "%1". Verify the format and try again. ' ,
75+ $ rowNumber
76+ )
77+ );
6678 }
6779
6880 $ countryId = $ this ->getCountryId ($ rowData , $ rowNumber , $ columnResolver );
69- $ regionId = $ this ->getRegionId ($ rowData , $ rowNumber , $ columnResolver , $ countryId );
81+ $ regionIds = $ this ->getRegionIds ($ rowData , $ rowNumber , $ columnResolver , $ countryId );
7082 $ zipCode = $ this ->getZipCode ($ rowData , $ columnResolver );
7183 $ conditionValue = $ this ->getConditionValue ($ rowData , $ rowNumber , $ conditionFullName , $ columnResolver );
7284 $ price = $ this ->getPrice ($ rowData , $ rowNumber , $ columnResolver );
7385
74- return [
75- 'website_id ' => $ websiteId ,
76- 'dest_country_id ' => $ countryId ,
77- 'dest_region_id ' => $ regionId ,
78- 'dest_zip ' => $ zipCode ,
79- 'condition_name ' => $ conditionShortName ,
80- 'condition_value ' => $ conditionValue ,
81- 'price ' => $ price ,
82- ];
86+ $ rates = [];
87+ foreach ($ regionIds as $ regionId ) {
88+ $ rates [] = [
89+ 'website_id ' => $ websiteId ,
90+ 'dest_country_id ' => $ countryId ,
91+ 'dest_region_id ' => $ regionId ,
92+ 'dest_zip ' => $ zipCode ,
93+ 'condition_name ' => $ conditionShortName ,
94+ 'condition_value ' => $ conditionValue ,
95+ 'price ' => $ price ,
96+ ];
97+ }
98+
99+ return $ rates ;
83100 }
84101
85102 /**
103+ * Get country id from provided row data.
104+ *
86105 * @param array $rowData
87106 * @param int $rowNumber
88107 * @param ColumnResolver $columnResolver
@@ -99,34 +118,53 @@ private function getCountryId(array $rowData, $rowNumber, ColumnResolver $column
99118 } elseif ($ countryCode === '* ' || $ countryCode === '' ) {
100119 $ countryId = '0 ' ;
101120 } else {
102- throw new RowException (__ ('Please correct Country "%1" in the Row #%2. ' , $ countryCode , $ rowNumber ));
121+ throw new RowException (
122+ __ (
123+ 'The "%1" country in row number "%2" is incorrect. Verify the country and try again. ' ,
124+ $ countryCode ,
125+ $ rowNumber
126+ )
127+ );
103128 }
129+
104130 return $ countryId ;
105131 }
106132
107133 /**
134+ * Retrieve region id from provided row data.
135+ *
108136 * @param array $rowData
109137 * @param int $rowNumber
110138 * @param ColumnResolver $columnResolver
111139 * @param int $countryId
112- * @return int|string
140+ * @return array
113141 * @throws ColumnNotFoundException
114142 * @throws RowException
115143 */
116- private function getRegionId (array $ rowData , $ rowNumber , ColumnResolver $ columnResolver , $ countryId )
144+ private function getRegionIds (array $ rowData , $ rowNumber , ColumnResolver $ columnResolver , $ countryId ): array
117145 {
118146 $ regionCode = $ columnResolver ->getColumnValue (ColumnResolver::COLUMN_REGION , $ rowData );
119147 if ($ countryId !== '0 ' && $ this ->locationDirectory ->hasRegionId ($ countryId , $ regionCode )) {
120- $ regionId = $ this ->locationDirectory ->getRegionId ($ countryId , $ regionCode );
148+ $ regionIds = $ this ->locationDirectory ->getRegionIds ($ countryId , $ regionCode );
121149 } elseif ($ regionCode === '* ' || $ regionCode === '' ) {
122- $ regionId = 0 ;
150+ $ regionIds = [ 0 ] ;
123151 } else {
124- throw new RowException (__ ('Please correct Region/State "%1" in the Row #%2. ' , $ regionCode , $ rowNumber ));
152+ throw new RowException (
153+ __ (
154+ 'The "%1" region or state in row number "%2" is incorrect. '
155+ . 'Verify the region or state and try again. ' ,
156+ $ regionCode ,
157+ $ rowNumber
158+ )
159+ );
125160 }
126- return $ regionId ;
161+
162+ return $ regionIds ;
127163 }
128164
129165 /**
166+ * Retrieve zip code from provided row data.
167+ *
130168 * @param array $rowData
131169 * @param ColumnResolver $columnResolver
132170 * @return float|int|null|string
@@ -138,10 +176,13 @@ private function getZipCode(array $rowData, ColumnResolver $columnResolver)
138176 if ($ zipCode === '' ) {
139177 $ zipCode = '* ' ;
140178 }
179+
141180 return $ zipCode ;
142181 }
143182
144183 /**
184+ * Get condition value form provided row data.
185+ *
145186 * @param array $rowData
146187 * @param int $rowNumber
147188 * @param string $conditionFullName
@@ -165,10 +206,13 @@ private function getConditionValue(array $rowData, $rowNumber, $conditionFullNam
165206 )
166207 );
167208 }
209+
168210 return $ value ;
169211 }
170212
171213 /**
214+ * Retrieve price from provided row data.
215+ *
172216 * @param array $rowData
173217 * @param int $rowNumber
174218 * @param ColumnResolver $columnResolver
@@ -181,13 +225,21 @@ private function getPrice(array $rowData, $rowNumber, ColumnResolver $columnReso
181225 $ priceValue = $ columnResolver ->getColumnValue (ColumnResolver::COLUMN_PRICE , $ rowData );
182226 $ price = $ this ->_parseDecimalValue ($ priceValue );
183227 if ($ price === false ) {
184- throw new RowException (__ ('Please correct Shipping Price "%1" in the Row #%2. ' , $ priceValue , $ rowNumber ));
228+ throw new RowException (
229+ __ (
230+ 'The "%1" shipping price in row number "%2" is incorrect. Verify the shipping price and try again. ' ,
231+ $ priceValue ,
232+ $ rowNumber
233+ )
234+ );
185235 }
236+
186237 return $ price ;
187238 }
188239
189240 /**
190241 * Parse and validate positive decimal value
242+ *
191243 * Return false if value is not decimal or is not positive
192244 *
193245 * @param string $value
@@ -202,6 +254,7 @@ private function _parseDecimalValue($value)
202254 $ result = $ value ;
203255 }
204256 }
257+
205258 return $ result ;
206259 }
207260}
0 commit comments