1010namespace Respect \Validation \Message ;
1111
1212use Respect \Validation \Exceptions \ComponentException ;
13+ use Respect \Validation \FailedResultIterator ;
1314use Respect \Validation \Result ;
1415
1516use function array_filter ;
16- use function array_key_exists ;
17- use function array_map ;
1817use function array_reduce ;
19- use function array_values ;
2018use function count ;
2119use function current ;
2220use function is_array ;
@@ -42,8 +40,8 @@ public function main(Result $result, array $templates, Translator $translator):
4240 {
4341 $ selectedTemplates = $ this ->selectTemplates ($ result , $ templates );
4442 if (!$ this ->isFinalTemplate ($ result , $ selectedTemplates )) {
45- foreach ($ this -> extractDeduplicatedChildren ($ result ) as $ child ) {
46- return $ this ->main ($ this -> resultWithPath ( $ result , $ child) , $ selectedTemplates , $ translator );
43+ foreach (new FailedResultIterator ($ result ) as $ child ) {
44+ return $ this ->main ($ child , $ selectedTemplates , $ translator );
4745 }
4846 }
4947
@@ -78,17 +76,14 @@ public function full(
7876 }
7977
8078 if (!$ isFinalTemplate ) {
81- $ results = array_map (
82- fn (Result $ child ) => $ this ->resultWithPath ($ result , $ child ),
83- $ this ->extractDeduplicatedChildren ($ result )
84- );
79+ $ results = new FailedResultIterator ($ result );
8580 foreach ($ results as $ child ) {
8681 $ rendered .= $ this ->full (
8782 $ child ,
8883 $ selectedTemplates ,
8984 $ translator ,
9085 $ depth ,
91- ...array_filter ($ results , static fn (Result $ sibling ) => $ sibling !== $ child )
86+ ...array_filter ($ results-> getArrayCopy () , static fn (Result $ sibling ) => $ sibling !== $ child )
9287 );
9388 $ rendered .= PHP_EOL ;
9489 }
@@ -105,7 +100,7 @@ public function full(
105100 public function array (Result $ result , array $ templates , Translator $ translator ): array
106101 {
107102 $ selectedTemplates = $ this ->selectTemplates ($ result , $ templates );
108- $ deduplicatedChildren = $ this -> extractDeduplicatedChildren ($ result );
103+ $ deduplicatedChildren = new FailedResultIterator ($ result );
109104 if (count ($ deduplicatedChildren ) === 0 || $ this ->isFinalTemplate ($ result , $ selectedTemplates )) {
110105 return [
111106 $ result ->getDeepestPath () ?? $ result ->id => $ this ->renderer ->render (
@@ -256,32 +251,4 @@ private function selectTemplates(Result $result, array $templates): array
256251
257252 return $ templates ;
258253 }
259-
260- /** @return array<Result> */
261- private function extractDeduplicatedChildren (Result $ result ): array
262- {
263- /** @var array<string, Result> $deduplicatedResults */
264- $ deduplicatedResults = [];
265- $ duplicateCounters = [];
266- foreach ($ result ->children as $ child ) {
267- $ id = $ child ->getDeepestPath () ?? $ child ->id ;
268- if (isset ($ duplicateCounters [$ id ])) {
269- $ id .= '. ' . ++$ duplicateCounters [$ id ];
270- } elseif (array_key_exists ($ id , $ deduplicatedResults )) {
271- $ deduplicatedResults [$ id . '.1 ' ] = $ deduplicatedResults [$ id ]?->withId($ id . '.1 ' );
272- unset($ deduplicatedResults [$ id ]);
273- $ duplicateCounters [$ id ] = 2 ;
274- $ id .= '.2 ' ;
275- }
276-
277- if ($ child ->path === null ) {
278- $ deduplicatedResults [$ id ] = $ child ->hasPassed ? null : $ child ->withId ($ id );
279- continue ;
280- }
281-
282- $ deduplicatedResults [$ id ] = $ child ->hasPassed ? null : $ child ;
283- }
284-
285- return array_values (array_filter ($ deduplicatedResults ));
286- }
287254}
0 commit comments