|
1
|
|
|
<?php |
|
2
|
|
|
namespace suda\framework\route\uri; |
|
3
|
|
|
|
|
4
|
|
|
use suda\framework\route\uri\parameter\Parameter; |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* 路由匹配工具 |
|
8
|
|
|
*/ |
|
9
|
|
|
class UriMatcher extends MatcherHelper |
|
10
|
|
|
{ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* 匹配Uri |
|
14
|
|
|
* |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $uri; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* 匹配的正则 |
|
21
|
|
|
* |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $match; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* URI中的参数 |
|
28
|
|
|
* |
|
29
|
|
|
* @var Parameter[] |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $parameter; |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
public function __construct(string $uri, string $match, array $parameter) |
|
35
|
|
|
{ |
|
36
|
|
|
$this->uri = $uri; |
|
37
|
|
|
$this->match = $match; |
|
38
|
|
|
$this->parameter = $parameter; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* 匹配URL |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $url |
|
45
|
|
|
* @param boolean $ignoreCase |
|
46
|
|
|
* @return array|null |
|
47
|
|
|
*/ |
|
48
|
|
|
public function match(string $url, bool $ignoreCase = true):?array |
|
49
|
|
|
{ |
|
50
|
|
|
$match = '#^'. $this->match.'$#'. ($ignoreCase?'i':''); |
|
51
|
|
|
$parameter = []; |
|
52
|
|
|
if (preg_match($match, $url, $parameter, PREG_UNMATCHED_AS_NULL) > 0) { |
|
53
|
|
|
return array_slice($parameter, 1); |
|
54
|
|
|
} |
|
55
|
|
|
return null; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* 构建参数 |
|
60
|
|
|
* |
|
61
|
|
|
* @param array $matchedParameter |
|
62
|
|
|
* @return array |
|
63
|
|
|
*/ |
|
64
|
|
|
public function buildParameter(array $matchedParameter):array |
|
65
|
|
|
{ |
|
66
|
|
|
$parameters = []; |
|
67
|
|
|
foreach ($this->parameter as $index => $parameter) { |
|
68
|
|
|
$match = $matchedParameter[$index] ?? null; |
|
69
|
|
|
if (null === $match) { |
|
70
|
|
|
$value = $parameter->getDefaultValue(); |
|
71
|
|
|
} else { |
|
72
|
|
|
$value = $parameter->unpackValue($match); |
|
73
|
|
|
} |
|
74
|
|
|
if ($value !== null) { |
|
75
|
|
|
$parameters[$parameter->getIndexName()] = $value; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
return $parameters; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
public function getParameter(string $name):?Parameter |
|
82
|
|
|
{ |
|
83
|
|
|
foreach ($this->parameter as $parameter) { |
|
84
|
|
|
if ($parameter->getIndexName() === $name) { |
|
85
|
|
|
return $parameter; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
return null; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Get 匹配的正则 |
|
93
|
|
|
* |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getMatch() |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->match; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Set 匹配的正则 |
|
103
|
|
|
* |
|
104
|
|
|
* @param string $match 匹配的正则 |
|
105
|
|
|
* |
|
106
|
|
|
* @return self |
|
107
|
|
|
*/ |
|
108
|
|
|
public function setMatch(string $match) |
|
109
|
|
|
{ |
|
110
|
|
|
$this->match = $match; |
|
111
|
|
|
|
|
112
|
|
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Get 匹配Uri |
|
117
|
|
|
* |
|
118
|
|
|
* @return string |
|
119
|
|
|
*/ |
|
120
|
|
|
public function getUri() |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->uri; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* 获取参数 |
|
127
|
|
|
* |
|
128
|
|
|
* @param integer $index |
|
129
|
|
|
* @return Parameter|null |
|
130
|
|
|
*/ |
|
131
|
|
|
public function getParameterByIndex(int $index):?Parameter |
|
132
|
|
|
{ |
|
133
|
|
|
foreach ($this->parameter as $parameter) { |
|
134
|
|
|
if ($parameter->getIndex() === $index) { |
|
135
|
|
|
return $parameter; |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
return null; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|