1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nip\Router\Parsers; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class AbstractParser |
7
|
|
|
* @package Nip\Router\Parsers |
8
|
|
|
*/ |
9
|
|
|
abstract class AbstractParser |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var \Nip\Request |
13
|
|
|
*/ |
14
|
|
|
protected $request; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $uri; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $map; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var array |
28
|
|
|
*/ |
29
|
|
|
protected $parts; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $params = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
protected $variables = []; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
protected $matches = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* AbstractParser constructor. |
48
|
|
|
* @param bool $map |
49
|
|
|
* @param array $params |
50
|
|
|
*/ |
51
|
12 |
|
public function __construct($map = false, $params = []) |
52
|
|
|
{ |
53
|
12 |
|
if ($map) { |
54
|
|
|
$this->setMap($map); |
55
|
12 |
|
} elseif ($this->map) { |
56
|
3 |
|
$this->parseMap(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
|
60
|
12 |
|
if ($params) { |
|
|
|
|
61
|
|
|
$this->setParams($params); |
62
|
|
|
} |
63
|
12 |
|
$this->init(); |
64
|
12 |
|
} |
65
|
|
|
|
66
|
11 |
|
protected function parseMap() |
67
|
|
|
{ |
68
|
11 |
|
$this->setParts(explode("/", trim($this->map, '/'))); |
69
|
11 |
|
} |
70
|
|
|
|
71
|
12 |
|
public function init() |
72
|
|
|
{ |
73
|
12 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param $uri |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
5 |
|
public function match($uri) |
80
|
|
|
{ |
81
|
5 |
|
$this->setUri($uri); |
82
|
|
|
|
83
|
5 |
|
return true; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param mixed $uri |
88
|
|
|
*/ |
89
|
5 |
|
public function setUri($uri) |
90
|
|
|
{ |
91
|
5 |
|
$this->uri = $uri; |
92
|
5 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param array $params |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
3 |
|
public function assemble($params = []) |
99
|
|
|
{ |
100
|
3 |
|
$return = $this->getMap(); |
101
|
|
|
|
102
|
3 |
|
if ($params) { |
|
|
|
|
103
|
3 |
|
foreach ($params as $key => $value) { |
104
|
3 |
|
if (stristr($return, ":" . $key) !== false) { |
105
|
3 |
|
$return = str_replace(":" . $key, $value, $return); |
106
|
3 |
|
unset($params[$key]); |
107
|
|
|
} |
108
|
3 |
|
if (array_key_exists($key, $this->params)) { |
109
|
|
|
unset($params[$key]); |
110
|
|
|
} |
111
|
|
|
} |
112
|
3 |
|
if ($params) { |
|
|
|
|
113
|
3 |
|
$return .= "?" . http_build_query($params); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
// set defaults |
118
|
3 |
|
if ($this->params) { |
|
|
|
|
119
|
|
|
foreach ($this->params as $key => $value) { |
120
|
|
|
if (is_string($value)) { |
121
|
|
|
$return = str_replace(":" . $key, $value, $return); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
3 |
|
return $return; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
6 |
|
public function getMap() |
133
|
|
|
{ |
134
|
6 |
|
return $this->map; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param boolean $map |
139
|
|
|
*/ |
140
|
10 |
|
public function setMap($map) |
141
|
|
|
{ |
142
|
10 |
|
$this->map = $map; |
|
|
|
|
143
|
10 |
|
$this->parseMap(); |
144
|
10 |
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param $params |
148
|
|
|
* @return array |
149
|
|
|
*/ |
150
|
1 |
|
public function stripEmptyParams($params) |
151
|
|
|
{ |
152
|
1 |
|
if (is_array($params)) { |
153
|
1 |
|
foreach ($params as $key => $param) { |
154
|
1 |
|
if (empty($param)) { |
155
|
|
|
unset($params[$key]); |
156
|
1 |
|
} elseif (is_array($param)) { |
157
|
|
|
$newParams = $this->stripEmptyParams($param); |
158
|
|
|
if (!is_array($newParams) or count($newParams) < 1) { |
159
|
|
|
unset($params[$key]); |
160
|
|
|
} else { |
161
|
|
|
$params[$key] = $newParams; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
1 |
|
return $params; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @return array |
172
|
|
|
*/ |
173
|
6 |
|
public function getParts() |
174
|
|
|
{ |
175
|
6 |
|
return $this->parts; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param array $parts |
180
|
|
|
*/ |
181
|
11 |
|
public function setParts($parts) |
182
|
|
|
{ |
183
|
11 |
|
$this->parts = $parts; |
184
|
11 |
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return array |
188
|
|
|
*/ |
189
|
2 |
|
public function getParams() |
190
|
|
|
{ |
191
|
2 |
|
return $this->params; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param array $params |
196
|
|
|
*/ |
197
|
2 |
|
public function setParams($params = []) |
198
|
|
|
{ |
199
|
2 |
|
if (count($params)) { |
200
|
2 |
|
foreach ($params as $key => $value) { |
201
|
2 |
|
$this->setParam($key, $value); |
202
|
|
|
} |
203
|
|
|
} |
204
|
2 |
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param $name |
208
|
|
|
* @param $value |
209
|
|
|
* @return $this |
210
|
|
|
*/ |
211
|
5 |
|
public function setParam($name, $value) |
212
|
|
|
{ |
213
|
5 |
|
$this->params[$name] = $value; |
214
|
|
|
|
215
|
5 |
|
return $this; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @param string $key |
220
|
|
|
* @return mixed|null |
221
|
|
|
*/ |
222
|
2 |
|
public function getParam($key) |
223
|
|
|
{ |
224
|
2 |
|
return $this->hasParam($key) ? $this->params[$key] : null; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @param $key |
229
|
|
|
* @return bool |
230
|
|
|
*/ |
231
|
2 |
|
public function hasParam($key) |
232
|
|
|
{ |
233
|
2 |
|
return isset($this->params[$key]); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @return array |
238
|
|
|
*/ |
239
|
|
|
public function getMatches() |
240
|
|
|
{ |
241
|
|
|
return $this->matches; |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* @return array |
246
|
|
|
*/ |
247
|
2 |
|
public function getVariables() |
248
|
|
|
{ |
249
|
2 |
|
return $this->variables; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @param array $variables |
254
|
|
|
*/ |
255
|
3 |
|
public function setVariables($variables) |
256
|
|
|
{ |
257
|
3 |
|
$this->variables = $variables; |
258
|
3 |
|
} |
259
|
|
|
|
260
|
|
|
// public function setModule($module) |
261
|
|
|
// { |
262
|
|
|
// $this->_module = $module; |
263
|
|
|
// } |
264
|
|
|
// |
265
|
|
|
// public function setController($controller) |
266
|
|
|
// { |
267
|
|
|
// $this->_controller = $controller; |
268
|
|
|
// } |
269
|
|
|
// |
270
|
|
|
// public function setAction($action) |
271
|
|
|
// { |
272
|
|
|
// $this->_action = $action; |
273
|
|
|
// } |
274
|
|
|
} |
275
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.