@@ -40,7 +40,7 @@ class Interpreter
4040 ];
4141
4242 /**
43- *
43+ * @var bool
4444 */
4545 private $ patchFiles = true ;
4646
@@ -84,13 +84,18 @@ class Interpreter
8484 */
8585 private $ comparisonFunction ;
8686
87+ /**
88+ * @var string
89+ */
90+ private $ outputKey = 'name ' ;
91+
8792 /**
8893 * Split multiple messages and process
8994 *
90- * @param string $xmlMsg Path to XML Message representation
91- * @param array $xmlSeg Segments processed by EDI\Analyser::loadSegmentsXml
92- * @param array $xmlSvc Service segments processed by EDI\Analyser::loadSegmentsXml
93- * @param array|null $messageTextConf Personalisation of error messages
95+ * @param string $xmlMsg Path to XML Message representation
96+ * @param array $xmlSeg Segments processed by EDI\Analyser::loadSegmentsXml
97+ * @param array $xmlSvc Service segments processed by EDI\Analyser::loadSegmentsXml
98+ * @param array|null $messageTextConf Personalisation of error messages
9499 */
95100 public function __construct (string $ xmlMsg , array $ xmlSeg , array $ xmlSvc , array $ messageTextConf = null )
96101 {
@@ -118,6 +123,11 @@ public function __construct(string $xmlMsg, array $xmlSeg, array $xmlSvc, array
118123 };
119124 }
120125
126+ /**
127+ * @param bool $flag
128+ *
129+ * @return void
130+ */
121131 public function togglePatching (bool $ flag )
122132 {
123133 $ this ->patchFiles = $ flag ;
@@ -171,6 +181,23 @@ public function setComparisonFunction(callable $func)
171181 $ this ->comparisonFunction = $ func ;
172182 }
173183
184+ /**
185+ * Set to true if UNCEFACT XML ID should be used instead of names
186+ *
187+ * @param bool $toggle
188+ *
189+ * @return void
190+ */
191+ public function toggleUseIdInsteadOfNameForOutput (bool $ toggle )
192+ {
193+ if ($ toggle ) {
194+ $ this ->outputKey = 'id ' ;
195+ }
196+ else {
197+ $ this ->outputKey = 'name ' ;
198+ }
199+ }
200+
174201 /**
175202 * Split multiple messages and process
176203 *
@@ -601,32 +628,32 @@ private function processSegment(array &$segment, array &$xmlMap, $segmentIdx, ar
601628 }
602629
603630 $ d_sub_desc_attr = $ sub_details_desc [$ d_n ]['attributes ' ];
604- if (!isset ($ jsoncomposite [$ d_sub_desc_attr [' name ' ]])) { //New
605- $ jsoncomposite [$ d_sub_desc_attr [' name ' ]] = $ d_detail ;
606- } elseif (\is_string ($ jsoncomposite [$ d_sub_desc_attr [' name ' ]])) { // More data than one string
607- $ jsoncomposite [$ d_sub_desc_attr [' name ' ]] = [
608- $ jsoncomposite [$ d_sub_desc_attr [' name ' ]],
631+ if (!isset ($ jsoncomposite [$ d_sub_desc_attr [$ this -> outputKey ]])) { //New
632+ $ jsoncomposite [$ d_sub_desc_attr [$ this -> outputKey ]] = $ d_detail ;
633+ } elseif (\is_string ($ jsoncomposite [$ d_sub_desc_attr [$ this -> outputKey ]])) { // More data than one string
634+ $ jsoncomposite [$ d_sub_desc_attr [$ this -> outputKey ]] = [
635+ $ jsoncomposite [$ d_sub_desc_attr [$ this -> outputKey ]],
609636 $ d_detail ,
610637 ];
611638 } else { // More and more
612- $ jsoncomposite [$ d_sub_desc_attr [' name ' ]][] = $ d_detail ;
639+ $ jsoncomposite [$ d_sub_desc_attr [$ this -> outputKey ]][] = $ d_detail ;
613640 }
614641 }
615642 } else {
616643 $ d_sub_desc_attr = $ sub_details_desc [0 ]['attributes ' ];
617- $ jsoncomposite [$ d_sub_desc_attr [' name ' ]] = $ detail ;
644+ $ jsoncomposite [$ d_sub_desc_attr [$ this -> outputKey ]] = $ detail ;
618645 }
619646 } else {
620647 $ jsoncomposite = $ detail ;
621648 }
622649
623- if (\array_key_exists ($ d_desc_attr [' name ' ], $ jsonelements )) {
624- $ jsonelements [$ d_desc_attr [' name ' ] . $ n ] = $ jsoncomposite ;
650+ if (\array_key_exists ($ d_desc_attr [$ this -> outputKey ], $ jsonelements )) {
651+ $ jsonelements [$ d_desc_attr [$ this -> outputKey ] . $ n ] = $ jsoncomposite ;
625652 } else {
626- $ jsonelements [$ d_desc_attr [' name ' ]] = $ jsoncomposite ;
653+ $ jsonelements [$ d_desc_attr [$ this -> outputKey ]] = $ jsoncomposite ;
627654 }
628655 }
629- $ jsonsegment ['key ' ] = $ attributes [' name ' ];
656+ $ jsonsegment ['key ' ] = $ attributes [$ this -> outputKey ];
630657 $ jsonsegment ['value ' ] = $ jsonelements ;
631658 } elseif ($ xmlMap !== $ this ->xmlSvc ) {
632659 $ jsonsegment = $ this ->processSegment ($ segment , $ this ->xmlSvc , $ segmentIdx , $ errors );
0 commit comments