From aaafc4e5f3cd6722cc5cfaf20016541c6c61f9d4 Mon Sep 17 00:00:00 2001 From: mark van der Feijst Date: Fri, 18 Oct 2024 16:48:16 +0200 Subject: [PATCH] Improve UNB Interchange Header recognition for syntax identifier --- src/EDI/Parser.php | 18 +++++++++--------- tests/EDITest/InterpreterTest.php | 5 ++++- tests/EDITest/ParserTest.php | 9 ++++++--- tests/files/example_utf8.edi | 1 + 4 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/EDI/Parser.php b/src/EDI/Parser.php index 48042a0..582a6c6 100644 --- a/src/EDI/Parser.php +++ b/src/EDI/Parser.php @@ -505,18 +505,18 @@ private function unwrap(string &$string): array ); } + $unbRegex = sprintf( + '/^(UNA[^%1$s]+%1$s\W?\W?)?UNB%2$s(?\w{4})%3$s/m', + $this->symbEnd, + $this->sepData, + $this->sepComp, + ); if ( ! $this->unbChecked - && - \strpos($string, 'UNB') === 0 + && false !== preg_match($unbRegex, $string, $unbMatches) + && isset($unbMatches['syntax_identifier']) ) { - $this->analyseUNB( - (string) \preg_replace( - "#^UNB\+#", - '', - \substr($string, 0, 8) - ) - ); + $this->analyseUNB($unbMatches['syntax_identifier']); } if (preg_match_all("/[A-Z0-9]+(?:\?'|$)[\r\n]+/i", $string, $matches, PREG_OFFSET_CAPTURE) > 0) { $this->errors[] = 'This file contains some segments without terminators'; diff --git a/tests/EDITest/InterpreterTest.php b/tests/EDITest/InterpreterTest.php index fe2232c..755bfe4 100644 --- a/tests/EDITest/InterpreterTest.php +++ b/tests/EDITest/InterpreterTest.php @@ -53,7 +53,9 @@ public function testServiceSegments() public function testBAPLIE() { $parser = new Parser(); - $parser->load(__DIR__ . '/../files/D95BBAPLIE.edi')->parse(); + $parser->load(__DIR__ . '/../files/D95BBAPLIE.edi'); + $syntaxId = $parser->getSyntaxIdentifier(); + $parser->parse(); $mapping = new \EDI\Mapping\MappingProvider('D95B'); $analyser = new Analyser(); @@ -67,6 +69,7 @@ public function testBAPLIE() static::assertCount(2, $interpreter->getMessages()); static::assertCount(0, $interpreter->getErrors()); static::assertCount(2, $interpreter->getServiceSegments()); + static::assertSame('UNOA', $syntaxId); static::assertSame([], $interpreter->getErrors()); } diff --git a/tests/EDITest/ParserTest.php b/tests/EDITest/ParserTest.php index 5799849..1f544d8 100644 --- a/tests/EDITest/ParserTest.php +++ b/tests/EDITest/ParserTest.php @@ -208,10 +208,13 @@ public function testLoadFile() public function testLoadWrappedFile() { - $p = new Parser(); - $p->load(__DIR__ . '/../files/example_wrapped.edi')->parse(); - $result = $p->errors(); + $parser = new Parser(); + $parser->load(__DIR__ . '/../files/example_wrapped.edi'); + $syntaxId = $parser->getSyntaxIdentifier(); + $parser->parse(); + $result = $parser->errors(); static::assertEmpty($result); + static::assertSame('IATB', $syntaxId); } public function testUNHWithoutMessageType() diff --git a/tests/files/example_utf8.edi b/tests/files/example_utf8.edi index 6585bfb..39a8cd0 100644 --- a/tests/files/example_utf8.edi +++ b/tests/files/example_utf8.edi @@ -1,3 +1,4 @@ +UNA:+.? ' UNB+UNOC:1+1556150:31B+8888888:ZZ+160727:0953+1' UNH+142+DESADV:0:96A:UN' BGM+351+Y02197250+700101'