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
|
|
|
|
18
|
|
|
/** @var \UAParser\Parser */ |
19
|
|
|
protected $parser; |
20
|
|
|
|
21
|
|
|
/** @var string */ |
22
|
|
|
public $basePath; |
23
|
|
|
|
24
|
|
|
/* ----------------------------------------------------------------- |
25
|
|
|
| Constructor |
26
|
|
|
| ----------------------------------------------------------------- |
27
|
|
|
*/ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* UserAgentParser constructor. |
31
|
|
|
* |
32
|
|
|
* @param \UAParser\Parser $parser |
33
|
|
|
* @param string $basePath |
34
|
|
|
* @param null $userAgent |
35
|
|
|
*/ |
36
|
15 |
|
public function __construct($parser, $basePath, $userAgent = null) |
37
|
|
|
{ |
38
|
15 |
|
if ( ! $userAgent && isset($_SERVER['HTTP_USER_AGENT'])) { |
39
|
|
|
$userAgent = $_SERVER['HTTP_USER_AGENT']; |
40
|
|
|
} |
41
|
|
|
|
42
|
15 |
|
$this->parser = $parser->parse($userAgent); |
|
|
|
|
43
|
15 |
|
$this->basePath = $basePath; |
44
|
15 |
|
} |
45
|
|
|
|
46
|
|
|
/* ----------------------------------------------------------------- |
47
|
|
|
| Main Methods |
48
|
|
|
| ----------------------------------------------------------------- |
49
|
|
|
*/ |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get the OS version. |
53
|
|
|
* |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
9 |
|
public function getOperatingSystemVersion() |
57
|
|
|
{ |
58
|
9 |
|
$os = $this->getOperatingSystem(); |
59
|
|
|
|
60
|
9 |
|
return $this->prepareVersion([$os->major, $os->minor, $os->patch]); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get the OS Family. |
65
|
|
|
* |
66
|
|
|
* @return string|null |
67
|
|
|
*/ |
68
|
9 |
|
public function getOperatingSystemFamily() |
69
|
|
|
{ |
70
|
|
|
try { |
71
|
9 |
|
return $this->parser->os->family; |
72
|
|
|
} |
73
|
|
|
catch (\Exception $e) { |
74
|
|
|
return null; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get the browser. |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
9 |
|
public function getBrowser() |
84
|
|
|
{ |
85
|
9 |
|
return $this->getUserAgent()->family; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Get the user agent version. |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
9 |
|
public function getUserAgentVersion() |
94
|
|
|
{ |
95
|
9 |
|
$ua = $this->getUserAgent(); |
96
|
|
|
|
97
|
9 |
|
return $this->prepareVersion([$ua->major, $ua->minor, $ua->patch]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get the original user agent. |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
9 |
|
public function getOriginalUserAgent() |
106
|
|
|
{ |
107
|
9 |
|
return $this->parser->originalUserAgent; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Get the user agent. |
112
|
|
|
* |
113
|
|
|
* @return \UAParser\Result\UserAgent |
114
|
|
|
*/ |
115
|
9 |
|
public function getUserAgent() |
116
|
|
|
{ |
117
|
9 |
|
return $this->parser->ua; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Get the operating system. |
122
|
|
|
* |
123
|
|
|
* @return \UAParser\Result\OperatingSystem |
124
|
|
|
*/ |
125
|
9 |
|
public function getOperatingSystem() |
126
|
|
|
{ |
127
|
9 |
|
return $this->parser->os; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Get the device. |
132
|
|
|
* |
133
|
|
|
* @return \UAParser\Result\Device |
134
|
|
|
*/ |
135
|
|
|
public function getDevice() |
136
|
|
|
{ |
137
|
|
|
return $this->parser->device; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/* ----------------------------------------------------------------- |
141
|
|
|
| Other Methods |
142
|
|
|
| ----------------------------------------------------------------- |
143
|
|
|
*/ |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Prepare the version. |
147
|
|
|
* |
148
|
|
|
* @param array $version |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
12 |
|
protected function prepareVersion(array $version) |
153
|
|
|
{ |
154
|
12 |
|
return implode('.', array_filter($version)); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..