1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Selami\Router; |
5
|
|
|
|
6
|
|
|
class Route |
7
|
|
|
{ |
8
|
|
|
private $requestMethod; |
9
|
|
|
private $statusCode; |
10
|
|
|
private $returnType; |
11
|
|
|
private $pattern; |
12
|
|
|
private $uriParameters; |
13
|
|
|
private $aliases; |
14
|
|
|
private $controller; |
15
|
|
|
private $realUri; |
16
|
|
|
|
17
|
|
|
public function __construct( |
18
|
|
|
string $requestMethod, |
19
|
|
|
string $pattern, |
20
|
|
|
int $statusCode, |
21
|
|
|
int $returnType, |
22
|
|
|
string $controller, |
23
|
|
|
array $uriParameters |
24
|
|
|
) { |
25
|
|
|
$this->requestMethod = $requestMethod; |
26
|
|
|
$this->pattern = $pattern; |
27
|
|
|
$this->statusCode = $statusCode; |
28
|
|
|
$this->returnType = $returnType; |
29
|
|
|
$this->controller = $controller; |
30
|
|
|
$this->uriParameters = $uriParameters; |
31
|
|
|
$this->aliases = []; |
32
|
|
|
$this->realUri = $this->buildRealUri($pattern, $uriParameters); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
private function buildRealUri($pattern, $uriParameters) : string |
36
|
|
|
{ |
37
|
|
|
if (count($uriParameters) > 0) { |
38
|
|
|
foreach ($uriParameters as $key => $value) { |
39
|
|
|
$pattern = preg_replace('/{'.$key.'(.*?)}/msi', $value, $pattern); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
return $pattern; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function withAliases(array $aliases) : self |
46
|
|
|
{ |
47
|
|
|
$new = clone $this; |
48
|
|
|
$new->aliases = $aliases; |
49
|
|
|
return $new; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function withStatusCode(int $statusCode) : self |
53
|
|
|
{ |
54
|
|
|
$new = clone $this; |
55
|
|
|
$new->statusCode = $statusCode; |
56
|
|
|
return $new; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function getStatusCode() : int |
60
|
|
|
{ |
61
|
|
|
return $this->statusCode; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getReturnType() : int |
65
|
|
|
{ |
66
|
|
|
return $this->returnType; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function getController() : string |
70
|
|
|
{ |
71
|
|
|
return $this->controller; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function getRequestMethod() : string |
75
|
|
|
{ |
76
|
|
|
return $this->requestMethod; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function getPattern() : string |
80
|
|
|
{ |
81
|
|
|
return $this->pattern; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function getRealUri() : string |
85
|
|
|
{ |
86
|
|
|
return $this->realUri; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getUriParameters() : array |
90
|
|
|
{ |
91
|
|
|
return $this->uriParameters; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function getAliases() : array |
95
|
|
|
{ |
96
|
|
|
return $this->aliases; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function getAlias(string $name) : string |
100
|
|
|
{ |
101
|
|
|
return $this->aliases[$name] ?? ''; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|