Passed
Push — master ( 723c8d...5629e4 )
by Ch
02:31
created
Router.php 3 patches
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -29,7 +29,7 @@  discard block
 block discarded – undo
29 29
 		$this->setRoutes($routes);
30 30
 		$this->setBasePath($basePath);
31 31
         $this->parser = $parser;
32
-		if(!$server) {
32
+		if (!$server) {
33 33
 			$this->server = $_SERVER;
34 34
 		}
35 35
     }
@@ -87,11 +87,11 @@  discard block
 block discarded – undo
87 87
 
88 88
 		foreach ($this->routes as $handler) {
89 89
 
90
-			if(!$this->parser->methodMatch($handler[0], $requestMethod, $handler[1], $requestUrl)) continue;
90
+			if (!$this->parser->methodMatch($handler[0], $requestMethod, $handler[1], $requestUrl)) continue;
91 91
 
92 92
 			return array(
93 93
                 'target' => $handler[2],
94
-                'params' => array_filter($this->parser->getParams(), function ($k) { return !is_numeric($k); }, ARRAY_FILTER_USE_KEY),
94
+                'params' => array_filter($this->parser->getParams(), function($k) { return !is_numeric($k); }, ARRAY_FILTER_USE_KEY),
95 95
                 'name'   => $handler[3]
96 96
             );
97 97
 		}
@@ -106,8 +106,8 @@  discard block
 block discarded – undo
106 106
      */
107 107
     public function __call($method, $arguments)
108 108
 	{
109
-		if(!in_array($method, array('get', 'post', 'delete', 'put', 'patch', 'update', 'all'))) {
110
-			throw new RouterException($method . ' not exist in the '. __CLASS__);
109
+		if (!in_array($method, array('get', 'post', 'delete', 'put', 'patch', 'update', 'all'))) {
110
+			throw new RouterException($method . ' not exist in the ' . __CLASS__);
111 111
 		}
112 112
 
113 113
 		$methods = $method == 'all' ? implode('|', $this->all) : $method;
@@ -143,7 +143,7 @@  discard block
 block discarded – undo
143 143
         if (!is_array($routes) && !$routes instanceof Traversable) {
144 144
             throw new RouterException('Routes should be an array or an instance of Traversable');
145 145
         }
146
-        if(!empty($routes)) {
146
+        if (!empty($routes)) {
147 147
             foreach ($routes as $route) {
148 148
                 call_user_func_array(array($this, 'map'), $route);
149 149
             }
Please login to merge, or discard this patch.
Braces   +3 added lines, -1 removed lines patch added patch discarded remove patch
@@ -87,7 +87,9 @@
 block discarded – undo
87 87
 
88 88
 		foreach ($this->routes as $handler) {
89 89
 
90
-			if(!$this->parser->methodMatch($handler[0], $requestMethod, $handler[1], $requestUrl)) continue;
90
+			if(!$this->parser->methodMatch($handler[0], $requestMethod, $handler[1], $requestUrl)) {
91
+				continue;
92
+			}
91 93
 
92 94
 			return array(
93 95
                 'target' => $handler[2],
Please login to merge, or discard this patch.
Indentation   +103 added lines, -103 removed lines patch added patch discarded remove patch
@@ -10,29 +10,29 @@  discard block
 block discarded – undo
10 10
 	protected $namedRoutes = [];
11 11
 	protected $basePath = '';
12 12
 	protected $all = ['get', 'post'];
13
-    protected $server;
14
-    /**
15
-     * @var RouterParser
16
-     */
17
-    private $parser;
18
-
19
-    /**
20
-     * Create router in one call from config.
21
-     *
22
-     * @param RouterParser $parser
23
-     * @param array $routes
24
-     * @param string $basePath
25
-     * @param null $server
26
-     */
13
+	protected $server;
14
+	/**
15
+	 * @var RouterParser
16
+	 */
17
+	private $parser;
18
+
19
+	/**
20
+	 * Create router in one call from config.
21
+	 *
22
+	 * @param RouterParser $parser
23
+	 * @param array $routes
24
+	 * @param string $basePath
25
+	 * @param null $server
26
+	 */
27 27
 	public function __construct(RouterParser $parser, $routes = [], $basePath = '', $server = null)
28 28
 	{
29 29
 		$this->setRoutes($routes);
30 30
 		$this->setBasePath($basePath);
31
-        $this->parser = $parser;
31
+		$this->parser = $parser;
32 32
 		if(!$server) {
33 33
 			$this->server = $_SERVER;
34 34
 		}
35
-    }
35
+	}
36 36
 
37 37
 	/**
38 38
 	 * Map a route to a target
@@ -45,7 +45,7 @@  discard block
 block discarded – undo
45 45
 	public function map($method, $route, $target, $routeName = null)
46 46
 	{
47 47
 		if (is_string($routeName)) {
48
-		    $this->handleException($routeName, "Can not redeclare route '%s'");
48
+			$this->handleException($routeName, "Can not redeclare route '%s'");
49 49
 			$this->namedRoutes[$routeName] = $route;
50 50
 		}
51 51
 
@@ -63,11 +63,11 @@  discard block
 block discarded – undo
63 63
 	 */
64 64
 	public function generate($routeName, array $params = [])
65 65
 	{
66
-        $this->handleException($routeName, "Route '%s' does not exist.", false);
66
+		$this->handleException($routeName, "Route '%s' does not exist.", false);
67 67
 
68 68
 		$route = $this->namedRoutes[$routeName];
69 69
 
70
-        return $this->parser->generate($this->basePath, $route, $params);
70
+		return $this->parser->generate($this->basePath, $route, $params);
71 71
 	}
72 72
 
73 73
 	/**
@@ -90,21 +90,21 @@  discard block
 block discarded – undo
90 90
 			if(!$this->parser->methodMatch($handler[0], $requestMethod, $handler[1], $requestUrl)) continue;
91 91
 
92 92
 			return array(
93
-                'target' => $handler[2],
94
-                'params' => array_filter($this->parser->getParams(), function ($k) { return !is_numeric($k); }, ARRAY_FILTER_USE_KEY),
95
-                'name'   => $handler[3]
96
-            );
93
+				'target' => $handler[2],
94
+				'params' => array_filter($this->parser->getParams(), function ($k) { return !is_numeric($k); }, ARRAY_FILTER_USE_KEY),
95
+				'name'   => $handler[3]
96
+			);
97 97
 		}
98 98
 
99 99
 		return false;
100 100
 	}
101 101
 
102
-    /**
103
-     * @param $method
104
-     * @param $arguments
105
-     * @throws RouterException
106
-     */
107
-    public function __call($method, $arguments)
102
+	/**
103
+	 * @param $method
104
+	 * @param $arguments
105
+	 * @throws RouterException
106
+	 */
107
+	public function __call($method, $arguments)
108 108
 	{
109 109
 		if(!in_array($method, array('get', 'post', 'delete', 'put', 'patch', 'update', 'all'))) {
110 110
 			throw new RouterException($method . ' not exist in the '. __CLASS__);
@@ -117,78 +117,78 @@  discard block
 block discarded – undo
117 117
 		call_user_func_array([$this, 'map'], $route);
118 118
 	}
119 119
 
120
-    /**
121
-     * Retrieves all routes.
122
-     * Useful if you want to process or display routes.
123
-     * @return array All routes.
124
-     */
125
-    public function getRoutes()
126
-    {
127
-        return $this->routes;
128
-    }
129
-
130
-    /**
131
-     * Add multiple routes at once from array in the following format:
132
-     *
133
-     *   $routes = array(
134
-     *      array($method, $route, $target, $name)
135
-     *   );
136
-     *
137
-     * @param array|Traversable $routes
138
-     * @return void
139
-     * @author Koen Punt
140
-     */
141
-    public function setRoutes($routes)
142
-    {
143
-        if (!is_array($routes) && !$routes instanceof Traversable) {
144
-            throw new RouterException('Routes should be an array or an instance of Traversable');
145
-        }
146
-        if(!empty($routes)) {
147
-            foreach ($routes as $route) {
148
-                call_user_func_array(array($this, 'map'), $route);
149
-            }
150
-        }
151
-    }
152
-
153
-    /**
154
-     * Set the base path.
155
-     * Useful if you are running your application from a subdirectory.
156
-     */
157
-    public function setBasePath($basePath)
158
-    {
159
-        $this->basePath = $basePath;
160
-    }
161
-
162
-    /**
163
-     * @param $routeName
164
-     * @param $message
165
-     * @param bool $cmpTo
166
-     * @throws RouterException
167
-     */
168
-    private function handleException($routeName, $message, $cmpTo = true)
169
-    {
170
-        if (array_key_exists($routeName, $this->namedRoutes) === $cmpTo) {
171
-            throw new RouterException(sprintf($message, $routeName));
172
-        }
173
-    }
174
-
175
-    /**
176
-     * @param $requestUrl
177
-     *
178
-     * @return mixed
179
-     */
180
-    private function getRequestUrl($requestUrl = null)
181
-    {
182
-        // set Request Url if it isn't passed as parameter
183
-        if (is_null($requestUrl)) {
184
-            $requestUrl = parse_url($this->server['REQUEST_URI'], PHP_URL_PATH);
185
-        }
186
-
187
-        return str_replace($this->basePath, '', strtok($requestUrl, '?'));
188
-    }
189
-
190
-    public function getParser()
191
-    {
192
-        return $this->parser;
193
-    }
120
+	/**
121
+	 * Retrieves all routes.
122
+	 * Useful if you want to process or display routes.
123
+	 * @return array All routes.
124
+	 */
125
+	public function getRoutes()
126
+	{
127
+		return $this->routes;
128
+	}
129
+
130
+	/**
131
+	 * Add multiple routes at once from array in the following format:
132
+	 *
133
+	 *   $routes = array(
134
+	 *      array($method, $route, $target, $name)
135
+	 *   );
136
+	 *
137
+	 * @param array|Traversable $routes
138
+	 * @return void
139
+	 * @author Koen Punt
140
+	 */
141
+	public function setRoutes($routes)
142
+	{
143
+		if (!is_array($routes) && !$routes instanceof Traversable) {
144
+			throw new RouterException('Routes should be an array or an instance of Traversable');
145
+		}
146
+		if(!empty($routes)) {
147
+			foreach ($routes as $route) {
148
+				call_user_func_array(array($this, 'map'), $route);
149
+			}
150
+		}
151
+	}
152
+
153
+	/**
154
+	 * Set the base path.
155
+	 * Useful if you are running your application from a subdirectory.
156
+	 */
157
+	public function setBasePath($basePath)
158
+	{
159
+		$this->basePath = $basePath;
160
+	}
161
+
162
+	/**
163
+	 * @param $routeName
164
+	 * @param $message
165
+	 * @param bool $cmpTo
166
+	 * @throws RouterException
167
+	 */
168
+	private function handleException($routeName, $message, $cmpTo = true)
169
+	{
170
+		if (array_key_exists($routeName, $this->namedRoutes) === $cmpTo) {
171
+			throw new RouterException(sprintf($message, $routeName));
172
+		}
173
+	}
174
+
175
+	/**
176
+	 * @param $requestUrl
177
+	 *
178
+	 * @return mixed
179
+	 */
180
+	private function getRequestUrl($requestUrl = null)
181
+	{
182
+		// set Request Url if it isn't passed as parameter
183
+		if (is_null($requestUrl)) {
184
+			$requestUrl = parse_url($this->server['REQUEST_URI'], PHP_URL_PATH);
185
+		}
186
+
187
+		return str_replace($this->basePath, '', strtok($requestUrl, '?'));
188
+	}
189
+
190
+	public function getParser()
191
+	{
192
+		return $this->parser;
193
+	}
194 194
 }
Please login to merge, or discard this patch.
RouterParser.php 3 patches
Indentation   +194 added lines, -194 removed lines patch added patch discarded remove patch
@@ -3,198 +3,198 @@
 block discarded – undo
3 3
 
4 4
 class RouterParser implements RouterParserInterface
5 5
 {
6
-    protected $params = [];
7
-    protected $matchTypes = [
8
-        'i'  => '[0-9]++',
9
-        'a'  => '[0-9A-Za-z]++',
10
-        'h'  => '[0-9A-Fa-f]++',
11
-        '*'  => '.+?',
12
-        '**' => '.++',
13
-        ''   => '[^/\.]++'
14
-    ];
15
-
16
-    /**
17
-     * Create router in one call from config.
18
-     *
19
-     * @param array $matchTypes
20
-     */
21
-    public function __construct($matchTypes = [])
22
-    {
23
-        $this->setMatchTypes($matchTypes);
24
-    }
25
-
26
-    /**
27
-     * Add named match types. It uses array_merge so keys can be overwritten.
28
-     *
29
-     * @param array $matchTypes The key is the name and the value is the regex.
30
-     */
31
-    public function setMatchTypes($matchTypes)
32
-    {
33
-        $this->matchTypes = array_merge($this->matchTypes, $matchTypes);
34
-    }
35
-
36
-    /**
37
-     * Get the url from a route name
38
-     *
39
-     * @param string $basePath
40
-     * @param string $route
41
-     * @param array $params
42
-     *
43
-     * @return string
44
-     */
45
-    public function generate($basePath, $route, array $params)
46
-    {
47
-        $url = $basePath . $route;
48
-
49
-        if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) {
50
-            foreach ($matches as $match) {
51
-                $block  = $match[0];
52
-                $pre    = $match[1];
53
-                $param  = $match[3];
54
-
55
-                if ($pre) {
56
-                    $block = substr($block, 1);
57
-                }
58
-
59
-                if (isset($params[$param])) {
60
-                    $url = str_replace($block, $params[$param], $url);
61
-                } elseif ($match[4]) {
62
-                    $url = str_replace($pre . $block, '', $url);
63
-                }
64
-            }
65
-        }
66
-
67
-        return $url;
68
-    }
69
-
70
-    /**
71
-     * @param $method
72
-     * @param $requestMethod
73
-     * @param string|array $routeString
74
-     * @param $requestUrl
75
-     *
76
-     * @return mixed
77
-     */
78
-    public function methodMatch($method, $requestMethod, $routeString, $requestUrl)
79
-    {
80
-        $method = strtolower($method);
81
-        $requestMethod = strtolower($requestMethod);
82
-        $methods = explode('|', $method);
83
-
84
-        if(in_array($requestMethod, $methods)) {
85
-            if($routeString == '*') {
86
-                return true;
87
-            }
88
-            elseif(isset($routeString[0]) && $routeString[0] == '@') {
89
-                $match = preg_match('`' . substr($routeString, 1) . '`u', $requestUrl, $this->params);
90
-                return $match;
91
-            }
92
-            elseif (($position = strpos($routeString, '[')) === false) {
93
-                return strcmp($requestUrl, $routeString) === 0;
94
-            }
95
-            else {
96
-                if (strncmp($requestUrl, $routeString, $position) !== 0) {
97
-                    return false;
98
-                }
99
-                $regex = $this->compileRoute($routeString, $requestUrl);
100
-                return preg_match($regex, $requestUrl, $this->params);
101
-            }
102
-        }
103
-
104
-        return false;
105
-    }
106
-
107
-    /**
108
-     * Compile the regex for a given route (EXPENSIVE)
109
-     */
110
-    private function compileRoute($routeString, $requestUrl)
111
-    {
112
-        $route = $this->getRoute($routeString, $requestUrl);
113
-
114
-        if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) {
115
-            $matchTypes = $this->matchTypes;
116
-            foreach ($matches as $match) {
117
-                list($block, $pre, $type, $param, $optional) = $match;
118
-                $pattern = $this->getRoutePattern($matchTypes, $pre, $type, $param, $optional);
119
-                $route = str_replace($block, $pattern, $route);
120
-            }
121
-        }
122
-
123
-        return "`^$route$`u";
124
-    }
125
-
126
-    private function getRoutePattern($matchTypes, $pre, $type, $param, $optional)
127
-    {
128
-        if (isset($matchTypes[$type])) {
129
-            $type = $matchTypes[$type];
130
-        }
131
-        if ($pre === '.') {
132
-            $pre = '\.';
133
-        }
134
-
135
-        //Older versions of PCRE require the 'P' in (?P<named>)
136
-        return '(?:'
137
-            . ($pre !== '' ? $pre : null)
138
-            . '('
139
-            . ($param !== '' ? "?P<$param>" : null)
140
-            . $type
141
-            . '))'
142
-            . ($optional !== '' ? '?' : null);
143
-    }
144
-
145
-    /**
146
-     * @param $routeString
147
-     * @param $requestUrl
148
-     *
149
-     * @return bool|string
150
-     */
151
-    private function getRoute($routeString, $requestUrl)
152
-    {
153
-        $iPointer = $jPointer = 0;
154
-        $nPointer = isset($routeString[0]) ? $routeString[0] : null;
155
-        $regex = $route = false;
156
-
157
-        // Find the longest non-regex substring and match it against the URI
158
-        while (true) {
159
-            if (!isset($routeString[$iPointer])) {
160
-                break;
161
-            }
162
-            if ($regex === false) {
163
-                if(!$this->getRouteRegexCheck($nPointer, $jPointer, $iPointer, $routeString, $requestUrl)) {
164
-                    continue;
165
-                }
166
-                $jPointer++;
167
-            }
168
-            $route .= $routeString[$iPointer++];
169
-        }
170
-
171
-        return $route;
172
-    }
173
-
174
-    private function getRouteRegexCheck($nPointer, $jPointer, $iPointer, $routeString, $requestUrl)
175
-    {
176
-        $cPointer = $nPointer;
177
-        $regex = in_array($cPointer, array('[', '(', '.'));
178
-        if (!$regex && isset($routeString[$iPointer+1])) {
179
-            $nPointer = $routeString[$iPointer + 1];
180
-            $regex = in_array($nPointer, array('?', '+', '*', '{'));
181
-        }
182
-        if (!$regex && $cPointer !== '/' && (!isset($requestUrl[$jPointer]) || $cPointer !== $requestUrl[$jPointer])) {
183
-            return false;
184
-        }
185
-        return true;
186
-    }
187
-
188
-    /**
189
-     * @return array
190
-     */
191
-    public function getParams()
192
-    {
193
-        return $this->params;
194
-    }
195
-
196
-    public function getMatchTypes()
197
-    {
198
-        return $this->matchTypes;
199
-    }
6
+	protected $params = [];
7
+	protected $matchTypes = [
8
+		'i'  => '[0-9]++',
9
+		'a'  => '[0-9A-Za-z]++',
10
+		'h'  => '[0-9A-Fa-f]++',
11
+		'*'  => '.+?',
12
+		'**' => '.++',
13
+		''   => '[^/\.]++'
14
+	];
15
+
16
+	/**
17
+	 * Create router in one call from config.
18
+	 *
19
+	 * @param array $matchTypes
20
+	 */
21
+	public function __construct($matchTypes = [])
22
+	{
23
+		$this->setMatchTypes($matchTypes);
24
+	}
25
+
26
+	/**
27
+	 * Add named match types. It uses array_merge so keys can be overwritten.
28
+	 *
29
+	 * @param array $matchTypes The key is the name and the value is the regex.
30
+	 */
31
+	public function setMatchTypes($matchTypes)
32
+	{
33
+		$this->matchTypes = array_merge($this->matchTypes, $matchTypes);
34
+	}
35
+
36
+	/**
37
+	 * Get the url from a route name
38
+	 *
39
+	 * @param string $basePath
40
+	 * @param string $route
41
+	 * @param array $params
42
+	 *
43
+	 * @return string
44
+	 */
45
+	public function generate($basePath, $route, array $params)
46
+	{
47
+		$url = $basePath . $route;
48
+
49
+		if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) {
50
+			foreach ($matches as $match) {
51
+				$block  = $match[0];
52
+				$pre    = $match[1];
53
+				$param  = $match[3];
54
+
55
+				if ($pre) {
56
+					$block = substr($block, 1);
57
+				}
58
+
59
+				if (isset($params[$param])) {
60
+					$url = str_replace($block, $params[$param], $url);
61
+				} elseif ($match[4]) {
62
+					$url = str_replace($pre . $block, '', $url);
63
+				}
64
+			}
65
+		}
66
+
67
+		return $url;
68
+	}
69
+
70
+	/**
71
+	 * @param $method
72
+	 * @param $requestMethod
73
+	 * @param string|array $routeString
74
+	 * @param $requestUrl
75
+	 *
76
+	 * @return mixed
77
+	 */
78
+	public function methodMatch($method, $requestMethod, $routeString, $requestUrl)
79
+	{
80
+		$method = strtolower($method);
81
+		$requestMethod = strtolower($requestMethod);
82
+		$methods = explode('|', $method);
83
+
84
+		if(in_array($requestMethod, $methods)) {
85
+			if($routeString == '*') {
86
+				return true;
87
+			}
88
+			elseif(isset($routeString[0]) && $routeString[0] == '@') {
89
+				$match = preg_match('`' . substr($routeString, 1) . '`u', $requestUrl, $this->params);
90
+				return $match;
91
+			}
92
+			elseif (($position = strpos($routeString, '[')) === false) {
93
+				return strcmp($requestUrl, $routeString) === 0;
94
+			}
95
+			else {
96
+				if (strncmp($requestUrl, $routeString, $position) !== 0) {
97
+					return false;
98
+				}
99
+				$regex = $this->compileRoute($routeString, $requestUrl);
100
+				return preg_match($regex, $requestUrl, $this->params);
101
+			}
102
+		}
103
+
104
+		return false;
105
+	}
106
+
107
+	/**
108
+	 * Compile the regex for a given route (EXPENSIVE)
109
+	 */
110
+	private function compileRoute($routeString, $requestUrl)
111
+	{
112
+		$route = $this->getRoute($routeString, $requestUrl);
113
+
114
+		if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) {
115
+			$matchTypes = $this->matchTypes;
116
+			foreach ($matches as $match) {
117
+				list($block, $pre, $type, $param, $optional) = $match;
118
+				$pattern = $this->getRoutePattern($matchTypes, $pre, $type, $param, $optional);
119
+				$route = str_replace($block, $pattern, $route);
120
+			}
121
+		}
122
+
123
+		return "`^$route$`u";
124
+	}
125
+
126
+	private function getRoutePattern($matchTypes, $pre, $type, $param, $optional)
127
+	{
128
+		if (isset($matchTypes[$type])) {
129
+			$type = $matchTypes[$type];
130
+		}
131
+		if ($pre === '.') {
132
+			$pre = '\.';
133
+		}
134
+
135
+		//Older versions of PCRE require the 'P' in (?P<named>)
136
+		return '(?:'
137
+			. ($pre !== '' ? $pre : null)
138
+			. '('
139
+			. ($param !== '' ? "?P<$param>" : null)
140
+			. $type
141
+			. '))'
142
+			. ($optional !== '' ? '?' : null);
143
+	}
144
+
145
+	/**
146
+	 * @param $routeString
147
+	 * @param $requestUrl
148
+	 *
149
+	 * @return bool|string
150
+	 */
151
+	private function getRoute($routeString, $requestUrl)
152
+	{
153
+		$iPointer = $jPointer = 0;
154
+		$nPointer = isset($routeString[0]) ? $routeString[0] : null;
155
+		$regex = $route = false;
156
+
157
+		// Find the longest non-regex substring and match it against the URI
158
+		while (true) {
159
+			if (!isset($routeString[$iPointer])) {
160
+				break;
161
+			}
162
+			if ($regex === false) {
163
+				if(!$this->getRouteRegexCheck($nPointer, $jPointer, $iPointer, $routeString, $requestUrl)) {
164
+					continue;
165
+				}
166
+				$jPointer++;
167
+			}
168
+			$route .= $routeString[$iPointer++];
169
+		}
170
+
171
+		return $route;
172
+	}
173
+
174
+	private function getRouteRegexCheck($nPointer, $jPointer, $iPointer, $routeString, $requestUrl)
175
+	{
176
+		$cPointer = $nPointer;
177
+		$regex = in_array($cPointer, array('[', '(', '.'));
178
+		if (!$regex && isset($routeString[$iPointer+1])) {
179
+			$nPointer = $routeString[$iPointer + 1];
180
+			$regex = in_array($nPointer, array('?', '+', '*', '{'));
181
+		}
182
+		if (!$regex && $cPointer !== '/' && (!isset($requestUrl[$jPointer]) || $cPointer !== $requestUrl[$jPointer])) {
183
+			return false;
184
+		}
185
+		return true;
186
+	}
187
+
188
+	/**
189
+	 * @return array
190
+	 */
191
+	public function getParams()
192
+	{
193
+		return $this->params;
194
+	}
195
+
196
+	public function getMatchTypes()
197
+	{
198
+		return $this->matchTypes;
199
+	}
200 200
 }
Please login to merge, or discard this patch.
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -81,11 +81,11 @@  discard block
 block discarded – undo
81 81
         $requestMethod = strtolower($requestMethod);
82 82
         $methods = explode('|', $method);
83 83
 
84
-        if(in_array($requestMethod, $methods)) {
85
-            if($routeString == '*') {
84
+        if (in_array($requestMethod, $methods)) {
85
+            if ($routeString == '*') {
86 86
                 return true;
87 87
             }
88
-            elseif(isset($routeString[0]) && $routeString[0] == '@') {
88
+            elseif (isset($routeString[0]) && $routeString[0] == '@') {
89 89
                 $match = preg_match('`' . substr($routeString, 1) . '`u', $requestUrl, $this->params);
90 90
                 return $match;
91 91
             }
@@ -160,7 +160,7 @@  discard block
 block discarded – undo
160 160
                 break;
161 161
             }
162 162
             if ($regex === false) {
163
-                if(!$this->getRouteRegexCheck($nPointer, $jPointer, $iPointer, $routeString, $requestUrl)) {
163
+                if (!$this->getRouteRegexCheck($nPointer, $jPointer, $iPointer, $routeString, $requestUrl)) {
164 164
                     continue;
165 165
                 }
166 166
                 $jPointer++;
@@ -175,7 +175,7 @@  discard block
 block discarded – undo
175 175
     {
176 176
         $cPointer = $nPointer;
177 177
         $regex = in_array($cPointer, array('[', '(', '.'));
178
-        if (!$regex && isset($routeString[$iPointer+1])) {
178
+        if (!$regex && isset($routeString[$iPointer + 1])) {
179 179
             $nPointer = $routeString[$iPointer + 1];
180 180
             $regex = in_array($nPointer, array('?', '+', '*', '{'));
181 181
         }
Please login to merge, or discard this patch.
Braces   +3 added lines, -6 removed lines patch added patch discarded remove patch
@@ -84,15 +84,12 @@
 block discarded – undo
84 84
         if(in_array($requestMethod, $methods)) {
85 85
             if($routeString == '*') {
86 86
                 return true;
87
-            }
88
-            elseif(isset($routeString[0]) && $routeString[0] == '@') {
87
+            } elseif(isset($routeString[0]) && $routeString[0] == '@') {
89 88
                 $match = preg_match('`' . substr($routeString, 1) . '`u', $requestUrl, $this->params);
90 89
                 return $match;
91
-            }
92
-            elseif (($position = strpos($routeString, '[')) === false) {
90
+            } elseif (($position = strpos($routeString, '[')) === false) {
93 91
                 return strcmp($requestUrl, $routeString) === 0;
94
-            }
95
-            else {
92
+            } else {
96 93
                 if (strncmp($requestUrl, $routeString, $position) !== 0) {
97 94
                     return false;
98 95
                 }
Please login to merge, or discard this patch.
RouterParserInterface.php 1 patch
Indentation   +28 added lines, -28 removed lines patch added patch discarded remove patch
@@ -3,34 +3,34 @@
 block discarded – undo
3 3
 
4 4
 interface RouterParserInterface
5 5
 {
6
-    /**
7
-     * Get the route parameters
8
-     *
9
-     * @return array
10
-     */
11
-    public function getParams();
6
+	/**
7
+	 * Get the route parameters
8
+	 *
9
+	 * @return array
10
+	 */
11
+	public function getParams();
12 12
 
13
-    /**
14
-     * Get the url from a route name
15
-     *
16
-     * @param string $basePath
17
-     * @param string $route
18
-     * @param array $params
19
-     *
20
-     * @return string
21
-     */
22
-    public function generate($basePath, $route, array $params);
13
+	/**
14
+	 * Get the url from a route name
15
+	 *
16
+	 * @param string $basePath
17
+	 * @param string $route
18
+	 * @param array $params
19
+	 *
20
+	 * @return string
21
+	 */
22
+	public function generate($basePath, $route, array $params);
23 23
 
24
-    /**
25
-     * Check if the request method match the route methods
26
-     * Update the $params
27
-     *
28
-     * @param string $method
29
-     * @param string $requestMethod
30
-     * @param string $routeString
31
-     * @param string $requestUrl
32
-     *
33
-     * @return mixed
34
-     */
35
-    public function methodMatch($method, $requestMethod, $routeString, $requestUrl);
24
+	/**
25
+	 * Check if the request method match the route methods
26
+	 * Update the $params
27
+	 *
28
+	 * @param string $method
29
+	 * @param string $requestMethod
30
+	 * @param string $routeString
31
+	 * @param string $requestUrl
32
+	 *
33
+	 * @return mixed
34
+	 */
35
+	public function methodMatch($method, $requestMethod, $routeString, $requestUrl);
36 36
 }
Please login to merge, or discard this patch.
examples/basic/index.php 3 patches
Indentation   +17 added lines, -17 removed lines patch added patch discarded remove patch
@@ -27,23 +27,23 @@
 block discarded – undo
27 27
 <h3>Current request: </h3>
28 28
 <pre>
29 29
      <?php
30
-     if($match) {
31
-         foreach($match as $key => $value) {
32
-             echo '<p>' . $key . ': ';
33
-             if(is_array($value)) {
34
-                 echo '<ul>';
35
-                 foreach($value as $k => $v) {
36
-                     echo '<li>'.$k.': '.$v.'</li>';
37
-                 }
38
-                 echo '</ul>';
39
-             }
40
-             else {
41
-                 echo $value;
42
-             }
43
-             echo '</p>';
44
-         }
45
-     }
46
-     ?>
30
+	 if($match) {
31
+		 foreach($match as $key => $value) {
32
+			 echo '<p>' . $key . ': ';
33
+			 if(is_array($value)) {
34
+				 echo '<ul>';
35
+				 foreach($value as $k => $v) {
36
+					 echo '<li>'.$k.': '.$v.'</li>';
37
+				 }
38
+				 echo '</ul>';
39
+			 }
40
+			 else {
41
+				 echo $value;
42
+			 }
43
+			 echo '</p>';
44
+		 }
45
+	 }
46
+	 ?>
47 47
  </pre>
48 48
 
49 49
 <h3>Try these requests: </h3>
Please login to merge, or discard this patch.
Spacing   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -11,10 +11,10 @@  discard block
 block discarded – undo
11 11
 $parser = new \HakimCh\Http\RouterParser();
12 12
 $router = new \HakimCh\Http\Router($parser);
13 13
 $router->setBasePath('/examples/basic');
14
-$router->map('GET|POST','/', 'home#index', 'home');
15
-$router->map('GET','/users/', array('c' => 'UserController', 'a' => 'ListAction'));
16
-$router->map('GET','/users/[i:id]', 'users#show', 'users_show');
17
-$router->map('POST','/users/[i:id]/[delete|update:action]', 'usersController#doAction', 'users_do');
14
+$router->map('GET|POST', '/', 'home#index', 'home');
15
+$router->map('GET', '/users/', array('c' => 'UserController', 'a' => 'ListAction'));
16
+$router->map('GET', '/users/[i:id]', 'users#show', 'users_show');
17
+$router->map('POST', '/users/[i:id]/[delete|update:action]', 'usersController#doAction', 'users_do');
18 18
 $router->map('GET', '/foo/[:controller]/[:action]', 'foo_action', 'foo_route');
19 19
 
20 20
 // match current request
@@ -27,13 +27,13 @@  discard block
 block discarded – undo
27 27
 <h3>Current request: </h3>
28 28
 <pre>
29 29
      <?php
30
-     if($match) {
31
-         foreach($match as $key => $value) {
30
+     if ($match) {
31
+         foreach ($match as $key => $value) {
32 32
              echo '<p>' . $key . ': ';
33
-             if(is_array($value)) {
33
+             if (is_array($value)) {
34 34
                  echo '<ul>';
35
-                 foreach($value as $k => $v) {
36
-                     echo '<li>'.$k.': '.$v.'</li>';
35
+                 foreach ($value as $k => $v) {
36
+                     echo '<li>' . $k . ': ' . $v . '</li>';
37 37
                  }
38 38
                  echo '</ul>';
39 39
              }
Please login to merge, or discard this patch.
Braces   +1 added lines, -2 removed lines patch added patch discarded remove patch
@@ -36,8 +36,7 @@
 block discarded – undo
36 36
                      echo '<li>'.$k.': '.$v.'</li>';
37 37
                  }
38 38
                  echo '</ul>';
39
-             }
40
-             else {
39
+             } else {
41 40
                  echo $value;
42 41
              }
43 42
              echo '</p>';
Please login to merge, or discard this patch.