1414
1515use function array_filter ;
1616use function array_key_exists ;
17+ use function array_merge ;
1718use function array_reduce ;
1819use function array_values ;
1920use function count ;
2021use function current ;
2122use function is_array ;
2223use function is_string ;
24+ use function key ;
2325use function Respect \Stringifier \stringify ;
2426use function rtrim ;
2527use function sprintf ;
@@ -35,7 +37,7 @@ public function __construct(
3537 }
3638
3739 /**
38- * @param array<string, mixed> $templates
40+ * @param array<string|int , mixed> $templates
3941 */
4042 public function main (Result $ result , array $ templates , Translator $ translator ): string
4143 {
@@ -50,7 +52,7 @@ public function main(Result $result, array $templates, Translator $translator):
5052 }
5153
5254 /**
53- * @param array<string, mixed> $templates
55+ * @param array<string|int , mixed> $templates
5456 */
5557 public function full (
5658 Result $ result ,
@@ -91,32 +93,49 @@ public function full(
9193 }
9294
9395 /**
94- * @param array<string, mixed> $templates
96+ * @param array<string|int , mixed> $templates
9597 *
96- * @return array<string, mixed>
98+ * @return array<string|int , mixed>
9799 */
98100 public function array (Result $ result , array $ templates , Translator $ translator ): array
99101 {
100102 $ selectedTemplates = $ this ->selectTemplates ($ result , $ templates );
101103 $ deduplicatedChildren = $ this ->extractDeduplicatedChildren ($ result );
102104 if (count ($ deduplicatedChildren ) === 0 || $ this ->isFinalTemplate ($ result , $ selectedTemplates )) {
103105 return [
104- $ result ->id => $ this ->renderer ->render ($ this ->getTemplated ($ result , $ selectedTemplates ), $ translator ),
106+ $ result ->path ?? $ result ->id => $ this ->renderer ->render (
107+ $ this ->getTemplated ($ result , $ selectedTemplates ),
108+ $ translator
109+ ),
105110 ];
106111 }
107112
108113 $ messages = [];
109114 foreach ($ deduplicatedChildren as $ child ) {
110- $ messages [$ child ->id ] = $ this ->array (
115+ $ childKey = $ child ->path ?? $ child ->id ;
116+
117+ $ messages [$ childKey ] = $ this ->array (
111118 $ child ,
112119 $ this ->selectTemplates ($ child , $ selectedTemplates ),
113120 $ translator
114121 );
115- if (count ($ messages [$ child ->id ]) !== 1 ) {
122+
123+ if ($ childKey == 'each ' && is_array ($ messages ['each ' ])) {
124+ $ messages = array_merge ($ messages , $ messages ['each ' ]);
125+ unset($ messages ['each ' ]);
126+ continue ;
127+ }
128+
129+ if (count ($ messages [$ childKey ]) !== 1 ) {
116130 continue ;
117131 }
118132
119- $ messages [$ child ->id ] = current ($ messages [$ child ->id ]);
133+ $ grantChildKey = key ($ messages [$ childKey ]);
134+ if ($ grantChildKey != $ childKey ) {
135+ continue ;
136+ }
137+
138+ $ messages [$ grantChildKey ] = current ($ messages [$ grantChildKey ]);
120139 }
121140
122141 if (count ($ messages ) > 1 ) {
@@ -165,56 +184,73 @@ private function isAlwaysVisible(Result $result, Result ...$siblings): bool
165184 );
166185 }
167186
168- /** @param array<string, mixed> $templates */
187+ /** @param array<string|int , mixed> $templates */
169188 private function getTemplated (Result $ result , array $ templates ): Result
170189 {
171190 if ($ result ->hasCustomTemplate ()) {
172191 return $ result ;
173192 }
174193
175- if (!isset ($ templates [$ result ->id ]) && isset ($ templates ['__root__ ' ])) {
176- return $ result ->withTemplate ($ templates ['__root__ ' ]);
194+ $ keys = [$ result ->name , $ result ->path , $ result ->id ];
195+ foreach ($ keys as $ key ) {
196+ if (isset ($ templates [$ key ]) && is_string ($ templates [$ key ])) {
197+ return $ result ->withTemplate ($ templates [$ key ]);
198+ }
177199 }
178200
179- if (! isset ($ templates [$ result -> id ])) {
180- return $ result ;
201+ if (isset ($ templates [' __root__ ' ])) {
202+ return $ result-> withTemplate ( $ templates [ ' __root__ ' ]) ;
181203 }
182204
183- $ template = $ templates [$ result ->id ];
184- if (is_string ($ template )) {
185- return $ result ->withTemplate ($ template );
205+ if (!isset ($ templates [$ result ->id ]) && !isset ($ templates [$ result ->path ]) && !isset ($ templates [$ result ->name ])) {
206+ return $ result ;
186207 }
187208
188209 throw new ComponentException (
189- sprintf ('Template for "%s" must be a string, %s given ' , $ result ->id , stringify ($ template ))
210+ sprintf (
211+ 'Template for "%s" must be a string, %s given ' ,
212+ $ result ->path ?? $ result ->name ?? $ result ->id ,
213+ stringify ($ templates )
214+ )
190215 );
191216 }
192217
193218 /**
194- * @param array<string, mixed> $templates
219+ * @param array<string|int , mixed> $templates
195220 */
196221 private function isFinalTemplate (Result $ result , array $ templates ): bool
197222 {
198- if (isset ($ templates [$ result ->id ]) && is_string ($ templates [$ result ->id ])) {
199- return true ;
223+ $ keys = [$ result ->name , $ result ->path , $ result ->id ];
224+ foreach ($ keys as $ key ) {
225+ if (isset ($ templates [$ key ]) && is_string ($ templates [$ key ])) {
226+ return true ;
227+ }
200228 }
201229
202230 if (count ($ templates ) !== 1 ) {
203231 return false ;
204232 }
205233
206- return isset ($ templates ['__root__ ' ]) || isset ($ templates [$ result ->id ]);
234+ foreach ($ keys as $ key ) {
235+ if (isset ($ templates [$ key ])) {
236+ return true ;
237+ }
238+ }
239+
240+ return isset ($ templates ['__root__ ' ]);
207241 }
208242
209243 /**
210- * @param array<string, mixed> $templates
244+ * @param array<string|int , mixed> $templates
211245 *
212- * @return array<string, mixed>
246+ * @return array<string|int , mixed>
213247 */
214- private function selectTemplates (Result $ message , array $ templates ): array
248+ private function selectTemplates (Result $ result , array $ templates ): array
215249 {
216- if (isset ($ templates [$ message ->id ]) && is_array ($ templates [$ message ->id ])) {
217- return $ templates [$ message ->id ];
250+ foreach ([$ result ->name , $ result ->path , $ result ->id ] as $ key ) {
251+ if (isset ($ templates [$ key ]) && is_array ($ templates [$ key ])) {
252+ return $ templates [$ key ];
253+ }
218254 }
219255
220256 return $ templates ;
@@ -227,7 +263,7 @@ private function extractDeduplicatedChildren(Result $result): array
227263 $ deduplicatedResults = [];
228264 $ duplicateCounters = [];
229265 foreach ($ result ->children as $ child ) {
230- $ id = $ child ->id ;
266+ $ id = $ child ->path ?? $ child -> id ;
231267 if (isset ($ duplicateCounters [$ id ])) {
232268 $ id .= '. ' . ++$ duplicateCounters [$ id ];
233269 } elseif (array_key_exists ($ id , $ deduplicatedResults )) {
@@ -236,7 +272,7 @@ private function extractDeduplicatedChildren(Result $result): array
236272 $ duplicateCounters [$ id ] = 2 ;
237273 $ id .= '.2 ' ;
238274 }
239- $ deduplicatedResults [$ id ] = $ child ->isValid ? null : $ child ->withId ($ id );
275+ $ deduplicatedResults [$ id ] = $ child ->isValid ? null : $ child ->withId (( string ) $ id );
240276 }
241277
242278 return array_values (array_filter ($ deduplicatedResults ));
0 commit comments