1
|
|
|
<?php |
2
|
|
|
namespace UserAgentParser\Provider; |
3
|
|
|
|
4
|
|
|
use UserAgentParser\Exception; |
5
|
|
|
use UserAgentParser\Result; |
6
|
|
|
|
7
|
|
|
abstract class AbstractProvider |
8
|
|
|
{ |
9
|
|
|
private $version; |
10
|
|
|
|
11
|
|
|
protected $defaultValues = []; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Return the name of the provider |
15
|
|
|
* |
16
|
|
|
* @return string |
17
|
|
|
*/ |
18
|
|
|
abstract public function getName(); |
19
|
|
|
|
20
|
|
|
abstract public function getComposerPackageName(); |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Return the version of the provider |
24
|
|
|
* |
25
|
|
|
* @return string null |
26
|
|
|
*/ |
27
|
2 |
|
public function getVersion() |
28
|
|
|
{ |
29
|
2 |
|
if ($this->version !== null) { |
30
|
1 |
|
return $this->version; |
31
|
|
|
} |
32
|
|
|
|
33
|
2 |
|
if ($this->getComposerPackageName() === null) { |
34
|
1 |
|
return; |
35
|
|
|
} |
36
|
|
|
|
37
|
2 |
|
$packages = $this->getComposerPackages(); |
38
|
|
|
|
39
|
2 |
|
if ($packages === null) { |
40
|
1 |
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
2 |
|
foreach ($packages as $package) { |
|
|
|
|
44
|
2 |
|
if ($package->name === $this->getComposerPackageName()) { |
45
|
1 |
|
$this->version = $package->version; |
46
|
|
|
|
47
|
2 |
|
break; |
48
|
|
|
} |
49
|
2 |
|
} |
50
|
|
|
|
51
|
2 |
|
return $this->version; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* |
56
|
|
|
* @return \stdClass null |
57
|
|
|
*/ |
58
|
2 |
|
private function getComposerPackages() |
59
|
|
|
{ |
60
|
2 |
|
if (! file_exists('composer.lock')) { |
61
|
1 |
|
return; |
62
|
|
|
} |
63
|
|
|
|
64
|
2 |
|
$content = file_get_contents('composer.lock'); |
65
|
2 |
|
if ($content === false || $content === '') { |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
2 |
|
$content = json_decode($content); |
70
|
|
|
|
71
|
2 |
|
return $content->packages; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* |
76
|
|
|
* @param mixed $value |
77
|
|
|
* @param array $additionalDefaultValues |
78
|
|
|
* @return boolean |
79
|
|
|
*/ |
80
|
2 |
|
protected function isRealResult($value, array $additionalDefaultValues = []) |
81
|
|
|
{ |
82
|
2 |
|
if ($value === '' || $value === null) { |
83
|
1 |
|
return false; |
84
|
|
|
} |
85
|
|
|
|
86
|
2 |
|
$value = (string) $value; |
87
|
|
|
|
88
|
2 |
|
$defaultValues = array_merge($this->defaultValues, $additionalDefaultValues); |
89
|
|
|
|
90
|
2 |
|
if (in_array($value, $defaultValues, true) === true) { |
91
|
1 |
|
return false; |
92
|
|
|
} |
93
|
|
|
|
94
|
2 |
|
return true; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* |
99
|
|
|
* @param string $userAgent |
100
|
|
|
* @param array $headers |
101
|
|
|
* |
102
|
|
|
* @throws Exception\NoResultFoundException |
103
|
|
|
* |
104
|
|
|
* @return Result\UserAgent |
105
|
|
|
*/ |
106
|
|
|
abstract public function parse($userAgent, array $headers = []); |
107
|
|
|
} |
108
|
|
|
|