10
10
*/
11
11
class Analyser
12
12
{
13
-
14
13
/**
15
14
* @var array<mixed>
16
15
*/
17
- public $ segments ;
16
+ public $ segments ;
18
17
19
18
/**
20
19
* @var array<mixed>
@@ -36,80 +35,14 @@ public function loadMessageXml(string $message_xml_file)
36
35
$ messageXml = new \SimpleXMLIterator ($ messageXmlString );
37
36
unset($ messageXmlString );
38
37
$ message = [
39
- " defaults " => $ this ->readMessageDefaults ($ messageXml ),
40
- " segments " => $ this ->readXmlNodes ($ messageXml ),
38
+ ' defaults ' => $ this ->readMessageDefaults ($ messageXml ),
39
+ ' segments ' => $ this ->readXmlNodes ($ messageXml ),
41
40
];
42
41
unset($ messageXml );
43
42
44
43
return $ message ;
45
44
}
46
45
47
- /**
48
- * read default values in given message xml
49
- *
50
- * @param \SimpleXMLElement $message
51
- *
52
- * @return array
53
- */
54
- protected function readMessageDefaults (\SimpleXMLElement $ message ): array
55
- {
56
- // init
57
- $ defaults = [];
58
-
59
- /* @var \SimpleXMLElement $defaultValueNode */
60
- foreach ($ message ->defaults [0 ] ?? [] as $ defaultValueNode ) {
61
- $ attributes = $ defaultValueNode ->attributes ();
62
- $ id = (string )$ attributes ->id ;
63
- $ defaults [$ id ] = (string )$ attributes ->value ;
64
- }
65
-
66
- return $ defaults ;
67
- }
68
-
69
- /**
70
- * read message segments and groups
71
- *
72
- * @param \SimpleXMLElement $element
73
- *
74
- * @return array
75
- */
76
- protected function readXmlNodes (\SimpleXMLElement $ element ): array
77
- {
78
- $ arrayElements = [];
79
- foreach ($ element as $ name => $ node ) {
80
- if ($ name == "defaults " ) {
81
- continue ;
82
- }
83
- $ arrayElement = [];
84
- $ arrayElement ["type " ] = $ name ;
85
- $ arrayElement ["attributes " ] = $ this ->readAttributesArray ($ node );
86
- $ details = $ this ->readXmlNodes ($ node );
87
- if (!empty ($ details )) {
88
- $ arrayElement ["details " ] = $ details ;
89
- }
90
- $ arrayElements [] = $ arrayElement ;
91
- }
92
-
93
- return $ arrayElements ;
94
- }
95
-
96
- /**
97
- * return an xml elements attributes in as array
98
- *
99
- * @param \SimpleXMLElement $element
100
- *
101
- * @return array
102
- */
103
- protected function readAttributesArray (\SimpleXMLElement $ element ): array
104
- {
105
- $ attributes = [];
106
- foreach ($ element ->attributes () ?? [] as $ attrName => $ attr ) {
107
- $ attributes [(string )$ attrName ] = (string )$ attr ;
108
- }
109
-
110
- return $ attributes ;
111
- }
112
-
113
46
/**
114
47
* get all data element codes
115
48
*
@@ -135,15 +68,15 @@ public function loadCodesXml(string $codesXml)
135
68
continue ;
136
69
}
137
70
138
- $ id = (string )$ codeCollectionAttributes ->id ;
71
+ $ id = (string ) $ codeCollectionAttributes ->id ;
139
72
$ codes [$ id ] = [];
140
73
foreach ($ codeCollection as $ codeNode ) {
141
74
\assert ($ codeNode instanceof \SimpleXMLIterator);
142
75
143
76
$ codeAttributes = $ codeNode ->attributes ();
144
77
if ($ codeAttributes !== null ) {
145
- $ code = (string )$ codeAttributes ->id ;
146
- $ codes [$ id ][$ code ] = (string )$ codeAttributes ->desc ;
78
+ $ code = (string ) $ codeAttributes ->id ;
79
+ $ codes [$ id ][$ code ] = (string ) $ codeAttributes ->desc ;
147
80
}
148
81
}
149
82
}
@@ -183,12 +116,12 @@ public function loadSegmentsXml(string $segment_xml_file)
183
116
/* @var \SimpleXMLElement $segmentNode */
184
117
$ segmentNode = $ segmentNode ;
185
118
186
- $ qualifier = (string )$ segmentNode ->attributes ()->id ;
119
+ $ qualifier = (string ) $ segmentNode ->attributes ()->id ;
187
120
$ segment = [];
188
- $ segment [" attributes " ] = $ this ->readAttributesArray ($ segmentNode );
121
+ $ segment [' attributes ' ] = $ this ->readAttributesArray ($ segmentNode );
189
122
$ details = $ this ->readXmlNodes ($ segmentNode );
190
123
if (!empty ($ details )) {
191
- $ segment [" details " ] = $ details ;
124
+ $ segment [' details ' ] = $ details ;
192
125
}
193
126
$ this ->segments [$ qualifier ] = $ segment ;
194
127
}
@@ -208,7 +141,6 @@ public function process(array $data, array $rawSegments = null): string
208
141
{
209
142
$ r = [];
210
143
foreach ($ data as $ nrow => $ segment ) {
211
-
212
144
$ id = $ segment [0 ];
213
145
214
146
$ r [] = '' ;
@@ -218,22 +150,21 @@ public function process(array $data, array $rawSegments = null): string
218
150
}
219
151
220
152
if (isset ($ this ->segments [$ id ])) {
221
-
222
153
$ attributes = $ this ->segments [$ id ]['attributes ' ];
223
154
$ details_desc = $ this ->segments [$ id ]['details ' ];
224
155
225
156
$ r [] = $ id . ' - ' . $ attributes ['name ' ];
226
- $ r [] = ' ( ' . \wordwrap ($ attributes ['desc ' ], 75 , PHP_EOL . ' ' ) . ') ' ;
157
+ $ r [] = ' ( ' . \wordwrap ($ attributes ['desc ' ], 75 , \ PHP_EOL . ' ' ) . ') ' ;
227
158
228
- $ jsonelements = [" segmentCode " => $ id ];
159
+ $ jsonelements = [' segmentCode ' => $ id ];
229
160
foreach ($ segment as $ idx => $ detail ) {
230
161
$ n = $ idx - 1 ;
231
162
if ($ idx == 0 || !isset ($ details_desc [$ n ])) {
232
163
continue ;
233
164
}
234
165
$ d_desc_attr = $ details_desc [$ n ]['attributes ' ];
235
166
$ l1 = ' ' . $ d_desc_attr ['id ' ] . ' - ' . $ d_desc_attr ['name ' ];
236
- $ l2 = ' ' . \wordwrap ($ d_desc_attr ['desc ' ], 71 , PHP_EOL . ' ' );
167
+ $ l2 = ' ' . \wordwrap ($ d_desc_attr ['desc ' ], 71 , \ PHP_EOL . ' ' );
237
168
238
169
if (\is_array ($ detail )) {
239
170
$ r [] = ' [ ' . $ n . '] ' . \implode (', ' , $ detail );
@@ -248,7 +179,7 @@ public function process(array $data, array $rawSegments = null): string
248
179
$ d_sub_desc_attr = $ sub_details_desc [$ d_n ]['attributes ' ];
249
180
$ r [] = ' [ ' . $ d_n . '] ' . $ d_detail ;
250
181
$ r [] = ' id: ' . $ d_sub_desc_attr ['id ' ] . ' - ' . $ d_sub_desc_attr ['name ' ];
251
- $ r [] = ' ' . \wordwrap ($ d_sub_desc_attr ['desc ' ], 69 , PHP_EOL . ' ' );
182
+ $ r [] = ' ' . \wordwrap ($ d_sub_desc_attr ['desc ' ], 69 , \ PHP_EOL . ' ' );
252
183
$ r [] = ' type: ' . $ d_sub_desc_attr ['type ' ];
253
184
254
185
$ jsoncomposite [$ d_sub_desc_attr ['name ' ]] = $ d_detail ;
@@ -277,8 +208,7 @@ public function process(array $data, array $rawSegments = null): string
277
208
if (!empty($d_sub_desc_attr)) {
278
209
var_dump($d_sub_desc_attr);
279
210
}
280
- */
281
-
211
+ */
282
212
}
283
213
}
284
214
$ jsonelements [$ d_desc_attr ['name ' ]] = $ jsoncomposite ;
@@ -292,21 +222,87 @@ public function process(array $data, array $rawSegments = null): string
292
222
$ jsonsegment [$ attributes ['name ' ]] = $ jsonelements ;
293
223
} else {
294
224
$ r [] = $ id ;
295
- $ jsonsegment [" UnrecognisedType " ] = $ segment ;
225
+ $ jsonsegment [' UnrecognisedType ' ] = $ segment ;
296
226
}
297
227
$ this ->jsonedi [] = $ jsonsegment ;
298
228
}
299
229
300
- return \implode (PHP_EOL , $ r );
230
+ return \implode (\ PHP_EOL , $ r );
301
231
}
302
232
303
233
/**
304
234
* return the processed EDI in json format
305
235
*
306
- * @return string| false
236
+ * @return false|string
307
237
*/
308
238
public function getJson ()
309
239
{
310
240
return \json_encode ($ this ->jsonedi );
311
241
}
242
+
243
+ /**
244
+ * read default values in given message xml
245
+ *
246
+ * @param \SimpleXMLElement $message
247
+ *
248
+ * @return array
249
+ */
250
+ protected function readMessageDefaults (\SimpleXMLElement $ message ): array
251
+ {
252
+ // init
253
+ $ defaults = [];
254
+
255
+ /* @var \SimpleXMLElement $defaultValueNode */
256
+ foreach ($ message ->defaults [0 ] ?? [] as $ defaultValueNode ) {
257
+ $ attributes = $ defaultValueNode ->attributes ();
258
+ $ id = (string ) $ attributes ->id ;
259
+ $ defaults [$ id ] = (string ) $ attributes ->value ;
260
+ }
261
+
262
+ return $ defaults ;
263
+ }
264
+
265
+ /**
266
+ * read message segments and groups
267
+ *
268
+ * @param \SimpleXMLElement $element
269
+ *
270
+ * @return array
271
+ */
272
+ protected function readXmlNodes (\SimpleXMLElement $ element ): array
273
+ {
274
+ $ arrayElements = [];
275
+ foreach ($ element as $ name => $ node ) {
276
+ if ($ name == 'defaults ' ) {
277
+ continue ;
278
+ }
279
+ $ arrayElement = [];
280
+ $ arrayElement ['type ' ] = $ name ;
281
+ $ arrayElement ['attributes ' ] = $ this ->readAttributesArray ($ node );
282
+ $ details = $ this ->readXmlNodes ($ node );
283
+ if (!empty ($ details )) {
284
+ $ arrayElement ['details ' ] = $ details ;
285
+ }
286
+ $ arrayElements [] = $ arrayElement ;
287
+ }
288
+
289
+ return $ arrayElements ;
290
+ }
291
+
292
+ /**
293
+ * return an xml elements attributes in as array
294
+ *
295
+ * @param \SimpleXMLElement $element
296
+ *
297
+ * @return array
298
+ */
299
+ protected function readAttributesArray (\SimpleXMLElement $ element ): array
300
+ {
301
+ $ attributes = [];
302
+ foreach ($ element ->attributes () ?? [] as $ attrName => $ attr ) {
303
+ $ attributes [(string ) $ attrName ] = (string ) $ attr ;
304
+ }
305
+
306
+ return $ attributes ;
307
+ }
312
308
}
0 commit comments