|
1
|
|
|
<?php namespace Arcanedev\LaravelTracker\Detectors; |
|
2
|
|
|
|
|
3
|
|
|
use Arcanedev\LaravelTracker\Contracts\Detectors\DeviceDetector as DeviceDetectorContract; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class DeviceDetector |
|
7
|
|
|
* |
|
8
|
|
|
* @package Arcanedev\LaravelTracker\Detectors |
|
9
|
|
|
* @author ARCANEDEV <[email protected]> |
|
10
|
|
|
*/ |
|
11
|
|
|
class DeviceDetector implements DeviceDetectorContract |
|
12
|
|
|
{ |
|
13
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
14
|
|
|
| Properties |
|
15
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
16
|
|
|
*/ |
|
17
|
|
|
/** @var \Jenssegers\Agent\Agent */ |
|
18
|
|
|
protected $agent; |
|
19
|
|
|
|
|
20
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
21
|
|
|
| Constructor |
|
22
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
23
|
|
|
*/ |
|
24
|
|
|
/** |
|
25
|
|
|
* DeviceDetector constructor. |
|
26
|
|
|
* |
|
27
|
|
|
* @param \Jenssegers\Agent\Agent $agent |
|
28
|
|
|
*/ |
|
29
|
24 |
|
public function __construct($agent) |
|
30
|
|
|
{ |
|
31
|
24 |
|
$this->agent = $agent; |
|
32
|
24 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/* ------------------------------------------------------------------------------------------------ |
|
35
|
|
|
| Main Functions |
|
36
|
|
|
| ------------------------------------------------------------------------------------------------ |
|
37
|
|
|
*/ |
|
38
|
|
|
/** |
|
39
|
|
|
* Detect kind, model and mobility. |
|
40
|
|
|
* |
|
41
|
|
|
* @return array |
|
42
|
|
|
*/ |
|
43
|
6 |
|
public function detect() |
|
44
|
|
|
{ |
|
45
|
|
|
return [ |
|
46
|
6 |
|
'kind' => $this->getDeviceKind(), |
|
47
|
6 |
|
'model' => $this->agent->device() ?: '', |
|
48
|
6 |
|
'is_mobile' => $this->agent->isMobile(), |
|
49
|
3 |
|
]; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Get the kind of device. |
|
54
|
|
|
* |
|
55
|
|
|
* @return string |
|
56
|
|
|
*/ |
|
57
|
6 |
|
public function getDeviceKind() |
|
58
|
|
|
{ |
|
59
|
6 |
|
if ($this->isTablet()) return 'Tablet'; |
|
60
|
6 |
|
if ($this->isPhone()) return 'Phone'; |
|
61
|
6 |
|
if ($this->isComputer()) return 'Computer'; |
|
62
|
|
|
|
|
63
|
|
|
return 'unavailable'; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Is this a tablet? |
|
68
|
|
|
* |
|
69
|
|
|
* @return bool |
|
70
|
|
|
*/ |
|
71
|
6 |
|
public function isTablet() |
|
72
|
|
|
{ |
|
73
|
6 |
|
return $this->agent->isTablet(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Is this a computer? |
|
78
|
|
|
* |
|
79
|
|
|
* @return bool |
|
80
|
|
|
*/ |
|
81
|
6 |
|
public function isComputer() |
|
82
|
|
|
{ |
|
83
|
6 |
|
return ! $this->agent->isMobile(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Is this a phone? |
|
88
|
|
|
* |
|
89
|
|
|
* @return bool |
|
90
|
|
|
*/ |
|
91
|
6 |
|
public function isPhone() |
|
92
|
|
|
{ |
|
93
|
6 |
|
return ! ($this->isTablet() || $this->isComputer()); |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|