1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mosaic\Routing; |
4
|
|
|
|
5
|
|
|
use UnexpectedValueException; |
6
|
|
|
|
7
|
|
|
class Route |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* The URI pattern the route responds to. |
11
|
|
|
* |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
protected $uri; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The HTTP methods the route responds to. |
18
|
|
|
* |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
protected $methods; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The route action array. |
25
|
|
|
* |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
protected $action; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
protected $parameters = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Create a new Route instance. |
37
|
|
|
* |
38
|
|
|
* @param string|array $methods |
39
|
|
|
* @param string $uri |
40
|
|
|
* @param Closure|array $action |
41
|
|
|
* |
42
|
|
|
* @throws UnexpectedValueException |
43
|
|
|
*/ |
44
|
29 |
|
public function __construct($methods, $uri, $action) |
45
|
|
|
{ |
46
|
29 |
|
$this->setUri($uri); |
47
|
29 |
|
$this->methods = (array) $methods; |
48
|
|
|
|
49
|
29 |
|
$this->action = $this->parseAction($action); |
50
|
|
|
|
51
|
28 |
|
if (in_array('GET', $this->methods) && !in_array('HEAD', $this->methods)) { |
52
|
16 |
|
$this->methods[] = 'HEAD'; |
53
|
|
|
} |
54
|
28 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param array $parameters |
58
|
|
|
*/ |
59
|
5 |
|
public function bind(array $parameters = []) |
60
|
|
|
{ |
61
|
5 |
|
$this->parameters = $parameters; |
62
|
5 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Parse the route action into a standard array. |
66
|
|
|
* |
67
|
|
|
* @param callable|array $action |
68
|
|
|
* |
69
|
|
|
* @throws UnexpectedValueException |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
29 |
|
protected function parseAction($action) |
73
|
|
|
{ |
74
|
29 |
|
if (is_callable($action)) { |
75
|
8 |
|
$action = ['uses' => $action]; |
76
|
22 |
|
} elseif (is_string($action)) { |
77
|
17 |
|
$action = ['uses' => $action]; |
78
|
|
|
} |
79
|
|
|
|
80
|
29 |
|
if (is_string($action['uses']) && strpos($action['uses'], '@') == 0) { |
81
|
1 |
|
throw new UnexpectedValueException(sprintf( |
82
|
1 |
|
'Invalid route action: [%s]', $action['uses'] |
83
|
|
|
)); |
84
|
|
|
} |
85
|
|
|
|
86
|
28 |
|
return $action; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
7 |
|
public function methods() : array |
93
|
|
|
{ |
94
|
7 |
|
return $this->methods; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return string |
99
|
|
|
*/ |
100
|
8 |
|
public function uri() : string |
101
|
|
|
{ |
102
|
8 |
|
return $this->uri; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
10 |
|
public function action() : array |
109
|
|
|
{ |
110
|
10 |
|
return $this->action; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return array |
115
|
|
|
*/ |
116
|
3 |
|
public function parameters() : array |
117
|
|
|
{ |
118
|
3 |
|
return $this->parameters; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param $uri |
123
|
|
|
*/ |
124
|
29 |
|
public function setUri(string $uri) |
125
|
|
|
{ |
126
|
29 |
|
$this->uri = '/' . trim($uri, '/'); |
127
|
29 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param string $name |
131
|
|
|
* |
132
|
|
|
* @return bool |
133
|
|
|
*/ |
134
|
1 |
|
public function hasParameter(string $name) |
135
|
|
|
{ |
136
|
1 |
|
return isset($this->parameters[$name]); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param string $name |
141
|
|
|
* |
142
|
|
|
* @return mixed |
143
|
|
|
*/ |
144
|
1 |
|
public function parameter(string $name) |
145
|
|
|
{ |
146
|
1 |
|
return $this->parameters[$name]; |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|