1
|
|
|
<?php namespace Arcanedev\LaravelTracker\Parsers; |
2
|
|
|
|
3
|
|
|
use Arcanedev\LaravelTracker\Contracts\Parsers\UserAgentParser as UserAgentParserContract; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class UserAgentParser |
7
|
|
|
* |
8
|
|
|
* @package Arcanedev\LaravelTracker\Parsers |
9
|
|
|
* @author ARCANEDEV <[email protected]> |
10
|
|
|
*/ |
11
|
|
|
class UserAgentParser implements UserAgentParserContract |
12
|
|
|
{ |
13
|
|
|
/* ------------------------------------------------------------------------------------------------ |
14
|
|
|
| Properties |
15
|
|
|
| ------------------------------------------------------------------------------------------------ |
16
|
|
|
*/ |
17
|
|
|
/** @var \UAParser\Parser */ |
18
|
|
|
protected $parser; |
19
|
|
|
|
20
|
|
|
/** @var string */ |
21
|
|
|
public $basePath; |
22
|
|
|
|
23
|
|
|
/* ------------------------------------------------------------------------------------------------ |
24
|
|
|
| Constructor |
25
|
|
|
| ------------------------------------------------------------------------------------------------ |
26
|
|
|
*/ |
27
|
|
|
/** |
28
|
|
|
* UserAgentParser constructor. |
29
|
|
|
* |
30
|
|
|
* @param \UAParser\Parser $parser |
31
|
|
|
* @param string $basePath |
32
|
|
|
* @param null $userAgent |
33
|
|
|
*/ |
34
|
24 |
|
public function __construct($parser, $basePath, $userAgent = null) |
|
|
|
|
35
|
|
|
{ |
36
|
24 |
|
if ( ! $userAgent && isset($_SERVER['HTTP_USER_AGENT'])) { |
37
|
|
|
$userAgent = $_SERVER['HTTP_USER_AGENT']; |
38
|
|
|
} |
39
|
|
|
|
40
|
24 |
|
$this->parser = $parser->parse($userAgent); |
|
|
|
|
41
|
24 |
|
$this->basePath = $basePath; |
42
|
24 |
|
} |
43
|
|
|
|
44
|
|
|
/* ------------------------------------------------------------------------------------------------ |
45
|
|
|
| Main Functions |
46
|
|
|
| ------------------------------------------------------------------------------------------------ |
47
|
|
|
*/ |
48
|
|
|
/** |
49
|
|
|
* Get the OS version. |
50
|
|
|
* |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
6 |
|
public function getOperatingSystemVersion() |
54
|
|
|
{ |
55
|
6 |
|
$os = $this->getOperatingSystem(); |
56
|
|
|
|
57
|
6 |
|
return $this->prepareVersion([$os->major, $os->minor, $os->patch]); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Get the OS Family. |
62
|
|
|
* |
63
|
|
|
* @return string|null |
64
|
|
|
*/ |
65
|
6 |
|
public function getOperatingSystemFamily() |
66
|
|
|
{ |
67
|
|
|
try { |
68
|
6 |
|
return $this->parser->os->family; |
|
|
|
|
69
|
|
|
} |
70
|
|
|
catch (\Exception $e) { |
|
|
|
|
71
|
|
|
return null; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get the browser. |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
6 |
|
public function getBrowser() |
81
|
|
|
{ |
82
|
6 |
|
return $this->getUserAgent()->family; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get the user agent version. |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
6 |
|
public function getUserAgentVersion() |
91
|
|
|
{ |
92
|
6 |
|
$ua = $this->getUserAgent(); |
93
|
|
|
|
94
|
6 |
|
return $this->prepareVersion([$ua->major, $ua->minor, $ua->patch]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get the original user agent. |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
6 |
|
public function getOriginalUserAgent() |
103
|
|
|
{ |
104
|
6 |
|
return $this->parser->originalUserAgent; |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get the user agent. |
109
|
|
|
* |
110
|
|
|
* @return \UAParser\Result\UserAgent |
111
|
|
|
*/ |
112
|
6 |
|
public function getUserAgent() |
113
|
|
|
{ |
114
|
6 |
|
return $this->parser->ua; |
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Get the operating system. |
119
|
|
|
* |
120
|
|
|
* @return \UAParser\Result\OperatingSystem |
121
|
|
|
*/ |
122
|
6 |
|
public function getOperatingSystem() |
123
|
|
|
{ |
124
|
6 |
|
return $this->parser->os; |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Get the device. |
129
|
|
|
* |
130
|
|
|
* @return \UAParser\Result\Device |
131
|
|
|
*/ |
132
|
|
|
public function getDevice() |
133
|
|
|
{ |
134
|
|
|
return $this->parser->device; |
|
|
|
|
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/* ------------------------------------------------------------------------------------------------ |
138
|
|
|
| Other Functions |
139
|
|
|
| ------------------------------------------------------------------------------------------------ |
140
|
|
|
*/ |
141
|
|
|
/** |
142
|
|
|
* Prepare the version. |
143
|
|
|
* |
144
|
|
|
* @param array $version |
145
|
|
|
* |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
6 |
|
protected function prepareVersion(array $version) |
149
|
|
|
{ |
150
|
6 |
|
return implode('.', array_filter($version)); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: