1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* The MIT License (MIT) |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2015 zepi |
6
|
|
|
* |
7
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
8
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
9
|
|
|
* in the Software without restriction, including without limitation the rights |
10
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
11
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
12
|
|
|
* furnished to do so, subject to the following conditions: |
13
|
|
|
* |
14
|
|
|
* The above copyright notice and this permission notice shall be included in |
15
|
|
|
* all copies or substantial portions of the Software. |
16
|
|
|
* |
17
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
18
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
19
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
20
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
21
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
22
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
23
|
|
|
* THE SOFTWARE. |
24
|
|
|
* |
25
|
|
|
*/ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The abstract RequestAbstract is the base for all request types |
29
|
|
|
* |
30
|
|
|
* @package Zepi\Turbo\Request |
31
|
|
|
* @author Matthias Zobrist <[email protected]> |
32
|
|
|
* @copyright Copyright (c) 2015 zepi |
33
|
|
|
*/ |
34
|
|
|
|
35
|
|
|
namespace Zepi\Turbo\Request; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The abstract RequestAbstract is the base for all request types |
39
|
|
|
* |
40
|
|
|
* @author Matthias Zobrist <[email protected]> |
41
|
|
|
* @copyright Copyright (c) 2015 zepi |
42
|
|
|
*/ |
43
|
|
|
abstract class RequestAbstract |
44
|
|
|
{ |
45
|
|
|
const OS_LINUX = 'linux'; |
46
|
|
|
const OS_WINDOWS = 'windows'; |
47
|
|
|
const OS_UNKNOWN = 'unknown'; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @access protected |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
protected $route; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @access protected |
57
|
|
|
* @var array |
58
|
|
|
*/ |
59
|
|
|
protected $params = array(); |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @access protected |
63
|
|
|
* @var array |
64
|
|
|
*/ |
65
|
|
|
protected $routeParams = array(); |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @access protected |
69
|
|
|
* @var string |
70
|
|
|
*/ |
71
|
|
|
protected $base; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @access protected |
75
|
|
|
* @var string |
76
|
|
|
*/ |
77
|
|
|
protected $locale; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @access protected |
81
|
|
|
* @var string |
82
|
|
|
*/ |
83
|
|
|
protected $operatingSystem; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @access protected |
87
|
|
|
* @var array |
88
|
|
|
*/ |
89
|
|
|
protected $data = array(); |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Constructs the object |
93
|
|
|
* |
94
|
|
|
* @access public |
95
|
|
|
* @param string $route |
96
|
|
|
* @param array params |
97
|
|
|
* @param string $base |
98
|
|
|
* @param string $locale |
99
|
|
|
* @param string $operatingSystem |
100
|
|
|
* @param array $data |
101
|
|
|
*/ |
102
|
48 |
|
public function __construct($route, $params, $base, $locale, $operatingSystem, $data = array()) |
103
|
|
|
{ |
104
|
48 |
|
$this->route = $route; |
105
|
48 |
|
$this->locale = $locale; |
106
|
48 |
|
$this->operatingSystem = $operatingSystem; |
107
|
|
|
|
108
|
48 |
|
if (is_array($params)) { |
109
|
48 |
|
$this->params = $params; |
110
|
|
|
} |
111
|
|
|
|
112
|
48 |
|
if (substr($base, -1) === '/') { |
113
|
31 |
|
$base = substr($base, 0, -1); |
114
|
|
|
} |
115
|
48 |
|
$this->base = $base; |
116
|
|
|
|
117
|
48 |
|
if (is_array($data)) { |
118
|
48 |
|
$this->data = $data; |
119
|
|
|
} |
120
|
48 |
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Returns the route of the request. |
124
|
|
|
* |
125
|
|
|
* @access public |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
1 |
|
public function getRoute() |
129
|
|
|
{ |
130
|
1 |
|
return $this->route; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Sets the route of the request |
135
|
|
|
* |
136
|
|
|
* @access public |
137
|
|
|
* @param string $route |
138
|
|
|
*/ |
139
|
2 |
|
public function setRoute($route) |
140
|
|
|
{ |
141
|
2 |
|
$this->route = $route; |
142
|
2 |
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Returns true if the given param key exists. |
146
|
|
|
* |
147
|
|
|
* @access public |
148
|
|
|
* @param string $key |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
2 |
|
public function hasParam($key) |
152
|
|
|
{ |
153
|
2 |
|
return (isset($this->params[$key])); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Returns the value for the given param key. If the |
158
|
|
|
* key does not exists the function will return false. |
159
|
|
|
* |
160
|
|
|
* @access public |
161
|
|
|
* @param string $key |
162
|
|
|
* @return mixed |
163
|
|
|
*/ |
164
|
2 |
|
public function getParam($key) |
165
|
|
|
{ |
166
|
2 |
|
if (!$this->hasParam($key)) { |
167
|
1 |
|
return false; |
168
|
|
|
} |
169
|
|
|
|
170
|
1 |
|
return $this->params[$key]; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Returns all params of the request |
175
|
|
|
* |
176
|
|
|
* @access public |
177
|
|
|
* @return array |
178
|
|
|
*/ |
179
|
1 |
|
public function getParams() |
180
|
|
|
{ |
181
|
1 |
|
return $this->params; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Adds an parameter which is detected in the route. |
186
|
|
|
* |
187
|
|
|
* @access public |
188
|
|
|
* @param mixed $param |
189
|
|
|
* @param string $key |
190
|
|
|
*/ |
191
|
1 |
|
public function addRouteParam($param, $key = '') |
192
|
|
|
{ |
193
|
1 |
|
$this->routeParams[] = $param; |
194
|
|
|
|
195
|
1 |
|
if ($key != '') { |
196
|
1 |
|
$this->routeParams[$key]= $param; |
197
|
|
|
} |
198
|
1 |
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Sets the array of route params for the request |
202
|
|
|
* |
203
|
|
|
* @access public |
204
|
|
|
* @param array $params |
205
|
|
|
* @return boolean |
206
|
|
|
*/ |
207
|
3 |
|
public function setRouteParams($params) |
208
|
|
|
{ |
209
|
3 |
|
if (!is_array($params)) { |
210
|
1 |
|
return false; |
211
|
|
|
} |
212
|
|
|
|
213
|
2 |
|
$this->routeParams = $params; |
214
|
|
|
|
215
|
2 |
|
return true; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Returns the route param for the given index. |
220
|
|
|
* |
221
|
|
|
* @access public |
222
|
|
|
* @param integer|string $key |
223
|
|
|
* @return false|mixed |
224
|
|
|
*/ |
225
|
3 |
|
public function getRouteParam($key) |
226
|
|
|
{ |
227
|
3 |
|
if (!isset($this->routeParams[$key])) { |
228
|
1 |
|
return false; |
229
|
|
|
} |
230
|
|
|
|
231
|
2 |
|
return $this->routeParams[$key]; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Returns the delimitier, which is used to split the route |
236
|
|
|
* into parts. |
237
|
|
|
* |
238
|
|
|
* @access public |
239
|
|
|
* @return string |
240
|
|
|
*/ |
241
|
|
|
abstract public function getRouteDelimiter(); |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* Returns the correct url for the given url part |
245
|
|
|
* |
246
|
|
|
* @access public |
247
|
|
|
* @param string $routePart |
248
|
|
|
* @return string |
249
|
|
|
*/ |
250
|
2 |
|
public function getFullRoute($routePart = '') |
251
|
|
|
{ |
252
|
2 |
|
if ($routePart == '') { |
253
|
1 |
|
$routePart = $this->route; |
254
|
|
|
} |
255
|
|
|
|
256
|
2 |
|
$delimiter = $this->getRouteDelimiter(); |
257
|
2 |
|
if (substr($routePart, 0, strlen($delimiter)) !== $delimiter) { |
258
|
1 |
|
$routePart = $delimiter . $routePart; |
259
|
|
|
} |
260
|
|
|
|
261
|
2 |
|
$posPoint = strrpos($routePart, '.'); |
262
|
2 |
|
if (substr($routePart, -1) !== $delimiter && ($posPoint === false || $posPoint < strrpos($routePart, $delimiter))) { |
263
|
2 |
|
$routePart .= $delimiter; |
264
|
|
|
} |
265
|
|
|
|
266
|
2 |
|
return $this->base . $routePart; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Returns the locale of the request |
271
|
|
|
* |
272
|
|
|
* @access public |
273
|
|
|
* @return string |
274
|
|
|
*/ |
275
|
1 |
|
public function getLocale() |
276
|
|
|
{ |
277
|
1 |
|
return $this->locale; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Returns the operating system of the machine |
282
|
|
|
* |
283
|
|
|
* @access public |
284
|
|
|
* @return string |
285
|
|
|
*/ |
286
|
|
|
public function getOperatingSystem() |
287
|
|
|
{ |
288
|
|
|
return $this->operatingSystem; |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
|