@@ -81,9 +81,9 @@ class Parser
81
81
private $ stringSafe = '§SS§ ' ;
82
82
83
83
/**
84
- * @var string|null Syntax identifier
84
+ * @var string UNB Syntax identifier
85
85
*/
86
- private $ syntaxID = 'UNOB ' ;
86
+ private string $ syntaxID = '' ;
87
87
88
88
/**
89
89
* @var string|null Message format from UNH
@@ -273,21 +273,25 @@ public function analyseUNH(array $line): void
273
273
}
274
274
275
275
/**
276
- * Check if the encoding of the text actually matches the one declared by the UNB syntax identifier .
276
+ * Check if the file's character encoding actually matches the one declared in the UNB header .
277
277
*
278
+ * @throws \LogicException
278
279
* @throws \RuntimeException
279
280
*/
280
281
public function checkEncoding (): bool
281
282
{
282
283
if (empty ($ this ->parsedfile )) {
283
- throw new \RuntimeException ('No text has been parsed yet ' );
284
+ throw new \LogicException ('No text has been parsed yet ' );
284
285
}
285
-
286
286
if (! isset (self ::$ charsets [$ this ->syntaxID ])) {
287
287
throw new \RuntimeException ('Unsupported syntax identifier: ' . $ this ->syntaxID );
288
288
}
289
289
290
- return mb_check_encoding ($ this ->parsedfile , self ::$ charsets [$ this ->syntaxID ]);
290
+ $ check = mb_check_encoding ($ this ->parsedfile , self ::$ charsets [$ this ->syntaxID ]);
291
+ if (!$ check )
292
+ $ this ->errors [] = 'Character encoding does not match declaration in UNB interchange header ' ;
293
+
294
+ return $ check ;
291
295
}
292
296
293
297
/**
@@ -329,21 +333,15 @@ public function getRawSegments(): array
329
333
}
330
334
331
335
/**
332
- * Get character encoding extracted from UNB header
336
+ * Get syntax identifier from the UNB header.
337
+ * Does not necessarily mean that the text is actually encoded as such.
333
338
*
334
339
* @return string
340
+ * @throws \RuntimeException
335
341
*/
336
- public function getCharset (): string
342
+ public function getSyntaxIdentifier (): string
337
343
{
338
- if (empty ($ this ->parsedfile )) {
339
- throw new \RuntimeException ('No text has been parsed yet ' );
340
- }
341
-
342
- if (! isset (self ::$ charsets [$ this ->syntaxID ])) {
343
- throw new \RuntimeException ('Unsupported syntax identifier: ' . $ this ->syntaxID );
344
- }
345
-
346
- return self ::$ charsets [$ this ->syntaxID ];
344
+ return $ this ->syntaxID ;
347
345
}
348
346
349
347
/**
@@ -457,7 +455,7 @@ private function resetUNA(): void
457
455
*/
458
456
private function resetUNB (): void
459
457
{
460
- $ this ->syntaxID = 'UNOB ' ;
458
+ $ this ->syntaxID = '' ;
461
459
$ this ->unbChecked = false ;
462
460
}
463
461
0 commit comments