diff --git a/src/EDI/Parser.php b/src/EDI/Parser.php index b82a73b..0dc8961 100644 --- a/src/EDI/Parser.php +++ b/src/EDI/Parser.php @@ -81,9 +81,9 @@ class Parser private $stringSafe = '§SS§'; /** - * @var string|null Syntax identifier + * @var string UNB Syntax identifier */ - private $syntaxID = 'UNOB'; + private string $syntaxID = ''; /** * @var string|null Message format from UNH @@ -132,9 +132,9 @@ class Parser private $unbChecked = false; /** - * Optionally disable workarounds + * Optionally disable workarounds. */ - private $strict = false; + private bool $strict = false; /** * Parse EDI array. @@ -273,21 +273,25 @@ public function analyseUNH(array $line): void } /** - * Check if the encoding of the text actually matches the one declared by the UNB syntax identifier. + * Check if the file's character encoding actually matches the one declared in the UNB header. * + * @throws \LogicException * @throws \RuntimeException */ public function checkEncoding(): bool { if (empty($this->parsedfile)) { - throw new \RuntimeException('No text has been parsed yet'); + throw new \LogicException('No text has been parsed yet'); } - if (! isset(self::$charsets[$this->syntaxID])) { throw new \RuntimeException('Unsupported syntax identifier: ' . $this->syntaxID); } - return mb_check_encoding($this->parsedfile, self::$charsets[$this->syntaxID]); + $check = mb_check_encoding($this->parsedfile, self::$charsets[$this->syntaxID]); + if(!$check) + $this->errors[] = 'Character encoding does not match declaration in UNB interchange header'; + + return $check; } /** @@ -299,9 +303,9 @@ public function errors(): array } /** - * Set Strict + * (Un)Set strict parsing. */ - public function isStrict($strict) + public function setStrict(bool $strict):void { $this->strict = $strict; } @@ -329,21 +333,15 @@ public function getRawSegments(): array } /** - * Get character encoding extracted from UNB header + * Get syntax identifier from the UNB header. + * Does not necessarily mean that the text is actually encoded as such. * * @return string + * @throws \RuntimeException */ - public function getCharset(): string + public function getSyntaxIdentifier(): string { - if (empty($this->parsedfile)) { - throw new \RuntimeException('No text has been parsed yet'); - } - - if (! isset(self::$charsets[$this->syntaxID])) { - throw new \RuntimeException('Unsupported syntax identifier: ' . $this->syntaxID); - } - - return self::$charsets[$this->syntaxID]; + return $this->syntaxID; } /** @@ -457,7 +455,7 @@ private function resetUNA(): void */ private function resetUNB(): void { - $this->syntaxID = 'UNOB'; + $this->syntaxID = ''; $this->unbChecked = false; }