@@ -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,41 @@ 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 ) {
116- continue ;
117- }
118114
119- $ messages [$ child ->id ] = current ($ messages [$ child ->id ]);
115+ if (count ($ children [$ childKey ]) === 1 ) {
116+ $ children [$ childKey ] = current ($ children [$ childKey ]);
117+ }
120118 }
121119
122- if (count ($ messages ) > 1 ) {
123- $ self = [
124- '__root__ ' => $ this ->renderer ->render ($ this ->getTemplated ($ result , $ selectedTemplates ), $ translator ),
125- ];
120+ if (count ($ children ) === 0 || $ this ->isFinalTemplate ($ result , $ selectedTemplates )) {
121+ return [$ result ->path ?? $ result ->id => $ messages ['__root__ ' ]];
122+ }
126123
127- return $ self + $ messages ;
124+ if ($ result ->path !== null ) {
125+ return [$ result ->path => $ messages + $ children ];
128126 }
129127
130- return $ messages ;
128+ return $ messages + $ children ;
131129 }
132130
133131 private function isAlwaysVisible (Result $ result , Result ...$ siblings ): bool
@@ -165,56 +163,66 @@ private function isAlwaysVisible(Result $result, Result ...$siblings): bool
165163 );
166164 }
167165
168- /** @param array<string, mixed> $templates */
166+ /** @param array<string|int , mixed> $templates */
169167 private function getTemplated (Result $ result , array $ templates ): Result
170168 {
171169 if ($ result ->hasCustomTemplate ()) {
172170 return $ result ;
173171 }
174172
175- if (!isset ($ templates [$ result ->id ]) && isset ($ templates ['__root__ ' ])) {
176- return $ result ->withTemplate ($ templates ['__root__ ' ]);
177- }
173+ foreach ([$ result ->path , $ result ->name , $ result ->id , '__root__ ' ] as $ key ) {
174+ if (!isset ($ templates [$ key ])) {
175+ continue ;
176+ }
178177
179- if (! isset ($ templates [$ result -> id ])) {
180- return $ result ;
181- }
178+ if (is_string ($ templates [$ key ])) {
179+ return $ result-> withTemplate ( $ templates [ $ key ]) ;
180+ }
182181
183- $ template = $ templates [ $ result -> id ];
184- if ( is_string ( $ template )) {
185- return $ result -> withTemplate ( $ template );
182+ throw new ComponentException (
183+ sprintf ( ' Template for "%s" must be a string, %s given ' , $ key , stringify ( $ templates [ $ key ]))
184+ );
186185 }
187186
188- throw new ComponentException (
189- sprintf ('Template for "%s" must be a string, %s given ' , $ result ->id , stringify ($ template ))
190- );
187+ return $ result ;
191188 }
192189
193190 /**
194- * @param array<string, mixed> $templates
191+ * @param array<string|int , mixed> $templates
195192 */
196193 private function isFinalTemplate (Result $ result , array $ templates ): bool
197194 {
198- if (isset ($ templates [$ result ->id ]) && is_string ($ templates [$ result ->id ])) {
199- return true ;
195+ $ keys = [$ result ->path , $ result ->name , $ result ->id ];
196+ foreach ($ keys as $ key ) {
197+ if (isset ($ templates [$ key ]) && is_string ($ templates [$ key ])) {
198+ return true ;
199+ }
200200 }
201201
202202 if (count ($ templates ) !== 1 ) {
203203 return false ;
204204 }
205205
206- return isset ($ templates ['__root__ ' ]) || isset ($ templates [$ result ->id ]);
206+ foreach ($ keys as $ key ) {
207+ if (isset ($ templates [$ key ])) {
208+ return true ;
209+ }
210+ }
211+
212+ return isset ($ templates ['__root__ ' ]);
207213 }
208214
209215 /**
210- * @param array<string, mixed> $templates
216+ * @param array<string|int , mixed> $templates
211217 *
212- * @return array<string, mixed>
218+ * @return array<string|int , mixed>
213219 */
214- private function selectTemplates (Result $ message , array $ templates ): array
220+ private function selectTemplates (Result $ result , array $ templates ): array
215221 {
216- if (isset ($ templates [$ message ->id ]) && is_array ($ templates [$ message ->id ])) {
217- return $ templates [$ message ->id ];
222+ foreach ([$ result ->path , $ result ->name , $ result ->id ] as $ key ) {
223+ if (isset ($ templates [$ key ]) && is_array ($ templates [$ key ])) {
224+ return $ templates [$ key ];
225+ }
218226 }
219227
220228 return $ templates ;
@@ -227,7 +235,7 @@ private function extractDeduplicatedChildren(Result $result): array
227235 $ deduplicatedResults = [];
228236 $ duplicateCounters = [];
229237 foreach ($ result ->children as $ child ) {
230- $ id = $ child ->id ;
238+ $ id = $ child ->path ?? $ child -> id ;
231239 if (isset ($ duplicateCounters [$ id ])) {
232240 $ id .= '. ' . ++$ duplicateCounters [$ id ];
233241 } elseif (array_key_exists ($ id , $ deduplicatedResults )) {
@@ -236,7 +244,7 @@ private function extractDeduplicatedChildren(Result $result): array
236244 $ duplicateCounters [$ id ] = 2 ;
237245 $ id .= '.2 ' ;
238246 }
239- $ deduplicatedResults [$ id ] = $ child ->isValid ? null : $ child ->withId ($ id );
247+ $ deduplicatedResults [$ id ] = $ child ->isValid ? null : $ child ->withId (( string ) $ id );
240248 }
241249
242250 return array_values (array_filter ($ deduplicatedResults ));
0 commit comments