1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* DeviceView.php |
4
|
|
|
* |
5
|
|
|
* @copyright More in license.md |
6
|
|
|
* @license http://www.ipublikuj.eu |
7
|
|
|
* @author Adam Kadlec http://www.ipublikuj.eu |
8
|
|
|
* @package iPublikuj:MobileDetect! |
9
|
|
|
* @subpackage Helpers |
10
|
|
|
* @since 1.0.0 |
11
|
|
|
* |
12
|
|
|
* @date 23.04.14 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
declare(strict_types = 1); |
16
|
|
|
|
17
|
|
|
namespace IPub\MobileDetect\Helpers; |
18
|
|
|
|
19
|
|
|
use Nette; |
20
|
|
|
use Nette\Application; |
21
|
|
|
use Nette\Http; |
22
|
|
|
use Nette\Utils; |
23
|
|
|
|
24
|
|
|
final class DeviceView extends Nette\Object |
25
|
|
|
{ |
26
|
|
|
const VIEW_MOBILE = 'mobile'; |
27
|
|
|
const VIEW_PHONE = 'phone'; |
28
|
|
|
const VIEW_TABLET = 'tablet'; |
29
|
|
|
const VIEW_FULL = 'full'; |
30
|
|
|
const VIEW_NOT_MOBILE = 'not_mobile'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Http\IRequest |
34
|
|
|
*/ |
35
|
|
|
private $httpRequest; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var Http\IResponse |
39
|
|
|
*/ |
40
|
|
|
private $httpResponse; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private $viewType; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var CookieSettings |
49
|
|
|
*/ |
50
|
|
|
private $cookieSettings; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
private $switchParameterName = 'device_view'; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param CookieSettings $cookieSettings |
59
|
|
|
* @param Http\IRequest $httpRequest |
60
|
|
|
* @param Http\IResponse $httpResponse |
61
|
|
|
*/ |
62
|
|
|
public function __construct(CookieSettings $cookieSettings, Http\IRequest $httpRequest, Http\IResponse $httpResponse) |
63
|
|
|
{ |
64
|
|
|
$this->cookieSettings = $cookieSettings; |
65
|
|
|
$this->httpRequest = $httpRequest; |
66
|
|
|
$this->httpResponse = $httpResponse; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return void |
71
|
|
|
*/ |
72
|
|
|
public function detectViewType() |
73
|
|
|
{ |
74
|
|
|
if ($this->httpRequest->getQuery($this->switchParameterName)) { |
75
|
|
|
$this->viewType = $this->httpRequest->getQuery($this->switchParameterName); |
76
|
|
|
|
77
|
|
|
} elseif ($this->httpRequest->getCookie($this->cookieSettings->getName())) { |
78
|
|
|
$this->viewType = $this->httpRequest->getCookie($this->cookieSettings->getName()); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Gets the view type for a device |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
|
|
public function getViewType() : string |
88
|
|
|
{ |
89
|
|
|
return $this->viewType; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Is the device in full view |
94
|
|
|
* |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
|
|
public function isFullView() : bool |
98
|
|
|
{ |
99
|
|
|
return $this->viewType === self::VIEW_FULL; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Is the device a tablet view type |
104
|
|
|
* |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
|
public function isTabletView() : bool |
108
|
|
|
{ |
109
|
|
|
return $this->viewType === self::VIEW_TABLET; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Is the device a phone view type |
114
|
|
|
* |
115
|
|
|
* @return bool |
116
|
|
|
*/ |
117
|
|
|
public function isPhoneView() : bool |
118
|
|
|
{ |
119
|
|
|
return $this->viewType === self::VIEW_PHONE; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Is the device a mobile view type |
124
|
|
|
* |
125
|
|
|
* @return bool |
126
|
|
|
*/ |
127
|
|
|
public function isMobileView() : bool |
128
|
|
|
{ |
129
|
|
|
return $this->viewType === self::VIEW_MOBILE || $this->isPhoneView() || $this->isTabletView(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Is not the device a mobile view type (PC, Mac, etc.) |
134
|
|
|
* |
135
|
|
|
* @return bool |
136
|
|
|
*/ |
137
|
|
|
public function isNotMobileView() : bool |
138
|
|
|
{ |
139
|
|
|
return $this->viewType === self::VIEW_NOT_MOBILE; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Sets the tablet view type |
144
|
|
|
* |
145
|
|
|
* @return void |
146
|
|
|
*/ |
147
|
|
|
public function setTabletView() |
148
|
|
|
{ |
149
|
|
|
$this->viewType = self::VIEW_TABLET; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Sets the phone view type |
154
|
|
|
* |
155
|
|
|
* @return void |
156
|
|
|
*/ |
157
|
|
|
public function setPhoneView() |
158
|
|
|
{ |
159
|
|
|
$this->viewType = self::VIEW_PHONE; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Sets the mobile view type |
164
|
|
|
* |
165
|
|
|
* @return void |
166
|
|
|
*/ |
167
|
|
|
public function setMobileView() |
168
|
|
|
{ |
169
|
|
|
$this->viewType = self::VIEW_MOBILE; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Sets the not mobile view type |
174
|
|
|
* |
175
|
|
|
* @return void |
176
|
|
|
*/ |
177
|
|
|
public function setNotMobileView() |
178
|
|
|
{ |
179
|
|
|
$this->viewType = self::VIEW_NOT_MOBILE; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param string $name |
184
|
|
|
* |
185
|
|
|
* @return void |
186
|
|
|
*/ |
187
|
|
|
public function setSwitchParameterName(string $name) |
188
|
|
|
{ |
189
|
|
|
$this->switchParameterName = $name; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @return string |
194
|
|
|
*/ |
195
|
|
|
public function getSwitchParameterName() : string |
196
|
|
|
{ |
197
|
|
|
$this->switchParameterName; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Gets the switch param value from the query string (GET header) |
202
|
|
|
* |
203
|
|
|
* @return string |
204
|
|
|
*/ |
205
|
|
|
public function getSwitchParameterValue() : string |
206
|
|
|
{ |
207
|
|
|
return $this->httpRequest->getQuery($this->switchParameterName, self::VIEW_FULL); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Has the Request the switch param in the query string (GET header) |
212
|
|
|
* |
213
|
|
|
* @return bool |
214
|
|
|
*/ |
215
|
|
|
public function hasSwitchParameter() : bool |
216
|
|
|
{ |
217
|
|
|
return $this->httpRequest->getQuery($this->switchParameterName, FALSE) ? TRUE : FALSE; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Gets the RedirectResponse by switch param value |
222
|
|
|
* |
223
|
|
|
* @param string $redirectUrl |
224
|
|
|
* |
225
|
|
|
* @return Application\Responses\RedirectResponse |
226
|
|
|
*/ |
227
|
|
|
public function getRedirectResponseBySwitchParam(string $redirectUrl) : Application\Responses\RedirectResponse |
228
|
|
|
{ |
229
|
|
|
$statusCode = 302; |
230
|
|
|
|
231
|
|
|
switch ($this->getSwitchParameterValue()) { |
232
|
|
|
case self::VIEW_MOBILE: |
233
|
|
|
$this->createCookie(self::VIEW_MOBILE); |
234
|
|
|
break; |
235
|
|
|
|
236
|
|
|
case self::VIEW_PHONE: |
237
|
|
|
$this->createCookie(self::VIEW_PHONE); |
238
|
|
|
break; |
239
|
|
|
|
240
|
|
|
case self::VIEW_TABLET: |
241
|
|
|
$this->createCookie(self::VIEW_TABLET); |
242
|
|
|
break; |
243
|
|
|
|
244
|
|
|
default: |
245
|
|
|
$this->createCookie(self::VIEW_FULL); |
246
|
|
|
break; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
return new Application\Responses\RedirectResponse($redirectUrl, $statusCode); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Modifies the Response for non-mobile devices |
254
|
|
|
* |
255
|
|
|
* @return Http\IResponse |
256
|
|
|
*/ |
257
|
|
|
public function modifyNotMobileResponse() : Http\IResponse |
258
|
|
|
{ |
259
|
|
|
// Create cookie |
260
|
|
|
$this->createCookie(self::VIEW_NOT_MOBILE); |
261
|
|
|
|
262
|
|
|
return $this->httpResponse; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Modifies the Response for tablet devices |
267
|
|
|
* |
268
|
|
|
* @return Http\IResponse |
269
|
|
|
*/ |
270
|
|
|
public function modifyTabletResponse() : Http\IResponse |
271
|
|
|
{ |
272
|
|
|
// Create cookie |
273
|
|
|
$this->createCookie(self::VIEW_TABLET); |
274
|
|
|
|
275
|
|
|
return $this->httpResponse; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Modifies the Response for phone devices |
280
|
|
|
* |
281
|
|
|
* @return Http\IResponse |
282
|
|
|
*/ |
283
|
|
|
public function modifyPhoneResponse() : Http\IResponse |
284
|
|
|
{ |
285
|
|
|
// Create cookie |
286
|
|
|
$this->createCookie(self::VIEW_PHONE); |
287
|
|
|
|
288
|
|
|
return $this->httpResponse; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
/** |
292
|
|
|
* Modifies the Response for mobile devices |
293
|
|
|
* |
294
|
|
|
* @return Http\IResponse |
295
|
|
|
*/ |
296
|
|
|
public function modifyMobileResponse() : Http\IResponse |
297
|
|
|
{ |
298
|
|
|
// Create cookie |
299
|
|
|
$this->createCookie(self::VIEW_MOBILE); |
300
|
|
|
|
301
|
|
|
return $this->httpResponse; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Gets the RedirectResponse for phone devices |
306
|
|
|
* |
307
|
|
|
* @param string $host Uri host |
308
|
|
|
* @param int $statusCode Status code |
309
|
|
|
* |
310
|
|
|
* @return Application\Responses\RedirectResponse |
311
|
|
|
*/ |
312
|
|
|
public function getPhoneRedirectResponse(string $host, int $statusCode) : Application\Responses\RedirectResponse |
313
|
|
|
{ |
314
|
|
|
// Create cookie |
315
|
|
|
$this->createCookie(self::VIEW_PHONE); |
316
|
|
|
|
317
|
|
|
return new Application\Responses\RedirectResponse($host, $statusCode); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Gets the RedirectResponse for tablet devices |
322
|
|
|
* |
323
|
|
|
* @param string $host Uri host |
324
|
|
|
* @param int $statusCode Status code |
325
|
|
|
* |
326
|
|
|
* @return Application\Responses\RedirectResponse |
327
|
|
|
*/ |
328
|
|
|
public function getTabletRedirectResponse(string $host, int $statusCode) : Application\Responses\RedirectResponse |
329
|
|
|
{ |
330
|
|
|
// Create cookie |
331
|
|
|
$this->createCookie(self::VIEW_TABLET); |
332
|
|
|
|
333
|
|
|
return new Application\Responses\RedirectResponse($host, $statusCode); |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Gets the RedirectResponse for mobile devices |
338
|
|
|
* |
339
|
|
|
* @param string $host Uri host |
340
|
|
|
* @param int $statusCode Status code |
341
|
|
|
* |
342
|
|
|
* @return Application\Responses\RedirectResponse |
343
|
|
|
*/ |
344
|
|
|
public function getMobileRedirectResponse(string $host, int $statusCode) : Application\Responses\RedirectResponse |
345
|
|
|
{ |
346
|
|
|
// Create cookie |
347
|
|
|
$this->createCookie(self::VIEW_MOBILE); |
348
|
|
|
|
349
|
|
|
return new Application\Responses\RedirectResponse($host, $statusCode); |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* Gets the cookie |
354
|
|
|
* |
355
|
|
|
* @param string $cookieValue |
356
|
|
|
*/ |
357
|
|
|
private function createCookie(string $cookieValue) |
358
|
|
|
{ |
359
|
|
|
// Store cookie in response |
360
|
|
|
$this->httpResponse->setCookie( |
361
|
|
|
$this->cookieSettings->getName(), |
362
|
|
|
$cookieValue, |
363
|
|
|
$this->cookieSettings->getExpiresTime(), |
364
|
|
|
$this->cookieSettings->getPath(), |
365
|
|
|
$this->cookieSettings->getDomain(), |
366
|
|
|
$this->cookieSettings->isSecure(), |
367
|
|
|
$this->cookieSettings->isHttpOnly() |
368
|
|
|
); |
369
|
|
|
} |
370
|
|
|
} |
371
|
|
|
|