1
|
|
|
<?php |
2
|
|
|
namespace UserAgentParser\Provider; |
3
|
|
|
|
4
|
|
|
use UserAgent; |
5
|
|
|
use UserAgentParser\Exception\NoResultFoundException; |
6
|
|
|
use UserAgentParser\Exception\PackageNotLoadedException; |
7
|
|
|
use UserAgentParser\Model; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Abstraction for zsxsoft/php-useragent |
11
|
|
|
* |
12
|
|
|
* @author Martin Keckeis <[email protected]> |
13
|
|
|
* @license MIT |
14
|
|
|
* @see https://github.com/zsxsoft/php-useragent |
15
|
|
|
*/ |
16
|
|
|
class Zsxsoft extends AbstractProvider |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* Name of the provider |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $name = 'Zsxsoft'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Homepage of the provider |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $homepage = 'https://github.com/zsxsoft/php-useragent'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Composer package name |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $packageName = 'zsxsoft/php-useragent'; |
38
|
|
|
|
39
|
|
|
protected $detectionCapabilities = [ |
40
|
|
|
|
41
|
|
|
'browser' => [ |
42
|
|
|
'name' => true, |
43
|
|
|
'version' => true, |
44
|
|
|
], |
45
|
|
|
|
46
|
|
|
'renderingEngine' => [ |
47
|
|
|
'name' => false, |
48
|
|
|
'version' => false, |
49
|
|
|
], |
50
|
|
|
|
51
|
|
|
'operatingSystem' => [ |
52
|
|
|
'name' => true, |
53
|
|
|
'version' => true, |
54
|
|
|
], |
55
|
|
|
|
56
|
|
|
'device' => [ |
57
|
|
|
'model' => true, |
58
|
|
|
'brand' => true, |
59
|
|
|
'type' => false, |
60
|
|
|
'isMobile' => false, |
61
|
|
|
'isTouch' => false, |
62
|
|
|
], |
63
|
|
|
|
64
|
|
|
'bot' => [ |
65
|
|
|
'isBot' => false, |
66
|
|
|
'name' => false, |
67
|
|
|
'type' => false, |
68
|
|
|
], |
69
|
|
|
]; |
70
|
|
|
|
71
|
|
|
protected $defaultValues = [ |
72
|
|
|
|
73
|
|
|
'general' => [ |
74
|
|
|
'/^Unknown$/i', |
75
|
|
|
], |
76
|
|
|
|
77
|
|
|
'browser' => [ |
78
|
|
|
'name' => [ |
79
|
|
|
'/^Mozilla Compatible$/i', |
80
|
|
|
], |
81
|
|
|
], |
82
|
|
|
|
83
|
|
|
'device' => [ |
84
|
|
|
'model' => [ |
85
|
|
|
'/^Browser$/i', |
86
|
|
|
'/^Android$/i', |
87
|
|
|
], |
88
|
|
|
], |
89
|
|
|
]; |
90
|
|
|
|
91
|
|
|
private $parser; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* |
95
|
|
|
* @param UserAgent $parser |
96
|
|
|
* @throws PackageNotLoadedException |
97
|
|
|
*/ |
98
|
16 |
|
public function __construct(UserAgent $parser = null) |
99
|
|
|
{ |
100
|
16 |
|
if ($parser === null && ! file_exists('vendor/' . $this->getPackageName() . '/composer.json')) { |
101
|
1 |
|
throw new PackageNotLoadedException('You need to install the package ' . $this->getPackageName() . ' to use this provider'); |
102
|
|
|
} |
103
|
|
|
|
104
|
15 |
|
$this->parser = $parser; |
105
|
15 |
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* |
109
|
|
|
* @return UserAgent |
110
|
|
|
*/ |
111
|
8 |
|
public function getParser() |
112
|
|
|
{ |
113
|
8 |
|
if ($this->parser !== null) { |
114
|
8 |
|
return $this->parser; |
115
|
|
|
} |
116
|
|
|
|
117
|
1 |
|
$this->parser = new UserAgent(); |
118
|
|
|
|
119
|
1 |
|
return $this->parser; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* |
124
|
|
|
* @param array $browser |
125
|
|
|
* @param array $os |
126
|
|
|
* @param array $device |
127
|
|
|
* |
128
|
|
|
* @return bool |
129
|
|
|
*/ |
130
|
7 |
|
private function hasResult(array $browser, array $os, array $device) |
131
|
|
|
{ |
132
|
7 |
|
if (isset($browser['name']) && $this->isRealResult($browser['name'], 'browser', 'name')) { |
133
|
1 |
|
return true; |
134
|
|
|
} |
135
|
|
|
|
136
|
6 |
|
if (isset($os['name']) && $this->isRealResult($os['name'])) { |
137
|
1 |
|
return true; |
138
|
|
|
} |
139
|
|
|
|
140
|
5 |
|
if (isset($device['brand']) && $this->isRealResult($device['brand'])) { |
141
|
1 |
|
return true; |
142
|
|
|
} |
143
|
|
|
|
144
|
4 |
|
if (isset($device['model']) && $this->isRealResult($device['model'], 'device', 'model')) { |
145
|
1 |
|
return true; |
146
|
|
|
} |
147
|
|
|
|
148
|
3 |
|
return false; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* |
153
|
|
|
* @param Model\Browser $browser |
154
|
|
|
* @param array $browserRaw |
155
|
|
|
*/ |
156
|
4 |
|
private function hydrateBrowser(Model\Browser $browser, array $browserRaw) |
157
|
|
|
{ |
158
|
4 |
|
if (isset($browserRaw['name'])) { |
159
|
1 |
|
$browser->setName($this->getRealResult($browserRaw['name'], 'browser', 'name')); |
160
|
|
|
} |
161
|
|
|
|
162
|
4 |
|
if (isset($browserRaw['version'])) { |
163
|
1 |
|
$browser->getVersion()->setComplete($this->getRealResult($browserRaw['version'])); |
164
|
|
|
} |
165
|
4 |
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* |
169
|
|
|
* @param Model\OperatingSystem $os |
170
|
|
|
* @param array $osRaw |
171
|
|
|
*/ |
172
|
4 |
|
private function hydrateOperatingSystem(Model\OperatingSystem $os, array $osRaw) |
173
|
|
|
{ |
174
|
4 |
|
if (isset($osRaw['name'])) { |
175
|
1 |
|
$os->setName($this->getRealResult($osRaw['name'])); |
176
|
|
|
} |
177
|
|
|
|
178
|
4 |
|
if (isset($osRaw['version'])) { |
179
|
1 |
|
$os->getVersion()->setComplete($this->getRealResult($osRaw['version'])); |
180
|
|
|
} |
181
|
4 |
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* |
185
|
|
|
* @param Model\Device $device |
186
|
|
|
* @param array $deviceRaw |
187
|
|
|
*/ |
188
|
4 |
|
private function hydrateDevice(Model\Device $device, array $deviceRaw) |
189
|
|
|
{ |
190
|
4 |
|
if (isset($deviceRaw['model'])) { |
191
|
2 |
|
$device->setModel($this->getRealResult($deviceRaw['model'], 'device', 'model')); |
192
|
|
|
} |
193
|
|
|
|
194
|
4 |
|
if (isset($deviceRaw['brand'])) { |
195
|
1 |
|
$device->setBrand($this->getRealResult($deviceRaw['brand'])); |
196
|
|
|
} |
197
|
4 |
|
} |
198
|
|
|
|
199
|
7 |
|
public function parse($userAgent, array $headers = []) |
200
|
|
|
{ |
201
|
7 |
|
$parser = $this->getParser(); |
202
|
7 |
|
$parser->analyze($userAgent); |
203
|
|
|
|
204
|
7 |
|
$browser = $parser->browser; |
205
|
7 |
|
$os = $parser->os; |
206
|
7 |
|
$device = $parser->device; |
207
|
7 |
|
$platform = $parser->platform; |
208
|
|
|
|
209
|
|
|
/* |
210
|
|
|
* No result found? |
211
|
|
|
*/ |
212
|
7 |
|
if ($this->hasResult($browser, $os, $device) !== true) { |
213
|
3 |
|
throw new NoResultFoundException('No result found for user agent: ' . $userAgent); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/* |
217
|
|
|
* Hydrate the model |
218
|
|
|
*/ |
219
|
4 |
|
$result = new Model\UserAgent(); |
220
|
4 |
|
$result->setProviderResultRaw([ |
221
|
4 |
|
'browser' => $browser, |
222
|
4 |
|
'os' => $os, |
223
|
4 |
|
'device' => $device, |
224
|
4 |
|
'platform' => $platform, |
225
|
|
|
]); |
226
|
|
|
|
227
|
|
|
/* |
228
|
|
|
* hydrate the result |
229
|
|
|
*/ |
230
|
4 |
|
$this->hydrateBrowser($result->getBrowser(), $browser); |
231
|
4 |
|
$this->hydrateOperatingSystem($result->getOperatingSystem(), $os); |
232
|
4 |
|
$this->hydrateDevice($result->getDevice(), $device); |
233
|
|
|
|
234
|
4 |
|
return $result; |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|