Skip to content

Commit a6490ac

Browse files
authored
Merge pull request #118 from gaxweb/master
Actually apply all filter elements to a row
2 parents 053f8db + f616036 commit a6490ac

File tree

1 file changed

+24
-40
lines changed

1 file changed

+24
-40
lines changed

src/EDI/Reader.php

Lines changed: 24 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -195,71 +195,55 @@ public function readEdiDataValueReq($filter, int $l1, $l2 = false)
195195
/**
196196
* read data value from parsed EDI data
197197
*
198-
* @param array<mixed>|string $filter 'AGR' - segment code
199-
* or ['AGR',['1'=>'BB']], where AGR segment code and first element equal 'BB'
200-
* or ['AGR',['1.0'=>'BB']], where AGR segment code and first element zero subelement
201-
* equal 'BB'
202-
* @param int $l1 first level item number (start by 1)
203-
* @param false|int $l2 second level item number (start by 0)
204-
* @param bool $required if required, but no exist, register error
198+
* @param array|string $filter 'AGR' - segment code
199+
* or ['AGR',['1'=>'BB']], where AGR segment code and first element equal 'BB'
200+
* or ['AGR',['1.0'=>'BB']], where AGR segment code and first element zero subelement
201+
* equal 'BB'
202+
* @param int $l1 first level item number (start by 1)
203+
* @param false|int $l2 second level item number (start by 0)
204+
* @param bool $required if required, but no exist, register error
205205
*
206206
* @return string|null
207207
*/
208208
public function readEdiDataValue($filter, int $l1, $l2 = false, bool $required = false)
209209
{
210-
// interpret filter parameters
210+
$segment = false;
211+
$segment_count = 0;
212+
$segment_name = $filter;
213+
$filter_elements = false;
211214
if (\is_array($filter)) {
212215
$segment_name = $filter[0];
213216
$filter_elements = $filter[1];
214-
} else {
215-
$segment_name = $filter;
216-
$filter_elements = false;
217217
}
218218

219-
// init
220-
$segment = false;
221-
$segment_count = 0;
222-
223-
// search segment, who conform to filter
219+
// search segments which conform to filter
224220
foreach ($this->parsedfile as $edi_row) {
225221
if ($edi_row[0] == $segment_name) {
226222
if ($filter_elements) {
227-
$filter_ok = false;
228223
foreach ($filter_elements as $el_id => $el_value) {
224+
$filter_ok = false;
229225
$f_el_list = \explode('.', (string) $el_id);
230226
if (\count($f_el_list) === 1) {
231-
if (
232-
isset($edi_row[$el_id])
233-
&&
234-
$edi_row[$el_id] == $el_value
235-
) {
227+
if (isset($edi_row[$el_id]) && $edi_row[$el_id] == $el_value) {
236228
$filter_ok = true;
237-
238-
break;
239229
}
240230
} elseif (
241231
isset($edi_row[$f_el_list[0]])
242-
&&
243-
(
232+
&& (
244233
(
245234
isset($edi_row[$f_el_list[0]][$f_el_list[1]])
246-
&&
247-
\is_array($edi_row[$f_el_list[0]])
248-
&&
249-
$edi_row[$f_el_list[0]][$f_el_list[1]] == $el_value
250-
)
251-
||
252-
(
235+
&& \is_array($edi_row[$f_el_list[0]])
236+
&& $edi_row[$f_el_list[0]][$f_el_list[1]] == $el_value
237+
) || (
253238
isset($edi_row[$f_el_list[0]])
254-
&&
255-
\is_string($edi_row[$f_el_list[0]])
256-
&&
257-
$edi_row[$f_el_list[0]] == $el_value
239+
&& \is_string($edi_row[$f_el_list[0]])
240+
&& $edi_row[$f_el_list[0]] == $el_value
258241
)
259242
)
260243
) {
261244
$filter_ok = true;
262-
245+
}
246+
if ($filter_ok === false) {
263247
break;
264248
}
265249
}
@@ -273,7 +257,7 @@ public function readEdiDataValue($filter, int $l1, $l2 = false, bool $required =
273257
}
274258
}
275259

276-
// no found segment
260+
// no segment found
277261
if (!$segment) {
278262
if ($required) {
279263
$this->errors[] = 'Segment "' . $segment_name . '" no exist';
@@ -282,7 +266,7 @@ public function readEdiDataValue($filter, int $l1, $l2 = false, bool $required =
282266
return null;
283267
}
284268

285-
// found more one segment - error
269+
// found more than one segment - error
286270
if ($segment_count > 1) {
287271
$this->errors[] = 'Segment "' . $segment_name . '" is ambiguous';
288272

0 commit comments

Comments
 (0)