1
1
<?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace EDI ;
6
+
2
7
/**
3
8
* EDIFACT Messages Parser
4
9
* (c)2016 Uldis Nelsons
5
10
*/
6
-
7
- namespace EDI ;
8
-
9
11
class Analyser
10
12
{
11
13
12
- public $ segments ;
14
+ public $ segments ;
13
15
private $ jsonedi ;
14
16
15
17
/**
16
18
* @param string $message_xml_file
19
+ *
17
20
* @return array
18
21
*/
19
- public function loadMessageXml ($ message_xml_file )
22
+ public function loadMessageXml ($ message_xml_file ): array
20
23
{
21
- $ messageXmlString = file_get_contents ($ message_xml_file );
24
+ $ messageXmlString = \ file_get_contents ($ message_xml_file );
22
25
$ messageXml = new \SimpleXMLIterator ($ messageXmlString );
23
26
unset($ messageXmlString );
24
27
$ message = [
25
- "defaults " => $ this ->readMessageDefaults ($ messageXml ),
26
- "segments " => $ this ->readXmlNodes ($ messageXml ),
28
+ "defaults " => $ this ->readMessageDefaults ($ messageXml ),
29
+ "segments " => $ this ->readXmlNodes ($ messageXml ),
27
30
];
28
31
unset($ messageXml );
32
+
29
33
return $ message ;
30
34
}
31
35
32
36
/**
33
37
* read default values in given message xml
34
- * @param \SimpleXmlElement $message
38
+ *
39
+ * @param \SimpleXMLElement $message
40
+ *
35
41
* @return array
36
42
*/
37
- protected function readMessageDefaults (\SimpleXmlElement $ message )
43
+ protected function readMessageDefaults (\SimpleXMLElement $ message ): array
38
44
{
39
- $ defaults = array ();
40
- $ defaultsNode = isset ($ message ->defaults [0 ]) ? $ message ->defaults [0 ] : array ();
41
- /**
42
- * @var \SimpleXMLElement $defaultValueNode
43
- */
44
- foreach ($ defaultsNode as $ defaultValueNode ) {
45
+ // init
46
+ $ defaults = [];
47
+
48
+ /* @var \SimpleXMLElement $defaultValueNode */
49
+ foreach ($ message ->defaults [0 ] ?? [] as $ defaultValueNode ) {
45
50
$ attributes = $ defaultValueNode ->attributes ();
46
- $ id = (string ) $ attributes ->id ;
47
- $ value = (string ) $ attributes ->value ;
48
- $ defaults [$ id ] = $ value ;
51
+ $ id = (string )$ attributes ->id ;
52
+ $ defaults [$ id ] = (string )$ attributes ->value ;
49
53
}
54
+
50
55
return $ defaults ;
51
56
}
52
57
53
58
/**
54
59
* read message segments and groups
55
- * @param \SimpleXmlElement $element
60
+ *
61
+ * @param \SimpleXMLElement $element
62
+ *
56
63
* @return array
57
64
*/
58
- protected function readXmlNodes (\SimpleXmlElement $ element )
65
+ protected function readXmlNodes (\SimpleXMLElement $ element ): array
59
66
{
60
67
$ arrayElements = [];
61
68
foreach ($ element as $ name => $ node ) {
@@ -71,73 +78,72 @@ protected function readXmlNodes(\SimpleXmlElement $element)
71
78
}
72
79
$ arrayElements [] = $ arrayElement ;
73
80
}
81
+
74
82
return $ arrayElements ;
75
83
}
76
84
77
85
/**
78
86
* return an xml elements attributes in as array
79
- * @param \SimpleXmlElement $element
87
+ *
88
+ * @param \SimpleXMLElement $element
89
+ *
80
90
* @return array
81
91
*/
82
- protected function readAttributesArray (\SimpleXmlElement $ element )
92
+ protected function readAttributesArray (\SimpleXMLElement $ element ): array
83
93
{
84
94
$ attributes = [];
85
95
foreach ($ element ->attributes () as $ attrName => $ attr ) {
86
- $ attributes [$ attrName ] = (string ) $ attr ;
96
+ $ attributes [( string ) $ attrName ] = (string )$ attr ;
87
97
}
98
+
88
99
return $ attributes ;
89
100
}
90
101
91
-
92
102
/**
93
103
* get all data element codes
104
+ *
94
105
* @param string $codesXml
106
+ *
95
107
* @return array
96
108
*/
97
- public function loadCodesXml ($ codesXml )
109
+ public function loadCodesXml ($ codesXml ): array
98
110
{
99
- $ codesXmlString = file_get_contents ($ codesXml );
111
+ $ codesXmlString = \ file_get_contents ($ codesXml );
100
112
$ codesXml = new \SimpleXMLIterator ($ codesXmlString );
101
113
unset($ codesXmlString );
102
114
$ codes = [];
103
- /**
104
- * @var \SimpleXmlIterator $codeCollection
105
- */
115
+ /* @var \SimpleXmlIterator $codeCollection */
106
116
foreach ($ codesXml as $ codeCollection ) {
107
117
$ id = (string )$ codeCollection ->attributes ()->id ;
108
118
$ codes [$ id ] = [];
109
- /**
110
- * @var \SimpleXmlIterator $codeNode
111
- */
119
+ /* @var \SimpleXmlIterator $codeNode */
112
120
foreach ($ codeCollection as $ codeNode ) {
113
121
$ codeAttributes = $ codeNode ->attributes ();
114
122
$ code = (string )$ codeAttributes ->id ;
115
- $ desc = (string )$ codeAttributes ->desc ;
116
- $ codes [$ id ][$ code ] = $ desc ;
123
+ $ codes [$ id ][$ code ] = (string )$ codeAttributes ->desc ;
117
124
}
118
125
}
119
- unset( $ codesXml );
126
+
120
127
return $ codes ;
121
128
}
122
129
123
130
/**
124
131
* convert segment definition from XML to array. Sequence of data_elements and
125
132
* composite_data_element same as in XML
133
+ *
126
134
* @param string $segment_xml_file
135
+ *
127
136
* @return array
128
137
*/
129
- public function loadSegmentsXml ($ segment_xml_file )
138
+ public function loadSegmentsXml ($ segment_xml_file ): array
130
139
{
140
+ $ segments_xml = \file_get_contents ($ segment_xml_file );
131
141
132
- $ segments_xml = file_get_contents ($ segment_xml_file );
133
-
134
- $ xml = simplexml_load_string ($ segments_xml );
142
+ $ xml = \simplexml_load_string ($ segments_xml );
135
143
unset($ segments_xml );
136
- $ this ->segments = array () ;
144
+ $ this ->segments = [] ;
137
145
138
- /**
139
- * @var \SimpleXmlElement $segmentNode
140
- */
146
+ /* @var \SimpleXMLElement $segmentNode */
141
147
foreach ($ xml as $ segmentNode ) {
142
148
$ qualifier = (string )$ segmentNode ->attributes ()->id ;
143
149
$ segment = [];
@@ -148,26 +154,29 @@ public function loadSegmentsXml($segment_xml_file)
148
154
}
149
155
$ this ->segments [$ qualifier ] = $ segment ;
150
156
}
157
+
151
158
return $ this ->segments ;
152
159
}
153
160
154
161
/**
155
162
* create readable EDI MESSAGE with comments
156
- * @param array $data by EDI\Parser:parse() created array from plain EDI message
163
+ *
164
+ * @param array $data by EDI\Parser:parse() created array from plain EDI message
157
165
* @param array $rawSegments (optional) List of raw segments from EDI\Parser::getRawSegments
166
+ *
158
167
* @return string file
159
168
*/
160
- public function process ($ data , $ rawSegments = null )
169
+ public function process ($ data , $ rawSegments = null ): string
161
170
{
162
- $ r = array () ;
171
+ $ r = [] ;
163
172
foreach ($ data as $ nrow => $ segment ) {
164
173
165
174
$ id = $ segment [0 ];
166
175
167
176
$ r [] = '' ;
168
177
$ jsonsegment = [];
169
- if (isset ($ rawSegments, $ rawSegments [$ nrow ])) {
170
- $ r [] = trim ($ rawSegments [$ nrow ]);
178
+ if (isset ($ rawSegments [$ nrow ])) {
179
+ $ r [] = \ trim ($ rawSegments [$ nrow ]);
171
180
}
172
181
173
182
if (isset ($ this ->segments [$ id ])) {
@@ -176,37 +185,32 @@ public function process($data, $rawSegments = null)
176
185
$ details_desc = $ this ->segments [$ id ]['details ' ];
177
186
178
187
$ r [] = $ id . ' - ' . $ attributes ['name ' ];
179
- $ r [] = ' ( ' . wordwrap ($ attributes ['desc ' ], 75 , PHP_EOL . ' ' ) . ') ' ;
188
+ $ r [] = ' ( ' . \ wordwrap ($ attributes ['desc ' ], 75 , PHP_EOL . ' ' ) . ') ' ;
180
189
181
190
$ jsonelements = ["segmentCode " => $ id ];
182
191
foreach ($ segment as $ idx => $ detail ) {
183
- $ n = $ idx- 1 ;
192
+ $ n = $ idx - 1 ;
184
193
if ($ idx == 0 || !isset ($ details_desc [$ n ])) {
185
194
continue ;
186
195
}
187
- $ d_desc_attr = $ details_desc [$ n ]['attributes ' ];
196
+ $ d_desc_attr = $ details_desc [$ n ]['attributes ' ];
188
197
$ l1 = ' ' . $ d_desc_attr ['id ' ] . ' - ' . $ d_desc_attr ['name ' ];
189
- $ l2 = ' ' . wordwrap ($ d_desc_attr ['desc ' ], 71 , PHP_EOL . ' ' );
198
+ $ l2 = ' ' . \ wordwrap ($ d_desc_attr ['desc ' ], 71 , PHP_EOL . ' ' );
190
199
191
- if (!is_array ($ detail )) {
192
- $ r [] = ' [ ' . $ n . '] ' . $ detail ;
193
- $ r [] = $ l1 ;
194
- $ r [] = $ l2 ;
195
- $ jsonelements [$ d_desc_attr ['name ' ]] = $ detail ;
196
- } else {
197
- $ r [] = ' [ ' . $ n . '] ' . implode (', ' , $ detail );
200
+ if (\is_array ($ detail )) {
201
+ $ r [] = ' [ ' . $ n . '] ' . \implode (', ' , $ detail );
198
202
$ r [] = $ l1 ;
199
203
$ r [] = $ l2 ;
200
204
201
205
$ jsoncomposite = [];
202
206
if (isset ($ details_desc [$ n ]['details ' ])) {
203
- $ sub_details_desc = $ details_desc [$ n ]['details ' ];
207
+ $ sub_details_desc = $ details_desc [$ n ]['details ' ];
204
208
205
209
foreach ($ detail as $ d_n => $ d_detail ) {
206
- $ d_sub_desc_attr = $ sub_details_desc [$ d_n ]['attributes ' ];
210
+ $ d_sub_desc_attr = $ sub_details_desc [$ d_n ]['attributes ' ];
207
211
$ r [] = ' [ ' . $ d_n . '] ' . $ d_detail ;
208
212
$ r [] = ' id: ' . $ d_sub_desc_attr ['id ' ] . ' - ' . $ d_sub_desc_attr ['name ' ];
209
- $ r [] = ' ' . wordwrap ($ d_sub_desc_attr ['desc ' ], 69 , PHP_EOL . ' ' );
213
+ $ r [] = ' ' . \ wordwrap ($ d_sub_desc_attr ['desc ' ], 69 , PHP_EOL . ' ' );
210
214
$ r [] = ' type: ' . $ d_sub_desc_attr ['type ' ];
211
215
212
216
$ jsoncomposite [$ d_sub_desc_attr ['name ' ]] = $ d_detail ;
@@ -221,21 +225,30 @@ public function process($data, $rawSegments = null)
221
225
}
222
226
223
227
//check for skipped data
224
- unset($ d_sub_desc_attr ['id ' ]);
225
- unset($ d_sub_desc_attr ['name ' ]);
226
- unset($ d_sub_desc_attr ['desc ' ]);
227
- unset($ d_sub_desc_attr ['type ' ]);
228
- unset($ d_sub_desc_attr ['maxlength ' ]);
229
- unset($ d_sub_desc_attr ['required ' ]);
230
- unset($ d_sub_desc_attr ['length ' ]);
228
+ unset(
229
+ $ d_sub_desc_attr ['id ' ],
230
+ $ d_sub_desc_attr ['name ' ],
231
+ $ d_sub_desc_attr ['desc ' ],
232
+ $ d_sub_desc_attr ['type ' ],
233
+ $ d_sub_desc_attr ['maxlength ' ],
234
+ $ d_sub_desc_attr ['required ' ],
235
+ $ d_sub_desc_attr ['length ' ]
236
+ );
237
+
238
+ /*
231
239
if (!empty($d_sub_desc_attr)) {
232
- var_dump ($ d_sub_desc_attr );
240
+ var_dump($d_sub_desc_attr);
233
241
}
242
+ */
234
243
235
244
}
236
245
}
237
246
$ jsonelements [$ d_desc_attr ['name ' ]] = $ jsoncomposite ;
238
- //exit;
247
+ } else {
248
+ $ r [] = ' [ ' . $ n . '] ' . $ detail ;
249
+ $ r [] = $ l1 ;
250
+ $ r [] = $ l2 ;
251
+ $ jsonelements [$ d_desc_attr ['name ' ]] = $ detail ;
239
252
}
240
253
}
241
254
$ jsonsegment [$ attributes ['name ' ]] = $ jsonelements ;
@@ -246,15 +259,16 @@ public function process($data, $rawSegments = null)
246
259
$ this ->jsonedi [] = $ jsonsegment ;
247
260
}
248
261
249
- return implode (PHP_EOL , $ r );
262
+ return \ implode (PHP_EOL , $ r );
250
263
}
251
264
252
265
/**
253
266
* return the processed EDI in json format
267
+ *
254
268
* @return string json
255
269
*/
256
- public function getJson ()
270
+ public function getJson (): string
257
271
{
258
- return json_encode ($ this ->jsonedi );
272
+ return \ json_encode ($ this ->jsonedi );
259
273
}
260
274
}
0 commit comments