1
|
|
|
<?php |
2
|
|
|
namespace UserAgentParser\Provider; |
3
|
|
|
|
4
|
|
|
use BrowscapPHP\Browscap; |
5
|
|
|
use stdClass; |
6
|
|
|
use UserAgentParser\Exception; |
7
|
|
|
use UserAgentParser\Model; |
8
|
|
|
|
9
|
|
|
class BrowscapPhp extends AbstractProvider |
10
|
|
|
{ |
11
|
|
|
protected $defaultValues = [ |
12
|
|
|
'DefaultProperties', |
13
|
|
|
'Default Browser', |
14
|
|
|
|
15
|
|
|
'unknown', |
16
|
|
|
]; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* |
20
|
|
|
* @var Browscap |
21
|
|
|
*/ |
22
|
|
|
private $parser; |
23
|
|
|
|
24
|
15 |
|
public function __construct(Browscap $parser) |
25
|
|
|
{ |
26
|
15 |
|
$this->setParser($parser); |
27
|
15 |
|
} |
28
|
|
|
|
29
|
1 |
|
public function getName() |
30
|
|
|
{ |
31
|
1 |
|
return 'BrowscapPhp'; |
32
|
|
|
} |
33
|
|
|
|
34
|
1 |
|
public function getComposerPackageName() |
35
|
|
|
{ |
36
|
1 |
|
return 'browscap/browscap-php'; |
37
|
|
|
} |
38
|
|
|
|
39
|
1 |
|
public function getVersion() |
40
|
|
|
{ |
41
|
1 |
|
return $this->getParser() |
42
|
1 |
|
->getCache() |
43
|
1 |
|
->getVersion(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* |
48
|
|
|
* @param Browscap $parser |
49
|
|
|
*/ |
50
|
15 |
|
public function setParser(Browscap $parser) |
51
|
|
|
{ |
52
|
15 |
|
$this->parser = $parser; |
53
|
15 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* |
57
|
|
|
* @return Browscap |
58
|
|
|
*/ |
59
|
13 |
|
public function getParser() |
60
|
|
|
{ |
61
|
13 |
|
return $this->parser; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* |
66
|
|
|
* @return array |
67
|
|
|
*/ |
68
|
1 |
|
private function getDeviceModelDefaultValues() |
69
|
|
|
{ |
70
|
|
|
return [ |
71
|
1 |
|
'general Desktop', |
72
|
1 |
|
'general Mobile Device', |
73
|
1 |
|
'general Mobile Phone', |
74
|
1 |
|
'general Tablet', |
75
|
1 |
|
]; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* |
80
|
|
|
* @param stdClass $resultRaw |
81
|
|
|
* @return boolean |
82
|
|
|
*/ |
83
|
5 |
|
private function isBot(stdClass $resultRaw) |
84
|
|
|
{ |
85
|
5 |
|
if (! isset($resultRaw->crawler) || $resultRaw->crawler !== true) { |
86
|
2 |
|
return false; |
87
|
|
|
} |
88
|
|
|
|
89
|
3 |
|
return true; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* |
94
|
|
|
* @param stdClass $resultRaw |
95
|
|
|
* |
96
|
|
|
* @return bool |
97
|
|
|
*/ |
98
|
11 |
|
private function hasResult(stdClass $resultRaw) |
99
|
|
|
{ |
100
|
11 |
|
if (! isset($resultRaw->browser)) { |
101
|
2 |
|
return false; |
102
|
|
|
} |
103
|
|
|
|
104
|
9 |
|
if ($this->isRealResult($resultRaw->browser) !== true) { |
105
|
4 |
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
5 |
|
return true; |
109
|
|
|
} |
110
|
|
|
|
111
|
11 |
|
public function parse($userAgent, array $headers = []) |
112
|
|
|
{ |
113
|
11 |
|
$parser = $this->getParser(); |
114
|
|
|
|
115
|
|
|
/* @var $resultRaw \stdClass */ |
116
|
11 |
|
$resultRaw = $parser->getBrowser($userAgent); |
117
|
|
|
|
118
|
|
|
/* |
119
|
|
|
* No result found? |
120
|
|
|
*/ |
121
|
11 |
|
if ($this->hasResult($resultRaw) !== true) { |
122
|
6 |
|
throw new Exception\NoResultFoundException('No result found for user agent: ' . $userAgent); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/* |
126
|
|
|
* Hydrate the model |
127
|
|
|
*/ |
128
|
5 |
|
$result = new Model\UserAgent(); |
129
|
5 |
|
$result->setProviderResultRaw($resultRaw); |
130
|
|
|
|
131
|
|
|
/* |
132
|
|
|
* Bot detection (does only work with full_php_browscap.ini) |
133
|
|
|
*/ |
134
|
5 |
|
if ($this->isBot($resultRaw) === true) { |
135
|
3 |
|
$bot = $result->getBot(); |
136
|
3 |
|
$bot->setIsBot(true); |
137
|
|
|
|
138
|
3 |
View Code Duplication |
if (isset($resultRaw->browser) && $this->isRealResult($resultRaw->browser) === true) { |
|
|
|
|
139
|
3 |
|
$bot->setName($resultRaw->browser); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
// @todo convert to a common set of types (over all vendors) |
143
|
3 |
|
if (isset($resultRaw->issyndicationreader) && $resultRaw->issyndicationreader === true) { |
144
|
1 |
|
$bot->setType('RSS'); |
145
|
3 |
|
} elseif (isset($resultRaw->browser_type) && $resultRaw->browser_type === 'Bot/Crawler') { |
146
|
1 |
|
$bot->setType('Crawler'); |
147
|
2 |
|
} elseif (isset($resultRaw->browser_type) && $this->isRealResult($resultRaw->browser_type) === true) { |
148
|
1 |
|
$bot->setType($resultRaw->browser_type); |
149
|
|
|
} |
150
|
|
|
|
151
|
3 |
|
return $result; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/* |
155
|
|
|
* browser |
156
|
|
|
*/ |
157
|
2 |
|
$browser = $result->getBrowser(); |
158
|
|
|
|
159
|
2 |
View Code Duplication |
if (isset($resultRaw->browser) && $this->isRealResult($resultRaw->browser) === true) { |
|
|
|
|
160
|
2 |
|
$browser->setName($resultRaw->browser); |
161
|
|
|
} |
162
|
|
|
|
163
|
2 |
View Code Duplication |
if (isset($resultRaw->version) && $this->isRealResult($resultRaw->version) === true) { |
|
|
|
|
164
|
2 |
|
$browser->getVersion()->setComplete($resultRaw->version); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/* |
168
|
|
|
* renderingEngine |
169
|
|
|
*/ |
170
|
2 |
|
$renderingEngine = $result->getRenderingEngine(); |
171
|
|
|
|
172
|
2 |
|
if (isset($resultRaw->renderingengine_name) && $this->isRealResult($resultRaw->renderingengine_name) === true) { |
173
|
1 |
|
$renderingEngine->setName($resultRaw->renderingengine_name); |
174
|
|
|
} |
175
|
|
|
|
176
|
2 |
View Code Duplication |
if (isset($resultRaw->renderingengine_version) && $this->isRealResult($resultRaw->renderingengine_version) === true) { |
|
|
|
|
177
|
1 |
|
$renderingEngine->getVersion()->setComplete($resultRaw->renderingengine_version); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/* |
181
|
|
|
* operatingSystem |
182
|
|
|
*/ |
183
|
2 |
|
$operatingSystem = $result->getOperatingSystem(); |
184
|
|
|
|
185
|
2 |
|
if (isset($resultRaw->platform) && $this->isRealResult($resultRaw->platform) === true) { |
186
|
1 |
|
$operatingSystem->setName($resultRaw->platform); |
187
|
|
|
} |
188
|
|
|
|
189
|
2 |
View Code Duplication |
if (isset($resultRaw->platform_version) && $this->isRealResult($resultRaw->platform_version) === true) { |
|
|
|
|
190
|
1 |
|
$operatingSystem->getVersion()->setComplete($resultRaw->platform_version); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/* |
194
|
|
|
* device |
195
|
|
|
*/ |
196
|
2 |
|
$device = $result->getDevice(); |
197
|
|
|
|
198
|
2 |
|
if (isset($resultRaw->device_name) && $this->isRealResult($resultRaw->device_name, $this->getDeviceModelDefaultValues()) === true) { |
199
|
1 |
|
$device->setModel($resultRaw->device_name); |
200
|
|
|
} |
201
|
|
|
|
202
|
2 |
|
if (isset($resultRaw->device_brand_name) && $this->isRealResult($resultRaw->device_brand_name) === true) { |
203
|
1 |
|
$device->setBrand($resultRaw->device_brand_name); |
204
|
|
|
} |
205
|
|
|
|
206
|
2 |
|
if (isset($resultRaw->device_type) && $this->isRealResult($resultRaw->device_type) === true) { |
207
|
|
|
// @todo convert to a common set of types (over all vendors) |
208
|
1 |
|
$device->setType($resultRaw->device_type); |
209
|
|
|
} |
210
|
|
|
|
211
|
2 |
|
if (isset($resultRaw->ismobiledevice) && $this->isRealResult($resultRaw->ismobiledevice) === true && $resultRaw->ismobiledevice === true) { |
212
|
1 |
|
$device->setIsMobile(true); |
213
|
|
|
} |
214
|
|
|
|
215
|
2 |
|
if (isset($resultRaw->device_pointing_method) && $resultRaw->device_pointing_method == 'touchscreen') { |
216
|
1 |
|
$device->setIsTouch(true); |
217
|
|
|
} |
218
|
|
|
|
219
|
2 |
|
return $result; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
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.