@@ -53,10 +53,9 @@ public function generatePath(string $name, array $attributes = [], array $queryP
5353 $ routePath = $ route ->getPath ();
5454
5555 $ routePartSets = array_reverse ($ this ->routeParser ->parse ($ routePath ));
56+ $ routeParts = $ this ->findMatchingRouteParts ($ routePartSets , $ attributes );
5657
57- $ routeIndex = $ this ->getRouteIndex ($ routePartSets , $ attributes );
58-
59- $ path = $ this ->generatePathFromAttributes ($ name , $ routePath , $ routePartSets , $ attributes , $ routeIndex );
58+ $ path = $ this ->buildPath ($ name , $ routePath , $ routeParts , $ attributes );
6059
6160 if ([] === $ queryParams ) {
6261 return $ this ->basePath .$ path ;
@@ -75,42 +74,46 @@ private function getRoute(string $name): RouteInterface
7574 }
7675
7776 /**
78- * @param array<int, array<int, array|string>> $routePartSets
79- * @param array<string> $attributes
77+ * @param array<int, array<int, array<int, string>|string>> $routePartSets
78+ * @param array<string> $attributes
79+ *
80+ * @return array<int, array<int, string>|string>
8081 */
81- private function getRouteIndex (array $ routePartSets , array $ attributes ): int
82+ private function findMatchingRouteParts (array $ routePartSets , array $ attributes ): array
8283 {
83- foreach ($ routePartSets as $ routeIndex => $ routeParts ) {
84- foreach ($ routeParts as $ routePart ) {
85- if (\is_array ($ routePart )) {
86- $ parameter = $ routePart [0 ];
87-
88- if (!isset ($ attributes [$ parameter ])) {
89- continue 2 ;
90- }
91- }
84+ foreach ($ routePartSets as $ routeParts ) {
85+ if ($ this ->hasAllRequiredAttributes ($ routeParts , $ attributes )) {
86+ return $ routeParts ;
9287 }
88+ }
89+
90+ return end ($ routePartSets ) ?: [];
91+ }
9392
94- return $ routeIndex ;
93+ /**
94+ * @param array<int, array<int, string>|string> $routeParts
95+ * @param array<string> $attributes
96+ */
97+ private function hasAllRequiredAttributes (array $ routeParts , array $ attributes ): bool
98+ {
99+ foreach ($ routeParts as $ routePart ) {
100+ if (\is_array ($ routePart ) && !isset ($ attributes [$ routePart [0 ]])) {
101+ return false ;
102+ }
95103 }
96104
97- return $ routeIndex ;
105+ return true ;
98106 }
99107
100108 /**
101- * @param array<int, array<int, array |string>> $routePartSets
102- * @param array<string> $attributes
109+ * @param array<int, array<int, string> |string> $routeParts
110+ * @param array<string> $attributes
103111 */
104- private function generatePathFromAttributes (
105- string $ name ,
106- string $ path ,
107- array $ routePartSets ,
108- array $ attributes ,
109- int $ routeIndex
110- ): string {
112+ private function buildPath (string $ name , string $ path , array $ routeParts , array $ attributes ): string
113+ {
111114 $ pathParts = [];
112115
113- foreach ($ routePartSets [ $ routeIndex ] as $ routePart ) {
116+ foreach ($ routeParts as $ routePart ) {
114117 $ pathParts [] = \is_array ($ routePart )
115118 ? $ this ->getAttributeValue ($ name , $ path , $ routePart , $ attributes )
116119 : $ routePart ;
0 commit comments