Skip to content

Commit 8a1d80b

Browse files
committed
Attempt to detect missing service segments in Interpreter when splitting the messages
1 parent 6e3aeb7 commit 8a1d80b

File tree

1 file changed

+51
-11
lines changed

1 file changed

+51
-11
lines changed

src/EDI/Interpreter.php

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ class Interpreter
1919
'MISSINGREQUIREDSEGMENT' => "Missing required segment",
2020
'NOTCONFORMANT' => "It looks like that this message isn't conformant to the mapping provided. (Not all segments were added)",
2121
'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",
2325
];
2426

2527
/**
@@ -38,6 +40,7 @@ public function __construct($xmlMsg, $xmlSeg, $xmlSvc, $messageTextConf = null)
3840
if ($messageTextConf !== null) {
3941
$this->messageTextConf = array_replace($this->messageTextConf, $messageTextConf);
4042
}
43+
$this->errors = [];
4144
}
4245

4346
/**
@@ -48,22 +51,19 @@ public function __construct($xmlMsg, $xmlSeg, $xmlSvc, $messageTextConf = null)
4851
*/
4952
public function prepare($parsed)
5053
{
51-
$this->msgs = self::splitMessages($parsed);
54+
$this->msgs = $this->splitMessages($parsed, $this->errors);
5255
$groups = [];
53-
$errors = [];
5456
$service = $this->msgs['service'];
5557
$this->serviceSeg = $this->processService($service);
5658

5759
foreach ($this->msgs as $k => $msg) {
5860
if ($k === 'service') {
5961
continue;
6062
}
61-
$grouped = $this->loopMessage($msg, $this->xmlMsg);
63+
$grouped = $this->loopMessage($msg, $this->xmlMsg, $this->errors);
6264
$groups[] = $grouped['message'];
63-
$errors[] = $grouped['errors'];
6465
}
6566
$this->ediGroups = $groups;
66-
$this->errors = $errors;
6767
return $groups;
6868
}
6969

@@ -73,34 +73,74 @@ public function prepare($parsed)
7373
* @param $parsed An array coming from EDI\Parser
7474
* @return array
7575
*/
76-
private static function splitMessages($parsed)
76+
private function splitMessages($parsed, &$errors)
7777
{
7878
$messages = [];
7979
$tmpmsg = [];
8080
$service = [];
81+
$hasInterchangeDelimiters = 0;
82+
$hasMessageDelimiters = 0;
8183

82-
foreach ($parsed as $segment) {
84+
foreach ($parsed as $c => $segment) {
8385
switch ($segment[0]) {
8486
case 'UNB':
87+
$hasInterchangeDelimiters = 0;
88+
$hasInterchangeDelimiters++;
8589
$service['UNB'] = $segment;
8690
break;
8791
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+
}
88101
$service['UNZ'] = $segment;
89102
break;
90103
case 'UNH':
104+
$hasMessageDelimiters = 0;
105+
$hasMessageDelimiters++;
91106
$tmpmsg = [$segment];
92107
break;
93108
case 'UNT':
109+
$hasMessageDelimiters--;
94110
$tmpmsg[] = $segment;
95111
$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+
}
96120
break;
97121
default:
98122
$tmpmsg[] = $segment;
99123
break;
100124
}
101125
}
102-
$messages['service'] = $service;
103126

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;
104144
return $messages;
105145
}
106146

@@ -111,7 +151,7 @@ private static function splitMessages($parsed)
111151
* @param $xml The xml representation of the message
112152
* @return array
113153
*/
114-
private function loopMessage($message, $xml)
154+
private function loopMessage($message, $xml, &$errors)
115155
{
116156
$groupedEdi = [];
117157
$errors = [];
@@ -129,7 +169,7 @@ private function loopMessage($message, $xml)
129169

130170
if ($segmentIdx != count($message)) {
131171
$errors[] = [
132-
"text" => $this->messageTextConf['NOTCONFORMANT'],
172+
"text" => $this->messageTextConf['NOTCONFORMANT']
133173
];
134174
}
135175
return ['message' => $groupedEdi, 'errors' => $errors];

0 commit comments

Comments
 (0)