9
9
use Magento \Framework \Phrase ;
10
10
use Magento \OfflineShipping \Model \ResourceModel \Carrier \Tablerate \LocationDirectory ;
11
11
12
+ /**
13
+ * Row parser.
14
+ */
12
15
class RowParser
13
16
{
14
17
/**
@@ -26,6 +29,8 @@ public function __construct(LocationDirectory $locationDirectory)
26
29
}
27
30
28
31
/**
32
+ * Retrieve columns.
33
+ *
29
34
* @return array
30
35
*/
31
36
public function getColumns ()
@@ -42,6 +47,8 @@ public function getColumns()
42
47
}
43
48
44
49
/**
50
+ * Parse provided row data.
51
+ *
45
52
* @param array $rowData
46
53
* @param int $rowNumber
47
54
* @param int $websiteId
@@ -62,27 +69,39 @@ public function parse(
62
69
) {
63
70
// validate row
64
71
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
+ );
66
78
}
67
79
68
80
$ countryId = $ this ->getCountryId ($ rowData , $ rowNumber , $ columnResolver );
69
- $ regionId = $ this ->getRegionId ($ rowData , $ rowNumber , $ columnResolver , $ countryId );
81
+ $ regionIds = $ this ->getRegionIds ($ rowData , $ rowNumber , $ columnResolver , $ countryId );
70
82
$ zipCode = $ this ->getZipCode ($ rowData , $ columnResolver );
71
83
$ conditionValue = $ this ->getConditionValue ($ rowData , $ rowNumber , $ conditionFullName , $ columnResolver );
72
84
$ price = $ this ->getPrice ($ rowData , $ rowNumber , $ columnResolver );
73
85
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 ;
83
100
}
84
101
85
102
/**
103
+ * Get country id from provided row data.
104
+ *
86
105
* @param array $rowData
87
106
* @param int $rowNumber
88
107
* @param ColumnResolver $columnResolver
@@ -99,34 +118,53 @@ private function getCountryId(array $rowData, $rowNumber, ColumnResolver $column
99
118
} elseif ($ countryCode === '* ' || $ countryCode === '' ) {
100
119
$ countryId = '0 ' ;
101
120
} 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
+ );
103
128
}
129
+
104
130
return $ countryId ;
105
131
}
106
132
107
133
/**
134
+ * Retrieve region id from provided row data.
135
+ *
108
136
* @param array $rowData
109
137
* @param int $rowNumber
110
138
* @param ColumnResolver $columnResolver
111
139
* @param int $countryId
112
- * @return int|string
140
+ * @return array
113
141
* @throws ColumnNotFoundException
114
142
* @throws RowException
115
143
*/
116
- private function getRegionId (array $ rowData , $ rowNumber , ColumnResolver $ columnResolver , $ countryId )
144
+ private function getRegionIds (array $ rowData , $ rowNumber , ColumnResolver $ columnResolver , $ countryId ): array
117
145
{
118
146
$ regionCode = $ columnResolver ->getColumnValue (ColumnResolver::COLUMN_REGION , $ rowData );
119
147
if ($ countryId !== '0 ' && $ this ->locationDirectory ->hasRegionId ($ countryId , $ regionCode )) {
120
- $ regionId = $ this ->locationDirectory ->getRegionId ($ countryId , $ regionCode );
148
+ $ regionIds = $ this ->locationDirectory ->getRegionIds ($ countryId , $ regionCode );
121
149
} elseif ($ regionCode === '* ' || $ regionCode === '' ) {
122
- $ regionId = 0 ;
150
+ $ regionIds = [ 0 ] ;
123
151
} 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
+ );
125
160
}
126
- return $ regionId ;
161
+
162
+ return $ regionIds ;
127
163
}
128
164
129
165
/**
166
+ * Retrieve zip code from provided row data.
167
+ *
130
168
* @param array $rowData
131
169
* @param ColumnResolver $columnResolver
132
170
* @return float|int|null|string
@@ -138,10 +176,13 @@ private function getZipCode(array $rowData, ColumnResolver $columnResolver)
138
176
if ($ zipCode === '' ) {
139
177
$ zipCode = '* ' ;
140
178
}
179
+
141
180
return $ zipCode ;
142
181
}
143
182
144
183
/**
184
+ * Get condition value form provided row data.
185
+ *
145
186
* @param array $rowData
146
187
* @param int $rowNumber
147
188
* @param string $conditionFullName
@@ -165,10 +206,13 @@ private function getConditionValue(array $rowData, $rowNumber, $conditionFullNam
165
206
)
166
207
);
167
208
}
209
+
168
210
return $ value ;
169
211
}
170
212
171
213
/**
214
+ * Retrieve price from provided row data.
215
+ *
172
216
* @param array $rowData
173
217
* @param int $rowNumber
174
218
* @param ColumnResolver $columnResolver
@@ -181,13 +225,21 @@ private function getPrice(array $rowData, $rowNumber, ColumnResolver $columnReso
181
225
$ priceValue = $ columnResolver ->getColumnValue (ColumnResolver::COLUMN_PRICE , $ rowData );
182
226
$ price = $ this ->_parseDecimalValue ($ priceValue );
183
227
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
+ );
185
235
}
236
+
186
237
return $ price ;
187
238
}
188
239
189
240
/**
190
241
* Parse and validate positive decimal value
242
+ *
191
243
* Return false if value is not decimal or is not positive
192
244
*
193
245
* @param string $value
@@ -202,6 +254,7 @@ private function _parseDecimalValue($value)
202
254
$ result = $ value ;
203
255
}
204
256
}
257
+
205
258
return $ result ;
206
259
}
207
260
}
0 commit comments