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