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