14
14
15
15
use function array_filter ;
16
16
use function array_key_exists ;
17
+ use function array_map ;
17
18
use function array_reduce ;
18
19
use function array_values ;
19
20
use function count ;
@@ -35,22 +36,22 @@ public function __construct(
35
36
}
36
37
37
38
/**
38
- * @param array<string, mixed> $templates
39
+ * @param array<string|int , mixed> $templates
39
40
*/
40
41
public function main (Result $ result , array $ templates , Translator $ translator ): string
41
42
{
42
43
$ selectedTemplates = $ this ->selectTemplates ($ result , $ templates );
43
44
if (!$ this ->isFinalTemplate ($ result , $ selectedTemplates )) {
44
45
foreach ($ this ->extractDeduplicatedChildren ($ result ) as $ child ) {
45
- return $ this ->main ($ child , $ selectedTemplates , $ translator );
46
+ return $ this ->main ($ this -> resultWithPath ( $ result , $ child) , $ selectedTemplates , $ translator );
46
47
}
47
48
}
48
49
49
50
return $ this ->renderer ->render ($ this ->getTemplated ($ result , $ selectedTemplates ), $ translator );
50
51
}
51
52
52
53
/**
53
- * @param array<string, mixed> $templates
54
+ * @param array<string|int , mixed> $templates
54
55
*/
55
56
public function full (
56
57
Result $ result ,
@@ -68,13 +69,19 @@ public function full(
68
69
$ rendered .= sprintf (
69
70
'%s- %s ' . PHP_EOL ,
70
71
$ indentation ,
71
- $ this ->renderer ->render ($ this ->getTemplated ($ result , $ selectedTemplates ), $ translator ),
72
+ $ this ->renderer ->render (
73
+ $ this ->getTemplated ($ depth > 0 ? $ result ->withDeepestPath () : $ result , $ selectedTemplates ),
74
+ $ translator
75
+ ),
72
76
);
73
77
$ depth ++;
74
78
}
75
79
76
80
if (!$ isFinalTemplate ) {
77
- $ results = $ this ->extractDeduplicatedChildren ($ result );
81
+ $ results = array_map (
82
+ fn (Result $ child ) => $ this ->resultWithPath ($ result , $ child ),
83
+ $ this ->extractDeduplicatedChildren ($ result )
84
+ );
78
85
foreach ($ results as $ child ) {
79
86
$ rendered .= $ this ->full (
80
87
$ child ,
@@ -91,37 +98,44 @@ public function full(
91
98
}
92
99
93
100
/**
94
- * @param array<string, mixed> $templates
101
+ * @param array<string|int , mixed> $templates
95
102
*
96
- * @return array<string, mixed>
103
+ * @return array<string|int , mixed>
97
104
*/
98
105
public function array (Result $ result , array $ templates , Translator $ translator ): array
99
106
{
100
107
$ selectedTemplates = $ this ->selectTemplates ($ result , $ templates );
101
108
$ deduplicatedChildren = $ this ->extractDeduplicatedChildren ($ result );
102
109
if (count ($ deduplicatedChildren ) === 0 || $ this ->isFinalTemplate ($ result , $ selectedTemplates )) {
103
110
return [
104
- $ result ->id => $ this ->renderer ->render ($ this ->getTemplated ($ result , $ selectedTemplates ), $ translator ),
111
+ $ result ->getDeepestPath () ?? $ result ->id => $ this ->renderer ->render (
112
+ $ this ->getTemplated ($ result ->withDeepestPath (), $ selectedTemplates ),
113
+ $ translator
114
+ ),
105
115
];
106
116
}
107
117
108
118
$ messages = [];
109
119
foreach ($ deduplicatedChildren as $ child ) {
110
- $ messages [$ child ->id ] = $ this ->array (
111
- $ child ,
120
+ $ key = $ child ->getDeepestPath () ?? $ child ->id ;
121
+ $ messages [$ key ] = $ this ->array (
122
+ $ this ->resultWithPath ($ result , $ child ),
112
123
$ this ->selectTemplates ($ child , $ selectedTemplates ),
113
124
$ translator
114
125
);
115
- if (count ($ messages [$ child -> id ]) !== 1 ) {
126
+ if (count ($ messages [$ key ]) !== 1 ) {
116
127
continue ;
117
128
}
118
129
119
- $ messages [$ child -> id ] = current ($ messages [$ child -> id ]);
130
+ $ messages [$ key ] = current ($ messages [$ key ]);
120
131
}
121
132
122
133
if (count ($ messages ) > 1 ) {
123
134
$ self = [
124
- '__root__ ' => $ this ->renderer ->render ($ this ->getTemplated ($ result , $ selectedTemplates ), $ translator ),
135
+ '__root__ ' => $ this ->renderer ->render (
136
+ $ this ->getTemplated ($ result ->withDeepestPath (), $ selectedTemplates ),
137
+ $ translator
138
+ ),
125
139
];
126
140
127
141
return $ self + $ messages ;
@@ -130,6 +144,19 @@ public function array(Result $result, array $templates, Translator $translator):
130
144
return $ messages ;
131
145
}
132
146
147
+ public function resultWithPath (Result $ parent , Result $ child ): Result
148
+ {
149
+ if ($ parent ->path !== null && $ child ->path !== null && $ child ->path !== $ parent ->path ) {
150
+ return $ child ->withPath ($ parent ->path );
151
+ }
152
+
153
+ if ($ parent ->path !== null && $ child ->path === null ) {
154
+ return $ child ->withPath ($ parent ->path );
155
+ }
156
+
157
+ return $ child ;
158
+ }
159
+
133
160
private function isAlwaysVisible (Result $ result , Result ...$ siblings ): bool
134
161
{
135
162
if ($ result ->isValid ) {
@@ -165,56 +192,66 @@ private function isAlwaysVisible(Result $result, Result ...$siblings): bool
165
192
);
166
193
}
167
194
168
- /** @param array<string, mixed> $templates */
195
+ /** @param array<string|int , mixed> $templates */
169
196
private function getTemplated (Result $ result , array $ templates ): Result
170
197
{
171
198
if ($ result ->hasCustomTemplate ()) {
172
199
return $ result ;
173
200
}
174
201
175
- if (!isset ($ templates [$ result ->id ]) && isset ($ templates ['__root__ ' ])) {
176
- return $ result ->withTemplate ($ templates ['__root__ ' ]);
177
- }
202
+ foreach ([$ result ->path , $ result ->name , $ result ->id , '__root__ ' ] as $ key ) {
203
+ if (!isset ($ templates [$ key ])) {
204
+ continue ;
205
+ }
178
206
179
- if (! isset ($ templates [$ result -> id ])) {
180
- return $ result ;
181
- }
207
+ if (is_string ($ templates [$ key ])) {
208
+ return $ result-> withTemplate ( $ templates [ $ key ]) ;
209
+ }
182
210
183
- $ template = $ templates [ $ result -> id ];
184
- if ( is_string ( $ template )) {
185
- return $ result -> withTemplate ( $ template );
211
+ throw new ComponentException (
212
+ sprintf ( ' Template for "%s" must be a string, %s given ' , $ key , stringify ( $ templates [ $ key ]))
213
+ );
186
214
}
187
215
188
- throw new ComponentException (
189
- sprintf ('Template for "%s" must be a string, %s given ' , $ result ->id , stringify ($ template ))
190
- );
216
+ return $ result ;
191
217
}
192
218
193
219
/**
194
- * @param array<string, mixed> $templates
220
+ * @param array<string|int , mixed> $templates
195
221
*/
196
222
private function isFinalTemplate (Result $ result , array $ templates ): bool
197
223
{
198
- if (isset ($ templates [$ result ->id ]) && is_string ($ templates [$ result ->id ])) {
199
- return true ;
224
+ $ keys = [$ result ->path , $ result ->name , $ result ->id ];
225
+ foreach ($ keys as $ key ) {
226
+ if (isset ($ templates [$ key ]) && is_string ($ templates [$ key ])) {
227
+ return true ;
228
+ }
200
229
}
201
230
202
231
if (count ($ templates ) !== 1 ) {
203
232
return false ;
204
233
}
205
234
206
- return isset ($ templates ['__root__ ' ]) || isset ($ templates [$ result ->id ]);
235
+ foreach ($ keys as $ key ) {
236
+ if (isset ($ templates [$ key ])) {
237
+ return true ;
238
+ }
239
+ }
240
+
241
+ return isset ($ templates ['__root__ ' ]);
207
242
}
208
243
209
244
/**
210
- * @param array<string, mixed> $templates
245
+ * @param array<string|int , mixed> $templates
211
246
*
212
- * @return array<string, mixed>
247
+ * @return array<string|int , mixed>
213
248
*/
214
- private function selectTemplates (Result $ message , array $ templates ): array
249
+ private function selectTemplates (Result $ result , array $ templates ): array
215
250
{
216
- if (isset ($ templates [$ message ->id ]) && is_array ($ templates [$ message ->id ])) {
217
- return $ templates [$ message ->id ];
251
+ foreach ([$ result ->path , $ result ->name , $ result ->id ] as $ key ) {
252
+ if (isset ($ templates [$ key ]) && is_array ($ templates [$ key ])) {
253
+ return $ templates [$ key ];
254
+ }
218
255
}
219
256
220
257
return $ templates ;
@@ -227,7 +264,7 @@ private function extractDeduplicatedChildren(Result $result): array
227
264
$ deduplicatedResults = [];
228
265
$ duplicateCounters = [];
229
266
foreach ($ result ->children as $ child ) {
230
- $ id = $ child ->id ;
267
+ $ id = $ child ->getDeepestPath () ?? $ child -> id ;
231
268
if (isset ($ duplicateCounters [$ id ])) {
232
269
$ id .= '. ' . ++$ duplicateCounters [$ id ];
233
270
} elseif (array_key_exists ($ id , $ deduplicatedResults )) {
@@ -236,7 +273,13 @@ private function extractDeduplicatedChildren(Result $result): array
236
273
$ duplicateCounters [$ id ] = 2 ;
237
274
$ id .= '.2 ' ;
238
275
}
239
- $ deduplicatedResults [$ id ] = $ child ->isValid ? null : $ child ->withId ($ id );
276
+
277
+ if ($ child ->path === null ) {
278
+ $ deduplicatedResults [$ id ] = $ child ->isValid ? null : $ child ->withId ($ id );
279
+ continue ;
280
+ }
281
+
282
+ $ deduplicatedResults [$ id ] = $ child ->isValid ? null : $ child ;
240
283
}
241
284
242
285
return array_values (array_filter ($ deduplicatedResults ));
0 commit comments