@@ -42,6 +42,12 @@ class Interpreter
42
42
*/
43
43
private $ patchFiles = true ;
44
44
45
+ /**
46
+ * @var bool
47
+ */
48
+ private $ forceArrayWhenRepeatable = false ;
49
+
50
+
45
51
/**
46
52
* @var \SimpleXMLElement
47
53
*/
@@ -141,6 +147,16 @@ public function togglePatching(bool $flag)
141
147
$ this ->patchFiles = $ flag ;
142
148
}
143
149
150
+ /**
151
+ * @param bool $flag
152
+ *
153
+ * @return void
154
+ */
155
+ public function forceArrayWhenRepeatable (bool $ flag )
156
+ {
157
+ $ this ->forceArrayWhenRepeatable = $ flag ;
158
+ }
159
+
144
160
/**
145
161
* Patch the error messages array
146
162
*
@@ -525,7 +541,7 @@ private function processXmlSegment(\SimpleXMLElement $elm, array &$message, int
525
541
if (\call_user_func ($ this ->comparisonFunction , $ message [$ segmentIdx ], $ elm )) {
526
542
$ jsonMessage = $ this ->processSegment ($ message [$ segmentIdx ], $ this ->xmlSeg , $ segmentIdx , $ errors );
527
543
$ segmentVisited = true ;
528
- $ this ->doAddArray ($ array , $ jsonMessage );
544
+ $ this ->doAddArray ($ array , $ jsonMessage, ( int ) $ elm [ ' maxrepeat ' ] );
529
545
++$ segmentIdx ;
530
546
} else {
531
547
if (!$ segmentVisited && isset ($ elm ['required ' ])) {
@@ -540,7 +556,7 @@ private function processXmlSegment(\SimpleXMLElement $elm, array &$message, int
540
556
if ($ this ->patchFiles && isset ($ this ->segmentTemplates [$ elmType ])) {
541
557
$ jsonMessage = $ this ->processSegment ($ this ->segmentTemplates [$ elmType ], $ this ->xmlSeg , $ segmentIdx , $ errors );
542
558
$ fixed = true ;
543
- $ this ->doAddArray ($ array , $ jsonMessage );
559
+ $ this ->doAddArray ($ array , $ jsonMessage, ( int ) $ elm [ ' maxrepeat ' ] );
544
560
}
545
561
546
562
$ errors [] = [
@@ -563,7 +579,7 @@ private function processXmlSegment(\SimpleXMLElement $elm, array &$message, int
563
579
*
564
580
* @return void
565
581
*/
566
- private function doAddArray (array &$ array , array &$ jsonMessage )
582
+ private function doAddArray (array &$ array , array &$ jsonMessage, $ maxRepeat = 1 )
567
583
{
568
584
if (isset ($ array [$ jsonMessage ['key ' ]])) {
569
585
if (
@@ -578,6 +594,12 @@ private function doAddArray(array &$array, array &$jsonMessage)
578
594
$ array [$ jsonMessage ['key ' ]][] = $ jsonMessage ['value ' ];
579
595
} else {
580
596
$ array [$ jsonMessage ['key ' ]] = $ jsonMessage ['value ' ];
597
+ // if segment can be repeated then the flag forces to be an array also
598
+ // if there's only one segment
599
+ if ($ maxRepeat > 1 && $ this ->forceArrayWhenRepeatable ) {
600
+ d ('lol ' , $ jsonMessage , $ maxRepeat );
601
+ $ array [$ jsonMessage ['key ' ]] = [$ jsonMessage ['value ' ]];
602
+ }
581
603
}
582
604
}
583
605
0 commit comments