1
|
|
|
<?php |
2
|
|
|
namespace UserAgentParser\Provider; |
3
|
|
|
|
4
|
|
|
use BrowscapPHP\Browscap; |
5
|
|
|
use stdClass; |
6
|
|
|
use UserAgentParser\Exception\NoResultFoundException; |
7
|
|
|
use UserAgentParser\Model; |
8
|
|
|
|
9
|
|
|
class BrowscapPhp extends AbstractProvider |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Name of the provider |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $name = 'BrowscapPhp'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Homepage of the provider |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $homepage = 'https://github.com/browscap/browscap-php'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Composer package name |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $packageName = 'browscap/browscap-php'; |
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' => true, |
54
|
|
|
'isTouch' => true, |
55
|
|
|
], |
56
|
|
|
|
57
|
|
|
'bot' => [ |
58
|
|
|
'isBot' => true, |
59
|
|
|
'name' => true, |
60
|
|
|
'type' => true, |
61
|
|
|
], |
62
|
|
|
]; |
63
|
|
|
|
64
|
|
|
protected $defaultValues = [ |
65
|
|
|
|
66
|
|
|
'general' => [ |
67
|
|
|
'/^unknown$/i', |
68
|
|
|
], |
69
|
|
|
|
70
|
|
|
'browser' => [ |
71
|
|
|
'name' => [ |
72
|
|
|
'/^Default Browser$/i', |
73
|
|
|
], |
74
|
|
|
], |
75
|
|
|
|
76
|
|
|
'device' => [ |
77
|
|
|
'model' => [ |
78
|
|
|
'/^general/i', |
79
|
|
|
'/desktop$/i', |
80
|
|
|
], |
81
|
|
|
], |
82
|
|
|
|
83
|
|
|
'bot' => [ |
84
|
|
|
'name' => [ |
85
|
|
|
'/^General Crawlers/i', |
86
|
|
|
], |
87
|
|
|
], |
88
|
|
|
|
89
|
|
|
]; |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* |
93
|
|
|
* @var Browscap |
94
|
|
|
*/ |
95
|
|
|
private $parser; |
96
|
|
|
|
97
|
26 |
|
public function __construct(Browscap $parser) |
98
|
|
|
{ |
99
|
26 |
|
$this->parser = $parser; |
100
|
26 |
|
} |
101
|
|
|
|
102
|
1 |
|
public function getVersion() |
103
|
|
|
{ |
104
|
1 |
|
return $this->getParser() |
105
|
1 |
|
->getCache() |
106
|
1 |
|
->getVersion(); |
107
|
|
|
} |
108
|
|
|
|
109
|
1 |
|
public function getUpdateDate() |
110
|
|
|
{ |
111
|
1 |
|
return; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* |
116
|
|
|
* @return Browscap |
117
|
|
|
*/ |
118
|
14 |
|
public function getParser() |
119
|
|
|
{ |
120
|
14 |
|
return $this->parser; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* |
125
|
|
|
* @param stdClass $resultRaw |
126
|
|
|
* |
127
|
|
|
* @return bool |
128
|
|
|
*/ |
129
|
12 |
|
private function hasResult(stdClass $resultRaw) |
130
|
|
|
{ |
131
|
12 |
|
if (! isset($resultRaw->browser) || $this->isRealResult($resultRaw->browser, 'browser', 'name') !== true) { |
132
|
4 |
|
return false; |
133
|
|
|
} |
134
|
|
|
|
135
|
8 |
|
return true; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* |
140
|
|
|
* @param stdClass $resultRaw |
141
|
|
|
* @return boolean |
142
|
|
|
*/ |
143
|
8 |
|
private function isBot(stdClass $resultRaw) |
144
|
|
|
{ |
145
|
8 |
|
if (! isset($resultRaw->crawler) || $resultRaw->crawler !== true) { |
146
|
4 |
|
return false; |
147
|
|
|
} |
148
|
|
|
|
149
|
4 |
|
return true; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* |
154
|
|
|
* @param Model\Bot $bot |
155
|
|
|
* @param stdClass $resultRaw |
156
|
|
|
*/ |
157
|
4 |
|
private function hydrateBot(Model\Bot $bot, stdClass $resultRaw) |
158
|
|
|
{ |
159
|
4 |
|
$bot->setIsBot(true); |
160
|
|
|
|
161
|
4 |
|
if (isset($resultRaw->browser) && $this->isRealResult($resultRaw->browser, 'bot', 'name') === true) { |
162
|
3 |
|
$bot->setName($resultRaw->browser); |
163
|
3 |
|
} |
164
|
|
|
|
165
|
4 |
|
if (isset($resultRaw->issyndicationreader) && $resultRaw->issyndicationreader === true) { |
166
|
1 |
|
$bot->setType('RSS'); |
167
|
4 |
|
} elseif (isset($resultRaw->browser_type) && $this->isRealResult($resultRaw->browser_type) === true) { |
168
|
2 |
|
$bot->setType($resultRaw->browser_type); |
169
|
2 |
|
} |
170
|
4 |
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* |
174
|
|
|
* @param Model\Browser $browser |
175
|
|
|
* @param stdClass $resultRaw |
176
|
|
|
*/ |
177
|
4 |
View Code Duplication |
private function hydrateBrowser(Model\Browser $browser, stdClass $resultRaw) |
|
|
|
|
178
|
|
|
{ |
179
|
4 |
|
if (isset($resultRaw->browser) && $this->isRealResult($resultRaw->browser, 'browser', 'name') === true) { |
180
|
4 |
|
$browser->setName($resultRaw->browser); |
181
|
4 |
|
} |
182
|
|
|
|
183
|
4 |
|
if (isset($resultRaw->version) && $this->isRealResult($resultRaw->version) === true) { |
184
|
2 |
|
$browser->getVersion()->setComplete($resultRaw->version); |
185
|
2 |
|
} |
186
|
4 |
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* |
190
|
|
|
* @param Model\RenderingEngine $engine |
191
|
|
|
* @param stdClass $resultRaw |
192
|
|
|
*/ |
193
|
4 |
|
private function hydrateRenderingEngine(Model\RenderingEngine $engine, stdClass $resultRaw) |
194
|
|
|
{ |
195
|
4 |
View Code Duplication |
if (isset($resultRaw->renderingengine_name) && $this->isRealResult($resultRaw->renderingengine_name) === true) { |
|
|
|
|
196
|
1 |
|
$engine->setName($resultRaw->renderingengine_name); |
197
|
1 |
|
} |
198
|
|
|
|
199
|
4 |
|
if (isset($resultRaw->renderingengine_version) && $this->isRealResult($resultRaw->renderingengine_version) === true) { |
200
|
1 |
|
$engine->getVersion()->setComplete($resultRaw->renderingengine_version); |
201
|
1 |
|
} |
202
|
4 |
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* |
206
|
|
|
* @param Model\OperatingSystem $os |
207
|
|
|
* @param stdClass $resultRaw |
208
|
|
|
*/ |
209
|
4 |
|
private function hydrateOperatingSystem(Model\OperatingSystem $os, stdClass $resultRaw) |
210
|
|
|
{ |
211
|
4 |
|
if (isset($resultRaw->platform) && $this->isRealResult($resultRaw->platform) === true) { |
212
|
1 |
|
$os->setName($resultRaw->platform); |
213
|
1 |
|
} |
214
|
|
|
|
215
|
4 |
|
if (isset($resultRaw->platform_version) && $this->isRealResult($resultRaw->platform_version) === true) { |
216
|
1 |
|
$os->getVersion()->setComplete($resultRaw->platform_version); |
217
|
1 |
|
} |
218
|
4 |
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* |
222
|
|
|
* @param Model\UserAgent $device |
223
|
|
|
* @param stdClass $resultRaw |
224
|
|
|
*/ |
225
|
4 |
|
private function hydrateDevice(Model\Device $device, stdClass $resultRaw) |
226
|
|
|
{ |
227
|
4 |
|
if (isset($resultRaw->device_name) && $this->isRealResult($resultRaw->device_name, 'device', 'model') === true) { |
228
|
1 |
|
$device->setModel($resultRaw->device_name); |
229
|
1 |
|
} |
230
|
|
|
|
231
|
4 |
View Code Duplication |
if (isset($resultRaw->device_brand_name) && $this->isRealResult($resultRaw->device_brand_name) === true) { |
|
|
|
|
232
|
1 |
|
$device->setBrand($resultRaw->device_brand_name); |
233
|
1 |
|
} |
234
|
|
|
|
235
|
4 |
View Code Duplication |
if (isset($resultRaw->device_type) && $this->isRealResult($resultRaw->device_type) === true) { |
|
|
|
|
236
|
|
|
// @todo convert to a common set of types (over all vendors) |
237
|
1 |
|
$device->setType($resultRaw->device_type); |
238
|
1 |
|
} |
239
|
|
|
|
240
|
4 |
|
if (isset($resultRaw->ismobiledevice) && $this->isRealResult($resultRaw->ismobiledevice) === true && $resultRaw->ismobiledevice === true) { |
241
|
1 |
|
$device->setIsMobile(true); |
242
|
1 |
|
} |
243
|
|
|
|
244
|
4 |
|
if (isset($resultRaw->device_pointing_method) && $resultRaw->device_pointing_method == 'touchscreen') { |
245
|
1 |
|
$device->setIsTouch(true); |
246
|
1 |
|
} |
247
|
4 |
|
} |
248
|
|
|
|
249
|
12 |
|
public function parse($userAgent, array $headers = []) |
250
|
|
|
{ |
251
|
12 |
|
$parser = $this->getParser(); |
252
|
|
|
|
253
|
|
|
/* @var $resultRaw \stdClass */ |
254
|
12 |
|
$resultRaw = $parser->getBrowser($userAgent); |
255
|
|
|
|
256
|
|
|
/* |
257
|
|
|
* No result found? |
258
|
|
|
*/ |
259
|
12 |
|
if ($this->hasResult($resultRaw) !== true) { |
260
|
4 |
|
throw new NoResultFoundException('No result found for user agent: ' . $userAgent); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/* |
264
|
|
|
* Hydrate the model |
265
|
|
|
*/ |
266
|
8 |
|
$result = new Model\UserAgent(); |
267
|
8 |
|
$result->setProviderResultRaw($resultRaw); |
268
|
|
|
|
269
|
|
|
/* |
270
|
|
|
* Bot detection (does only work with full_php_browscap.ini) |
271
|
|
|
*/ |
272
|
8 |
|
if ($this->isBot($resultRaw) === true) { |
273
|
4 |
|
$this->hydrateBot($result->getBot(), $resultRaw); |
274
|
|
|
|
275
|
4 |
|
return $result; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/* |
279
|
|
|
* hydrate the result |
280
|
|
|
*/ |
281
|
4 |
|
$this->hydrateBrowser($result->getBrowser(), $resultRaw); |
282
|
4 |
|
$this->hydrateRenderingEngine($result->getRenderingEngine(), $resultRaw); |
283
|
4 |
|
$this->hydrateOperatingSystem($result->getOperatingSystem(), $resultRaw); |
284
|
4 |
|
$this->hydrateDevice($result->getDevice(), $resultRaw); |
285
|
|
|
|
286
|
4 |
|
return $result; |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
|
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.