1010 */
1111class Analyser
1212{
13-
1413 /**
1514 * @var array<mixed>
1615 */
17- public $ segments ;
16+ public $ segments ;
1817
1918 /**
2019 * @var array<mixed>
@@ -36,80 +35,14 @@ public function loadMessageXml(string $message_xml_file)
3635 $ messageXml = new \SimpleXMLIterator ($ messageXmlString );
3736 unset($ messageXmlString );
3837 $ message = [
39- " defaults " => $ this ->readMessageDefaults ($ messageXml ),
40- " segments " => $ this ->readXmlNodes ($ messageXml ),
38+ ' defaults ' => $ this ->readMessageDefaults ($ messageXml ),
39+ ' segments ' => $ this ->readXmlNodes ($ messageXml ),
4140 ];
4241 unset($ messageXml );
4342
4443 return $ message ;
4544 }
4645
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-
11346 /**
11447 * get all data element codes
11548 *
@@ -135,15 +68,15 @@ public function loadCodesXml(string $codesXml)
13568 continue ;
13669 }
13770
138- $ id = (string )$ codeCollectionAttributes ->id ;
71+ $ id = (string ) $ codeCollectionAttributes ->id ;
13972 $ codes [$ id ] = [];
14073 foreach ($ codeCollection as $ codeNode ) {
14174 \assert ($ codeNode instanceof \SimpleXMLIterator);
14275
14376 $ codeAttributes = $ codeNode ->attributes ();
14477 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 ;
14780 }
14881 }
14982 }
@@ -183,12 +116,12 @@ public function loadSegmentsXml(string $segment_xml_file)
183116 /* @var \SimpleXMLElement $segmentNode */
184117 $ segmentNode = $ segmentNode ;
185118
186- $ qualifier = (string )$ segmentNode ->attributes ()->id ;
119+ $ qualifier = (string ) $ segmentNode ->attributes ()->id ;
187120 $ segment = [];
188- $ segment [" attributes " ] = $ this ->readAttributesArray ($ segmentNode );
121+ $ segment [' attributes ' ] = $ this ->readAttributesArray ($ segmentNode );
189122 $ details = $ this ->readXmlNodes ($ segmentNode );
190123 if (!empty ($ details )) {
191- $ segment [" details " ] = $ details ;
124+ $ segment [' details ' ] = $ details ;
192125 }
193126 $ this ->segments [$ qualifier ] = $ segment ;
194127 }
@@ -208,7 +141,6 @@ public function process(array $data, array $rawSegments = null): string
208141 {
209142 $ r = [];
210143 foreach ($ data as $ nrow => $ segment ) {
211-
212144 $ id = $ segment [0 ];
213145
214146 $ r [] = '' ;
@@ -218,22 +150,21 @@ public function process(array $data, array $rawSegments = null): string
218150 }
219151
220152 if (isset ($ this ->segments [$ id ])) {
221-
222153 $ attributes = $ this ->segments [$ id ]['attributes ' ];
223154 $ details_desc = $ this ->segments [$ id ]['details ' ];
224155
225156 $ r [] = $ id . ' - ' . $ attributes ['name ' ];
226- $ r [] = ' ( ' . \wordwrap ($ attributes ['desc ' ], 75 , PHP_EOL . ' ' ) . ') ' ;
157+ $ r [] = ' ( ' . \wordwrap ($ attributes ['desc ' ], 75 , \ PHP_EOL . ' ' ) . ') ' ;
227158
228- $ jsonelements = [" segmentCode " => $ id ];
159+ $ jsonelements = [' segmentCode ' => $ id ];
229160 foreach ($ segment as $ idx => $ detail ) {
230161 $ n = $ idx - 1 ;
231162 if ($ idx == 0 || !isset ($ details_desc [$ n ])) {
232163 continue ;
233164 }
234165 $ d_desc_attr = $ details_desc [$ n ]['attributes ' ];
235166 $ 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 . ' ' );
237168
238169 if (\is_array ($ detail )) {
239170 $ r [] = ' [ ' . $ n . '] ' . \implode (', ' , $ detail );
@@ -248,7 +179,7 @@ public function process(array $data, array $rawSegments = null): string
248179 $ d_sub_desc_attr = $ sub_details_desc [$ d_n ]['attributes ' ];
249180 $ r [] = ' [ ' . $ d_n . '] ' . $ d_detail ;
250181 $ 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 . ' ' );
252183 $ r [] = ' type: ' . $ d_sub_desc_attr ['type ' ];
253184
254185 $ jsoncomposite [$ d_sub_desc_attr ['name ' ]] = $ d_detail ;
@@ -277,8 +208,7 @@ public function process(array $data, array $rawSegments = null): string
277208 if (!empty($d_sub_desc_attr)) {
278209 var_dump($d_sub_desc_attr);
279210 }
280- */
281-
211+ */
282212 }
283213 }
284214 $ jsonelements [$ d_desc_attr ['name ' ]] = $ jsoncomposite ;
@@ -292,21 +222,87 @@ public function process(array $data, array $rawSegments = null): string
292222 $ jsonsegment [$ attributes ['name ' ]] = $ jsonelements ;
293223 } else {
294224 $ r [] = $ id ;
295- $ jsonsegment [" UnrecognisedType " ] = $ segment ;
225+ $ jsonsegment [' UnrecognisedType ' ] = $ segment ;
296226 }
297227 $ this ->jsonedi [] = $ jsonsegment ;
298228 }
299229
300- return \implode (PHP_EOL , $ r );
230+ return \implode (\ PHP_EOL , $ r );
301231 }
302232
303233 /**
304234 * return the processed EDI in json format
305235 *
306- * @return string| false
236+ * @return false|string
307237 */
308238 public function getJson ()
309239 {
310240 return \json_encode ($ this ->jsonedi );
311241 }
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+ }
312308}
0 commit comments