Skip to content

Commit cce1d49

Browse files
authored
Merge pull request #142 from feyst/master
Improve UNB Interchange Header recognition for syntax identifier
2 parents 9f2c96e + aaafc4e commit cce1d49

File tree

4 files changed

+20
-13
lines changed

4 files changed

+20
-13
lines changed

src/EDI/Parser.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -505,18 +505,18 @@ private function unwrap(string &$string): array
505505
);
506506
}
507507

508+
$unbRegex = sprintf(
509+
'/^(UNA[^%1$s]+%1$s\W?\W?)?UNB%2$s(?<syntax_identifier>\w{4})%3$s/m',
510+
$this->symbEnd,
511+
$this->sepData,
512+
$this->sepComp,
513+
);
508514
if (
509515
! $this->unbChecked
510-
&&
511-
\strpos($string, 'UNB') === 0
516+
&& false !== preg_match($unbRegex, $string, $unbMatches)
517+
&& isset($unbMatches['syntax_identifier'])
512518
) {
513-
$this->analyseUNB(
514-
(string) \preg_replace(
515-
"#^UNB\+#",
516-
'',
517-
\substr($string, 0, 8)
518-
)
519-
);
519+
$this->analyseUNB($unbMatches['syntax_identifier']);
520520
}
521521
if (preg_match_all("/[A-Z0-9]+(?:\?'|$)[\r\n]+/i", $string, $matches, PREG_OFFSET_CAPTURE) > 0) {
522522
$this->errors[] = 'This file contains some segments without terminators';

tests/EDITest/InterpreterTest.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ public function testServiceSegments()
5353
public function testBAPLIE()
5454
{
5555
$parser = new Parser();
56-
$parser->load(__DIR__ . '/../files/D95BBAPLIE.edi')->parse();
56+
$parser->load(__DIR__ . '/../files/D95BBAPLIE.edi');
57+
$syntaxId = $parser->getSyntaxIdentifier();
58+
$parser->parse();
5759

5860
$mapping = new \EDI\Mapping\MappingProvider('D95B');
5961
$analyser = new Analyser();
@@ -67,6 +69,7 @@ public function testBAPLIE()
6769
static::assertCount(2, $interpreter->getMessages());
6870
static::assertCount(0, $interpreter->getErrors());
6971
static::assertCount(2, $interpreter->getServiceSegments());
72+
static::assertSame('UNOA', $syntaxId);
7073

7174
static::assertSame([], $interpreter->getErrors());
7275
}

tests/EDITest/ParserTest.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,13 @@ public function testLoadFile()
208208

209209
public function testLoadWrappedFile()
210210
{
211-
$p = new Parser();
212-
$p->load(__DIR__ . '/../files/example_wrapped.edi')->parse();
213-
$result = $p->errors();
211+
$parser = new Parser();
212+
$parser->load(__DIR__ . '/../files/example_wrapped.edi');
213+
$syntaxId = $parser->getSyntaxIdentifier();
214+
$parser->parse();
215+
$result = $parser->errors();
214216
static::assertEmpty($result);
217+
static::assertSame('IATB', $syntaxId);
215218
}
216219

217220
public function testUNHWithoutMessageType()

tests/files/example_utf8.edi

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
UNA:+.? '
12
UNB+UNOC:1+1556150:31B+8888888:ZZ+160727:0953+1'
23
UNH+142+DESADV:0:96A:UN'
34
BGM+351+Y02197250+700101'

0 commit comments

Comments
 (0)