Skip to content

Commit

Permalink
Merge pull request #142 from feyst/master
Browse files Browse the repository at this point in the history
Improve UNB Interchange Header recognition for syntax identifier
  • Loading branch information
sabas authored Oct 21, 2024
2 parents 9f2c96e + aaafc4e commit cce1d49
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 13 deletions.
18 changes: 9 additions & 9 deletions src/EDI/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -505,18 +505,18 @@ private function unwrap(string &$string): array
);
}

$unbRegex = sprintf(
'/^(UNA[^%1$s]+%1$s\W?\W?)?UNB%2$s(?<syntax_identifier>\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';
Expand Down
5 changes: 4 additions & 1 deletion tests/EDITest/InterpreterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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());
}
Expand Down
9 changes: 6 additions & 3 deletions tests/EDITest/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions tests/files/example_utf8.edi
Original file line number Diff line number Diff line change
@@ -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'
Expand Down

0 comments on commit cce1d49

Please sign in to comment.