1
|
|
|
<?php |
2
|
|
|
namespace UserAgentParser\Provider; |
3
|
|
|
|
4
|
|
|
use UAParser\Result as UAResult; |
5
|
|
|
use UAParser\Result\Result as UAParserResult; |
6
|
|
|
use UserAgentParser\Exception; |
7
|
|
|
use UserAgentParser\Model; |
8
|
|
|
|
9
|
|
|
class YzalisUAParser extends AbstractProvider |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Name of the provider |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $name = 'YzalisUAParser'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Homepage of the provider |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $homepage = 'https://github.com/yzalis/UAParser'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Composer package name |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $packageName = 'yzalis/ua-parser'; |
31
|
|
|
|
32
|
|
|
protected $detectionCapabilities = [ |
33
|
|
|
|
34
|
|
|
'browser' => [ |
35
|
|
|
'name' => true, |
36
|
|
|
'version' => true, |
37
|
|
|
], |
38
|
|
|
|
39
|
|
|
'renderingEngine' => [ |
40
|
|
|
'name' => true, |
41
|
|
|
'version' => true, |
42
|
|
|
], |
43
|
|
|
|
44
|
|
|
'operatingSystem' => [ |
45
|
|
|
'name' => true, |
46
|
|
|
'version' => true, |
47
|
|
|
], |
48
|
|
|
|
49
|
|
|
'device' => [ |
50
|
|
|
'model' => true, |
51
|
|
|
'brand' => true, |
52
|
|
|
'type' => true, |
53
|
|
|
'isMobile' => false, |
54
|
|
|
'isTouch' => false, |
55
|
|
|
], |
56
|
|
|
|
57
|
|
|
'bot' => [ |
58
|
|
|
'isBot' => false, |
59
|
|
|
'name' => false, |
60
|
|
|
'type' => false, |
61
|
|
|
], |
62
|
|
|
]; |
63
|
|
|
|
64
|
|
|
protected $defaultValues = [ |
65
|
|
|
'Other', |
66
|
|
|
]; |
67
|
|
|
|
68
|
|
|
private $parser; |
69
|
|
|
|
70
|
14 |
View Code Duplication |
public function __construct(\UAParser\UAParser $parser = null) |
|
|
|
|
71
|
|
|
{ |
72
|
14 |
|
if (! class_exists('UAParser\UAParser', true)) { |
73
|
1 |
|
throw new Exception\PackageNotLoadedException('You need to install ' . $this->getHomepage() . ' to use this provider'); |
74
|
|
|
} |
75
|
|
|
|
76
|
13 |
|
$this->parser = $parser; |
77
|
13 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* |
81
|
|
|
* @return \UAParser\UAParser |
82
|
|
|
*/ |
83
|
7 |
|
public function getParser() |
84
|
|
|
{ |
85
|
7 |
|
if ($this->parser !== null) { |
86
|
7 |
|
return $this->parser; |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
$this->parser = new \UAParser\UAParser(); |
90
|
|
|
|
91
|
1 |
|
return $this->parser; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* |
96
|
|
|
* @param UAParserResult $resultRaw |
97
|
|
|
* |
98
|
|
|
* @return bool |
99
|
|
|
*/ |
100
|
6 |
|
private function hasResult(UAParserResult $resultRaw) |
101
|
|
|
{ |
102
|
|
|
/* @var $browserRaw \UAParser\Result\BrowserResult */ |
103
|
6 |
|
$browserRaw = $resultRaw->getBrowser(); |
104
|
|
|
|
105
|
6 |
|
if ($browserRaw !== null && $this->isRealResult($browserRaw->getFamily()) === true) { |
106
|
1 |
|
return true; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/* @var $osRaw \UAParser\Result\OperatingSystemResult */ |
110
|
5 |
|
$osRaw = $resultRaw->getOperatingSystem(); |
111
|
|
|
|
112
|
5 |
|
if ($osRaw !== null && $this->isRealResult($osRaw->getFamily()) === true) { |
113
|
1 |
|
return true; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/* @var $deviceRaw \UAParser\Result\DeviceResult */ |
117
|
4 |
|
$deviceRaw = $resultRaw->getDevice(); |
118
|
|
|
|
119
|
4 |
|
if ($deviceRaw !== null && $this->isRealResult($deviceRaw->getConstructor()) === true) { |
120
|
1 |
|
return true; |
121
|
|
|
} |
122
|
|
|
|
123
|
3 |
|
if ($deviceRaw !== null && $this->isRealResult($deviceRaw->getModel()) === true) { |
124
|
1 |
|
return true; |
125
|
|
|
} |
126
|
|
|
|
127
|
2 |
|
return false; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* |
132
|
|
|
* @param Model\Browser $browser |
133
|
|
|
* @param UAResult\BrowserResult $browserRaw |
134
|
|
|
*/ |
135
|
4 |
View Code Duplication |
private function hydrateBrowser(Model\Browser $browser, UAResult\BrowserResult $browserRaw) |
|
|
|
|
136
|
|
|
{ |
137
|
4 |
|
if ($this->isRealResult($browserRaw->getFamily()) === true) { |
138
|
1 |
|
$browser->setName($browserRaw->getFamily()); |
139
|
|
|
} |
140
|
|
|
|
141
|
4 |
|
if ($this->isRealResult($browserRaw->getVersionString()) === true) { |
142
|
1 |
|
$browser->getVersion()->setComplete($browserRaw->getVersionString()); |
143
|
|
|
} |
144
|
4 |
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* |
148
|
|
|
* @param Model\RenderingEngine $engine |
149
|
|
|
* @param UAResult\RenderingEngineResult $renderingEngineRaw |
150
|
|
|
*/ |
151
|
4 |
View Code Duplication |
private function hydrateRenderingEngine(Model\RenderingEngine $engine, UAResult\RenderingEngineResult $renderingEngineRaw) |
|
|
|
|
152
|
|
|
{ |
153
|
4 |
|
if ($this->isRealResult($renderingEngineRaw->getFamily()) === true) { |
154
|
1 |
|
$engine->setName($renderingEngineRaw->getFamily()); |
155
|
|
|
} |
156
|
|
|
|
157
|
4 |
|
if ($this->isRealResult($renderingEngineRaw->getVersion()) === true) { |
158
|
1 |
|
$engine->getVersion()->setComplete($renderingEngineRaw->getVersion()); |
159
|
|
|
} |
160
|
4 |
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* |
164
|
|
|
* @param Model\OperatingSystem $os |
165
|
|
|
* @param UAResult\OperatingSystemResult $osRaw |
166
|
|
|
*/ |
167
|
4 |
|
private function hydrateOperatingSystem(Model\OperatingSystem $os, UAResult\OperatingSystemResult $osRaw) |
168
|
|
|
{ |
169
|
4 |
|
if ($this->isRealResult($osRaw->getFamily()) === true) { |
170
|
1 |
|
$os->setName($osRaw->getFamily()); |
171
|
|
|
} |
172
|
|
|
|
173
|
4 |
|
if ($this->isRealResult($osRaw->getMajor()) === true) { |
174
|
1 |
|
$os->getVersion()->setMajor($osRaw->getMajor()); |
175
|
|
|
|
176
|
1 |
|
if ($this->isRealResult($osRaw->getMinor()) === true) { |
177
|
1 |
|
$os->getVersion()->setMinor($osRaw->getMinor()); |
178
|
|
|
} |
179
|
|
|
|
180
|
1 |
|
if ($this->isRealResult($osRaw->getPatch()) === true) { |
181
|
1 |
|
$os->getVersion()->setPatch($osRaw->getPatch()); |
182
|
|
|
} |
183
|
|
|
} |
184
|
4 |
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* |
188
|
|
|
* @param Model\UserAgent $device |
189
|
|
|
* @param UAResult\DeviceResult $deviceRaw |
190
|
|
|
*/ |
191
|
4 |
|
private function hydrateDevice(Model\Device $device, UAResult\DeviceResult $deviceRaw) |
192
|
|
|
{ |
193
|
4 |
|
if ($this->isRealResult($deviceRaw->getModel()) === true) { |
194
|
2 |
|
$device->setModel($deviceRaw->getModel()); |
195
|
|
|
} |
196
|
|
|
|
197
|
4 |
|
if ($this->isRealResult($deviceRaw->getConstructor()) === true) { |
198
|
1 |
|
$device->setBrand($deviceRaw->getConstructor()); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
// removed desktop type, since it's a default value and not really detected |
202
|
4 |
|
if ($this->isRealResult($deviceRaw->getType()) === true && $deviceRaw->getType() !== 'desktop') { |
203
|
2 |
|
$device->setType($deviceRaw->getType()); |
204
|
|
|
} |
205
|
4 |
|
} |
206
|
|
|
|
207
|
6 |
|
public function parse($userAgent, array $headers = []) |
208
|
|
|
{ |
209
|
6 |
|
$parser = $this->getParser(); |
210
|
|
|
|
211
|
|
|
/* @var $resultRaw \UAParser\Result\Result */ |
212
|
6 |
|
$resultRaw = $parser->parse($userAgent); |
213
|
|
|
|
214
|
|
|
/* |
215
|
|
|
* No result found? |
216
|
|
|
*/ |
217
|
6 |
|
if ($this->hasResult($resultRaw) !== true) { |
218
|
2 |
|
throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent); |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/* @var $emailRaw \UAParser\Result\EmailClientResult */ |
222
|
|
|
// currently not used...any idea to implement it? |
223
|
|
|
|
224
|
|
|
/* |
225
|
|
|
* Hydrate the model |
226
|
|
|
*/ |
227
|
4 |
|
$result = new Model\UserAgent(); |
228
|
4 |
|
$result->setProviderResultRaw($resultRaw); |
229
|
|
|
|
230
|
|
|
/* |
231
|
|
|
* Bot detection is currently not possible |
232
|
|
|
*/ |
233
|
|
|
|
234
|
|
|
/* |
235
|
|
|
* hydrate the result |
236
|
|
|
*/ |
237
|
4 |
|
$this->hydrateBrowser($result->getBrowser(), $resultRaw->getBrowser()); |
238
|
4 |
|
$this->hydrateRenderingEngine($result->getRenderingEngine(), $resultRaw->getRenderingEngine()); |
239
|
4 |
|
$this->hydrateOperatingSystem($result->getOperatingSystem(), $resultRaw->getOperatingSystem()); |
240
|
4 |
|
$this->hydrateDevice($result->getDevice(), $resultRaw->getDevice()); |
241
|
|
|
|
242
|
4 |
|
return $result; |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.