@@ -20,13 +20,23 @@ class Interpreter
20
20
21
21
public $ messageTextConf = [
22
22
'MISSINGREQUIREDSEGMENT ' => "Missing required segment " ,
23
+ 'MISSINGREQUIREDGROUP ' => "Missing required group " ,
23
24
'NOTCONFORMANT ' => "It looks like that this message isn't conformant to the mapping provided. (Not all segments were added) " ,
24
25
'TOOMANYELEMENTS_COMPOSITE ' => "This composite data element has more elements than expected " ,
25
26
'TOOMANYELEMENTS ' => "This segment has more data elements than expected " ,
26
27
'MISSINGINTERCHANGEDELIMITER ' => "The file has at least one UNB or UNZ missing " ,
27
28
'MISSINGMESSAGEDELIMITER ' => "The message has at least one UNH or UNT missing "
28
29
];
29
30
31
+ public $ segmentTemplates = [
32
+ 'DTM ' => ['DTM ' , '999 ' , 'XXX ' ]
33
+ ];
34
+
35
+ public $ groupTemplates = [
36
+ 'SG1 ' =>
37
+ [['TDT ' , '20 ' , 'XXX ' ]]
38
+ ];
39
+
30
40
/**
31
41
* Split multiple messages and process
32
42
*
@@ -50,6 +60,36 @@ public function __construct($xmlMsg, $xmlSeg, $xmlSvc, $messageTextConf = null)
50
60
};
51
61
}
52
62
63
+ /**
64
+ * Patch the error messages array
65
+ *
66
+ * @param $messageTextConf An array with same keys as the internal $messageTextConf
67
+ * @return void
68
+ */
69
+ public function setMessageTextConf ($ messageTextConf ) {
70
+ $ this ->messageTextConf = array_replace ($ this ->messageTextConf , $ messageTextConf );
71
+ }
72
+
73
+ /**
74
+ * Add fake segments used to patch the message if a required segment is missing
75
+ *
76
+ * @param $segmentTemplates An array with segments (having the segment name as key)
77
+ * @return void
78
+ */
79
+ public function setSegmentTemplates ($ segmentTemplates ) {
80
+ $ this ->segmentTemplates = $ segmentTemplates ;
81
+ }
82
+
83
+ /**
84
+ * Add fake groups used to patch the message if a required group is missing
85
+ *
86
+ * @param $groupTemplates An array with segments (having the group name as key)
87
+ * @return void
88
+ */
89
+ public function setGroupTemplates ($ groupTemplates ) {
90
+ $ this ->groupTemplates = $ groupTemplates ;
91
+ }
92
+
53
93
/**
54
94
* Split multiple messages and process
55
95
*
@@ -195,22 +235,40 @@ private function loopMessage($message, $xml, &$errors)
195
235
* Process an XML Group
196
236
*
197
237
*/
198
- private function processXmlGroup ($ elm , $ message , &$ segmentIdx , &$ array , &$ errors )
238
+ private function processXmlGroup ($ elm , & $ message , &$ segmentIdx , &$ array , &$ errors )
199
239
{
240
+ $ groupVisited = false ;
200
241
$ newGroup = [];
201
242
for ($ g = 0 ; $ g < $ elm ['maxrepeat ' ]; $ g ++) {
202
243
$ grouptemp = [];
203
244
if ($ message [$ segmentIdx ][0 ] != $ elm ->children ()[0 ]['id ' ]) {
204
- break ;
245
+ if (!$ groupVisited && isset ($ elm ['required ' ])) {
246
+ $ elmType = $ elm ['id ' ]->__toString ();
247
+ $ fixed = false ;
248
+ if (isset ($ this ->groupTemplates [$ elmType ])) {
249
+ array_splice ($ message , $ segmentIdx , 0 , $ this ->groupTemplates [$ elmType ]);
250
+ $ fixed = true ;
251
+ }
252
+ $ errors [] = [
253
+ "text " => $ this ->messageTextConf ['MISSINGREQUIREDGROUP ' ]." " .($ fixed ? '(patched) ' : '' ),
254
+ "position " => $ segmentIdx ,
255
+ "segmentId " => $ elmType
256
+ ];
257
+ } else {
258
+ break ;
259
+ }
205
260
}
206
261
foreach ($ elm ->children () as $ elm2 ) {
207
262
if ($ elm2 ->getName () == "group " ) {
208
263
$ this ->processXmlGroup ($ elm2 , $ message , $ segmentIdx , $ grouptemp , $ errors );
209
264
} else {
210
265
$ this ->processXmlSegment ($ elm2 , $ message , $ segmentIdx , $ grouptemp , $ errors );
211
266
}
267
+ $ groupVisited = true ;
212
268
}
269
+
213
270
$ newGroup [] = $ grouptemp ;
271
+
214
272
}
215
273
if (count ($ newGroup ) == 0 ) {
216
274
return ;
@@ -233,13 +291,24 @@ private function processXmlSegment($elm, $message, &$segmentIdx, &$array, &$erro
233
291
$ segmentIdx ++;
234
292
} else {
235
293
if (!$ segmentVisited && isset ($ elm ['required ' ])) {
294
+ $ fixed = false ;
295
+ $ elmType = $ elm ['id ' ]->__toString ();
296
+ if ($ elm ['replacewith ' ] !== null ) {
297
+ $ elmType = $ elm ['replacewith ' ]->__toString ();
298
+ }
299
+ if (isset ($ this ->segmentTemplates [$ elmType ])) {
300
+ $ jsonMessage = $ this ->processSegment ($ this ->segmentTemplates [$ elmType ], $ this ->xmlSeg , $ segmentIdx , $ errors );
301
+ $ segmentVisited = true ;
302
+ $ fixed = true ;
303
+ $ this ->doAddArray ($ array , $ jsonMessage );
304
+ }
236
305
$ errors [] = [
237
- "text " => $ this ->messageTextConf ['MISSINGREQUIREDSEGMENT ' ],
306
+ "text " => $ this ->messageTextConf ['MISSINGREQUIREDSEGMENT ' ]. " " .( $ fixed ? ' (patched) ' : '' ) ,
238
307
"position " => $ segmentIdx ,
239
308
"segmentId " => $ elm ['id ' ]->__toString (),
240
309
];
241
310
}
242
- break ;
311
+ return ;
243
312
}
244
313
}
245
314
}
@@ -321,7 +390,6 @@ private function processSegment($segment, &$xmlMap, $segmentIdx, &$errors = null
321
390
} else { // More and more
322
391
$ jsoncomposite [$ d_sub_desc_attr ['name ' ]][] = $ d_detail ;
323
392
}
324
-
325
393
}
326
394
}
327
395
} else {
0 commit comments