@@ -14,79 +14,64 @@ class Route
1414 protected $ name ;
1515
1616 /**
17- * @var array
17+ * @var array<string>
1818 */
19- protected $ controller ;
19+ protected $ controller = [] ;
2020
2121 /**
2222 * @var string
2323 */
2424 protected $ path ;
2525
2626 /**
27- * @var array
28- */
29- protected $ varsNames = [];
30-
31- /**
32- * @var array
27+ * @var array<string>
3328 */
3429 protected $ vars = [];
3530
3631 /**
37- * @var array
32+ * @var array<string>
3833 */
39- protected $ methods = [' GET ' , ' POST ' ];
34+ protected $ methods = [];
4035
41- /**
42- * Route constructor.
43- * @param string $name
44- * @param string $path
45- * @param array $controller
46- * @param array $methods
47- */
48- public function __construct (string $ name , string $ path , array $ controller , array $ methods = [])
36+ public function __construct (string $ name , string $ path , array $ controller , array $ methods = ['GET ' , 'POST ' ])
4937 {
5038 $ this ->name = $ name ;
5139 $ this ->path = $ path ;
5240 $ this ->controller = $ controller ;
5341 $ this ->methods = $ methods ;
5442 }
5543
56- /**
57- * @return bool
58- */
5944 public function hasVars (): bool
6045 {
61- return ! empty ( $ this ->getVarsNames ()) ;
46+ return $ this ->getVarsNames () !== [] ;
6247 }
6348
64- /**
65- * @param string $path
66- * @return array|null
67- */
68- public function match (string $ path ): ?array
49+ public function match (string $ path , string $ method ): bool
6950 {
70- if (preg_match ('#^ ' .$ this ->generateRegex ().'$#sD ' , $ this ->trimPath ($ path ), $ matches )) {
71- return array_filter ($ matches , function ($ key ) {
51+ if (
52+ in_array ($ method , $ this ->getMethods ()) &&
53+ preg_match ('#^ ' . $ this ->generateRegex () . '$#sD ' , $ this ->trimPath ($ path ), $ matches )
54+ ) {
55+
56+ $ values = array_filter ($ matches , function ($ key ) {
7257 return is_string ($ key );
7358 }, ARRAY_FILTER_USE_KEY );
59+
60+ foreach ($ values as $ key => $ value ) {
61+ $ this ->addVar ($ key , $ value );
62+ }
63+
64+ return true ;
7465 }
7566
76- return null ;
67+ return false ;
7768 }
7869
79- /**
80- * @return string
81- */
8270 public function getName (): string
8371 {
8472 return $ this ->name ;
8573 }
8674
87- /**
88- * @return string
89- */
9075 public function getPath (): string
9176 {
9277 return $ this ->path ;
@@ -111,28 +96,25 @@ public function getVars(): array
11196 public function getVarsNames (): array
11297 {
11398 preg_match_all ('/{[^}]*}/ ' , $ this ->path , $ matches );
114- return reset ($ matches );
99+ return reset ($ matches ) ?: [] ;
115100 }
116101
117102 public function getMethods (): array
118103 {
119104 return $ this ->methods ;
120105 }
121106
122- private function trimPath (string $ path ) : string
107+ private function trimPath (string $ path ): string
123108 {
124- return '/ ' . rtrim (ltrim (trim ($ path ), '/ ' ), '/ ' );
109+ return '/ ' . rtrim (ltrim (trim ($ path ), '/ ' ), '/ ' );
125110 }
126111
127- /**
128- * @return string
129- */
130112 private function generateRegex (): string
131113 {
132114 $ regex = $ this ->path ;
133115 foreach ($ this ->getVarsNames () as $ variable ) {
134- $ varName = trim ($ variable ,'{\} ' );
135- $ regex = str_replace ($ variable , '(?P< ' . $ varName. '>[^/]++) ' , $ regex );
116+ $ varName = trim ($ variable , '{\} ' );
117+ $ regex = str_replace ($ variable , '(?P< ' . $ varName . '>[^/]++) ' , $ regex );
136118 }
137119 return $ regex ;
138120 }
0 commit comments