66
77use HackRouting \HttpException \NotFoundException ;
88use HackRouting \PrefixMatching \PrefixMap ;
9-
10- use function array_filter ;
119use function array_merge ;
10+ use function is_string ;
1211use function preg_match ;
1312use function strlen ;
1413use function substr ;
1514
16- use const ARRAY_FILTER_USE_KEY ;
17-
1815/**
1916 * @template TResponder
2017 *
@@ -50,13 +47,14 @@ public static function fromFlatMap(array $map): PrefixMatchingResolver
5047 /**
5148 * @param array<string, Tr> $flat_map
5249 */
53- static fn (array $ flat_map ): PrefixMap => PrefixMap::fromFlatMap ($ flat_map ),
50+ static fn (array $ flat_map ): PrefixMap => PrefixMap::fromFlatMap ($ flat_map ),
5451 $ map ,
5552 ));
5653 }
5754
5855 /**
5956 * @param non-empty-string $method
57+ * @param non-empty-string $path
6058 *
6159 * @return array{0: TResponder, array<string, string>}
6260 *
@@ -69,17 +67,20 @@ public function resolve(string $method, string $path): array
6967 throw new NotFoundException ();
7068 }
7169
72- return $ this -> resolveWithMap ($ path , $ map );
70+ return self :: resolveWithMap ($ path , $ map );
7371 }
7472
7573 /**
76- * @param PrefixMap<TResponder> $map
74+ * @template T
7775 *
78- * @return array{0: TResponder, array<string, string>}
76+ * @param non-empty-string $path
77+ * @param PrefixMap<T> $map
78+ *
79+ * @return array{0: T, array<string, string>}
7980 *
8081 * @throws NotFoundException
8182 */
82- private function resolveWithMap (string $ path , PrefixMap $ map ): array
83+ private static function resolveWithMap ($ path , $ map )
8384 {
8485 if (isset ($ map ->literals [$ path ])) {
8586 return [$ map ->literals [$ path ], []];
@@ -88,7 +89,7 @@ private function resolveWithMap(string $path, PrefixMap $map): array
8889 if ($ prefixes = $ map ->prefixes ) {
8990 $ prefix = substr ($ path , 0 , $ map ->getPrefixLength ());
9091 if (isset ($ prefixes [$ prefix ])) {
91- return $ this -> resolveWithMap (
92+ return self :: resolveWithMap (
9293 substr ($ path , $ map ->prefixLength ),
9394 $ prefixes [$ prefix ],
9495 );
@@ -104,18 +105,19 @@ private function resolveWithMap(string $path, PrefixMap $map): array
104105 $ remaining = substr ($ path , strlen ($ matched ));
105106
106107 /** @var array<string, string> $data */
107- $ data = array_filter ($ matches , 'is_string ' , ARRAY_FILTER_USE_KEY );
108-
109- if ($ sub ->isResponder ()) {
110- if ($ remaining === '' ) {
111- return [$ sub ->getResponder (), $ data ];
108+ $ data = [];
109+ foreach ($ matches as $ name => $ match ) {
110+ if (is_string ($ name )) {
111+ $ data [$ name ] = $ match ;
112112 }
113+ }
113114
114- continue ;
115+ if ($ remaining === '' ) {
116+ return [$ sub ->getResponder (), $ data ];
115117 }
116118
117119 try {
118- [$ responder , $ sub_data ] = $ this -> resolveWithMap ($ remaining , $ sub ->getMap ());
120+ [$ responder , $ sub_data ] = self :: resolveWithMap ($ remaining , $ sub ->getMap ());
119121 } catch (NotFoundException ) {
120122 continue ;
121123 }
0 commit comments