1
1
<?php
2
2
/**
3
3
* EDIFACT Messages Parser
4
- * (c)2016 Stefano Sabatini
4
+ * (c)2018 Stefano Sabatini
5
5
*/
6
6
7
7
namespace EDI ;
@@ -76,69 +76,98 @@ public function __construct($url = null)
76
76
if ($ this ->unbChecked !== false ) {
77
77
$ this ->resetUNB ();
78
78
}
79
+
79
80
$ this ->errors =array ();
81
+ $ this ->parsedfile =array ();
82
+
80
83
if ($ url ===null ) {
81
84
return ;
82
85
}
83
86
if (is_array ($ url )) {
84
- $ tmparr =$ url ;
87
+ /**
88
+ * Object constructed with an array as argument
89
+ */
85
90
if (count ($ url ) == 1 ) {
86
- $ tmparr =$ this ->unwrap ($ url [0 ]);
91
+ $ url =$ this ->unwrap ($ url [0 ]);
87
92
}
88
- $ this ->rawSegments = $ tmparr ;
89
- $ this ->parse ($ tmparr );
93
+ $ this ->rawSegments = $ url ;
94
+ $ this ->parse ($ url );
90
95
} elseif (file_exists ($ url )) {
91
- $ this ->load ($ url ); //FILE URL
96
+ /**
97
+ * Object constructed with a path to a file as argument
98
+ */
99
+ $ this ->load ($ url );
92
100
} else {
93
- $ this ->loadString ($ url ); //STRING
101
+ /**
102
+ * Object constructed with a string as argument
103
+ */
104
+ $ this ->loadString ($ url );
94
105
}
95
106
}
96
107
97
- //Parse edi array
98
- public function parse ($ file2 )
108
+ /**
109
+ * Parse edi array
110
+ *
111
+ * @param array $file2
112
+ *
113
+ * @return array
114
+ */
115
+ public function parse (&$ file2 )
99
116
{
100
- $ i =0 ;
101
- $ this ->errors =array ();
102
- foreach ($ file2 as $ x => &$ line ) {
103
- $ i ++;
104
- $ line = preg_replace ('#[\x00\r\n]# ' , '' , $ line ); //null byte and carriage return removal (CR+LF)
117
+
118
+ //while ($line = array_shift($file2))
119
+ $ t = count ($ file2 );
120
+ for ($ i = 1 ; $ i <= $ t ; $ i ++) {
121
+ $ line = array_shift ($ file2 );
122
+
123
+ /**
124
+ * Null byte and carriage return removal (CR+LF)
125
+ */
126
+ $ line = preg_replace ('#[\x00\r\n]# ' , '' , $ line );
105
127
if (preg_match ($ this ->stripChars , $ line )) {
106
128
$ this ->errors []="There's a not printable character on line " .$ i .": " . $ line ;
107
129
}
108
- $ line = preg_replace ($ this ->stripChars , '' , trim ($ line )); //basic sanitization, remove non printable chars
109
- if (strlen ($ line )<2 ) {
110
- unset($ file2 [$ x ]);
130
+
131
+ /**
132
+ * Basic sanitization, remove non printable chars
133
+ */
134
+ $ line = preg_replace ($ this ->stripChars , '' , trim ($ line ));
135
+ if (strlen ($ line ) < 2 ) {
111
136
continue ;
112
137
}
138
+
113
139
switch (substr ($ line , 0 , 3 )) {
114
140
case "UNA " :
115
141
if (!$ this ->unaChecked ) {
116
142
$ this ->analyseUNA (substr ($ line , 4 , 6 ));
117
143
}
118
- unset($ file2 [$ x ]);
119
144
break ;
120
145
case "UNB " :
121
146
$ line =$ this ->splitSegment ($ line );
122
147
if (!$ this ->unbChecked ) {
123
148
$ this ->analyseUNB ($ line [1 ]);
124
149
}
150
+ $ this ->parsedfile [] = $ line ;
125
151
break ;
126
152
case "UNH " :
127
153
$ line =$ this ->splitSegment ($ line );
128
154
$ this ->analyseUNH ($ line );
155
+ $ this ->parsedfile [] = $ line ;
129
156
break ;
130
157
default :
131
158
$ line =$ this ->splitSegment ($ line );
159
+ $ this ->parsedfile [] = $ line ;
132
160
break ;
133
161
}
134
162
}
135
- $ this ->parsedfile =array_values ($ file2 ); //reindex
136
- return $ file2 ;
163
+ return $ this ->parsedfile ;
137
164
}
138
165
139
166
140
167
/**
141
168
* Reset UNA's characters definition
169
+ *
170
+ * @return void
142
171
*/
143
172
private function resetUNA ()
144
173
{
@@ -153,6 +182,8 @@ private function resetUNA()
153
182
154
183
/**
155
184
* Reset UNB's encoding definition
185
+ *
186
+ * @return void
156
187
*/
157
188
private function resetUNB ()
158
189
{
@@ -162,7 +193,10 @@ private function resetUNB()
162
193
163
194
/**
164
195
* Read UNA's characters definition
196
+ *
165
197
* @param string $line : UNA definition line (without UNA tag). Example : :+.? '
198
+ *
199
+ * @return void
166
200
*/
167
201
public function analyseUNA ($ line )
168
202
{
@@ -189,24 +223,33 @@ public function analyseUNA($line)
189
223
}
190
224
191
225
/**
192
- * Read UNA's characters definition
193
- * @param string $line : UNB definition line (without UNB tag). Example UNOA:2
226
+ * UNB line analysis
227
+ *
228
+ * @param string $encoding UNB definition line (without UNB tag). Example UNOA:2
229
+ *
230
+ * @return void
194
231
*/
195
232
public function analyseUNB ($ encoding )
196
233
{
197
234
if (is_array ($ encoding )) {
198
235
$ encoding = $ encoding [0 ];
199
236
}
200
237
$ this ->encoding = $ encoding ;
201
- if (isset ($ this ->encodingToStripChars [$ encoding ])) { // we have a normed char set for your content
238
+ /**
239
+ * If there's a regex defined for this character set, use it
240
+ */
241
+ if (isset ($ this ->encodingToStripChars [$ encoding ])) {
202
242
$ this ->setStripRegex ($ this ->encodingToStripChars [$ encoding ]);
203
243
}
204
244
$ this ->unbChecked = true ;
205
245
}
206
246
207
247
/**
208
248
* Identify message type
209
- * @param string $line : UNH segment
249
+ *
250
+ * @param string $line UNH segment
251
+ *
252
+ * @return void
210
253
*/
211
254
public function analyseUNH ($ line )
212
255
{
@@ -222,7 +265,13 @@ public function analyseUNH($line)
222
265
$ this ->messageDirectory = $ lineElement [2 ];
223
266
}
224
267
225
- //unwrap string splitting rows on terminator (if not escaped)
268
+ /**
269
+ * Unwrap string splitting rows on terminator (if not escaped)
270
+ *
271
+ * @param string $string
272
+ *
273
+ * @return void
274
+ */
226
275
private function unwrap ($ string )
227
276
{
228
277
if (!$ this ->unaChecked && substr ($ string , 0 , 3 ) === "UNA " ) {
@@ -232,16 +281,15 @@ private function unwrap($string)
232
281
$ this ->analyseUNB (preg_replace ("#^UNB\+# " , "" , substr ($ string , 0 , 8 )));
233
282
}
234
283
235
- $ file2 =array ();
236
284
$ file =preg_split (self ::$ DELIMITER ."(?<! " .$ this ->symbRel .") " .$ this ->symbEnd .self ::$ DELIMITER ."i " , $ string );
237
285
$ end = stripslashes ($ this ->symbEnd );
238
- foreach ($ file as &$ line ) {
239
- $ temp =$ line .$ end ;
240
- if ($ temp !=$ end ) {
241
- $ file2 []=$ temp ;
286
+ foreach ($ file as $ fc => &$ line ) {
287
+ if (trim ($ line ) == '' ) {
288
+ unset($ file [$ fc ]);
242
289
}
290
+ $ line .= $ end;
243
291
}
244
- return $ file2 ;
292
+ return $ file ;
245
293
}
246
294
247
295
//Segments
@@ -268,8 +316,9 @@ private function splitData($str)
268
316
$ arr=preg_split(self::$ DELIMITER ."(?<! " .$ this ->symbRel .") " .$ this ->sepComp .self ::$ DELIMITER , $ str ); //split on sepComp if not escaped (negative lookbehind)
269
317
if (count ($ arr )==1 ) {
270
318
return preg_replace(self::$ DELIMITER .$ this ->symbRel ."(?= " .$ this ->symbRel .")| " .$ this ->symbRel ."(?= " .$ this ->sepData .")| " .$ this ->symbRel ."(?= " .$ this ->sepComp .")| " .$ this ->symbRel ."(?= " .$ this ->symbEnd .") " .self ::$ DELIMITER , "" , $ str ); //remove symbRel if not escaped
271
- } foreach ($ arr as &$ value ) {
272
- $ value =preg_replace (self ::$ DELIMITER .$ this ->symbRel ."(?= " .$ this ->symbRel .")| " .$ this ->symbRel ."(?= " .$ this ->sepData .")| " .$ this ->symbRel ."(?= " .$ this ->sepComp .")| " .$ this ->symbRel ."(?= " .$ this ->symbEnd .") " .self ::$ DELIMITER , "" , $ value );
319
+ }
320
+ foreach ($ arr as &$ value ) {
321
+ $ value=preg_replace(self::$ DELIMITER .$ this ->symbRel ."(?= " .$ this ->symbRel .")| " .$ this ->symbRel ."(?= " .$ this ->sepData .")| " .$ this ->symbRel ."(?= " .$ this ->sepComp .")| " .$ this ->symbRel ."(?= " .$ this ->symbEnd .") " .self ::$ DELIMITER , "" , $ value );
273
322
}
274
323
return $ arr;
275
324
}
@@ -300,11 +349,11 @@ public function load($url)
300
349
}
301
350
302
351
//load the message from a string
303
- public function loadString ($ string )
352
+ public function loadString(& $ string )
304
353
{
305
- $ arr = $ this ->unwrap ($ string );
306
- $ this ->rawSegments = $ arr ;
307
- return $ this ->parse ($ arr );
354
+ $ string = $ this ->unwrap($ string);
355
+ $ this ->rawSegments = $ string ;
356
+ return $ this ->parse ($ string );
308
357
}
309
358
310
359
// change the default regex used for stripping invalid characters
@@ -322,4 +371,4 @@ public function getMessageDirectory()
322
371
{
323
372
return $ this ->messageDirectory ;
324
373
}
325
- }
374
+ }
0 commit comments