@@ -23,7 +23,7 @@ class Interpreter
2323 'MISSINGMESSAGEDELIMITER ' => 'The message has at least one UNH or UNT missing ' ,
2424 'TOOMANYSEGMENTS ' => 'The message has some additional segments beyond the maximum repetition allowed ' ,
2525 'TOOMANYGROUPS ' => 'The message has some additional groups beyond the maximum repetition allowed ' ,
26- 'SPURIOUSSEGMENT ' => 'This segment is spurious '
26+ 'SPURIOUSSEGMENT ' => 'This segment is spurious ' ,
2727 ];
2828
2929 /**
@@ -160,16 +160,17 @@ public function __construct(string $xmlMsg, array $xmlSeg, array $xmlSvc, array
160160 public function togglePatching (bool $ flag )
161161 {
162162 $ this ->patchFiles = $ flag ;
163+
163164 return $ this ;
164165 }
165166
166-
167167 /**
168168 * @return void
169169 */
170170 public function toggleSegmentGroup (bool $ flag )
171171 {
172172 $ this ->segmentGroup = $ flag ;
173+
173174 return $ this ;
174175 }
175176
@@ -179,6 +180,7 @@ public function toggleSegmentGroup(bool $flag)
179180 public function forceArrayWhenRepeatable (bool $ flag )
180181 {
181182 $ this ->forceArrayWhenRepeatable = $ flag ;
183+
182184 return $ this ;
183185 }
184186
@@ -191,6 +193,7 @@ public function forceArrayWhenRepeatable(bool $flag)
191193 public function setMessageTextConf (array $ messageTextConf )
192194 {
193195 $ this ->messageTextConf = \array_replace ($ this ->messageTextConf , $ messageTextConf );
196+
194197 return $ this ;
195198 }
196199
@@ -203,6 +206,7 @@ public function setMessageTextConf(array $messageTextConf)
203206 public function setSegmentTemplates (array $ segmentTemplates )
204207 {
205208 $ this ->segmentTemplates = $ segmentTemplates ;
209+
206210 return $ this ;
207211 }
208212
@@ -215,6 +219,7 @@ public function setSegmentTemplates(array $segmentTemplates)
215219 public function setGroupTemplates (array $ groupTemplates )
216220 {
217221 $ this ->groupTemplates = $ groupTemplates ;
222+
218223 return $ this ;
219224 }
220225
@@ -227,6 +232,7 @@ public function setGroupTemplates(array $groupTemplates)
227232 public function setCodes (array $ codes )
228233 {
229234 $ this ->codes = $ codes ;
235+
230236 return $ this ;
231237 }
232238
@@ -239,6 +245,7 @@ public function setCodes(array $codes)
239245 public function setComparisonFunction (callable $ func )
240246 {
241247 $ this ->comparisonFunction = $ func ;
248+
242249 return $ this ;
243250 }
244251
@@ -251,6 +258,7 @@ public function setComparisonFunction(callable $func)
251258 public function setReplacementFunction (callable $ func )
252259 {
253260 $ this ->replacementFunction = $ func ;
261+
254262 return $ this ;
255263 }
256264
@@ -267,6 +275,7 @@ public function toggleUseIdInsteadOfNameForOutput(bool $toggle)
267275 } else {
268276 $ this ->outputKey = 'name ' ;
269277 }
278+
270279 return $ this ;
271280 }
272281
@@ -599,7 +608,7 @@ private function processXmlSegment(\SimpleXMLElement $elm, array &$message, int
599608 $ segmentVisited = true ;
600609 $ this ->doAddArray ($ array , $ jsonMessage , (int ) $ elm ['maxrepeat ' ]);
601610 $ segmentIdx ++;
602- } else if ($ this ->replacementFunction !== null && $ replacementSegment = \call_user_func ($ this ->replacementFunction , $ message [$ segmentIdx ], $ elm )) {
611+ } elseif ($ this ->replacementFunction !== null && $ replacementSegment = \call_user_func ($ this ->replacementFunction , $ message [$ segmentIdx ], $ elm )) {
603612 //the function shall return false, true or a new segment
604613 $ fixed = false ;
605614
@@ -627,18 +636,20 @@ private function processXmlSegment(\SimpleXMLElement $elm, array &$message, int
627636 'segmentId ' => (string ) $ elm ['id ' ],
628637 ];
629638 $ segmentIdx ++;
639+
630640 continue ;
631641 } else {
632642 if (! $ segmentVisited && isset ($ elm ['required ' ])) {
633643 $ segmentVisited = true ;
634- if (isset ($ message [$ segmentIdx+ 1 ]) && \call_user_func ($ this ->comparisonFunction , $ message [$ segmentIdx+ 1 ], $ elm )) {
644+ if (isset ($ message [$ segmentIdx + 1 ]) && \call_user_func ($ this ->comparisonFunction , $ message [$ segmentIdx + 1 ], $ elm )) {
635645 $ errors [] = [
636646 'text ' => $ this ->messageTextConf ['SPURIOUSSEGMENT ' ].($ this ->patchFiles ? ' (skipped) ' : '' ),
637647 'position ' => $ segmentIdx ,
638648 'segmentId ' => (string ) $ message [$ segmentIdx ][0 ],
639649 ];
640650 $ segmentIdx ++; //just move the index
641651 $ i --; //but don't count as repetition
652+
642653 continue ;
643654 }
644655
@@ -672,8 +683,8 @@ private function processXmlSegment(\SimpleXMLElement $elm, array &$message, int
672683 $ loopMove = 0 ;
673684 while (
674685 isset ($ message [$ segmentIdx ]) &&
675- \call_user_func ($ this ->comparisonFunction , $ message [$ segmentIdx+ $ loopMove ], $ elm ) &&
676- (string )$ elm ['id ' ] !== $ this ->currentGroupHeader
686+ \call_user_func ($ this ->comparisonFunction , $ message [$ segmentIdx + $ loopMove ], $ elm ) &&
687+ (string ) $ elm ['id ' ] !== $ this ->currentGroupHeader
677688 ) {
678689 $ errors [] = [
679690 'text ' => $ this ->messageTextConf ['TOOMANYSEGMENTS ' ].($ this ->patchFiles ? ' (skipped) ' : '' ),
@@ -735,7 +746,7 @@ private function processSegment(array &$segment, array &$xmlMap, $segmentIdx, ar
735746
736747 $ jsonelements = [
737748 'segmentIdx ' => $ segmentIdx ,
738- 'segmentCode ' => $ id
749+ 'segmentCode ' => $ id,
739750 ];
740751
741752 if ($ this ->segmentGroup ) {
@@ -783,7 +794,9 @@ private function processSegment(array &$segment, array &$xmlMap, $segmentIdx, ar
783794 }
784795
785796 $ d_sub_desc_attr = $ sub_details_desc [$ d_n ]['attributes ' ];
786-
797+ //print_r($d_sub_desc_attr);
798+ //print_r($d_detail);
799+ //die();
787800 if ($ this ->codes !== null && isset ($ this ->codes [$ d_sub_desc_attr ['id ' ]]) && is_array ($ this ->codes [$ d_sub_desc_attr ['id ' ]])) { //if codes is set enable translation of the value
788801 if (isset ($ this ->codes [$ d_sub_desc_attr ['id ' ]][$ d_detail ])) {
789802 $ d_detail = $ this ->codes [$ d_sub_desc_attr ['id ' ]][$ d_detail ];
@@ -859,9 +872,19 @@ public function rebuildArray()
859872 if ($ this ->codes !== null ) {
860873 throw new \LogicException ('Run the Interpreter without calling setCodes() ' );
861874 }
875+ $ unh = $ this ->serviceSeg ['interchangeHeader ' ];
876+ unset($ unh ['segmentIdx ' ]);
877+ unset($ unh ['segmentGroup ' ]);
878+ $ unz = $ this ->serviceSeg ['interchangeTrailer ' ];
879+ unset($ unz ['segmentIdx ' ]);
880+ unset($ unz ['segmentGroup ' ]);
862881
863- return $ this ->recursionReconstruct ($ this ->ediGroups );
882+ $ rebuilt = $ this ->recursionReconstruct ($ this ->ediGroups );
864883
884+ array_unshift ($ rebuilt , $ unh );
885+ $ rebuilt [] = $ unz ;
886+
887+ return $ rebuilt ;
865888 }
866889
867890 private function recursionReconstruct ($ tempArr )
@@ -875,7 +898,7 @@ private function recursionReconstruct($tempArr)
875898 $ reconstructArr [$ k ] = $ i ;
876899 }
877900 } else {
878- $ idx= $ arr ['segmentIdx ' ];
901+ $ idx = $ arr ['segmentIdx ' ];
879902 unset($ arr ['segmentIdx ' ]);
880903 if ($ this ->segmentGroup ) {
881904 unset($ arr ['segmentGroup ' ]);
@@ -884,6 +907,7 @@ private function recursionReconstruct($tempArr)
884907 }
885908 }
886909 }
910+
887911 return $ reconstructArr ;
888912 }
889913}
0 commit comments