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 ;
@@ -97,26 +99,45 @@ public function full(
9799 */
98100 public function array (Result $ result , array $ templates , Translator $ translator ): array
99101 {
102+ $ whoamid = sprintf ('%s.%s.%s ' , $ result ->rule ::class, $ result ->id , $ result ->name );
100103 $ selectedTemplates = $ this ->selectTemplates ($ result , $ templates );
101104 $ deduplicatedChildren = $ this ->extractDeduplicatedChildren ($ result );
102105 if (count ($ deduplicatedChildren ) === 0 || $ this ->isFinalTemplate ($ result , $ selectedTemplates )) {
103106 return [
104- $ result ->id => $ this ->renderer ->render ($ this ->getTemplated ($ result , $ selectedTemplates ), $ translator ),
107+ $ result ->path ?? $ result ->id => $ this ->renderer ->render (
108+ $ this ->getTemplated ($ result , $ selectedTemplates ),
109+ $ translator
110+ ),
105111 ];
106112 }
107113
108114 $ messages = [];
109115 foreach ($ deduplicatedChildren as $ child ) {
110- $ messages [$ child ->id ] = $ this ->array (
116+ $ childKey = $ child ->path ?? $ child ->id ;
117+
118+ $ messages [$ childKey ] = $ this ->array (
111119 $ child ,
112120 $ this ->selectTemplates ($ child , $ selectedTemplates ),
113121 $ translator
114122 );
115- if (count ($ messages [$ child ->id ]) !== 1 ) {
123+
124+ if ($ childKey == 'each ' && is_array ($ messages ['each ' ])) {
125+ $ messages = array_merge ($ messages , $ messages ['each ' ]);
126+ unset($ messages ['each ' ]);
127+ continue ;
128+ }
129+
130+ if (count ($ messages [$ childKey ]) !== 1 ) {
116131 continue ;
117132 }
118133
119- $ messages [$ child ->id ] = current ($ messages [$ child ->id ]);
134+ $ grantChildKey = key ($ messages [$ childKey ]);
135+ if ($ grantChildKey != $ childKey ) {
136+ continue ;
137+ }
138+
139+
140+ $ messages [$ grantChildKey ] = current ($ messages [$ grantChildKey ]);
120141 }
121142
122143 if (count ($ messages ) > 1 ) {
@@ -172,21 +193,27 @@ private function getTemplated(Result $result, array $templates): Result
172193 return $ result ;
173194 }
174195
175- if (!isset ($ templates [$ result ->id ]) && isset ($ templates ['__root__ ' ])) {
176- return $ result ->withTemplate ($ templates ['__root__ ' ]);
196+ $ keys = [$ result ->name , $ result ->path , $ result ->id ];
197+ foreach ($ keys as $ key ) {
198+ if (isset ($ templates [$ key ]) && is_string ($ templates [$ key ])) {
199+ return $ result ->withTemplate ($ templates [$ key ]);
200+ }
177201 }
178202
179- if (! isset ($ templates [$ result -> id ])) {
180- return $ result ;
203+ if (isset ($ templates [' __root__ ' ])) {
204+ return $ result-> withTemplate ( $ templates [ ' __root__ ' ]) ;
181205 }
182206
183- $ template = $ templates [$ result ->id ];
184- if (is_string ($ template )) {
185- return $ result ->withTemplate ($ template );
207+ if (!isset ($ templates [$ result ->id ]) && !isset ($ templates [$ result ->path ]) && !isset ($ templates [$ result ->name ])) {
208+ return $ result ;
186209 }
187210
188211 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+ )
190217 );
191218 }
192219
@@ -195,26 +222,37 @@ private function getTemplated(Result $result, array $templates): Result
195222 */
196223 private function isFinalTemplate (Result $ result , array $ templates ): bool
197224 {
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+ }
200230 }
201231
202232 if (count ($ templates ) !== 1 ) {
203233 return false ;
204234 }
205235
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__ ' ]);
207243 }
208244
209245 /**
210246 * @param array<string, mixed> $templates
211247 *
212248 * @return array<string, mixed>
213249 */
214- private function selectTemplates (Result $ message , array $ templates ): array
250+ private function selectTemplates (Result $ result , array $ templates ): array
215251 {
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+ }
218256 }
219257
220258 return $ templates ;
@@ -227,7 +265,7 @@ private function extractDeduplicatedChildren(Result $result): array
227265 $ deduplicatedResults = [];
228266 $ duplicateCounters = [];
229267 foreach ($ result ->children as $ child ) {
230- $ id = $ child ->id ;
268+ $ id = $ child ->path ?? $ child -> id ;
231269 if (isset ($ duplicateCounters [$ id ])) {
232270 $ id .= '. ' . ++$ duplicateCounters [$ id ];
233271 } elseif (array_key_exists ($ id , $ deduplicatedResults )) {
@@ -236,7 +274,7 @@ private function extractDeduplicatedChildren(Result $result): array
236274 $ duplicateCounters [$ id ] = 2 ;
237275 $ id .= '.2 ' ;
238276 }
239- $ deduplicatedResults [$ id ] = $ child ->isValid ? null : $ child ->withId ($ id );
277+ $ deduplicatedResults [$ id ] = $ child ->isValid ? null : $ child ->withId (( string ) $ id );
240278 }
241279
242280 return array_values (array_filter ($ deduplicatedResults ));
0 commit comments