6
6
7
7
class RouterRoute extends RouterEntry {
8
8
9
+ const PARAMETERS_REGEX_MATCH = '{([A-Za-z\-\_]*?)} ' ;
10
+
9
11
protected $ url ;
10
12
protected $ requestTypes ;
11
13
@@ -18,13 +20,13 @@ public function __construct($url, $callback) {
18
20
$ this ->requestTypes = array ();
19
21
}
20
22
21
- protected function parseParameters ($ url , $ multiple = false ) {
23
+ protected function parseParameters ($ url , $ multiple = false , $ regex = self :: PARAMETERS_REGEX_MATCH ) {
22
24
$ parameters = array ();
23
25
24
26
if ($ multiple ) {
25
- preg_match_all ('/{([A-Za-z\-\_]*?)} /is ' , $ url , $ parameters );
27
+ preg_match_all ('/ ' . $ regex . ' /is ' , $ url , $ parameters );
26
28
} else {
27
- preg_match ('/{([A-Za-z\-\_]*?)} /is ' , $ url , $ parameters );
29
+ preg_match ('/ ' . $ regex . ' /is ' , $ url , $ parameters );
28
30
}
29
31
30
32
if (isset ($ parameters [1 ]) && count ($ parameters [1 ]) > 0 ) {
@@ -42,43 +44,58 @@ public function matchRoute(Request $request) {
42
44
$ url = parse_url ($ request ->getUri ());
43
45
$ url = $ url ['path ' ];
44
46
45
- $ url = explode ('/ ' , trim ($ url , '/ ' ));
46
- $ route = explode ('/ ' , trim ($ this ->url , '/ ' ));
47
+ $ route = $ this ->url ;
47
48
48
- // Check if url parameter count matches
49
- if (count ($ url ) === count ($ route )) {
49
+ $ routeMatch = preg_replace ('/ ' .self ::PARAMETERS_REGEX_MATCH .'/is ' , '' , $ route );
50
50
51
- $ parameters = array ();
51
+ // Check if url parameter count matches
52
+ if (stripos ($ url , $ routeMatch ) === 0 ) {
52
53
53
54
$ matches = true ;
54
55
55
- // Check if url matches
56
- foreach ($ route as $ i => $ path ) {
57
- $ parameter = $ this ->parseParameters ($ path );
56
+ if ($ this ->regexMatch ) {
57
+ $ parameters = $ this ->parseParameters ($ url , true , $ this ->regexMatch );
58
58
59
- // Check if parameter of path matches, otherwise quit..
60
- if (is_null ($ parameter ) && strtolower ($ path ) != strtolower ($ url [$ i ])) {
61
- $ matches = false ;
62
- break ;
59
+ // If regex doesn't match, make sure to return an array
60
+ if (!is_array ($ parameters )) {
61
+ $ parameters = array ();
63
62
}
64
63
65
- // Save parameter if we have one
66
- if ($ parameter ) {
67
- $ parameterValue = $ url [$ i ];
68
- $ regex = (isset ($ this ->parametersRegex [$ parameter ]) ? $ this ->parametersRegex [$ parameter ] : null );
64
+ } else {
69
65
70
- if ($ regex !== null ) {
71
- // Use the regular expression rule provided to filter the value
72
- $ matches = array ();
73
- preg_match ('/ ' .$ regex .'/is ' , $ url [$ i ], $ matches );
66
+ $ url = explode ('/ ' , $ url );
67
+ $ route = explode ('/ ' , $ route );
74
68
75
- if (count ($ matches )) {
76
- $ parameterValue = $ matches [0 ];
77
- }
69
+ $ parameters = array ();
70
+
71
+ // Check if url matches
72
+ foreach ($ route as $ i => $ path ) {
73
+ $ parameter = $ this ->parseParameters ($ path , false );
74
+
75
+ // Check if parameter of path matches, otherwise quit..
76
+ if (is_null ($ parameter ) && strtolower ($ path ) != strtolower ($ url [$ i ])) {
77
+ $ matches = false ;
78
+ break ;
78
79
}
79
80
80
- // Add parameter value
81
- $ parameters [$ parameter ] = $ parameterValue ;
81
+ // Save parameter if we have one
82
+ if ($ parameter ) {
83
+ $ parameterValue = $ url [$ i ];
84
+ $ regex = (isset ($ this ->parametersRegex [$ parameter ]) ? $ this ->parametersRegex [$ parameter ] : null );
85
+
86
+ if ($ regex !== null ) {
87
+ // Use the regular expression rule provided to filter the value
88
+ $ matches = array ();
89
+ preg_match ('/ ' . $ regex . '/is ' , $ url [$ i ], $ matches );
90
+
91
+ if (count ($ matches )) {
92
+ $ parameterValue = $ matches [0 ];
93
+ }
94
+ }
95
+
96
+ // Add parameter value
97
+ $ parameters [$ parameter ] = $ parameterValue ;
98
+ }
82
99
}
83
100
}
84
101
0 commit comments