Skip to content

Commit 7e188d9

Browse files
author
Fad M.R
committed
add static method to create new instance of Route and improve code
1 parent 8bcd033 commit 7e188d9

File tree

5 files changed

+79
-19
lines changed

5 files changed

+79
-19
lines changed

src/Route.php

+42-11
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44

55
namespace DevCoder;
66

7+
use DevCoder\Traits\RouteTrait;
8+
79
/**
810
* Class Route
911
* @package DevCoder
1012
*/
1113
final class Route
1214
{
15+
use RouteTrait;
16+
1317
/**
1418
* @var string
1519
*/
@@ -21,9 +25,9 @@ final class Route
2125
private $path;
2226

2327
/**
24-
* @var array<string>
28+
* @var mixed
2529
*/
26-
private $parameters = [];
30+
private $handler;
2731

2832
/**
2933
* @var array<string>
@@ -33,27 +37,27 @@ final class Route
3337
/**
3438
* @var array<string>
3539
*/
36-
private $vars = [];
40+
private $attributes = [];
3741

3842
/**
3943
* Route constructor.
4044
* @param string $name
4145
* @param string $path
42-
* @param array $parameters
43-
* $parameters = [
46+
* @param mixed $handler
47+
* $handler = [
4448
* 0 => (string) Controller name : HomeController::class.
4549
* 1 => (string|null) Method name or null if invoke method
4650
* ]
4751
* @param array $methods
4852
*/
49-
public function __construct(string $name, string $path, array $parameters, array $methods = ['GET'])
53+
public function __construct(string $name, string $path, $handler, array $methods = ['GET'])
5054
{
5155
if ($methods === []) {
5256
throw new \InvalidArgumentException('HTTP methods argument was empty; must contain at least one method');
5357
}
5458
$this->name = $name;
5559
$this->path = $path;
56-
$this->parameters = $parameters;
60+
$this->handler = $handler;
5761
$this->methods = $methods;
5862
}
5963

@@ -70,7 +74,7 @@ public function match(string $path, string $method): bool
7074
return is_string($key);
7175
}, ARRAY_FILTER_USE_KEY);
7276
foreach ($values as $key => $value) {
73-
$this->vars[$key] = $value;
77+
$this->attributes[$key] = $value;
7478
}
7579
return true;
7680
}
@@ -87,9 +91,17 @@ public function getPath(): string
8791
return $this->path;
8892
}
8993

90-
public function getParameters(): array
94+
/**
95+
* @deprecated use getHandler()
96+
*/
97+
public function getParameters()
98+
{
99+
return $this->getHandler();
100+
}
101+
102+
public function getHandler()
91103
{
92-
return $this->parameters;
104+
return $this->handler;
93105
}
94106

95107
public function getMethods(): array
@@ -103,13 +115,32 @@ public function getVarsNames(): array
103115
return reset($matches) ?? [];
104116
}
105117

118+
/**
119+
* @deprecated use hasAttributes()
120+
*/
106121
public function hasVars(): bool
122+
{
123+
return $this->hasAttributes();
124+
}
125+
126+
public function hasAttributes(): bool
107127
{
108128
return $this->getVarsNames() !== [];
109129
}
110130

131+
/**
132+
* @deprecated use getAttributes()
133+
*/
111134
public function getVars(): array
112135
{
113-
return $this->vars;
136+
return $this->getAttributes();
137+
}
138+
139+
/**
140+
* @return array<string>
141+
*/
142+
public function getAttributes(): array
143+
{
144+
return $this->attributes;
114145
}
115146
}

src/Router.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ final class Router implements RouterInterface
1212
private const NO_ROUTE = 404;
1313

1414
/**
15-
* @var \ArrayIterator<Route>
15+
* @var \ArrayObject<Route>
1616
*/
1717
private $routes;
1818

@@ -27,7 +27,7 @@ final class Router implements RouterInterface
2727
*/
2828
public function __construct(array $routes = [])
2929
{
30-
$this->routes = new \ArrayIterator();
30+
$this->routes = new \ArrayObject();
3131
$this->urlGenerator = new UrlGenerator($this->routes);
3232
foreach ($routes as $route) {
3333
$this->add($route);
@@ -65,7 +65,7 @@ public function generateUri(string $name, array $parameters = []): string
6565
return $this->urlGenerator->generate($name, $parameters);
6666
}
6767

68-
public function getUrlgenerator(): UrlGenerator
68+
public function getUrlGenerator(): UrlGenerator
6969
{
7070
return $this->urlGenerator;
7171
}

src/RouterMiddleware.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ public function process(
4141
{
4242
try {
4343
$route = $this->router->match($request);
44-
$controller = $route->getParameters();
44+
$routeHandler = $route->getHandler();
4545
$attributes = array_merge([
46-
self::CONTROLLER => $controller[0],
47-
self::ACTION => $controller[1] ?? null,
46+
self::CONTROLLER => $routeHandler[0],
47+
self::ACTION => $routeHandler[1] ?? null,
4848
self::NAME => $route->getName(),
49-
], $route->getVars());
49+
], $route->getAttributes());
5050

5151
foreach ($attributes as $key => $value) {
5252
$request = $request->withAttribute($key, $value);

src/Traits/RouteTrait.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace DevCoder\Traits;
4+
5+
use DevCoder\Route as BaseRoute;
6+
7+
trait RouteTrait
8+
{
9+
public static function get(string $name, string $path, array $parameters): BaseRoute
10+
{
11+
return new BaseRoute($name, $path, $parameters);
12+
}
13+
14+
public static function post(string $name, string $path, array $parameters): BaseRoute
15+
{
16+
return new BaseRoute($name, $path, $parameters, ['POST']);
17+
}
18+
19+
public static function put(string $name, string $path, array $parameters): BaseRoute
20+
{
21+
return new BaseRoute($name, $path, $parameters, ['PUT']);
22+
}
23+
24+
public static function delete(string $name, string $path, array $parameters): BaseRoute
25+
{
26+
return new BaseRoute($name, $path, $parameters, ['DELETE']);
27+
}
28+
}

src/UrlGenerator.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ public function generate(string $name, array $parameters = []): string
2323
sprintf('Unknown %s name route', $name)
2424
);
2525
}
26+
/*** @var Route $route */
2627
$route = $this->routes[$name];
27-
if ($route->hasVars() && $parameters === []) {
28+
if ($route->hasAttributes() === true && $parameters === []) {
2829
throw new \InvalidArgumentException(
2930
sprintf('%s route need parameters: %s', $name, implode(',', $route->getVarsNames()))
3031
);

0 commit comments

Comments
 (0)