Skip to content

Commit 566b582

Browse files
author
Ralf Hartung
committed
Issue #60
Putting repeating sub segments in an array if necessary
1 parent b165444 commit 566b582

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/EDI/Interpreter.php

+18-1
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,8 @@ private function processSegment($segment, &$xmlMap, $segmentIdx, &$errors = null
250250
$attributes = $xmlMap[$id]['attributes'];
251251
$details_desc = $xmlMap[$id]['details'];
252252

253+
$entryCount = [];
254+
253255
$jsonelements = ["segmentIdx" => $segmentIdx, "segmentCode" => $id];
254256
foreach ($segment as $idx => $detail) {
255257
$n = $idx - 1;
@@ -269,6 +271,12 @@ private function processSegment($segment, &$xmlMap, $segmentIdx, &$errors = null
269271

270272
$d_desc_attr = $details_desc[$n]['attributes'];
271273

274+
if (!array_key_exists($d_desc_attr['name'], $entryCount)) {
275+
$entryCount[$d_desc_attr['name']] = 0;
276+
}
277+
278+
$entryCount[$d_desc_attr['name']]++;
279+
272280
$jsoncomposite = [];
273281
if (isset($details_desc[$n]['details']) && $detail !== '') {
274282
$sub_details_desc = $details_desc[$n]['details'];
@@ -300,7 +308,16 @@ private function processSegment($segment, &$xmlMap, $segmentIdx, &$errors = null
300308
} else {
301309
$jsoncomposite = $detail;
302310
}
303-
$jsonelements[$d_desc_attr['name']] = $jsoncomposite;
311+
312+
if ($entryCount[$d_desc_attr['name']] === 1) {
313+
$jsonelements[$d_desc_attr['name']] = $jsoncomposite;
314+
} else {
315+
if ($entryCount[$d_desc_attr['name']] === 2) {
316+
$jsonelements[$d_desc_attr['name']] = [$jsonelements[$d_desc_attr['name']]];
317+
}
318+
319+
$jsonelements[$d_desc_attr['name']][] = $jsoncomposite;
320+
}
304321
}
305322
$jsonsegment['key'] = $attributes['name'];
306323
$jsonsegment['value'] = $jsonelements;

0 commit comments

Comments
 (0)