|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Simply\Router; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Represents a matched request route. |
|
7
|
|
|
* @author Riikka Kalliomäki <[email protected]> |
|
8
|
|
|
* @copyright Copyright (c) 2018 Riikka Kalliomäki |
|
9
|
|
|
* @license http://opensource.org/licenses/mit-license.php MIT License |
|
10
|
|
|
*/ |
|
11
|
|
|
class Route |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var RouteDefinition Definition of the matched route */ |
|
14
|
|
|
private $definition; |
|
15
|
|
|
|
|
16
|
|
|
/** @var string Requested HTTP method */ |
|
17
|
|
|
private $method; |
|
18
|
|
|
|
|
19
|
|
|
/** @var string[] Segments of the requested path */ |
|
20
|
|
|
private $segments; |
|
21
|
|
|
|
|
22
|
|
|
/** @var string[] Values for the parameters in the requested route */ |
|
23
|
|
|
private $values; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Route constructor. |
|
27
|
|
|
* @param RouteDefinition $definition The definition of the matched route |
|
28
|
|
|
* @param string $method The requested HTTP method |
|
29
|
|
|
* @param string[] $segments The segments of the requested path |
|
30
|
|
|
* @param string[] $values The values for the parameters in the requested route |
|
31
|
|
|
*/ |
|
32
|
14 |
|
public function __construct(RouteDefinition $definition, string $method, array $segments, array $values) |
|
33
|
|
|
{ |
|
34
|
14 |
|
$this->definition = $definition; |
|
35
|
14 |
|
$this->method = $method; |
|
36
|
14 |
|
$this->segments = $segments; |
|
37
|
14 |
|
$this->values = $values; |
|
38
|
14 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Returns the requested HTTP method. |
|
42
|
|
|
* @return string The requested HTTP method |
|
43
|
|
|
*/ |
|
44
|
11 |
|
public function getMethod(): string |
|
45
|
|
|
{ |
|
46
|
11 |
|
return $this->method; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Returns the handler for the requested route. |
|
51
|
|
|
* @return mixed The handler for the requested route |
|
52
|
|
|
*/ |
|
53
|
12 |
|
public function getHandler() |
|
54
|
|
|
{ |
|
55
|
12 |
|
return $this->definition->getHandler(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Returns the unencoded canonical path for the requested route. |
|
60
|
|
|
* @return string The canonical path for the requested route |
|
61
|
|
|
*/ |
|
62
|
10 |
|
public function getPath(): string |
|
63
|
|
|
{ |
|
64
|
10 |
|
if (\count($this->segments) === 0) { |
|
65
|
1 |
|
return '/'; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
9 |
|
$path = sprintf('/%s/', implode('/', $this->segments)); |
|
69
|
|
|
|
|
70
|
9 |
|
if (!$this->definition->hasSlash()) { |
|
71
|
1 |
|
$path = substr($path, 0, -1); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
9 |
|
return $path; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Returns the encoded URL for the requested route. |
|
79
|
|
|
* @return string The encoded URL for the requested route |
|
80
|
|
|
*/ |
|
81
|
10 |
|
public function getUrl(): string |
|
82
|
|
|
{ |
|
83
|
10 |
|
return $this->definition->formatUrl($this->values); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Returns the value for the given parameter from the requested route. |
|
88
|
|
|
* @param string $name The name of the parameter |
|
89
|
|
|
* @return string The value for the parameter |
|
90
|
|
|
*/ |
|
91
|
7 |
|
public function getParameter(string $name): string |
|
92
|
|
|
{ |
|
93
|
7 |
|
if (!isset($this->values[$name])) { |
|
94
|
1 |
|
throw new \InvalidArgumentException("Invalid route parameter '$name'"); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
7 |
|
return $this->values[$name]; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Returns all parameters from the requested route as an associative array. |
|
102
|
|
|
* @return string[] All parameters from the requested route |
|
103
|
|
|
*/ |
|
104
|
10 |
|
public function getParameters(): array |
|
105
|
|
|
{ |
|
106
|
10 |
|
return $this->values; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|