1
|
|
|
<?php |
2
|
|
|
namespace UserAgentParser\Provider; |
3
|
|
|
|
4
|
|
|
use UAParser\Result\Result as UAParserResult; |
5
|
|
|
use UserAgentParser\Exception; |
6
|
|
|
use UserAgentParser\Model; |
7
|
|
|
|
8
|
|
|
class YzalisUAParser extends AbstractProvider |
9
|
|
|
{ |
10
|
|
|
protected $defaultValues = [ |
11
|
|
|
'Other', |
12
|
|
|
]; |
13
|
|
|
|
14
|
|
|
private $parser; |
15
|
|
|
|
16
|
1 |
|
public function getName() |
17
|
|
|
{ |
18
|
1 |
|
return 'YzalisUAParser'; |
19
|
|
|
} |
20
|
|
|
|
21
|
2 |
|
public function getComposerPackageName() |
22
|
|
|
{ |
23
|
2 |
|
return 'yzalis/ua-parser'; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* |
28
|
|
|
* @param \UAParser\UAParser $parser |
29
|
|
|
*/ |
30
|
7 |
|
public function setParser(\UAParser\UAParser $parser = null) |
31
|
|
|
{ |
32
|
7 |
|
$this->parser = $parser; |
33
|
7 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* |
37
|
|
|
* @return \UAParser\UAParser |
38
|
|
|
*/ |
39
|
7 |
|
public function getParser() |
40
|
|
|
{ |
41
|
7 |
|
if ($this->parser !== null) { |
42
|
7 |
|
return $this->parser; |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
$this->parser = new \UAParser\UAParser(); |
46
|
|
|
|
47
|
1 |
|
return $this->parser; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* |
52
|
|
|
* @param UAParserResult $resultRaw |
53
|
|
|
* |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
6 |
|
private function hasResult(UAParserResult $resultRaw) |
57
|
|
|
{ |
58
|
|
|
/* @var $browserRaw \UAParser\Result\BrowserResult */ |
59
|
6 |
|
$browserRaw = $resultRaw->getBrowser(); |
60
|
|
|
|
61
|
6 |
|
if ($browserRaw !== null && $this->isRealResult($browserRaw->getFamily()) === true) { |
62
|
1 |
|
return true; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/* @var $osRaw \UAParser\Result\OperatingSystemResult */ |
66
|
5 |
|
$osRaw = $resultRaw->getOperatingSystem(); |
67
|
|
|
|
68
|
5 |
|
if ($osRaw !== null && $this->isRealResult($osRaw->getFamily()) === true) { |
69
|
1 |
|
return true; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/* @var $deviceRaw \UAParser\Result\DeviceResult */ |
73
|
4 |
|
$deviceRaw = $resultRaw->getDevice(); |
74
|
|
|
|
75
|
4 |
|
if ($deviceRaw !== null && $this->isRealResult($deviceRaw->getConstructor()) === true) { |
76
|
1 |
|
return true; |
77
|
|
|
} |
78
|
|
|
|
79
|
3 |
|
if ($deviceRaw !== null && $this->isRealResult($deviceRaw->getModel()) === true) { |
80
|
1 |
|
return true; |
81
|
|
|
} |
82
|
|
|
|
83
|
2 |
|
return false; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* |
88
|
|
|
* @param UAParserResult $resultRaw |
89
|
|
|
* |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
4 |
|
private function isMobile(UAParserResult $resultRaw) |
93
|
|
|
{ |
94
|
|
|
/* @var $deviceRaw \UAParser\Result\DeviceResult */ |
95
|
4 |
|
$deviceRaw = $resultRaw->getDevice(); |
96
|
|
|
|
97
|
4 |
|
if ($deviceRaw->getType() === 'mobile') { |
98
|
1 |
|
return true; |
99
|
|
|
} |
100
|
|
|
|
101
|
3 |
|
if ($deviceRaw->getType() === 'tablet') { |
102
|
1 |
|
return true; |
103
|
|
|
} |
104
|
|
|
|
105
|
2 |
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
6 |
|
public function parse($userAgent, array $headers = []) |
109
|
|
|
{ |
110
|
6 |
|
$parser = $this->getParser(); |
111
|
|
|
|
112
|
|
|
/* @var $resultRaw \UAParser\Result\Result */ |
113
|
6 |
|
$resultRaw = $parser->parse($userAgent); |
114
|
|
|
|
115
|
|
|
/* |
116
|
|
|
* No result found? |
117
|
|
|
*/ |
118
|
6 |
|
if ($this->hasResult($resultRaw) !== true) { |
119
|
2 |
|
throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/* @var $browserRaw \UAParser\Result\BrowserResult */ |
123
|
4 |
|
$browserRaw = $resultRaw->getBrowser(); |
124
|
|
|
|
125
|
|
|
/* @var $renderingEngineRaw \UAParser\Result\RenderingEngineResult */ |
126
|
4 |
|
$renderingEngineRaw = $resultRaw->getRenderingEngine(); |
127
|
|
|
|
128
|
|
|
/* @var $osRaw \UAParser\Result\OperatingSystemResult */ |
129
|
4 |
|
$osRaw = $resultRaw->getOperatingSystem(); |
130
|
|
|
|
131
|
|
|
/* @var $deviceRaw \UAParser\Result\DeviceResult */ |
132
|
4 |
|
$deviceRaw = $resultRaw->getDevice(); |
133
|
|
|
|
134
|
|
|
/* @var $emailRaw \UAParser\Result\EmailClientResult */ |
135
|
|
|
// currently not used...any idea to implement it? |
136
|
|
|
|
137
|
|
|
/* |
138
|
|
|
* Hydrate the model |
139
|
|
|
*/ |
140
|
4 |
|
$result = new Model\UserAgent(); |
141
|
4 |
|
$result->setProviderResultRaw($resultRaw); |
142
|
|
|
|
143
|
|
|
/* |
144
|
|
|
* Bot detection |
145
|
|
|
* i think this is currently not possible |
146
|
|
|
*/ |
147
|
|
|
|
148
|
|
|
/* |
149
|
|
|
* Browser |
150
|
|
|
*/ |
151
|
4 |
|
$browser = $result->getBrowser(); |
152
|
|
|
|
153
|
4 |
|
if ($this->isRealResult($browserRaw->getFamily()) === true) { |
154
|
1 |
|
$browser->setName($browserRaw->getFamily()); |
155
|
|
|
} |
156
|
|
|
|
157
|
4 |
|
if ($this->isRealResult($browserRaw->getVersionString()) === true) { |
158
|
1 |
|
$browser->getVersion()->setComplete($browserRaw->getVersionString()); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/* |
162
|
|
|
* renderingEngine |
163
|
|
|
*/ |
164
|
4 |
|
$renderingEngine = $result->getRenderingEngine(); |
165
|
|
|
|
166
|
4 |
|
if ($this->isRealResult($renderingEngineRaw->getFamily()) === true) { |
167
|
1 |
|
$renderingEngine->setName($renderingEngineRaw->getFamily()); |
168
|
|
|
} |
169
|
|
|
|
170
|
4 |
|
if ($this->isRealResult($renderingEngineRaw->getVersion()) === true) { |
171
|
1 |
|
$renderingEngine->getVersion()->setComplete($renderingEngineRaw->getVersion()); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/* |
175
|
|
|
* operatingSystem |
176
|
|
|
*/ |
177
|
4 |
|
$operatingSystem = $result->getOperatingSystem(); |
178
|
|
|
|
179
|
4 |
|
if ($this->isRealResult($osRaw->getFamily()) === true) { |
180
|
1 |
|
$operatingSystem->setName($osRaw->getFamily()); |
181
|
|
|
} |
182
|
|
|
|
183
|
4 |
|
if ($this->isRealResult($osRaw->getMajor()) === true) { |
184
|
1 |
|
$operatingSystem->getVersion()->setMajor($osRaw->getMajor()); |
185
|
|
|
|
186
|
1 |
|
if ($this->isRealResult($osRaw->getMinor()) === true) { |
187
|
1 |
|
$operatingSystem->getVersion()->setMinor($osRaw->getMinor()); |
188
|
|
|
} |
189
|
|
|
|
190
|
1 |
|
if ($this->isRealResult($osRaw->getPatch()) === true) { |
191
|
1 |
|
$operatingSystem->getVersion()->setPatch($osRaw->getPatch()); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/* |
196
|
|
|
* device |
197
|
|
|
*/ |
198
|
4 |
|
$device = $result->getDevice(); |
199
|
|
|
|
200
|
4 |
|
if ($this->isRealResult($deviceRaw->getModel()) === true) { |
201
|
2 |
|
$device->setModel($deviceRaw->getModel()); |
202
|
|
|
} |
203
|
|
|
|
204
|
4 |
|
if ($this->isRealResult($deviceRaw->getConstructor()) === true) { |
205
|
1 |
|
$device->setBrand($deviceRaw->getConstructor()); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
// removed desktop type, since it's a default value and not really detected |
209
|
4 |
|
if ($this->isRealResult($deviceRaw->getType()) === true && $deviceRaw->getType() !== 'desktop') { |
210
|
2 |
|
$device->setType($deviceRaw->getType()); |
211
|
|
|
} |
212
|
|
|
|
213
|
4 |
|
if ($this->isMobile($resultRaw) === true) { |
214
|
2 |
|
$device->setIsMobile(true); |
215
|
|
|
} |
216
|
|
|
|
217
|
4 |
|
return $result; |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|