@@ -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,46 @@ 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 );
101101 $ 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- }
107102
108- $ messages = [];
103+ $ messages = [
104+ '__root__ ' => $ this ->renderer ->render ($ this ->getTemplated ($ result , $ selectedTemplates ), $ translator ),
105+ ];
106+
107+ $ children = [];
108+
109109 foreach ($ deduplicatedChildren as $ child ) {
110- $ messages [$ child ->id ] = $ this ->array (
110+ $ childKey = $ child ->path ?? $ child ->id ;
111+
112+ $ children [$ childKey ] = $ this ->array (
111113 $ child ,
112114 $ this ->selectTemplates ($ child , $ selectedTemplates ),
113115 $ translator
114116 );
115- if (count ($ messages [$ child ->id ]) !== 1 ) {
117+
118+ if (count ($ children [$ childKey ]) !== 1 ) {
116119 continue ;
117120 }
118121
119- $ messages [ $ child -> id ] = current ($ messages [ $ child -> id ]);
122+ $ children [ $ childKey ] = current ($ children [ $ childKey ]);
120123 }
121124
122- if (count ($ messages ) > 1 ) {
123- $ self = [
124- '__root__ ' => $ this ->renderer ->render ($ this ->getTemplated ($ result , $ selectedTemplates ), $ translator ),
125- ];
125+ if (count ($ children ) === 0 || $ this ->isFinalTemplate ($ result , $ selectedTemplates )) {
126+ return [$ result ->path ?? $ result ->id => $ messages ['__root__ ' ]];
127+ }
126128
127- return $ self + $ messages ;
129+ if ($ result ->path !== null ) {
130+ return [$ result ->path => $ messages + $ children ];
128131 }
129132
130- return $ messages ;
133+ return $ messages + $ children ;
131134 }
132135
133136 private function isAlwaysVisible (Result $ result , Result ...$ siblings ): bool
@@ -165,56 +168,69 @@ private function isAlwaysVisible(Result $result, Result ...$siblings): bool
165168 );
166169 }
167170
168- /** @param array<string, mixed> $templates */
171+ /** @param array<string|int , mixed> $templates */
169172 private function getTemplated (Result $ result , array $ templates ): Result
170173 {
171174 if ($ result ->hasCustomTemplate ()) {
172175 return $ result ;
173176 }
174177
175- if (!isset ($ templates [$ result ->id ]) && isset ($ templates ['__root__ ' ])) {
176- return $ result ->withTemplate ($ templates ['__root__ ' ]);
178+ $ keys = [$ result ->name , $ result ->path , $ result ->id , '__root__ ' ];
179+ foreach ($ keys as $ key ) {
180+ if (isset ($ templates [$ key ]) && is_string ($ templates [$ key ])) {
181+ return $ result ->withTemplate ($ templates [$ key ]);
182+ }
177183 }
178184
179- if (!isset ($ templates [$ result ->id ])) {
185+ if (!isset ($ templates [$ result ->id ]) && ! isset ( $ templates [ $ result -> path ]) && ! isset ( $ templates [ $ result -> name ]) ) {
180186 return $ result ;
181187 }
182188
183- $ template = $ templates [$ result ->id ];
184- if (is_string ($ template )) {
185- return $ result ->withTemplate ($ template );
186- }
187-
188189 throw new ComponentException (
189- sprintf ('Template for "%s" must be a string, %s given ' , $ result ->id , stringify ($ template ))
190+ sprintf (
191+ 'Template for "%s" must be a string, %s given ' ,
192+ $ result ->path ?? $ result ->name ?? $ result ->id ,
193+ stringify ($ templates )
194+ )
190195 );
191196 }
192197
193198 /**
194- * @param array<string, mixed> $templates
199+ * @param array<string|int , mixed> $templates
195200 */
196201 private function isFinalTemplate (Result $ result , array $ templates ): bool
197202 {
198- if (isset ($ templates [$ result ->id ]) && is_string ($ templates [$ result ->id ])) {
199- return true ;
203+ $ keys = [$ result ->name , $ result ->path , $ result ->id ];
204+ foreach ($ keys as $ key ) {
205+ if (isset ($ templates [$ key ]) && is_string ($ templates [$ key ])) {
206+ return true ;
207+ }
200208 }
201209
202210 if (count ($ templates ) !== 1 ) {
203211 return false ;
204212 }
205213
206- return isset ($ templates ['__root__ ' ]) || isset ($ templates [$ result ->id ]);
214+ foreach ($ keys as $ key ) {
215+ if (isset ($ templates [$ key ])) {
216+ return true ;
217+ }
218+ }
219+
220+ return isset ($ templates ['__root__ ' ]);
207221 }
208222
209223 /**
210- * @param array<string, mixed> $templates
224+ * @param array<string|int , mixed> $templates
211225 *
212- * @return array<string, mixed>
226+ * @return array<string|int , mixed>
213227 */
214- private function selectTemplates (Result $ message , array $ templates ): array
228+ private function selectTemplates (Result $ result , array $ templates ): array
215229 {
216- if (isset ($ templates [$ message ->id ]) && is_array ($ templates [$ message ->id ])) {
217- return $ templates [$ message ->id ];
230+ foreach ([$ result ->name , $ result ->path , $ result ->id ] as $ key ) {
231+ if (isset ($ templates [$ key ]) && is_array ($ templates [$ key ])) {
232+ return $ templates [$ key ];
233+ }
218234 }
219235
220236 return $ templates ;
@@ -227,7 +243,7 @@ private function extractDeduplicatedChildren(Result $result): array
227243 $ deduplicatedResults = [];
228244 $ duplicateCounters = [];
229245 foreach ($ result ->children as $ child ) {
230- $ id = $ child ->id ;
246+ $ id = $ child ->path ?? $ child -> id ;
231247 if (isset ($ duplicateCounters [$ id ])) {
232248 $ id .= '. ' . ++$ duplicateCounters [$ id ];
233249 } elseif (array_key_exists ($ id , $ deduplicatedResults )) {
@@ -236,7 +252,7 @@ private function extractDeduplicatedChildren(Result $result): array
236252 $ duplicateCounters [$ id ] = 2 ;
237253 $ id .= '.2 ' ;
238254 }
239- $ deduplicatedResults [$ id ] = $ child ->isValid ? null : $ child ->withId ($ id );
255+ $ deduplicatedResults [$ id ] = $ child ->isValid ? null : $ child ->withId (( string ) $ id );
240256 }
241257
242258 return array_values (array_filter ($ deduplicatedResults ));
0 commit comments