@@ -19,7 +19,9 @@ class Interpreter
19
19
'MISSINGREQUIREDSEGMENT ' => "Missing required segment " ,
20
20
'NOTCONFORMANT ' => "It looks like that this message isn't conformant to the mapping provided. (Not all segments were added) " ,
21
21
'TOOMANYELEMENTS_COMPOSITE ' => "This composite data element has more elements than expected " ,
22
- 'TOOMANYELEMENTS ' => "This segment has more data elements than expected "
22
+ 'TOOMANYELEMENTS ' => "This segment has more data elements than expected " ,
23
+ 'MISSINGINTERCHANGEDELIMITER ' => "The file has at least one UNB or UNZ missing " ,
24
+ 'MISSINGMESSAGEDELIMITER ' => "The message has at least one UNH or UNT missing " ,
23
25
];
24
26
25
27
/**
@@ -38,6 +40,7 @@ public function __construct($xmlMsg, $xmlSeg, $xmlSvc, $messageTextConf = null)
38
40
if ($ messageTextConf !== null ) {
39
41
$ this ->messageTextConf = array_replace ($ this ->messageTextConf , $ messageTextConf );
40
42
}
43
+ $ this ->errors = [];
41
44
}
42
45
43
46
/**
@@ -48,22 +51,19 @@ public function __construct($xmlMsg, $xmlSeg, $xmlSvc, $messageTextConf = null)
48
51
*/
49
52
public function prepare ($ parsed )
50
53
{
51
- $ this ->msgs = self :: splitMessages ($ parsed );
54
+ $ this ->msgs = $ this -> splitMessages ($ parsed, $ this -> errors );
52
55
$ groups = [];
53
- $ errors = [];
54
56
$ service = $ this ->msgs ['service ' ];
55
57
$ this ->serviceSeg = $ this ->processService ($ service );
56
58
57
59
foreach ($ this ->msgs as $ k => $ msg ) {
58
60
if ($ k === 'service ' ) {
59
61
continue ;
60
62
}
61
- $ grouped = $ this ->loopMessage ($ msg , $ this ->xmlMsg );
63
+ $ grouped = $ this ->loopMessage ($ msg , $ this ->xmlMsg , $ this -> errors );
62
64
$ groups [] = $ grouped ['message ' ];
63
- $ errors [] = $ grouped ['errors ' ];
64
65
}
65
66
$ this ->ediGroups = $ groups ;
66
- $ this ->errors = $ errors ;
67
67
return $ groups ;
68
68
}
69
69
@@ -73,34 +73,74 @@ public function prepare($parsed)
73
73
* @param $parsed An array coming from EDI\Parser
74
74
* @return array
75
75
*/
76
- private static function splitMessages ($ parsed )
76
+ private function splitMessages ($ parsed, & $ errors )
77
77
{
78
78
$ messages = [];
79
79
$ tmpmsg = [];
80
80
$ service = [];
81
+ $ hasInterchangeDelimiters = 0 ;
82
+ $ hasMessageDelimiters = 0 ;
81
83
82
- foreach ($ parsed as $ segment ) {
84
+ foreach ($ parsed as $ c => $ segment ) {
83
85
switch ($ segment [0 ]) {
84
86
case 'UNB ' :
87
+ $ hasInterchangeDelimiters = 0 ;
88
+ $ hasInterchangeDelimiters ++;
85
89
$ service ['UNB ' ] = $ segment ;
86
90
break ;
87
91
case 'UNZ ' :
92
+ $ hasInterchangeDelimiters --;
93
+ if ($ hasInterchangeDelimiters != 0 ) {
94
+ $ sid = ($ hasInterchangeDelimiters < 0 ) ? "UNB " : "UNZ " ;
95
+ $ errors [] = [
96
+ "text " => $ this ->messageTextConf ['MISSINGINTERCHANGEDELIMITER ' ],
97
+ "position " => $ c ,
98
+ "segmentId " => $ sid
99
+ ];
100
+ }
88
101
$ service ['UNZ ' ] = $ segment ;
89
102
break ;
90
103
case 'UNH ' :
104
+ $ hasMessageDelimiters = 0 ;
105
+ $ hasMessageDelimiters ++;
91
106
$ tmpmsg = [$ segment ];
92
107
break ;
93
108
case 'UNT ' :
109
+ $ hasMessageDelimiters --;
94
110
$ tmpmsg [] = $ segment ;
95
111
$ messages [] = $ tmpmsg ;
112
+ if ($ hasMessageDelimiters != 0 ) {
113
+ $ sid = ($ hasMessageDelimiters < 0 ) ? "UNH " : "UNT " ;
114
+ $ errors [] = [
115
+ "text " => $ this ->messageTextConf ['MISSINGMESSAGEDELIMITER ' ],
116
+ "position " => $ c ,
117
+ "segmentId " => $ sid
118
+ ];
119
+ }
96
120
break ;
97
121
default :
98
122
$ tmpmsg [] = $ segment ;
99
123
break ;
100
124
}
101
125
}
102
- $ messages ['service ' ] = $ service ;
103
126
127
+ if ($ hasInterchangeDelimiters != 0 ) {
128
+ $ sid = ($ hasInterchangeDelimiters < 0 ) ? "UNB " : "UNZ " ;
129
+ $ errors [] = [
130
+ "text " => $ this ->messageTextConf ['MISSINGINTERCHANGEDELIMITER ' ],
131
+ "position " => $ c ,
132
+ "segmentId " => $ sid
133
+ ];
134
+ }
135
+ if ($ hasMessageDelimiters != 0 ) {
136
+ $ sid = ($ hasMessageDelimiters < 0 ) ? "UNH " : "UNT " ;
137
+ $ errors [] = [
138
+ "text " => $ this ->messageTextConf ['MISSINGMESSAGEDELIMITER ' ],
139
+ "position " => $ c ,
140
+ "segmentId " => $ sid
141
+ ];
142
+ }
143
+ $ messages ['service ' ] = $ service ;
104
144
return $ messages ;
105
145
}
106
146
@@ -111,7 +151,7 @@ private static function splitMessages($parsed)
111
151
* @param $xml The xml representation of the message
112
152
* @return array
113
153
*/
114
- private function loopMessage ($ message , $ xml )
154
+ private function loopMessage ($ message , $ xml, & $ errors )
115
155
{
116
156
$ groupedEdi = [];
117
157
$ errors = [];
@@ -129,7 +169,7 @@ private function loopMessage($message, $xml)
129
169
130
170
if ($ segmentIdx != count ($ message )) {
131
171
$ errors [] = [
132
- "text " => $ this ->messageTextConf ['NOTCONFORMANT ' ],
172
+ "text " => $ this ->messageTextConf ['NOTCONFORMANT ' ]
133
173
];
134
174
}
135
175
return ['message ' => $ groupedEdi , 'errors ' => $ errors ];
0 commit comments