1
|
|
|
<?php |
2
|
|
|
namespace UserAgentParser\Provider; |
3
|
|
|
|
4
|
|
|
use UAParser\Parser; |
5
|
|
|
use UserAgentParser\Exception\NoResultFoundException; |
6
|
|
|
use UserAgentParser\Exception\PackageNotLoadedException; |
7
|
|
|
use UserAgentParser\Model; |
8
|
|
|
|
9
|
|
|
class UAParser extends AbstractProvider |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Name of the provider |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $name = 'UAParser'; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Homepage of the provider |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $homepage = 'https://github.com/ua-parser/uap-php'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Composer package name |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $packageName = 'ua-parser/uap-php'; |
31
|
|
|
|
32
|
|
|
protected $detectionCapabilities = [ |
33
|
|
|
|
34
|
|
|
'browser' => [ |
35
|
|
|
'name' => true, |
36
|
|
|
'version' => true, |
37
|
|
|
], |
38
|
|
|
|
39
|
|
|
'renderingEngine' => [ |
40
|
|
|
'name' => false, |
41
|
|
|
'version' => false, |
42
|
|
|
], |
43
|
|
|
|
44
|
|
|
'operatingSystem' => [ |
45
|
|
|
'name' => true, |
46
|
|
|
'version' => true, |
47
|
|
|
], |
48
|
|
|
|
49
|
|
|
'device' => [ |
50
|
|
|
'model' => true, |
51
|
|
|
'brand' => true, |
52
|
|
|
'type' => false, |
53
|
|
|
'isMobile' => false, |
54
|
|
|
'isTouch' => false, |
55
|
|
|
], |
56
|
|
|
|
57
|
|
|
'bot' => [ |
58
|
|
|
'isBot' => true, |
59
|
|
|
'name' => true, |
60
|
|
|
'type' => false, |
61
|
|
|
], |
62
|
|
|
]; |
63
|
|
|
|
64
|
|
|
protected $defaultValues = [ |
65
|
|
|
|
66
|
|
|
'general' => [ |
67
|
|
|
'/^Other$/i', |
68
|
|
|
], |
69
|
|
|
|
70
|
|
|
'device' => [ |
71
|
|
|
|
72
|
|
|
'brand' => [ |
73
|
|
|
'/^Generic/i', |
74
|
|
|
], |
75
|
|
|
|
76
|
|
|
'model' => [ |
77
|
|
|
'/^Smartphone$/i', |
78
|
13 |
|
'/^Feature Phone$/i', |
79
|
|
|
'/^iOS-Device$/i', |
80
|
13 |
|
'/^Tablet$/i', |
81
|
1 |
|
'/^Touch$/i', |
82
|
|
|
'/^Windows$/i', |
83
|
|
|
'/^Windows Phone$/i', |
84
|
12 |
|
], |
85
|
12 |
|
], |
86
|
|
|
|
87
|
|
|
'bot' => [ |
88
|
|
|
'name' => [ |
89
|
|
|
'/^Other$/i', |
90
|
|
|
'/^crawler$/i', |
91
|
6 |
|
'/^robot$/i', |
92
|
|
|
'/^crawl$/i', |
93
|
6 |
|
'/^Spider$/i', |
94
|
6 |
|
], |
95
|
|
|
], |
96
|
|
|
]; |
97
|
1 |
|
|
98
|
|
|
private $parser; |
99
|
1 |
|
|
100
|
|
|
/** |
101
|
|
|
* |
102
|
|
|
* @param Parser $parser |
103
|
|
|
* @throws PackageNotLoadedException |
104
|
|
|
*/ |
105
|
|
View Code Duplication |
public function __construct(Parser $parser = null) |
|
|
|
|
106
|
|
|
{ |
107
|
|
|
if ($parser === null && ! file_exists('vendor/' . $this->getPackageName() . '/composer.json')) { |
108
|
5 |
|
throw new PackageNotLoadedException('You need to install the package ' . $this->getPackageName() . ' to use this provider'); |
109
|
|
|
} |
110
|
5 |
|
|
111
|
2 |
|
$this->parser = $parser; |
112
|
|
|
} |
113
|
|
|
|
114
|
3 |
|
/** |
115
|
1 |
|
* |
116
|
|
|
* @return Parser |
117
|
|
|
*/ |
118
|
2 |
|
public function getParser() |
119
|
1 |
|
{ |
120
|
|
|
if ($this->parser !== null) { |
121
|
|
|
return $this->parser; |
122
|
1 |
|
} |
123
|
|
|
|
124
|
|
|
$this->parser = Parser::create(); |
125
|
3 |
|
|
126
|
|
|
return $this->parser; |
127
|
|
|
} |
128
|
3 |
|
|
129
|
3 |
|
/** |
130
|
3 |
|
* |
131
|
3 |
|
* @param \UAParser\Result\Client $resultRaw |
132
|
|
|
* |
133
|
|
|
* @return bool |
134
|
3 |
|
*/ |
135
|
|
|
private function hasResult(\UAParser\Result\Client $resultRaw) |
136
|
|
|
{ |
137
|
3 |
|
if ($this->isRealResult($resultRaw->ua->family)) { |
138
|
3 |
|
return true; |
139
|
3 |
|
} |
140
|
3 |
|
|
141
|
|
|
if ($this->isRealResult($resultRaw->os->family)) { |
142
|
|
|
return true; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
if ($this->isRealResult($resultRaw->device->model, 'device', 'model')) { |
146
|
|
|
return true; |
147
|
|
|
} |
148
|
|
|
|
149
|
4 |
|
return false; |
150
|
|
|
} |
151
|
4 |
|
|
152
|
1 |
|
/** |
153
|
|
|
* |
154
|
|
|
* @param \UAParser\Result\Client $resultRaw |
155
|
3 |
|
* |
156
|
|
|
* @return bool |
157
|
|
|
*/ |
158
|
|
|
private function isBot(\UAParser\Result\Client $resultRaw) |
159
|
|
|
{ |
160
|
|
|
if ($resultRaw->device->family === 'Spider') { |
161
|
|
|
return true; |
162
|
|
|
} |
163
|
1 |
|
|
164
|
|
|
return false; |
165
|
1 |
|
} |
166
|
|
|
|
167
|
1 |
|
/** |
168
|
1 |
|
* |
169
|
1 |
|
* @param Model\Bot $bot |
170
|
1 |
|
* @param \UAParser\Result\Client $resultRaw |
171
|
|
|
*/ |
172
|
|
|
private function hydrateBot(Model\Bot $bot, \UAParser\Result\Client $resultRaw) |
173
|
|
|
{ |
174
|
|
|
$bot->setIsBot(true); |
175
|
|
|
|
176
|
|
|
if ($this->isRealResult($resultRaw->ua->family, 'bot', 'name') === true) { |
177
|
3 |
|
$bot->setName($resultRaw->ua->family); |
178
|
|
|
} |
179
|
3 |
|
} |
180
|
1 |
|
|
181
|
1 |
|
/** |
182
|
|
|
* |
183
|
3 |
|
* @param Model\Browser $browser |
184
|
1 |
|
* @param \UAParser\Result\UserAgent $uaRaw |
185
|
1 |
|
*/ |
186
|
|
View Code Duplication |
private function hydrateBrowser(Model\Browser $browser, \UAParser\Result\UserAgent $uaRaw) |
|
|
|
|
187
|
3 |
|
{ |
188
|
1 |
|
if ($this->isRealResult($uaRaw->family) === true) { |
189
|
1 |
|
$browser->setName($uaRaw->family); |
190
|
|
|
} |
191
|
3 |
|
|
192
|
1 |
|
if ($this->isRealResult($uaRaw->major) === true) { |
193
|
1 |
|
$browser->getVersion()->setMajor($uaRaw->major); |
194
|
3 |
|
} |
195
|
|
|
|
196
|
|
|
if ($this->isRealResult($uaRaw->minor) === true) { |
197
|
|
|
$browser->getVersion()->setMinor($uaRaw->minor); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
if ($this->isRealResult($uaRaw->patch) === true) { |
201
|
3 |
|
$browser->getVersion()->setPatch($uaRaw->patch); |
202
|
|
|
} |
203
|
3 |
|
} |
204
|
1 |
|
|
205
|
1 |
|
/** |
206
|
|
|
* |
207
|
3 |
|
* @param Model\OperatingSystem $os |
208
|
1 |
|
* @param \UAParser\Result\OperatingSystem $osRaw |
209
|
1 |
|
*/ |
210
|
|
View Code Duplication |
private function hydrateOperatingSystem(Model\OperatingSystem $os, \UAParser\Result\OperatingSystem $osRaw) |
|
|
|
|
211
|
3 |
|
{ |
212
|
1 |
|
if ($this->isRealResult($osRaw->family) === true) { |
213
|
1 |
|
$os->setName($osRaw->family); |
214
|
|
|
} |
215
|
3 |
|
|
216
|
1 |
|
if ($this->isRealResult($osRaw->major) === true) { |
217
|
1 |
|
$os->getVersion()->setMajor($osRaw->major); |
218
|
3 |
|
} |
219
|
|
|
|
220
|
|
|
if ($this->isRealResult($osRaw->minor) === true) { |
221
|
|
|
$os->getVersion()->setMinor($osRaw->minor); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
if ($this->isRealResult($osRaw->patch) === true) { |
225
|
3 |
|
$os->getVersion()->setPatch($osRaw->patch); |
226
|
|
|
} |
227
|
3 |
|
} |
228
|
1 |
|
|
229
|
1 |
|
/** |
230
|
|
|
* |
231
|
3 |
|
* @param Model\UserAgent $device |
232
|
1 |
|
* @param \UAParser\Result\Device $deviceRaw |
233
|
1 |
|
*/ |
234
|
3 |
|
private function hydrateDevice(Model\Device $device, \UAParser\Result\Device $deviceRaw) |
235
|
|
|
{ |
236
|
5 |
|
if ($this->isRealResult($deviceRaw->model, 'device', 'model') === true) { |
237
|
|
|
$device->setModel($deviceRaw->model); |
238
|
5 |
|
} |
239
|
|
|
|
240
|
|
|
if ($this->isRealResult($deviceRaw->brand, 'device', 'brand') === true) { |
241
|
5 |
|
$device->setBrand($deviceRaw->brand); |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
public function parse($userAgent, array $headers = []) |
246
|
5 |
|
{ |
247
|
1 |
|
$parser = $this->getParser(); |
248
|
|
|
|
249
|
|
|
/* @var $resultRaw \UAParser\Result\Client */ |
250
|
|
|
$resultRaw = $parser->parse($userAgent); |
251
|
|
|
|
252
|
|
|
/* |
253
|
4 |
|
* No result found? |
254
|
4 |
|
*/ |
255
|
|
|
if ($this->hasResult($resultRaw) !== true) { |
256
|
|
|
throw new NoResultFoundException('No result found for user agent: ' . $userAgent); |
257
|
|
|
} |
258
|
|
|
|
259
|
4 |
|
/* |
260
|
1 |
|
* Hydrate the model |
261
|
|
|
*/ |
262
|
1 |
|
$result = new Model\UserAgent(); |
263
|
|
|
$result->setProviderResultRaw($resultRaw); |
264
|
|
|
|
265
|
|
|
/* |
266
|
|
|
* Bot detection |
267
|
|
|
*/ |
268
|
3 |
|
if ($this->isBot($resultRaw) === true) { |
269
|
|
|
$this->hydrateBot($result->getBot(), $resultRaw); |
270
|
3 |
|
|
271
|
3 |
|
return $result; |
272
|
|
|
} |
273
|
3 |
|
|
274
|
|
|
/* |
275
|
|
|
* hydrate the result |
276
|
|
|
*/ |
277
|
|
|
$this->hydrateBrowser($result->getBrowser(), $resultRaw->ua); |
278
|
|
|
// renderingEngine not available |
279
|
|
|
$this->hydrateOperatingSystem($result->getOperatingSystem(), $resultRaw->os); |
280
|
|
|
$this->hydrateDevice($result->getDevice(), $resultRaw->device); |
281
|
|
|
|
282
|
|
|
return $result; |
283
|
|
|
} |
284
|
|
|
} |
285
|
|
|
|
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.