Skip to content

Commit e9dfe89

Browse files
author
Fady Michel
committed
improvement router and unit test
1 parent a68b354 commit e9dfe89

File tree

5 files changed

+62
-87
lines changed

5 files changed

+62
-87
lines changed

src/Route.php

Lines changed: 26 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/Router.php

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
<?php
22

3-
43
namespace DevCoder;
54

6-
use Exception;
75
use Psr\Http\Message\ServerRequestInterface;
86

97
/**
@@ -15,13 +13,13 @@ class Router implements RouterInterface
1513
const NO_ROUTE = 1;
1614

1715
/**
18-
* @var Route[]
16+
* @var array<Route>
1917
*/
2018
protected $routes = [];
2119

2220
/**
2321
* Router constructor.
24-
* @param Route[]
22+
* @param $routes array<Route>
2523
*/
2624
public function __construct(array $routes = [])
2725
{
@@ -42,45 +40,48 @@ public function add(Route $route): self
4240
/**
4341
* @param ServerRequestInterface $serverRequest
4442
* @return Route
45-
* @throws Exception
43+
* @throws \Exception
4644
*/
4745
public function match(ServerRequestInterface $serverRequest): Route
4846
{
49-
$path = $serverRequest->getUri()->getPath();
50-
foreach ($this->routes as $route) {
47+
return $this->matchFromPath($serverRequest->getUri()->getPath(), $serverRequest->getMethod());
48+
}
5149

52-
$varsValues = $route->match($path);
53-
if ($varsValues === null || !in_array($serverRequest->getMethod(), $route->getMethods())) {
50+
/**
51+
* @param string $path
52+
* @param string $method
53+
* @return Route
54+
* @throws \Exception
55+
*/
56+
public function matchFromPath(string $path, string $method): Route
57+
{
58+
foreach ($this->routes as $route) {
59+
if ($route->match($path, $method) === false) {
5460
continue;
5561
}
56-
57-
foreach ($varsValues as $key => $value) {
58-
$route->addVar($key, $value);
59-
}
60-
6162
return $route;
6263
}
6364

64-
throw new Exception('Aucune route ne correspond à l\'URL', self::NO_ROUTE);
65+
throw new \Exception('Aucune route ne correspond à l\'URL', self::NO_ROUTE);
6566
}
6667

6768
public function generateUri(string $name, array $parameters = []): string
6869
{
6970
if (!array_key_exists($name, $this->routes)) {
70-
throw new Exception(sprintf('%s name route doesnt exist', $name));
71+
throw new \Exception(sprintf('%s name route doesnt exist', $name));
7172
}
7273

7374
$route = $this->routes[$name];
7475
if ($route->hasVars() && empty($parameters)) {
75-
throw new Exception(sprintf('%s route need parameters: %s', $name, implode(',', $route->getVarsNames()))
76+
throw new \Exception(sprintf('%s route need parameters: %s', $name, implode(',', $route->getVarsNames()))
7677
);
7778
}
7879

7980
$uri = $route->getPath();
8081
foreach ($route->getVarsNames() as $variable) {
8182
$varName = trim($variable, '{\}');
8283
if (!array_key_exists($varName, $parameters)) {
83-
throw new Exception(sprintf('%s not found in parameters to generate url', $varName));
84+
throw new \Exception(sprintf('%s not found in parameters to generate url', $varName));
8485
}
8586
$uri = str_replace($variable, $parameters[$varName], $uri);
8687
}

src/RouterInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ interface RouterInterface
1717
*/
1818
public function match(ServerRequestInterface $serverRequest) : Route;
1919

20+
/**
21+
* @param string $path
22+
* @param string $method
23+
* @return Route
24+
* @throws \Exception if no found route.
25+
*/
26+
public function matchFromPath(string $path, string $method) : Route;
27+
2028
/**
2129
* @param string $name
2230
* @param array $parameters

tests/RouteTest.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@
1313
*/
1414
class RouteTest extends TestCase {
1515

16-
17-
1816
public function testNotMatchRoute()
1917
{
2018
$routeWithoutAttribute = new Route('view_articles','/view/article/', ['App\\Controller\\HomeController', 'home']);
2119
$routeWithAttribute = new Route('view_article','/view/article/{article}', ['App\\Controller\\HomeController', 'home']);
2220

23-
$this->assertNull($routeWithoutAttribute->match('/view/article/1'));
24-
$this->assertNull($routeWithAttribute->match('/view/article/'));
21+
$this->assertFalse($routeWithoutAttribute->match('/view/article/1', 'GET'));
22+
$this->assertFalse($routeWithoutAttribute->match('/view/article/1', 'PUT'));
23+
$this->assertFalse($routeWithAttribute->match('/view/article/', 'POST'));
2524
}
2625

2726
public function testMatchRoute()
@@ -30,9 +29,9 @@ public function testMatchRoute()
3029
$routeWithAttributes = new Route('view_article_page','/view/article/{article}/{page}', ['App\\Controller\\HomeController', 'home']);
3130
$routeWithoutAttribute = new Route('view_articles','/view/article', ['App\\Controller\\HomeController', 'home']);
3231

33-
$this->assertInternalType('array',$routeWithAttribute->match('/view/article/1'));
34-
$this->assertInternalType('array',$routeWithAttributes->match('/view/article/1/24'));
35-
$this->assertInternalType('array',$routeWithoutAttribute->match('/view/article/'));
32+
$this->assertTrue($routeWithAttribute->match('/view/article/1', 'GET'));
33+
$this->assertTrue(!$routeWithAttribute->match('/view/article/1', 'PUT'));
34+
$this->assertTrue($routeWithAttributes->match('/view/article/1/24','GET'));
35+
$this->assertTrue($routeWithoutAttribute->match('/view/article/','GET'));
3636
}
37-
38-
}
37+
}

tests/RouterTest.php

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,4 @@
11
<?php
2-
/**
3-
* Created by PhpStorm.
4-
* User: fadymichel
5-
* Date: 20/05/18
6-
* Time: 01:57
7-
*/
82

93
namespace Test\DevCoder;
104

@@ -18,7 +12,6 @@
1812
*/
1913
class RouterTest extends TestCase
2014
{
21-
2215
/**
2316
* @var Router
2417
*/
@@ -39,7 +32,6 @@ public function __construct($name = null, array $data = [], $dataName = '')
3932
->add($routeArticleWithParams);
4033
}
4134

42-
4335
public function testGenerateUrl() {
4436

4537
$urlHome = $this->router->generateUri('home_page');
@@ -53,11 +45,4 @@ public function testGenerateUrl() {
5345
$this->assertEquals($routeArticleWithParams, '/view/article/25/3');
5446

5547
}
56-
57-
public function testResolveRoute() {
58-
59-
60-
61-
}
62-
63-
}
48+
}

0 commit comments

Comments
 (0)