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