|
1
|
|
|
<?php |
|
2
|
|
|
namespace UserAgentParser\Provider; |
|
3
|
|
|
|
|
4
|
|
|
use UserAgentParser\Exception\NoResultFoundException; |
|
5
|
|
|
use UserAgentParser\Exception\PackageNotLoadedException; |
|
6
|
|
|
use UserAgentParser\Model; |
|
7
|
|
|
use WhichBrowser\Parser as WhichBrowserParser; |
|
8
|
|
|
|
|
9
|
|
|
class WhichBrowser extends AbstractProvider |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Name of the provider |
|
13
|
|
|
* |
|
14
|
|
|
* @var string |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $name = 'WhichBrowser'; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Homepage of the provider |
|
20
|
|
|
* |
|
21
|
|
|
* @var string |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $homepage = 'https://github.com/WhichBrowser/Parser'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Composer package name |
|
27
|
|
|
* |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $packageName = 'whichbrowser/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' => true, |
|
54
|
|
|
'isTouch' => false, |
|
55
|
|
|
], |
|
56
|
|
|
|
|
57
|
|
|
'bot' => [ |
|
58
|
|
|
'isBot' => true, |
|
59
|
|
|
'name' => true, |
|
60
|
|
|
'type' => false, |
|
61
|
|
|
], |
|
62
|
|
|
]; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Used for unitTests mocking |
|
66
|
|
|
* |
|
67
|
|
|
* @var WhichBrowserParser |
|
68
|
|
|
*/ |
|
69
|
|
|
private $parser; |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* |
|
73
|
|
|
* @throws PackageNotLoadedException |
|
74
|
|
|
*/ |
|
75
|
15 |
|
public function __construct() |
|
76
|
|
|
{ |
|
77
|
15 |
|
if (! file_exists('vendor/' . $this->getPackageName() . '/composer.json')) { |
|
78
|
1 |
|
throw new PackageNotLoadedException('You need to install the package ' . $this->getPackageName() . ' to use this provider'); |
|
79
|
|
|
} |
|
80
|
14 |
|
} |
|
81
|
|
|
|
|
82
|
1 |
|
/** |
|
83
|
|
|
* |
|
84
|
1 |
|
* @param array $headers |
|
85
|
|
|
* @return WhichBrowserParser |
|
86
|
|
|
*/ |
|
87
|
1 |
|
public function getParser(array $headers) |
|
88
|
|
|
{ |
|
89
|
1 |
|
if ($this->parser !== null) { |
|
90
|
|
|
return $this->parser; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return new WhichBrowserParser($headers); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
8 |
|
* |
|
98
|
|
|
* @param Model\Bot $bot |
|
99
|
8 |
|
* @param \WhichBrowser\Model\Browser $browserRaw |
|
100
|
7 |
|
*/ |
|
101
|
|
|
private function hydrateBot(Model\Bot $bot, \WhichBrowser\Model\Browser $browserRaw) |
|
102
|
|
|
{ |
|
103
|
1 |
|
$bot->setIsBot(true); |
|
104
|
|
|
|
|
105
|
|
|
if ($this->isRealResult($browserRaw->getName()) === true) { |
|
106
|
|
|
$bot->setName($browserRaw->getName()); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
1 |
|
* |
|
112
|
|
|
* @param Model\Browser $browser |
|
113
|
1 |
|
* @param \WhichBrowser\Model\Browser $browserRaw |
|
114
|
|
|
*/ |
|
115
|
1 |
|
private function hydrateBrowser(Model\Browser $browser, \WhichBrowser\Model\Browser $browserRaw) |
|
116
|
1 |
|
{ |
|
117
|
1 |
|
if ($this->isRealResult($browserRaw->getName()) === true) { |
|
118
|
1 |
|
$browser->setName($browserRaw->getName()); |
|
119
|
|
|
|
|
120
|
|
|
if ($this->isRealResult($browserRaw->getVersion()) === true) { |
|
121
|
|
|
$browser->getVersion()->setComplete($browserRaw->getVersion()); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return; |
|
125
|
5 |
|
} |
|
126
|
|
|
|
|
127
|
5 |
|
if (isset($browserRaw->using) && $browserRaw->using instanceof \WhichBrowser\Model\Using) { |
|
|
|
|
|
|
128
|
1 |
|
/* @var $usingRaw \WhichBrowser\Model\Using */ |
|
129
|
|
|
$usingRaw = $browserRaw->using; |
|
130
|
1 |
|
|
|
131
|
1 |
|
if ($this->isRealResult($usingRaw->getName()) === true) { |
|
132
|
1 |
|
$browser->setName($usingRaw->getName()); |
|
133
|
|
|
|
|
134
|
1 |
|
if ($this->isRealResult($usingRaw->getVersion()) === true) { |
|
135
|
|
|
$browser->getVersion()->setComplete($usingRaw->getVersion()); |
|
136
|
|
|
} |
|
137
|
4 |
|
} |
|
138
|
|
|
} |
|
139
|
1 |
|
} |
|
140
|
|
|
|
|
141
|
1 |
|
/** |
|
142
|
1 |
|
* |
|
143
|
|
|
* @param Model\RenderingEngine $engine |
|
144
|
1 |
|
* @param \WhichBrowser\Model\Engine $engineRaw |
|
145
|
1 |
|
*/ |
|
146
|
1 |
|
private function hydrateRenderingEngine(Model\RenderingEngine $engine, \WhichBrowser\Model\Engine $engineRaw) |
|
147
|
1 |
|
{ |
|
148
|
1 |
|
if ($this->isRealResult($engineRaw->getName()) === true) { |
|
149
|
4 |
|
$engine->setName($engineRaw->getName()); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
if ($this->isRealResult($engineRaw->getVersion()) === true) { |
|
153
|
|
|
$engine->getVersion()->setComplete($engineRaw->getVersion()); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
5 |
|
|
|
157
|
|
|
/** |
|
158
|
5 |
|
* |
|
159
|
1 |
|
* @param Model\OperatingSystem $os |
|
160
|
1 |
|
* @param \WhichBrowser\Model\Os $osRaw |
|
161
|
|
|
*/ |
|
162
|
5 |
|
private function hydrateOperatingSystem(Model\OperatingSystem $os, \WhichBrowser\Model\Os $osRaw) |
|
163
|
1 |
|
{ |
|
164
|
1 |
|
if ($this->isRealResult($osRaw->getName()) === true) { |
|
165
|
5 |
|
$os->setName($osRaw->getName()); |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
if ($this->isRealResult($osRaw->getVersion()) === true) { |
|
169
|
|
|
$os->getVersion()->setComplete($osRaw->getVersion()); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
5 |
|
|
|
173
|
|
|
/** |
|
174
|
5 |
|
* |
|
175
|
1 |
|
* @param Model\Device $device |
|
176
|
1 |
|
* @param \WhichBrowser\Model\Device $deviceRaw |
|
177
|
|
|
* @param WhichBrowserParser $parser |
|
178
|
5 |
|
*/ |
|
179
|
1 |
|
private function hydrateDevice(Model\Device $device, \WhichBrowser\Model\Device $deviceRaw, WhichBrowserParser $parser) |
|
180
|
1 |
|
{ |
|
181
|
5 |
|
if ($this->isRealResult($deviceRaw->getModel()) === true) { |
|
182
|
|
|
$device->setModel($deviceRaw->getModel()); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
if ($this->isRealResult($deviceRaw->getManufacturer()) === true) { |
|
186
|
|
|
$device->setBrand($deviceRaw->getManufacturer()); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
5 |
|
if ($this->isRealResult($parser->getType()) === true) { |
|
190
|
|
|
$device->setType($parser->getType()); |
|
191
|
5 |
|
} |
|
192
|
1 |
|
|
|
193
|
1 |
|
if ($parser->isMobile() === true) { |
|
194
|
|
|
$device->setIsMobile(true); |
|
195
|
5 |
|
} |
|
196
|
1 |
|
} |
|
197
|
1 |
|
|
|
198
|
|
|
public function parse($userAgent, array $headers = []) |
|
199
|
5 |
|
{ |
|
200
|
1 |
|
$headers['User-Agent'] = $userAgent; |
|
201
|
1 |
|
|
|
202
|
|
|
$parser = $this->getParser($headers); |
|
203
|
5 |
|
|
|
204
|
1 |
|
/* |
|
205
|
1 |
|
* No result found? |
|
206
|
5 |
|
*/ |
|
207
|
|
|
if ($parser->isDetected() !== true) { |
|
208
|
7 |
|
throw new NoResultFoundException('No result found for user agent: ' . $userAgent); |
|
209
|
|
|
} |
|
210
|
7 |
|
|
|
211
|
|
|
/* |
|
212
|
7 |
|
* Hydrate the model |
|
213
|
|
|
*/ |
|
214
|
|
|
$result = new Model\UserAgent(); |
|
215
|
|
|
$result->setProviderResultRaw($parser->toArray()); |
|
216
|
|
|
|
|
217
|
7 |
|
/* |
|
218
|
1 |
|
* Bot detection |
|
219
|
|
|
*/ |
|
220
|
|
|
if ($parser->getType() === 'bot') { |
|
221
|
|
|
$this->hydrateBot($result->getBot(), $parser->browser); |
|
222
|
|
|
|
|
223
|
|
|
return $result; |
|
224
|
6 |
|
} |
|
225
|
6 |
|
|
|
226
|
|
|
/* |
|
227
|
|
|
* hydrate the result |
|
228
|
|
|
*/ |
|
229
|
|
|
$this->hydrateBrowser($result->getBrowser(), $parser->browser); |
|
230
|
6 |
|
$this->hydrateRenderingEngine($result->getRenderingEngine(), $parser->engine); |
|
231
|
1 |
|
$this->hydrateOperatingSystem($result->getOperatingSystem(), $parser->os); |
|
232
|
|
|
$this->hydrateDevice($result->getDevice(), $parser->device, $parser); |
|
233
|
1 |
|
|
|
234
|
|
|
return $result; |
|
235
|
|
|
} |
|
236
|
|
|
} |
|
237
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.jsonfile (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.jsonto be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
requireorrequire-devsection?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceofchecks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.