@@ -28,7 +28,7 @@ class Parser
28
28
/**
29
29
* @var string
30
30
*/
31
- private $ stripChars = "/[ \x01- \x1F\x80- \xFF]/ " ; //UNOB encoding set
31
+ private $ stripChars = "/[ \x01- \x1F\x80- \xFF]/ " ; // UNOB encoding set
32
32
33
33
/**
34
34
* @var string
@@ -156,19 +156,21 @@ public function __construct($url = null)
156
156
*/
157
157
public function parse (&$ file2 ): array
158
158
{
159
- $ t = \count ($ file2 );
160
- for ($ i = 1 ; $ i <= $ t ; $ i ++) {
161
- $ line = \array_shift ($ file2 );
162
-
163
- // Null byte and carriage return removal (CR+LF)
164
- $ line = \preg_replace ('#[\x00\r\n]# ' , '' , $ line );
165
- if (\preg_match ($ this ->stripChars , $ line )) {
166
- $ this ->errors [] = "There's a not printable character on line " . $ i . ": " . $ line ;
159
+ $ i = 0 ;
160
+ foreach ($ file2 as &$ line ) {
161
+ ++$ i ;
162
+
163
+ // Null byte and carriage return removal. (CR+LF)
164
+ $ line = str_replace (["\x00" , "\r" , "\n" ], '' , $ line );
165
+
166
+ // Basic sanitization, remove non printable chars.
167
+ $ lineTrim = \trim ($ line );
168
+ $ line = \preg_replace ($ this ->stripChars , '' , $ lineTrim );
169
+ $ line_bytes = \strlen ($ line );
170
+ if ($ line_bytes !== \strlen ($ lineTrim )) {
171
+ $ this ->errors [] = "There's a not printable character on line " . $ i . ": " . $ lineTrim ;
167
172
}
168
-
169
- // Basic sanitization, remove non printable chars
170
- $ line = \preg_replace ($ this ->stripChars , '' , \trim ($ line ));
171
- if (\strlen ($ line ) < 2 ) {
173
+ if ($ line_bytes < 2 ) {
172
174
continue ;
173
175
}
174
176
@@ -200,7 +202,6 @@ public function parse(&$file2): array
200
202
return $ this ->parsedfile ;
201
203
}
202
204
203
-
204
205
/**
205
206
* Reset UNA's characters definition
206
207
*
@@ -240,19 +241,19 @@ private function resetUNB()
240
241
public function analyseUNA ($ line )
241
242
{
242
243
$ line = \substr ($ line , 0 , 6 );
243
- if (isset($ line{ 0 } )) {
244
- $ this ->sepComp = \preg_quote ($ line{ 0 } , self ::$ DELIMITER );
245
- if (isset ($ line{ 1 } )) {
246
- $ this ->sepData = \preg_quote ($ line{ 1 } , self ::$ DELIMITER );
247
- if (isset ($ line{ 2 } )) {
248
- $ this ->sepDec = $ line{ 2 } ; // See later if a preg_quote is needed
249
- if (isset ($ line{ 3 } )) {
250
- $ this ->symbRel = \preg_quote ($ line{ 3 } , self ::$ DELIMITER );
251
- $ this ->symbUnescapedRel = $ line{ 3 } ;
252
- if (isset ($ line{ 4 } )) {
253
- $ this ->symbRep = $ line{ 4 } ; // See later if a preg_quote is needed
254
- if (isset ($ line{ 5 } )) {
255
- $ this ->symbEnd = \preg_quote ($ line{ 5 } , self ::$ DELIMITER );
244
+ if (isset ($ line[ 0 ] )) {
245
+ $ this ->sepComp = \preg_quote ($ line[ 0 ] , self ::$ DELIMITER );
246
+ if (isset ($ line[ 1 ] )) {
247
+ $ this ->sepData = \preg_quote ($ line[ 1 ] , self ::$ DELIMITER );
248
+ if (isset ($ line[ 2 ] )) {
249
+ $ this ->sepDec = $ line[ 2 ] ; // See later if a preg_quote is needed
250
+ if (isset ($ line[ 3 ] )) {
251
+ $ this ->symbRel = \preg_quote ($ line[ 3 ] , self ::$ DELIMITER );
252
+ $ this ->symbUnescapedRel = $ line[ 3 ] ;
253
+ if (isset ($ line[ 4 ] )) {
254
+ $ this ->symbRep = $ line[ 4 ] ; // See later if a preg_quote is needed
255
+ if (isset ($ line[ 5 ] )) {
256
+ $ this ->symbEnd = \preg_quote ($ line[ 5 ] , self ::$ DELIMITER );
256
257
}
257
258
}
258
259
}
@@ -275,9 +276,7 @@ public function analyseUNB($encoding)
275
276
$ encoding = $ encoding [0 ];
276
277
}
277
278
$ this ->encoding = $ encoding ;
278
- /**
279
- * If there's a regex defined for this character set, use it
280
- */
279
+ // If there's a regex defined for this character set, use it.
281
280
if (isset ($ this ->encodingToStripChars [$ encoding ])) {
282
281
$ this ->setStripRegex ($ this ->encodingToStripChars [$ encoding ]);
283
282
}
@@ -296,12 +295,14 @@ public function analyseUNH($line)
296
295
if (\count ($ line ) < 3 ) {
297
296
return ;
298
297
}
298
+
299
299
$ lineElement = $ line [2 ];
300
300
if (!\is_array ($ lineElement )) {
301
301
$ this ->messageFormat = $ lineElement ;
302
302
303
303
return ;
304
304
}
305
+
305
306
$ this ->messageFormat = $ lineElement [0 ];
306
307
$ this ->messageDirectory = $ lineElement [2 ];
307
308
}
@@ -315,11 +316,19 @@ public function analyseUNH($line)
315
316
*/
316
317
private function unwrap (&$ string ): array
317
318
{
318
- if (!$ this ->unaChecked && \strpos ($ string , "UNA " ) === 0 ) {
319
+ if (
320
+ !$ this ->unaChecked
321
+ &&
322
+ \strpos ($ string , "UNA " ) === 0
323
+ ) {
319
324
$ this ->analyseUNA (\preg_replace ("#^UNA# " , "" , substr ($ string , 0 , 9 )));
320
325
}
321
326
322
- if (!$ this ->unbChecked && \strpos ($ string , "UNB " ) === 0 ) {
327
+ if (
328
+ !$ this ->unbChecked
329
+ &&
330
+ \strpos ($ string , "UNB " ) === 0
331
+ ) {
323
332
$ this ->analyseUNB (\preg_replace ("#^UNB\+# " , "" , substr ($ string , 0 , 8 )));
324
333
}
325
334
0 commit comments