1
|
|
|
<?php |
2
|
|
|
namespace UserAgentParser\Provider; |
3
|
|
|
|
4
|
|
|
use UserAgentParser\Exception\NoResultFoundException; |
5
|
|
|
use UserAgentParser\Exception\PackageNotLoadedException; |
6
|
|
|
use UserAgentParser\Model; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Abstraction for donatj/PhpUserAgent |
10
|
|
|
* |
11
|
|
|
* @author Martin Keckeis <[email protected]> |
12
|
|
|
* @license MIT |
13
|
|
|
* @see https://github.com/donatj/PhpUserAgent |
14
|
|
|
*/ |
15
|
|
|
class DonatjUAParser extends AbstractProvider |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Name of the provider |
19
|
|
|
* |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $name = 'DonatjUAParser'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Homepage of the provider |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $homepage = 'https://github.com/donatj/PhpUserAgent'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Composer package name |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $packageName = 'donatj/phpuseragentparser'; |
37
|
|
|
|
38
|
|
|
protected $detectionCapabilities = [ |
39
|
|
|
|
40
|
|
|
'browser' => [ |
41
|
|
|
'name' => true, |
42
|
|
|
'version' => true, |
43
|
|
|
], |
44
|
|
|
|
45
|
|
|
'renderingEngine' => [ |
46
|
|
|
'name' => false, |
47
|
|
|
'version' => false, |
48
|
|
|
], |
49
|
|
|
|
50
|
|
|
'operatingSystem' => [ |
51
|
|
|
'name' => false, |
52
|
|
|
'version' => false, |
53
|
|
|
], |
54
|
|
|
|
55
|
|
|
'device' => [ |
56
|
|
|
'model' => false, |
57
|
|
|
'brand' => false, |
58
|
|
|
'type' => false, |
59
|
|
|
'isMobile' => false, |
60
|
|
|
'isTouch' => false, |
61
|
|
|
], |
62
|
|
|
|
63
|
|
|
'bot' => [ |
64
|
|
|
'isBot' => false, |
65
|
|
|
'name' => false, |
66
|
|
|
'type' => false, |
67
|
|
|
], |
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
private $functionName = '\parse_user_agent'; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* |
74
|
|
|
* @throws PackageNotLoadedException |
75
|
|
|
*/ |
76
|
10 |
|
public function __construct() |
77
|
|
|
{ |
78
|
10 |
|
$this->checkIfInstalled(); |
79
|
10 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* |
83
|
|
|
* @param array $resultRaw |
84
|
|
|
* |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
3 |
|
private function hasResult(array $resultRaw) |
88
|
|
|
{ |
89
|
3 |
|
if ($this->isRealResult($resultRaw['browser'])) { |
90
|
2 |
|
return true; |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
return false; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* |
98
|
|
|
* @param Model\Browser $browser |
99
|
|
|
* @param array $resultRaw |
100
|
|
|
*/ |
101
|
2 |
|
private function hydrateBrowser(Model\Browser $browser, array $resultRaw) |
102
|
|
|
{ |
103
|
2 |
|
$browser->setName($this->getRealResult($resultRaw['browser'])); |
104
|
2 |
|
$browser->getVersion()->setComplete($this->getRealResult($resultRaw['version'])); |
105
|
2 |
|
} |
106
|
|
|
|
107
|
3 |
|
public function parse($userAgent, array $headers = []) |
108
|
|
|
{ |
109
|
3 |
|
$functionName = $this->functionName; |
110
|
|
|
|
111
|
3 |
|
$resultRaw = $functionName($userAgent); |
112
|
|
|
|
113
|
3 |
|
if ($this->hasResult($resultRaw) !== true) { |
114
|
1 |
|
throw new NoResultFoundException('No result found for user agent: ' . $userAgent); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/* |
118
|
|
|
* Hydrate the model |
119
|
|
|
*/ |
120
|
2 |
|
$result = new Model\UserAgent($this->getName(), $this->getVersion()); |
121
|
2 |
|
$result->setProviderResultRaw($resultRaw); |
122
|
|
|
|
123
|
|
|
/* |
124
|
|
|
* Bot detection - is currently not possible! |
125
|
|
|
*/ |
126
|
|
|
|
127
|
|
|
/* |
128
|
|
|
* hydrate the result |
129
|
|
|
*/ |
130
|
2 |
|
$this->hydrateBrowser($result->getBrowser(), $resultRaw); |
131
|
|
|
// renderingEngine not available |
132
|
|
|
// os is mixed with device informations |
133
|
|
|
// device is mixed with os |
134
|
|
|
|
135
|
2 |
|
return $result; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|