@@ -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
@@ -132,9 +132,9 @@ class Parser
132
132
private $ unbChecked = false ;
133
133
134
134
/**
135
- * Optionally disable workarounds
135
+ * Optionally disable workarounds.
136
136
*/
137
- private $ strict = false ;
137
+ private bool $ strict = false ;
138
138
139
139
/**
140
140
* Parse EDI array.
@@ -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
/**
@@ -299,9 +303,9 @@ public function errors(): array
299
303
}
300
304
301
305
/**
302
- * Set Strict
306
+ * (Un) Set strict parsing.
303
307
*/
304
- public function isStrict ( $ strict )
308
+ public function setStrict ( bool $ strict ): void
305
309
{
306
310
$ this ->strict = $ strict ;
307
311
}
@@ -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