|
1
|
|
|
<?php |
|
2
|
|
|
namespace UserAgentParser\Provider; |
|
3
|
|
|
|
|
4
|
|
|
use UserAgentParser\Exception; |
|
5
|
|
|
use UserAgentParser\Model; |
|
6
|
|
|
|
|
7
|
|
|
abstract class AbstractProvider |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* |
|
11
|
|
|
* @var string |
|
12
|
|
|
*/ |
|
13
|
|
|
private $version; |
|
14
|
|
|
|
|
15
|
|
|
protected $defaultValues = []; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Per default the provider cannot detect anything |
|
19
|
|
|
* Activate them in $detectionCapabilities |
|
20
|
|
|
* |
|
21
|
|
|
* @var array |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $allDetectionCapabilities = [ |
|
24
|
|
|
'browser' => [ |
|
25
|
|
|
'name' => false, |
|
26
|
|
|
'version' => false, |
|
27
|
|
|
], |
|
28
|
|
|
|
|
29
|
|
|
'renderingEngine' => [ |
|
30
|
|
|
'name' => false, |
|
31
|
|
|
'version' => false, |
|
32
|
|
|
], |
|
33
|
|
|
|
|
34
|
|
|
'operatingSystem' => [ |
|
35
|
|
|
'name' => false, |
|
36
|
|
|
'version' => false, |
|
37
|
|
|
], |
|
38
|
|
|
|
|
39
|
|
|
'device' => [ |
|
40
|
|
|
'model' => false, |
|
41
|
|
|
'brand' => false, |
|
42
|
|
|
'type' => false, |
|
43
|
|
|
'isMobile' => false, |
|
44
|
|
|
'isTouch' => false, |
|
45
|
|
|
], |
|
46
|
|
|
|
|
47
|
|
|
'bot' => [ |
|
48
|
|
|
'isBot' => false, |
|
49
|
|
|
'name' => false, |
|
50
|
|
|
'type' => false, |
|
51
|
|
|
], |
|
52
|
|
|
]; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Set this in each Provider implementation |
|
56
|
|
|
* |
|
57
|
|
|
* @var array |
|
58
|
|
|
*/ |
|
59
|
|
|
protected $detectionCapabilities = []; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Return the name of the provider |
|
63
|
|
|
* |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
abstract public function getName(); |
|
67
|
|
|
|
|
68
|
|
|
abstract public function getComposerPackageName(); |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Return the version of the provider |
|
72
|
|
|
* |
|
73
|
|
|
* @return string null |
|
74
|
|
|
*/ |
|
75
|
2 |
|
public function getVersion() |
|
76
|
|
|
{ |
|
77
|
2 |
|
if ($this->version !== null) { |
|
78
|
1 |
|
return $this->version; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
2 |
|
if ($this->getComposerPackageName() === null) { |
|
82
|
1 |
|
return; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
2 |
|
$packages = $this->getComposerPackages(); |
|
86
|
|
|
|
|
87
|
2 |
|
if ($packages === null) { |
|
88
|
1 |
|
return; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
2 |
|
foreach ($packages as $package) { |
|
92
|
2 |
|
if ($package->name === $this->getComposerPackageName()) { |
|
93
|
1 |
|
$this->version = $package->version; |
|
94
|
|
|
|
|
95
|
1 |
|
break; |
|
96
|
|
|
} |
|
97
|
2 |
|
} |
|
98
|
|
|
|
|
99
|
2 |
|
return $this->version; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* |
|
104
|
|
|
* @return \stdClass null |
|
105
|
|
|
*/ |
|
106
|
2 |
|
private function getComposerPackages() |
|
107
|
|
|
{ |
|
108
|
2 |
|
if (! file_exists('composer.lock')) { |
|
109
|
1 |
|
return; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
2 |
|
$content = file_get_contents('composer.lock'); |
|
113
|
2 |
|
if ($content === false || $content === '') { |
|
114
|
|
|
return; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
2 |
|
$content = json_decode($content); |
|
118
|
|
|
|
|
119
|
2 |
|
return $content->packages; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* What kind of capabilities this provider can detect |
|
124
|
|
|
* |
|
125
|
|
|
* @return array |
|
126
|
|
|
*/ |
|
127
|
1 |
|
public function getDetectionCapabilities() |
|
128
|
|
|
{ |
|
129
|
1 |
|
return array_merge($this->allDetectionCapabilities, $this->detectionCapabilities); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* |
|
134
|
|
|
* @param mixed $value |
|
135
|
|
|
* @param array $additionalDefaultValues |
|
136
|
|
|
* @return boolean |
|
137
|
|
|
*/ |
|
138
|
2 |
|
protected function isRealResult($value, array $additionalDefaultValues = []) |
|
139
|
|
|
{ |
|
140
|
2 |
|
if ($value === '' || $value === null) { |
|
141
|
1 |
|
return false; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
2 |
|
$value = (string) $value; |
|
145
|
|
|
|
|
146
|
2 |
|
$defaultValues = array_merge($this->defaultValues, $additionalDefaultValues); |
|
147
|
|
|
|
|
148
|
2 |
|
if (in_array($value, $defaultValues, true) === true) { |
|
149
|
1 |
|
return false; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
2 |
|
return true; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Parse the given user agent and return a result if possible |
|
157
|
|
|
* |
|
158
|
|
|
* @param string $userAgent |
|
159
|
|
|
* @param array $headers |
|
160
|
|
|
* |
|
161
|
|
|
* @throws Exception\NoResultFoundException |
|
162
|
|
|
* |
|
163
|
|
|
* @return Model\UserAgent |
|
164
|
|
|
*/ |
|
165
|
|
|
abstract public function parse($userAgent, array $headers = []); |
|
166
|
|
|
} |
|
167
|
|
|
|