@@ -35,7 +35,7 @@ public function __construct(
3535 }
3636
3737 /**
38- * @param array<string, mixed> $templates
38+ * @param array<string|int , mixed> $templates
3939 */
4040 public function main (Result $ result , array $ templates , Translator $ translator ): string
4141 {
@@ -50,7 +50,7 @@ public function main(Result $result, array $templates, Translator $translator):
5050 }
5151
5252 /**
53- * @param array<string, mixed> $templates
53+ * @param array<string|int , mixed> $templates
5454 */
5555 public function full (
5656 Result $ result ,
@@ -91,43 +91,43 @@ public function full(
9191 }
9292
9393 /**
94- * @param array<string, mixed> $templates
94+ * @param array<string|int , mixed> $templates
9595 *
96- * @return array<string, mixed>
96+ * @return array<string|int , mixed>
9797 */
9898 public function array (Result $ result , array $ templates , Translator $ translator ): array
9999 {
100100 $ selectedTemplates = $ this ->selectTemplates ($ result , $ templates );
101- $ 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- ];
106- }
101+ $ messages = [
102+ '__root__ ' => $ this ->renderer ->render ($ this ->getTemplated ($ result , $ selectedTemplates ), $ translator ),
103+ ];
104+
105+ $ children = [];
106+ foreach ($ this ->extractDeduplicatedChildren ($ result ) as $ child ) {
107+ $ childKey = $ child ->path ?? $ child ->id ;
107108
108- $ messages = [];
109- foreach ($ deduplicatedChildren as $ child ) {
110- $ messages [$ child ->id ] = $ this ->array (
109+ $ children [$ childKey ] = $ this ->array (
111110 $ child ,
112111 $ this ->selectTemplates ($ child , $ selectedTemplates ),
113112 $ translator
114113 );
115- if (count ($ messages [$ child ->id ]) !== 1 ) {
114+
115+ if (count ($ children [$ childKey ]) !== 1 ) {
116116 continue ;
117117 }
118118
119- $ messages [ $ child -> id ] = current ($ messages [ $ child -> id ]);
119+ $ children [ $ childKey ] = current ($ children [ $ childKey ]);
120120 }
121121
122- if (count ($ messages ) > 1 ) {
123- $ self = [
124- '__root__ ' => $ this ->renderer ->render ($ this ->getTemplated ($ result , $ selectedTemplates ), $ translator ),
125- ];
122+ if (count ($ children ) === 0 || $ this ->isFinalTemplate ($ result , $ selectedTemplates )) {
123+ return [$ result ->path ?? $ result ->id => $ messages ['__root__ ' ]];
124+ }
126125
127- return $ self + $ messages ;
126+ if ($ result ->path !== null ) {
127+ return [$ result ->path => $ messages + $ children ];
128128 }
129129
130- return $ messages ;
130+ return $ messages + $ children ;
131131 }
132132
133133 private function isAlwaysVisible (Result $ result , Result ...$ siblings ): bool
@@ -165,56 +165,66 @@ private function isAlwaysVisible(Result $result, Result ...$siblings): bool
165165 );
166166 }
167167
168- /** @param array<string, mixed> $templates */
168+ /** @param array<string|int , mixed> $templates */
169169 private function getTemplated (Result $ result , array $ templates ): Result
170170 {
171171 if ($ result ->hasCustomTemplate ()) {
172172 return $ result ;
173173 }
174174
175- if (!isset ($ templates [$ result ->id ]) && isset ($ templates ['__root__ ' ])) {
176- return $ result ->withTemplate ($ templates ['__root__ ' ]);
177- }
175+ foreach ([$ result ->path , $ result ->name , $ result ->id , '__root__ ' ] as $ key ) {
176+ if (!isset ($ templates [$ key ])) {
177+ continue ;
178+ }
178179
179- if (! isset ($ templates [$ result -> id ])) {
180- return $ result ;
181- }
180+ if (is_string ($ templates [$ key ])) {
181+ return $ result-> withTemplate ( $ templates [ $ key ]) ;
182+ }
182183
183- $ template = $ templates [ $ result -> id ];
184- if ( is_string ( $ template )) {
185- return $ result -> withTemplate ( $ template );
184+ throw new ComponentException (
185+ sprintf ( ' Template for "%s" must be a string, %s given ' , $ key , stringify ( $ templates [ $ key ]))
186+ );
186187 }
187188
188- throw new ComponentException (
189- sprintf ('Template for "%s" must be a string, %s given ' , $ result ->id , stringify ($ template ))
190- );
189+ return $ result ;
191190 }
192191
193192 /**
194- * @param array<string, mixed> $templates
193+ * @param array<string|int , mixed> $templates
195194 */
196195 private function isFinalTemplate (Result $ result , array $ templates ): bool
197196 {
198- if (isset ($ templates [$ result ->id ]) && is_string ($ templates [$ result ->id ])) {
199- return true ;
197+ $ keys = [$ result ->path , $ result ->name , $ result ->id ];
198+ foreach ($ keys as $ key ) {
199+ if (isset ($ templates [$ key ]) && is_string ($ templates [$ key ])) {
200+ return true ;
201+ }
200202 }
201203
202204 if (count ($ templates ) !== 1 ) {
203205 return false ;
204206 }
205207
206- return isset ($ templates ['__root__ ' ]) || isset ($ templates [$ result ->id ]);
208+ foreach ($ keys as $ key ) {
209+ if (isset ($ templates [$ key ])) {
210+ return true ;
211+ }
212+ }
213+
214+ return isset ($ templates ['__root__ ' ]);
207215 }
208216
209217 /**
210- * @param array<string, mixed> $templates
218+ * @param array<string|int , mixed> $templates
211219 *
212- * @return array<string, mixed>
220+ * @return array<string|int , mixed>
213221 */
214- private function selectTemplates (Result $ message , array $ templates ): array
222+ private function selectTemplates (Result $ result , array $ templates ): array
215223 {
216- if (isset ($ templates [$ message ->id ]) && is_array ($ templates [$ message ->id ])) {
217- return $ templates [$ message ->id ];
224+ foreach ([$ result ->path , $ result ->name , $ result ->id ] as $ key ) {
225+ if (isset ($ templates [$ key ]) && is_array ($ templates [$ key ])) {
226+ return $ templates [$ key ];
227+ }
218228 }
219229
220230 return $ templates ;
@@ -227,7 +237,7 @@ private function extractDeduplicatedChildren(Result $result): array
227237 $ deduplicatedResults = [];
228238 $ duplicateCounters = [];
229239 foreach ($ result ->children as $ child ) {
230- $ id = $ child ->id ;
240+ $ id = $ child ->path ?? $ child -> id ;
231241 if (isset ($ duplicateCounters [$ id ])) {
232242 $ id .= '. ' . ++$ duplicateCounters [$ id ];
233243 } elseif (array_key_exists ($ id , $ deduplicatedResults )) {
@@ -236,7 +246,7 @@ private function extractDeduplicatedChildren(Result $result): array
236246 $ duplicateCounters [$ id ] = 2 ;
237247 $ id .= '.2 ' ;
238248 }
239- $ deduplicatedResults [$ id ] = $ child ->isValid ? null : $ child ->withId ($ id );
249+ $ deduplicatedResults [$ id ] = $ child ->isValid ? null : $ child ->withId (( string ) $ id );
240250 }
241251
242252 return array_values (array_filter ($ deduplicatedResults ));
0 commit comments